当前位置:   article > 正文

spring快速搭建聊天AI_spring ai 部署

spring ai 部署

官网url: https://spring.io/projects/spring-ai

本文演示的是open AI

1创建java项目

2.拿到AI的key(没有的话可以去淘宝花几块钱买一个)

//YOUR_API_KEY写你自己的open AI的key

spring.ai.openai.api-key=YOUR_API_KEY
spring.ai.openai.chat.options.model=gpt-3.5-turbo
spring.ai.openai.chat.options.temperature=0.7

3.导入依赖

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>

 4.编写接口

  1. @RestController
  2. public class ChatController {
  3. private final OpenAiChatClient chatClient;
  4. @Autowired
  5. public ChatController(OpenAiChatClient chatClient) {
  6. this.chatClient = chatClient;
  7. }
  8. @GetMapping("/ai/generate")
  9. public Map generate(@RequestParam(value = "message", defaultValue = "帮我写一个for循环") String message) {
  10. return Map.of("generation", chatClient.call(message));
  11. }
  12. @GetMapping("/ai/generateStream")
  13. public Flux<ChatResponse> generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
  14. Prompt prompt = new Prompt(new UserMessage(message));
  15. return chatClient.stream(prompt);
  16. }
  17. }

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

闽ICP备14008679号