要将ChatGPT集成到您的Spring Boot应用程序中,可以按照以下步骤进行操作:
1.在OpenAI网站上注册并获取您的API密钥。这将允许您使用ChatGPT API进行与模型的交互。
2.在您的Spring Boot应用程序中添加必要的依赖项。在您的pom.xml文件中,添加以下依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
复制
3.创建一个ChatGPTService类来处理与ChatGPT的交互。在该类中,您可以使用HTTP客户端(例如Apache HttpClient)来发送请求到ChatGPT API,并处理API的响应。
下面是一个示例:
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.IOException;
@Service
public class ChatGPTService {
@Value("${openai.api.key}")
private String apiKey;
public String getChatGPTResponse(String message) throws IOException {
String url = "https://api.openai.com/v1/chat/completions";
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost request = new HttpPost(url);
request.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + apiKey);
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
StringEntity params = new StringEntity("{\"model\": \"gpt-3.5-turbo\", \"messages\": [{\"role\": \"system\", \"content\": \"You are a helpful assistant.\"}, {\"role\": \"user\", \"content\": \""+message+"\"}]}");
request.setEntity(params);
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity, "UTF-8");
return responseString;
}
}
复制
这个示例中,ChatGPTService类使用了openai.api.key配置属性,您可以使用Spring的属性配置方式将API密钥注入到该类中。
4.创建一个ChatController类来处理来自客户端的请求。在该类中,您可以使用ChatGPTService类来发送用户的消息并获得ChatGPT的响应。
下面是一个示例:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
@RestController
@RequestMapping("/chat")
public class ChatController {
@Autowired
private ChatGPTService chatGPTService;
@PostMapping
public ResponseEntity<String> chat(@Validated @RequestBody String message) {
try {
String response = chatGPTService.getChatGPTResponse(message);
return ResponseEntity.ok(response);
} catch (IOException e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An error occurred");
}
}
}
这个示例中,ChatController类包含一个chat方法,它处理/chat端点的POST请求。该方法使用ChatGPTService类来发送用户的消息并返回ChatGPT的响应。
5.在您的application.properties文件中添加以下配置项:
openai.api.key=YOUR_API_KEY
将YOUR_API_KEY替换为您在步骤1中获取的API密钥。
6.启动您的Spring Boot应用程序,并尝试通过发送POST请求到http://localhost:8080/chat来与ChatGPT进行交互。
例如,您可以使用cURL命令:
curl -X POST -H "Content-Type: application/json" -d "你好" http://localhost:8080/chat
执行一下,然后会在控制台上看到类似的输出内容
你好!我是一款AI语言模型,有什么我可以帮助你的吗?
评论