最简单的springAi+文心一言模型,实现人工智能对话
一、获取文心一言密钥进入百度智能云获取API Key和Secret Key,地址百度智能云
https://i-blog.csdnimg.cn/direct/3ef778a5f48e4bdc83008846f3b4f4c5.png
二、创建一个springboot项目
起首导入依赖
<dependency>
<groupId>group.springframework.ai</groupId>
<artifactId>spring-ai-qianfan-spring-boot-starter</artifactId>
<version>1.1.0</version>
</dependency> 注意!要前往中心堆栈下载
<repository>
<id>central</id>
<url>https://repo.maven.apache.org/maven2</url>
</repository> 创建配置文件application.properties
填写好申请的API Key和Secret Key
https://i-blog.csdnimg.cn/direct/85946b54f4b44d9cb89f209d809196f1.png
spring.ai.qianfan.api-key=
spring.ai.qianfan.secret-key=
spring.ai.qianfan.chat.options.model=ernie_speed
spring.ai.qianfan.chat.options.temperature=0.7
创建一个ChatController类
源代码可以在spring官网查察:spring官网源代码
https://i-blog.csdnimg.cn/direct/f9d66d51953046258072971534378ffe.png
https://i-blog.csdnimg.cn/direct/74825129cfcd4e2eb356375c3f02c519.png
@RestController
public class ChatController {
private final QianFanChatModel chatClient;
@Autowired
public ChatController(QianFanChatModel chatClient) {
this.chatClient = chatClient;
}
@GetMapping("/ai/generate")
public Map generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
return Map.of("generation", this.chatClient.call(message));
}
@GetMapping("/ai/generateStream")
public Flux<ChatResponse> generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
var prompt = new Prompt(new UserMessage(message));
return this.chatClient.stream(prompt);
} 三、展示结果
启动项目,输入”你好“
localhost:8080/ai/generate?message="你好"
https://i-blog.csdnimg.cn/direct/1540c5a5a5664816ae1f736a3f4a4fa1.png
返回结果
https://i-blog.csdnimg.cn/direct/5122e20dcba14ea4a35f333e02ac5faf.png
四、源代码
Gitee源代码:https://gitee.com/zyGitee983/learning-notes.git
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]