赞
踩
1,首先要能成功注册账号
注册成功会送一定的额度用户接口调用,使用api-key调用会消耗额度.赠送的额度会有有效期
2,生成自己的 api-key
一分钟内只能响应三次, 如需要大于一分钟内三次响应的必须使用付费的调用(非赠送)
需要为api-key选择支付费用方式. 详情可以查看官方文档了. 这里只做代码测试,不做演示
代码如下:
1,控制器
- @RestController
- @RequestMapping("api/v1/chat/gpt")
- @RequiredArgsConstructor
- public class TestController {
-
- private static final String API_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
-
- private static final String CHAT_BASE_URL = "https://api.openai.com/%s";
-
- private static final String CHAT_URL = "/v1/chat/completions";
-
- @GetMapping("test")
- public String wxCallback(@RequestParam("message") String message) {
- HttpHeaders headers = new HttpHeaders();
- headers.add("Authorization", "Bearer " + API_KEY);
- headers.add("Content-Type", "application/json");
- ChatGptRequest request = new ChatGptRequest(message);
- HttpEntity<ChatGptRequest> httpEntity = new HttpEntity<>(request, headers);
- //本机代理 如果是全局梯子了之后可以注释本段
- RestTemplate restTemplate = new RestTemplate(new SimpleClientHttpRequestFactory() {{
- setProxy(new java.net.Proxy(java.net.Proxy.Type.HTTP, new InetSocketAddress("192.168.31.168", 7890)));
- setConnectTimeout(180000);
- setReadTimeout(180000);
- }});
- ResponseEntity<ChatGptResponse> response;
- response = restTemplate.exchange(String.format(CHAT_BASE_URL, CHAT_URL), HttpMethod.POST, httpEntity, ChatGptResponse.class);
- ChatGptResponse result = response.getBody();
- if (result != null) {
- return "CHAT-GPT :" + result.getChoices().get(0).getMessage().getContent();
- } else {
- return "No response from ChatGPT API.";
- }
- }
2,请求封装也可以做成可传参, 这里为了方便直接固定了模型和用户ID,
用户ID 可以用来识别上下文, 因为是单用户(自用) 所以 固定一个用户ID就可以了
- import com.google.common.collect.Lists;
- import lombok.Getter;
- import lombok.Setter;
-
- import java.util.List;
-
- @Setter
- @Getter
- public class ChatGptRequest {
-
- /**
- * 模型 具体可以查看官方文档 https://platform.openai.com/docs/models
- */
- private String model = "gpt-3.5-turbo";
- /**
- * 消息
- */
- private List<ChoiceMessage> messages = Lists.newArrayList();
-
- /**
- * 用户ID
- */
- private String user = "123456";
-
- /**
- * 随机值
- * 0.0对于同一个问题会回答相同答案
- */
- private Double temperature = 0.9;
-
- public ChatGptRequest(String message) {
- this.messages.add(new ChoiceMessage(message));
- }
- }
-
-
- import lombok.Getter;
- import lombok.NoArgsConstructor;
- import lombok.Setter;
-
- @Setter
- @Getter
- @NoArgsConstructor
- public class ChoiceMessage {
-
- /**
- * 角色
- */
- private String role;
-
- /**
- * 消息体
- */
- private String content;
-
- public ChoiceMessage(String content) {
- this.role = "user";
- this.content = content;
- }
- }
-
-
- import lombok.Getter;
- import lombok.Setter;
-
- import java.util.Date;
- import java.util.List;
-
- @Setter
- @Getter
- public class ChatGptResponse {
-
- private String id;
-
- private String object;
-
- private Date created;
-
- private String model;
-
- private List<ChoiceResponse> choices;
- }
- import lombok.Getter;
- import lombok.Setter;
-
-
- @Setter
- @Getter
- public class ChoiceResponse {
-
- private ChoiceMessage message;
-
- //省略了其他属性
- }
- import lombok.Getter;
- import lombok.NoArgsConstructor;
- import lombok.Setter;
-
-
- @Setter
- @Getter
- @NoArgsConstructor
- public class ChoiceMessage {
-
- /**
- * 角色
- */
- private String role;
-
- /**
- * 消息体
- */
- private String content;
-
- public ChoiceMessage(String content) {
- this.role = "user";
- this.content = content;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。