当前位置:   article > 正文

SpringBoot整合OpenAI实现AI聊天 (精简demo)

SpringBoot整合OpenAI实现AI聊天 (精简demo)

1. OpenAI官网

  1. /**
  2. * 官网获取密钥基本条件
  3. *
  4. * 1. 翻墙, 能访问外网
  5. * 2. 拥有国外手机号码
  6. * 3. 注册账号
  7. * 4. 获取密钥
  8. */
  9. https://openai.com/

2. 获取OpenAI密钥 (怎么简单怎么来)
  1. // 直接在淘宝上购买, 买多几个随机访问
  2. sk-xxxx
 

3. 依赖
  1. <dependency>
  2. <groupId>com.alibaba.fastjson2</groupId>
  3. <artifactId>fastjson2</artifactId>
  4. <version>2.0.32</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>cn.hutool</groupId>
  8. <artifactId>hutool-all</artifactId>
  9. <version>5.3.3</version>
  10. </dependency>

4. Message
  1. import lombok.AllArgsConstructor;
  2. import lombok.Data;
  3. import lombok.NoArgsConstructor;
  4. @Data
  5. @AllArgsConstructor
  6. @NoArgsConstructor
  7. public class Message {
  8. private String role;
  9. private String content;
  10. }

5. Choices
  1. import lombok.AllArgsConstructor;
  2. import lombok.Data;
  3. import lombok.NoArgsConstructor;
  4. @Data
  5. @AllArgsConstructor
  6. @NoArgsConstructor
  7. public class Choices {
  8. private Message message;
  9. }

6. RequestBody
  1. import lombok.AllArgsConstructor;
  2. import lombok.Data;
  3. import lombok.NoArgsConstructor;
  4. import java.util.List;
  5. @Data
  6. @AllArgsConstructor
  7. @NoArgsConstructor
  8. public class RequestBody {
  9. private String model;
  10. private List<Message> messages;
  11. }

7. ResponseBody
  1. import lombok.AllArgsConstructor;
  2. import lombok.Data;
  3. import lombok.NoArgsConstructor;
  4. import java.util.List;
  5. @Data
  6. @AllArgsConstructor
  7. @NoArgsConstructor
  8. public class ResponseBody {
  9. private List<Choices> choices;
  10. }

8. OpenAiUtils
  1. import cn.hutool.http.HttpRequest;
  2. import com.alibaba.fastjson.JSON;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.springframework.stereotype.Component;
  5. import java.net.InetSocketAddress;
  6. import java.net.Proxy;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. @Slf4j
  10. @Component
  11. public class OpenAiUtils {
  12. private static final String URL = "https://api.openai.com/v1/chat/completions";
  13. private static final String API_KEY = "sk-xxx";
  14. public String getAiRespondBody(String content) {
  15. log.info("用户提问内容: {}", content);
  16. // 代理IP地址
  17. String proxyHost = "127.0.0.1";
  18. // 代理端口号
  19. int proxyPort = 2802;
  20. Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
  21. List<Message> list = new ArrayList<>();
  22. Message message = new Message();
  23. message.setRole("user");
  24. message.setContent(content);
  25. list.add(message);
  26. RequestBody requestBody = new RequestBody();
  27. requestBody.setModel("gpt-3.5-turbo");
  28. requestBody.setMessages(list);
  29. String data = JSON.toJSONString(requestBody);
  30. String resJSON = HttpRequest.post(URL)
  31. .header("Content-Type", "application/json")
  32. .header("Authorization", "Bearer " + API_KEY)
  33. .body(data)
  34. .setProxy(proxy)
  35. .execute()
  36. .body();
  37. ResponseBody body = JSON.parseObject(resJSON, ResponseBody.class);
  38. String outputContent = body.getChoices().get(0).getMessage().getContent();
  39. log.info("AI回复结果: {}",outputContent);
  40. return outputContent;
  41. }
  42. }
翻墙后的代理服务器

9. OpenAIController
  1. import com.ruoyi.common.core.domain.AjaxResult;
  2. import io.swagger.annotations.ApiOperation;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.PostMapping;
  5. import org.springframework.web.bind.annotation.RequestBody;
  6. import org.springframework.web.bind.annotation.RestController;
  7. /**
  8. * OpenAI 聊天
  9. */
  10. @RestController
  11. public class OpenAIController {
  12. @Autowired
  13. private OpenAiUtils openAiUtils;
  14. @ApiOperation("用户提问-AI回复")
  15. @PostMapping("/auth/ai")
  16. public AjaxResult getAiRespondBody(@RequestBody String content){
  17. String respContent =openAiUtils.getAiRespondBody(content);
  18. return AjaxResult.success("操作成功",respContent);
  19. }
  20. }

10. 测试, 响应结果
  1. 用户提问内容: { "content":"现在的食盐还能吃吗?"}
  2. AI回复结果: 现在的食盐是可以安全食用的。食盐主要由钠和氯化物组成,经过加工和质量控制确保无害。然而,过量摄入盐可能对健康有害,因此应遵循适量食用的原则。根据世界卫生组织的建议,成年人每天的钠摄入量不应超过5克。适量食用食盐可以满足身体对钠的需求,但过量食用可能与高血压、心脏病和中风风险增加相关。如果你有特殊的健康状况或需求,建议咨询医生或营养师的建议。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/608228
推荐阅读
相关标签
  

闽ICP备14008679号