建项目
项目结构
application.properties
openai.chatgtp.model=gpt-3.5-turbo
openai.chatgtp.api.key=REPLACE_WITH_YOUR_API_KEY
openai.chatgtp.api.url=https://api.openai.com/v1/chat/completionsopenai.chatgtp.max-completions=1
openai.chatgtp.temperature=0
openai.chatgtp.max_tokens=100
l
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.1.2</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.openai.chatgtp</groupId><artifactId>chatgtp</artifactId><version>0.0.1-SNAPSHOT</version><name>chatgtp</name><description>ChatGTP Integration with Spring Boot Application</description><properties><java.version>17</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><image><builder>paketobuildpacks/builder-jammy-base:latest</builder></image><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build></project>
configurations
OpenAIChatGtpConfig
package com.openai.chatgtp.configurations;import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;@Configuration
public class OpenAIChatGtpConfig {@Value("${openai.chatgtp.api.key}")private String openaiApiKey;@Beanpublic RestTemplate restTemplate() {RestTemplate restTemplate = new RestTemplate();restTemplate.getInterceptors().add((request, body, execution) -> {request.getHeaders().add("Authorization", "Bearer " + openaiApiKey);return execution.execute(request, body);});return restTemplate;}
}
controllers
ChatBotController
package com.openai.chatgtp.controllers;import com.openai.chatgtp.dtos.ChatBotRequest;
import com.openai.chatgtp.dtos.ChatBotResponse;
import com.openai.chatgtp.dtos.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;import java.util.List;@RestController
public class ChatBotController {@Autowiredprivate RestTemplate restTemplate;@Value("${openai.chatgtp.model}")private String model;@Value("${openai.chatgtp.max-completions}")private int maxCompletions;@Value("${openai.chatgtp.temperature}")private double temperature;@Value("${openai.chatgtp.max_tokens}")private int maxTokens;@Value("${openai.chatgtp.api.url}")private String apiUrl;@PostMapping("/chat")public ChatBotResponse chat(@RequestParam("prompt") String prompt) {ChatBotRequest request = new ChatBotRequest(model,List.of(new Message("user", prompt)),maxCompletions,temperature,maxTokens);ChatBotResponse chatBotResponse = restTemplate.postForObject(apiUrl, request, ChatBotResponse.class);return chatBotResponse;}
}
dtos
ChatBotRequest
package com.openai.chatgtp.dtos;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import java.util.List;@Data
@NoArgsConstructor
@AllArgsConstructor
public class ChatBotRequest {private String model;private List<Message> messages;private int n;private double temperature;private int max_tokens;
}
ChatBotResponse
package com.openai.chatgtp.dtos;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import java.util.List;@Data
@AllArgsConstructor
@NoArgsConstructor
public class ChatBotResponse {private List<Choice> choices;@Data@AllArgsConstructor@NoArgsConstructorpublic static class Choice {private int index;private Message message;}
}
Message
package com.openai.chatgtp.dtos;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@NoArgsConstructor
@AllArgsConstructor
public class Message {private String role;private String content;}
ChatgtpApplication
package com.openai.chatgtp;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;@SpringBootApplication
public class ChatgtpApplication {public static void main(String[] args) {SpringApplication.run(ChatgtpApplication.class, args);}}