Spring AI(6) :对话机器人-会话历史

本章代码已分享至Gitee:https://gitee.com/lengcz/ai-study.git

文章目录

本章代码已分享至Gitee:https://gitee.com/lengcz/ai-study.git

会话历史

  1. 添加ChatHisoryRepository
java 复制代码
import java.util.List;

public interface ChatHistoryRepository {

    /**
     * 保存会话记录
     * @param type
     * @param chatId
     */
    void save(String type,String chatId);

    /**
     * 获取会话id集合
     * @param type
     * @return
     */
    List<String> getChatIds(String type);

    /**
     * 删除会话
     * @param chatId
     */
    void delete(String chatId);
}
  1. 添加实现类
java 复制代码
public class InMemoryChatHistoryRepository implements ChatHistoryRepository{

   private final Map<String,List<String>> chatHistory = new HashMap<>();

    @Override
    public void save(String type, String chatId) {
        chatHistory.computeIfAbsent(type,k->new ArrayList<>());
        List<String> chatIds = chatHistory.get(type);
        if(chatIds.contains(chatId)){
            return;
        }
        chatIds.add(chatId);
    }

    @Override
    public List<String> getChatIds(String type) {
        return chatHistory.getOrDefault(type,new ArrayList<>());
    }

    @Override
    public void delete(String chatId) {

    }
}
  1. 聊天时保存chatId
java 复制代码
//1.保存会话id
   chatHistoryRepository.save("chat",chatId);
  1. 编写获取chatIds 的接口
java 复制代码
@RequiredArgsConstructor //有参构造器
@RestController
@RequestMapping("/ai/history")
public class ChatHistoryController {

    private final ChatHistoryRepository chatHistoryRepository;

    private final ChatMemory chatMemory;

    /**
     * 获取会话ID列表
     * @param type
     * @return
     */
    @GetMapping("/{type}")
    public List<String> getChatIds(@PathVariable("type") String type){
        return chatHistoryRepository.getChatIds(type);
    }
}
  1. 启动服务器测试这个接口,这样这个页面的左侧聊天历史就可以了。
bash 复制代码
http://localhost:8080/ai/history/chat
  1. 编写历史消息接口,定义一个消息实体MessageVO ,然后通过ChatMemory查询历史消息,并将消息转换成刚才定义的List< MessageVO >
java 复制代码
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.ai.chat.messages.Message;

@NoArgsConstructor
@Data
public class MessageVO {

    private String role;

    private String content;

    public MessageVO(Message message){
        switch (message.getMessageType()){
            case USER:
                role ="user";
                break;
            case ASSISTANT:
                role = "assistant";
                break;
//            case  SYSTEM:
//                role = "system";
//                break;
//            case TOOL:
//                role = "function";
//                break;
            default:
                role= "unknown";
                break;
        }
        this.content = message.getText();
    }
}
java 复制代码
**
     * 查询历史消息详情
     * @param type
     * @param chatId
     * @return
     */
    @GetMapping("/{type}/{chatId}")
    public List<MessageVO> getChatHistory(@PathVariable("type") String type, @PathVariable("chatId") String chatId){
        List<Message> messages = chatMemory.get(chatId, Integer.MAX_VALUE);
        if(null== messages){
            return List.of();
        }
        return messages.stream().map(MessageVO::new).toList();
    }
  1. 上面完成了查询聊天详情的接口,启动服务,然后增加几个聊天内容和记录,然后查看接口消息。


    聊天后,刷新页面,会发现我们的聊天内容还在,这就说明了,会话的历史已经记录了。
相关推荐
汤姆宝库1 小时前
蒸馏的三层实现:logits级、序列级与数据级
人工智能·深度学习·机器学习
云半S一2 小时前
Agent学习总结补充:2
人工智能·经验分享·笔记·agent·学习分享
Resean02232 小时前
架构设计系列(十六):DDD 与代码架构
java·开发语言·架构
学长毕设3 小时前
基于SpringBoot 车辆租赁系统平台-附源码
java·spring boot·后端
wflynn3 小时前
Inkling 2 Mini:26B 参数循环 Transformer 的本地多模态模型
人工智能·ai
hryou09223 小时前
模型已经会欺骗评估员?24人爆料,OpenAI为何考虑停训
人工智能·ai编程
Csvn3 小时前
Agent 能调工具那天,权限就该已经修好了——最小权限 + 人工确认(S03)
人工智能·aigc·agent
Eyfcom4 小时前
〖物流低代码实战 06〗AI 生成 Schema 的工程边界:大模型能否直接接管快递软件的页面搭建?
人工智能·低代码·快递软件·物流低代码实战
甲维斯4 小时前
GLM5.4和GPT6.1赛马,搞定超级玛丽32关!
人工智能
qyr67894 小时前
全球汽车和航空航天用磁铁材料市场竞争格局变化趋势
大数据·人工智能·物联网·自动化·汽车和航空航天用磁铁材料