继续上一章的内容,创建一个 Config 配置类
java
package com.hza.ai.config;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AiConfig {
@Bean
ChatClient chatClient(ChatClient.Builder builder) {
return builder.defaultSystem("你是一个超市的客服,超市的信息是:小萌超市,超市里有一款热卖的马超周边。")
.build();
}
}
修改 Controller 代码
java
package com.hza.ai.controller;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/ai")
public class AiController {
//智能对话客户端
@Autowired
private ChatClient chatClient;
@GetMapping("/chart")
public String generation(@RequestParam(value="message",defaultValue = "给我讲个笑话") String message) {
//prompt:提示词
return this.chatClient.prompt()
//用户信息
.user(message)
//远程请求大模型
.call()
//返回文本
.content();
}
}
运行项目进行验证:
