当前位置:   article > 正文

Spring ai 快速入门及使用,构建你自己的ai_spring-ai 使用方法

spring-ai 使用方法

第一步:创建springboot项目 jdk必须是17及以上 1.8用不了

第二步 选择web和ai的依赖 选择openai

第三步 需要配置openai key 配置

分享个免费或的apikey的地方New API 会免费赠送1刀的token

  1. spring.application.name=springAI
  2. spring.ai.openai.base-url=https://api.xty.app
  3. spring.ai.openai.api-key=sk-DvbisfiKYZMkKjxICe542cEe16B74d41B763E23c449d83Ed
  4. spring.ai.openai.chat.options.model=gpt-3.5-turbo
  5. server.port=8811

第四步创建个controller 

  1. import org.springframework.ai.chat.ChatResponse;
  2. import org.springframework.ai.chat.messages.UserMessage;
  3. import org.springframework.ai.chat.prompt.Prompt;
  4. import org.springframework.ai.openai.OpenAiChatClient;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.web.bind.annotation.*;
  7. import reactor.core.publisher.Flux;
  8. import java.util.Map;
  9. @RestController
  10. public class ChatController {
  11. private final OpenAiChatClient chatClient;
  12. @Autowired
  13. public ChatController(OpenAiChatClient chatClient) {
  14. this.chatClient = chatClient;
  15. }
  16. @GetMapping("/ai/generate")
  17. public Map generate(@RequestParam(value = "message", defaultValue = "讲个笑话") String message) {
  18. return Map.of("generation", chatClient.call(message));
  19. }
  20. @GetMapping("/ai/generateStream")
  21. public Flux<ChatResponse> generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
  22. Prompt prompt = new Prompt(new UserMessage(message));
  23. return chatClient.stream(prompt);
  24. }
  25. }

效果展示:

前端页面

  1. <script setup lang="ts">
  2. import {ref} from 'vue'
  3. const msg = ref('');
  4. const res = ref([]);
  5. const sendMsg = ()=>{
  6. const message = encodeURIComponent(msg.value);
  7. let source = new EventSource(`http://localhost:8811/ai/generateStream?message=${message}`)
  8. let count =0;
  9. source.onmessage = (e)=>{
  10. console.log(e.data)
  11. if(e.data==='')
  12. {
  13. count++;
  14. }
  15. if(count===2)
  16. {
  17. source.close;
  18. }
  19. res.value.push(e.data)
  20. }
  21. }
  22. </script>
  23. <template>
  24. <div id="container">
  25. <div id="history">
  26. <span v-for="(r, i) in res" :key="i">{{ r }}</span>
  27. </div>
  28. <div id="chat">
  29. <textarea v-model="msg"></textarea>
  30. <button @click="sendMsg">send</button>
  31. </div>
  32. </div>
  33. </template>
  34. <style scoped>
  35. * {
  36. margin:0;
  37. padding:0;
  38. }
  39. #container{
  40. display: flex;
  41. flex-direction: row;
  42. flex-wrap: wrap;
  43. width: 100%;
  44. height: 100vh;
  45. background-color: white;
  46. border: 1px solid black;
  47. }
  48. #history{
  49. width: 400px;
  50. height: 400px;
  51. border: #f9f9f9;
  52. }
  53. #chat{
  54. width: 400px;
  55. height: 200;
  56. border: #747bff;
  57. }
  58. textarea{
  59. width: 400px;
  60. }
  61. button{
  62. width: 100px;
  63. height: 60px;
  64. }
  65. </style>

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

闽ICP备14008679号