feat(ai): 集成多种大模型并支持会话记忆功能
- 新增 Ollama、智谱 AI 和 OpenAI 大模型接入配置- 实现基于 ChatMemory 的会话上下文管理 - 添加流式输出接口以提升响应体验 - 更新加密工具类密钥及测试数据 - 引入多个 AI 控制器用于不同模型的服务调用 - 在 pom.xml 中添加相关依赖项以支持多模型集成
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package com.hanserwei.airobot.controller;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.ai.chat.messages.UserMessage;
|
||||
import org.springframework.ai.chat.model.Generation;
|
||||
import org.springframework.ai.chat.prompt.Prompt;
|
||||
import org.springframework.ai.openai.OpenAiChatModel;
|
||||
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;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/v5/ai")
|
||||
public class OpenAIController {
|
||||
|
||||
@Resource
|
||||
private OpenAiChatModel chatModel;
|
||||
|
||||
/**
|
||||
* 普通对话
|
||||
* @param message 对话输入内容
|
||||
* @return 对话结果
|
||||
*/
|
||||
@GetMapping("/generate")
|
||||
public String generate(@RequestParam(value = "message", defaultValue = "你是谁?") String message) {
|
||||
// 一次性返回结果
|
||||
return chatModel.call(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 流式对话
|
||||
* @param message 对话输入内容
|
||||
* @return 对话结果
|
||||
*/
|
||||
@GetMapping(value = "/generateStream", produces = "text/html;charset=utf-8")
|
||||
public Flux<String> generateStream(@RequestParam(value = "message", defaultValue = "你是谁?") String message) {
|
||||
// 构建提示词
|
||||
Prompt prompt = new Prompt(new UserMessage(message));
|
||||
|
||||
// 流式输出
|
||||
return chatModel.stream(prompt)
|
||||
.mapNotNull(chatResponse -> {
|
||||
Generation generation = chatResponse.getResult();
|
||||
return generation.getOutput().getText();
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user