赞
踩
官网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.编写接口
- @RestController
- public class ChatController {
-
- private final OpenAiChatClient chatClient;
-
- @Autowired
- public ChatController(OpenAiChatClient chatClient) {
- this.chatClient = chatClient;
- }
-
- @GetMapping("/ai/generate")
- public Map generate(@RequestParam(value = "message", defaultValue = "帮我写一个for循环") String message) {
- return Map.of("generation", chatClient.call(message));
- }
-
- @GetMapping("/ai/generateStream")
- public Flux<ChatResponse> generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
- Prompt prompt = new Prompt(new UserMessage(message));
- return chatClient.stream(prompt);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。