当前位置: 首页 > news >正文

[langchian]使用langchain构建一个chatbot

https://python.langchain.com/docs/tutorials/chatbot/
很明显,到这一部分难度上来了。

pip install langchain-core langgraph

当我们调用大模型的时候,如果没有进行额外的操作,大模型是没有记忆状态的。在langchain中可以说两次llm.invoke()是独立的。
为了让大模型了解上下文,我们需要把对话的历史传递个大模型。

from langchain.chat_models import init_chat_model
from langchain_core.messages import HumanMessagefrom dotenv import load_dotenv
load_dotenv()model = init_chat_model("deepseek-chat")
result = model.invoke([HumanMessage(content="Hi! I'm Bob")])
print(result)result2 = model.invoke([HumanMessage(content="What's my name?")])
print(result2)result3 = model.invoke([HumanMessage(content="Hi! I'm Bob"),AIMessage(content="Hello Bob! How can I assist you today?"),HumanMessage(content="What's my name?"),]
)
print(result3)

Message 持久化

persistence这词我还没记住
在这里插入图片描述

为了实现message persistence,引入了langgraph的概念,langgraph到底是什么我们后面再了解,这里先简单使用下。

from langchain.chat_models import init_chat_model
from langchain_core.messages import HumanMessage,AIMessagefrom langgraph.checkpoint.memory import MemorySaver
from langgraph.graph import START, MessagesState, StateGraphfrom dotenv import load_dotenv
load_dotenv()model = init_chat_model("deepseek-chat")# Define a new graph
workflow = StateGraph(state_schema=MessagesState)# Define the function that calls the model
def call_model(state: MessagesState):response = model.invoke(state["messages"])return {"messages": response}# Define the (single) node in the graph
workflow.add_edge(START, "model")
workflow.add_node("model", call_model)# Add memory
memory = MemorySaver()
app = workflow.compile(checkpointer=memory)config = {"configurable": {"thread_id": "abc123"}}query = "Hi! I'm Bob."input_messages = [HumanMessage(query)]
output = app.invoke({"messages": input_messages}, config)
output["messages"][-1].pretty_print()  # output contains all messages in statequery = "What's my name?"input_messages = [HumanMessage(query)]
output = app.invoke({"messages": input_messages}, config)
output["messages"][-1].pretty_print()

Managing Conversation History

One important concept to understand when building chatbots is how to manage conversation history. If left unmanaged, the list of messages will grow unbounded and potentially overflow the context window of the LLM. Therefore, it is important to add a step that limits the size of the messages you are passing in.

from langchain_core.messages import trim_messages

对messages进行裁剪。

由于大多数语言模型对输入的 token 数量有限制,当对话历史过长时,需要进行裁剪(trim)以避免超出限制。采取合适的trim_messages 策略就是用来适当地裁剪消息,保留最关键的信息,同时移除不必要或较旧的内容。

看到这里我才发现,即使是一个看起来很小的功能,就是消息裁剪的功能,里面的细节也是多了去了。
比如用户说,请帮我记住我的地址是上海,那么当发生裁剪时,这条消息是最不应该被裁剪的。这就是一条user case,如何实现这个user case呢?

首先messages要入库,至于这个库是内存还是数据库不重要,重要的是messages要先存起来,然后再来说我们要怎么处理它。


全栈工程师的涌现,之前也有全栈工程师,但是数量极其有限,自从大模型出来之后,全栈变得容易了。

http://www.dtcms.com/a/326375.html

相关文章:

  • JS深拷贝 浅拷贝、CSS垂直水平居中
  • CRM(客户关系管理)框架详解
  • 【09-神经网络介绍2】
  • 快速了解TF-IDF算法
  • 高精度蓝牙定位:技术、应用与未来发展
  • AI Copilot
  • istio如何采集method、url指标
  • Linux系统编程Day12 -- 环境变量(初识)
  • [特殊字符][特殊字符][特殊字符]【Maven】pom依赖的版本推荐与依赖冲突问题
  • C#使用EPPlus读写Excel
  • 定制化4G专网架构,满足多行业专属需求
  • 在线代码比对工具
  • HTML5中华美食网站源码
  • 布控球:临时布防场景的高清回传利器-伟博
  • 双椒派™ E2000D 开发板深度解析
  • 【Altium designer】一键添加多个器件参数的“备注”
  • conda一键配置python开发环境
  • echarts 柱状图堆叠踩坑指南 (已解决)
  • 读《精益数据分析》:媒体内容平台全链路梳理
  • 超算中心的机器上怎么部署Linux的?
  • 3.6 修改vuex的状态Mutations ,Actions
  • Tricentis Tosca:现代软件测试的自动化利器
  • Java 包装类简单认识泛型
  • Mysql——单表最多数据量多少需要分表
  • Redis 01 数据结构
  • SSM+Dubbo+Zookeeper框架和springcloud框架,写业务的时候主要区别在哪?
  • 【listlist模拟】
  • 提升行车安全的关键技术:BSD(盲点监测)与DSM(驾驶员监测)是如何工作的?
  • AI(领域)应用落地技术决策指南:从双路径架构到系统性实施
  • Centos 用http ftp搭建本地yum源 保姆级教程