营销网站费用,wordpress小工具页脚,舆情分析网站免费,做网站公司1、开始咯 
我们来看看如何在 Spring Boot 中调用 OpenAI ChatGPT API。 
我们将创建一个 Spring Boot 应用程序#xff0c;该应用程序将通过调用 OpenAI ChatGPT API 生成对提示的响应。 
2、OpenAI ChatGPT API 
在开始具体讲解之前#xff0c;让我们先探讨一下我们将在本教…1、开始咯 
我们来看看如何在 Spring Boot 中调用 OpenAI ChatGPT API。 
我们将创建一个 Spring Boot 应用程序该应用程序将通过调用 OpenAI ChatGPT API 生成对提示的响应。 
2、OpenAI ChatGPT API 
在开始具体讲解之前让我们先探讨一下我们将在本教程中使用的 OpenAI ChatGPT API。我们将调用创建聊天完成 API 来生成对提示的响应。 
2.1 API 参数与认证 
我们看一下API的强制请求参数 
model这是我们将向其发送请求的模型的版本。该模型有几个版本可用。我们将使用 gpt-3.5-turbo 模型这是该模型公开的最新版本message消息是对模型的提示。每条消息都需要两个字段角色和内容。角色字段指定消息的发送者。请求中它将是“用户”响应中它将是“助手”。内容字段是实际的消息。 
为了使用 API 进行身份验证我们将生成一个 OpenAI API 密钥。我们将在调用 API 时在 Authorization 标头中设置此密钥。 
cURL 格式的示例请求如下所示 
$ curl https://api.openai.com/v1/chat/completions \-H Content-Type: application/json \-H Authorization: Bearer $OPENAI_API_KEY \-d {model: gpt-3.5-turbo,messages: [{role: user, content: Hello!}]}此外该 API 还接受许多可选参数来修改响应。 
接着我们将重点关注一个简单的请求但让我们看一下一些有助于调整响应的可选参数 
n如果我们想增加生成的响应数量可以指定。默认值为 1temperature控制响应的随机性。默认值为 1最随机max_tokens用于限制响应中令牌的最大数量。默认值是无穷大这意味着响应将与模型可以生成的一样长。一般来说最好将此值设置为合理的数字以避免生成很长的响应并产生很高的成本。 
2.2 API Response 
API 响应将是一个带有一些元数据和选择字段的 JSON 对象。选择字段将是一个对象数组。每个对象都有一个文本字段其中包含对提示的响应。 
选择数组中的对象数量将等于请求中的可选 n 参数。如果未指定 n 参数则选项数组将包含单个对象。 
具体代码 
{id: chatcmpl-123,object: chat.completion,created: 1677652288,choices: [{index: 0,message: {role: assistant,content: \n\n 来啦老弟……},finish_reason: stop}],usage: {prompt_tokens: 9,completion_tokens: 12,total_tokens: 21}
}响应中的使用字段将包含提示和响应中使用的令牌数量。这用于计算 API 调用的成本。 
3、具体案例 
我们将创建一个使用 OpenAI ChatGPT API 的 Spring Boot 应用程序。 
为此我们将创建一个 Spring Boot Rest API该 API 接受提示作为请求参数将其传递给 OpenAI ChatGPT API并将响应作为响应正文返回。 
3.1 添加依赖 
首先我们创建一个 Spring Boot 项目。我们需要该项目的 Spring Boot Starter Web 依赖 
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId
/dependency3.2 DTO 
接下来我们创建一个与 OpenAI ChatGPT API 的请求参数对应的 DTO 
public class ChatRequest {private String model;private ListMessage messages;private int n;private double temperature;public ChatRequest(String model, String prompt) {this.model  model;this.messages  new ArrayList();this.messages.add(new Message(user, prompt));}// getters and setters
}继续定义 Message 类 
public class Message {private String role;private String content;// constructor, getters and setters
}然后我们为响应创建一个 DTO 
public class ChatResponse {private ListChoice choices;// constructors, getters and setterspublic static class Choice {private int index;private Message message;// constructors, getters and setters}
}3.3 控制器 
我们创建一个控制器它将接受提示作为请求参数并返回响应作为响应正文 
RestController
public class ChatController {Qualifier(openaiRestTemplate)Autowiredprivate RestTemplate restTemplate;Value(${openai.model})private String model;Value(${openai.api.url})private String apiUrl;GetMapping(/chat)public String chat(RequestParam String prompt) {// create a requestChatRequest request  new ChatRequest(model, prompt);// call the APIChatResponse response  restTemplate.postForObject(apiUrl, request, ChatResponse.class);if (response  null || response.getChoices()  null || response.getChoices().isEmpty()) {return No response;}// return the first responsereturn response.getChoices().get(0).getMessage().getContent();}
}分析一下代码中一些重要部分 
我们使用 Qualifier 注释来注入我们将在下一节中创建的 RestTemplate bean使用 RestTemplate bean我们使用 postForObject() 方法调用 OpenAI ChatGPT API。 postForObject() 方法将 URL、请求对象和响应类作为参数最后我们读取回复的选择列表并返回第一个回复。 
3.4 RestTemplate 
我们定义一个自定义 RestTemplate bean它将使用 OpenAI API 密钥进行身份验证 
Configuration
public class OpenAIRestTemplateConfig {Value(${openai.api.key})private String openaiApiKey;BeanQualifier(openaiRestTemplate)public RestTemplate openaiRestTemplate() {RestTemplate restTemplate  new RestTemplate();restTemplate.getInterceptors().add((request, body, execution) - {request.getHeaders().add(Authorization, Bearer   openaiApiKey);return execution.execute(request, body);});return restTemplate;}
}3.5 Properties 
在 application.properties 文件中提供 API 的属性 
openai.modelgpt-3.5-turbo
openai.api.urlhttps://api.openai.com/v1/chat/completions
openai.api.keyyour-api-key然后就可以运行程序了。 
4、总结 
我们探索了 OpenAI ChatGPT API 以生成对提示的响应。我们创建了一个 Spring Boot 应用程序它调用 API 来生成对提示的响应。