当前位置:   article > 正文

Spring-AI基于GPT3.5实现开发WEB应用------Spring-AI框架_springai设置3.5

springai设置3.5
  1. package com.alatus.springai.controller;
  2. import jakarta.annotation.Resource;
  3. import org.springframework.ai.chat.ChatResponse;
  4. import org.springframework.ai.chat.prompt.Prompt;
  5. import org.springframework.ai.openai.OpenAiChatOptions;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RequestParam;
  8. import org.springframework.web.bind.annotation.RestController;
  9. import org.springframework.ai.openai.OpenAiChatClient;
  10. import reactor.core.publisher.Flux;
  11. @RestController
  12. public class ChatController {
  13. @Resource
  14. private OpenAiChatClient openAiChatClient;
  15. @RequestMapping(value = "/ai/chat")
  16. public String chat(@RequestParam(value = "msg") String msg){
  17. return openAiChatClient.call(msg);
  18. }
  19. @RequestMapping(value = "/ai/chat2")
  20. public Object chat2(@RequestParam(value = "msg")String msg){
  21. ChatResponse call = openAiChatClient.call(new Prompt(msg));
  22. return call.getResult().getOutput().getContent();
  23. //第二个是获取完整对象用的
  24. // return call;
  25. }
  26. @RequestMapping(value = "/ai/chat3")
  27. public Object chat3(@RequestParam(value = "msg")String msg){
  28. // 可选参数如果配置文件和代码中都出现了,以代码配置为准
  29. ChatResponse call = openAiChatClient.call(new Prompt(msg,
  30. OpenAiChatOptions.builder()
  31. // .withModel("gpt-4-32k")//GPT版本,32K是参数
  32. .withTemperature(0.1F)//温度越高,回答越不精确,温度越低越精确
  33. .build()));
  34. return call.getResult().getOutput().getContent();
  35. //第二个是获取完整对象用的
  36. // return call;
  37. }
  38. @RequestMapping(value = "/ai/chat4")
  39. public Object chat4(@RequestParam(value = "msg")String msg){
  40. // flux将结果以序列返回
  41. Flux<ChatResponse> flux = openAiChatClient.stream(new Prompt(msg,
  42. OpenAiChatOptions.builder()
  43. // .withModel("gpt-4-32k")//GPT版本,32K是参数
  44. .withTemperature(0.1F)//温度越高,回答越不精确,温度越低越精确
  45. .build()));
  46. // 数据的序列,一序列的数据,一个一个的数据返回
  47. flux.toStream().forEach(chatResponse -> {
  48. System.out.print(chatResponse.getResult().getOutput().getContent());
  49. });
  50. return flux.collectList();
  51. }
  52. }
package com.alatus.springai.controller;

import jakarta.annotation.Resource;
import org.springframework.ai.chat.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.ai.openai.OpenAiChatClient;
import reactor.core.publisher.Flux;

@RestController
public class ChatController {
    @Resource
    private OpenAiChatClient openAiChatClient;

    @RequestMapping(value = "/ai/chat")
    public String chat(@RequestParam(value = "msg") String msg){
        return openAiChatClient.call(msg);
    }

    @RequestMapping(value = "/ai/chat2")
    public Object chat2(@RequestParam(value = "msg")String msg){
        ChatResponse call = openAiChatClient.call(new Prompt(msg));
        return call.getResult().getOutput().getContent();
        //第二个是获取完整对象用的
//        return call;
    }

    @RequestMapping(value = "/ai/chat3")
    public Object chat3(@RequestParam(value = "msg")String msg){
//        可选参数如果配置文件和代码中都出现了,以代码配置为准
        ChatResponse call = openAiChatClient.call(new Prompt(msg,
                OpenAiChatOptions.builder()
//                        .withModel("gpt-4-32k")//GPT版本,32K是参数
                        .withTemperature(0.1F)//温度越高,回答越不精确,温度越低越精确
                        .build()));
        return call.getResult().getOutput().getContent();
        //第二个是获取完整对象用的
//        return call;
    }

    @RequestMapping(value = "/ai/chat4")
    public Object chat4(@RequestParam(value = "msg")String msg){
//        flux将结果以序列返回
        Flux<ChatResponse> flux = openAiChatClient.stream(new Prompt(msg,
                OpenAiChatOptions.builder()
//                        .withModel("gpt-4-32k")//GPT版本,32K是参数
                        .withTemperature(0.1F)//温度越高,回答越不精确,温度越低越精确
                        .build()));
//        数据的序列,一序列的数据,一个一个的数据返回
        flux.toStream().forEach(chatResponse -> {
            System.out.print(chatResponse.getResult().getOutput().getContent());
        });
        return flux.collectList();
    }
}
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/735397
推荐阅读
相关标签
  

闽ICP备14008679号