当前位置:   article > 正文

SpringBoot 集成 ChatGPT(附实战源码)_springboot整合chatgpt

springboot整合chatgpt

建项目

项目结构

application.properties

  1. openai.chatgtp.model=gpt-3.5-turbo
  2. openai.chatgtp.api.key=REPLACE_WITH_YOUR_API_KEY
  3. openai.chatgtp.api.url=https://api.openai.com/v1/chat/completions
  4. openai.chatgtp.max-completions=1
  5. openai.chatgtp.temperature=0
  6. openai.chatgtp.max_tokens=100
  7. l

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>3.1.2</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.openai.chatgtp</groupId>
  12. <artifactId>chatgtp</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>chatgtp</name>
  15. <description>ChatGTP Integration with Spring Boot Application</description>
  16. <properties>
  17. <java.version>17</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-web</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.projectlombok</groupId>
  26. <artifactId>lombok</artifactId>
  27. <optional>true</optional>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-starter-test</artifactId>
  32. <scope>test</scope>
  33. </dependency>
  34. </dependencies>
  35. <build>
  36. <plugins>
  37. <plugin>
  38. <groupId>org.springframework.boot</groupId>
  39. <artifactId>spring-boot-maven-plugin</artifactId>
  40. <configuration>
  41. <image>
  42. <builder>paketobuildpacks/builder-jammy-base:latest</builder>
  43. </image>
  44. <excludes>
  45. <exclude>
  46. <groupId>org.projectlombok</groupId>
  47. <artifactId>lombok</artifactId>
  48. </exclude>
  49. </excludes>
  50. </configuration>
  51. </plugin>
  52. </plugins>
  53. </build>
  54. </project>

configurations

OpenAIChatGtpConfig

  1. package com.openai.chatgtp.configurations;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.web.client.RestTemplate;
  6. @Configuration
  7. public class OpenAIChatGtpConfig {
  8. @Value("${openai.chatgtp.api.key}")
  9. private String openaiApiKey;
  10. @Bean
  11. public RestTemplate restTemplate() {
  12. RestTemplate restTemplate = new RestTemplate();
  13. restTemplate.getInterceptors().add((request, body, execution) -> {
  14. request.getHeaders().add("Authorization", "Bearer " + openaiApiKey);
  15. return execution.execute(request, body);
  16. });
  17. return restTemplate;
  18. }
  19. }

controllers

ChatBotController

  1. package com.openai.chatgtp.controllers;
  2. import com.openai.chatgtp.dtos.ChatBotRequest;
  3. import com.openai.chatgtp.dtos.ChatBotResponse;
  4. import com.openai.chatgtp.dtos.Message;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.beans.factory.annotation.Value;
  7. import org.springframework.web.bind.annotation.PostMapping;
  8. import org.springframework.web.bind.annotation.RequestParam;
  9. import org.springframework.web.bind.annotation.RestController;
  10. import org.springframework.web.client.RestTemplate;
  11. import java.util.List;
  12. @RestController
  13. public class ChatBotController {
  14. @Autowired
  15. private RestTemplate restTemplate;
  16. @Value("${openai.chatgtp.model}")
  17. private String model;
  18. @Value("${openai.chatgtp.max-completions}")
  19. private int maxCompletions;
  20. @Value("${openai.chatgtp.temperature}")
  21. private double temperature;
  22. @Value("${openai.chatgtp.max_tokens}")
  23. private int maxTokens;
  24. @Value("${openai.chatgtp.api.url}")
  25. private String apiUrl;
  26. @PostMapping("/chat")
  27. public ChatBotResponse chat(@RequestParam("prompt") String prompt) {
  28. ChatBotRequest request = new ChatBotRequest(model,
  29. List.of(new Message("user", prompt)),
  30. maxCompletions,
  31. temperature,
  32. maxTokens);
  33. ChatBotResponse chatBotResponse = restTemplate.postForObject(apiUrl, request, ChatBotResponse.class);
  34. return chatBotResponse;
  35. }
  36. }

dtos

ChatBotRequest

  1. package com.openai.chatgtp.dtos;
  2. import lombok.AllArgsConstructor;
  3. import lombok.Data;
  4. import lombok.NoArgsConstructor;
  5. import java.util.List;
  6. @Data
  7. @NoArgsConstructor
  8. @AllArgsConstructor
  9. public class ChatBotRequest {
  10. private String model;
  11. private List<Message> messages;
  12. private int n;
  13. private double temperature;
  14. private int max_tokens;
  15. }

ChatBotResponse

  1. package com.openai.chatgtp.dtos;
  2. import lombok.AllArgsConstructor;
  3. import lombok.Data;
  4. import lombok.NoArgsConstructor;
  5. import java.util.List;
  6. @Data
  7. @AllArgsConstructor
  8. @NoArgsConstructor
  9. public class ChatBotResponse {
  10. private List<Choice> choices;
  11. @Data
  12. @AllArgsConstructor
  13. @NoArgsConstructor
  14. public static class Choice {
  15. private int index;
  16. private Message message;
  17. }
  18. }

Message

  1. package com.openai.chatgtp.dtos;
  2. import lombok.AllArgsConstructor;
  3. import lombok.Data;
  4. import lombok.NoArgsConstructor;
  5. @Data
  6. @NoArgsConstructor
  7. @AllArgsConstructor
  8. public class Message {
  9. private String role;
  10. private String content;
  11. }

ChatgtpApplication

  1. package com.openai.chatgtp;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. @SpringBootApplication
  6. public class ChatgtpApplication {
  7. public static void main(String[] args) {
  8. SpringApplication.run(ChatgtpApplication.class, args);
  9. }
  10. }
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号