一、介绍
Spring Boot 是一种用于创建独立的、基于 Spring 的应用程序的框架。它的主要目的是简化 Spring 应用程序的开发和部署过程。在本文中,我将向您展示如何使用 Spring Boot 框架与 ChatGPT 进行整合。
二、获取api密钥
OpenAI API
注意,不会在生成秘密API密钥后再次显示密钥。
三、整合chatGPT
1、pom.xml中添加依赖
<dependency><groupId>com.theokanning.openai-gpt3-java</groupId><artifactId>service</artifactId><version>0.11.0</version></dependency><dependency><groupId>com.theokanning.openai-gpt3-java</groupId><artifactId>client</artifactId><version>0.11.0</version></dependency>
2、domain
package com.lxy.openai.domain;import lombok.Data;/*** @author lixingyu*/
@Data
public class OpenAi {String model;Double temperature;Double topP;Double frequencyPenalty;Double presencePenalty;String stop;String role;public OpenAi(String model,Double temperature, Double topP, Double frequencyPenalty, Double presencePenalty) {this.model = model;this.temperature = temperature;this.topP = topP;this.frequencyPenalty = frequencyPenalty;this.presencePenalty = presencePenalty;}
}
3、工具类编写
package com.lxy.openai.utils;import com.lxy.openai.domain.OpenAi;
import com.theokanning.openai.completion.chat.ChatCompletionChoice;
import com.theokanning.openai.completion.chat.ChatCompletionRequest;
import com.theokanning.openai.completion.chat.ChatMessage;
import com.theokanning.openai.service.OpenAiService;
import com.theokanning.openai.completion.CompletionChoice;
import com.theokanning.openai.completion.CompletionRequest;
import java.time.Duration;
import java.util.*;/*** 调用OpenAi的gpt-3.5-turbo 与 text-davinci-003** @author lixingyu*/
public class OpenAiUtils {public static String API_KEY = "api密钥";public static Duration TIMEOUT = Duration.ofSeconds(300);public static final Map<String, OpenAi> DEFAULT_MODEL= new HashMap<>();static {DEFAULT_MODEL.put("turbo", new OpenAi("gpt-3.5-turbo", 0.2, 0.1, 2d, 2d));DEFAULT_MODEL.put("davinci-003", new OpenAi("text-davinci-003", 0.9, 0.1, 1.0, 1.0));}/*** 获取ai** @param openAi* @param prompt* @return*/public static List<CompletionChoice> getAiResult(OpenAi openAi, String prompt) {OpenAiService service = new OpenAiService(API_KEY, TIMEOUT);CompletionRequest.CompletionRequestBuilder builder = CompletionRequest.builder().model(openAi.getModel()).prompt(prompt).temperature(openAi.getTemperature()).maxTokens(1000).topP(openAi.getTopP()).frequencyPenalty(openAi.getFrequencyPenalty()).presencePenalty(openAi.getPresencePenalty());CompletionRequest completionRequest = builder.build();return service.createCompletion(completionRequest).getChoices();}public static List<ChatCompletionChoice> getAiChatResult(OpenAi openAi, String prompt) {OpenAiService service = new OpenAiService(API_KEY, TIMEOUT);List<ChatMessage> messageList = new ArrayList<>();ChatMessage message = new ChatMessage();message.setContent(prompt);message.setRole("user");messageList.add(message);ChatCompletionRequest.ChatCompletionRequestBuilder requestBuilder = ChatCompletionRequest.builder() .model(openAi.getModel()).messages(messageList).temperature(openAi.getTemperature()).maxTokens(3000).topP(openAi.getTopP()).frequencyPenalty(openAi.getFrequencyPenalty()).presencePenalty(openAi.getPresencePenalty());ChatCompletionRequest build = requestBuilder.build();return service.createChatCompletion(build).getChoices();}/*** 文本* @param text 文本* @return*/public static List<CompletionChoice> getRating(String text) {OpenAi openAi = DEFAULT_MODEL.get("davinci-003");return getAiResult(openAi, text);}/*** GPT-3.5型号* @param text 文本* @return*/public static List<ChatCompletionChoice> getChatRating(String text) {OpenAi openAi = DEFAULT_MODEL.get("turbo");return getAiChatResult(openAi, text);}}
四、总结
本文主要使用gpt-3.5-turbo与text-davinci-003,具体接口说明可参考官方说明OpenAI API。GPT-4是OpenAI最新推出的语言模型,但是它目前仍处于测试阶段,并且只向少数研究人员和企业客户授予了访问权限。此外,OpenAI也已经更新了API的定价和使用方式,需要访问他们的网站获取更多信息。