langchain三种message功能
调用测试。
·
1. HumanMessage:用户身份的消息载体
核心功能:
专门用于包装用户发送的输入内容,向 LangChain 和 LLM 明确传递 “这条消息来自人类用户,是需要响应或处理的需求”。它是对话的 “需求起点”,所有后续的 LLM 思考、工具调用,本质都是为了回应用户通过 HumanMessage 提出的问题或指令。
关键特性:
(1)角色唯一:仅代表 “人类用户”,无其他身份含义。
(2)内容类型:用户的自然语言输入(如问题、指令、补充说明),例如 “帮我查北京明天的天气”
(3)使用场景:
对话初始化时,将用户的第一个查询包装成 HumanMessage 传入 LangChain 对话链(如 ConversationChain);多轮对话中,用户的每一次追问、补充信息,都需用 HumanMessage 包装后追加到 chat_history 数组
tools = [add, subtract, multiply]#可以自己定义简单函数,这里仅作参考
llm_with_tools = llm.bind_tools(tools)
query = "What is 3 + 2?"
chat_history = [HumanMessage(content=query)]
response_1 = llm_with_tools.invoke(chat_history)
chat_history.append(response_1)
2.AiMessage:输出载体
功能
AIMessage 的本质是为 LLM 生成的内容打上 “AI 角色标签”,向 LangChain 框架、外部工具及后续对话流程明确传递:“这条消息由大语言模型生成“ ,它response的内容可能包括对用户需求的直接回答,也可能包含需要触发的工具,以及需要的参数,指令等内容。它解决了 “如何让系统识别‘AI 输出内容’” 的核心问题 —— 若没有 AIMessage 包装,LLM 生成的文本会与用户输入、工具结果混淆,导致对话逻辑断裂(如模型无法追溯自己之前的输出,重复回答或忽略上下文)。
tool_calls_1 = response_1.tool_calls
#llm会判断出tools列表需要使用的工具
tool_1_name = tool_calls_1[0]["name"]
#获取格式化后的参数信息,比如把一句话里的参数经过llm变为字典格式,以便工具调用
tool_1_args = tool_calls_1[0]["args"]
tool_call_1_id = tool_calls_1[0]["id"]
3. ToolMessage:工具身份的结果 / 反馈载体
核心功能
专门用于包装工具(如 API、数据库、计算器等)的输出结果或状态反馈,向 LangChain 和 LLM 明确传递 “这条消息来自工具,是对之前工具调用请求的响应”。它是 “LLM 调用工具” 流程的关键环节。LLM 先生成工具调用指令,工具执行后将结果用 ToolMessage 包装回传,LLM 再基于此结果整理成用户能理解的回答。
关键特性
(1)角色:仅代表 “工具”,传递工具的执行结果(成功数据、错误提示等)。
(2)内容类型:工具的结构化 / 非结构化输出,例如:API 返回的 JSON 数据(如天气 API 返回的 {"city":"北京","temp":25,"weather":"晴"});数据库查询的表格结果;工具执行失败的提示(如 “API 密钥无效,无法获取数据”)。
(3)关联属性:通常需要携带 tool_call_id—— 与 LLM 之前生成的 “工具调用请求”(如 ToolCall 对象)绑定,确保 LLM 能识别 “这是哪个调用的返回结果”(避免多工具调用时结果混淆)。
(4)使用场景:
LLM 判断需要调用工具(如 “查天气需调用天气 API”),生成工具调用指令后,工具执行完毕,将结果用 ToolMessage 包装并追加到 chat_history;
LLM 收到 ToolMessage 后,会分析工具结果是否满足用户需求:若满足,则整理成自然语言回答;若不满足(如数据不全),则可能生成新的工具调用指令,触发下一轮工具交互。
tool_map = {
"add": add,
"subtract": subtract,
"multiply": multiply
}
tool_response = tool_map[tool_1_name].invoke(tool_1_args)
tool_message = ToolMessage(content=tool_response, tool_call_id=tool_call_1_id)
#print(tool_message)
#保留上下文并查看先前的对话,以获得更好的对话体验。
chat_history.append(tool_message)
完整流程总结
class ToolCallingAgent:
def __init__(self, llm):
self.llm_with_tools = llm.bind_tools(tools)
self.tool_map = tool_map
def run(self, query: str) -> str:
# Step 1: Initial user message
chat_history = [HumanMessage(content=query)]
# Step 2: LLM chooses tool
response = self.llm_with_tools.invoke(chat_history)
if not response.tool_calls:
return response.contet # Direct response, no tool needed
# Step 3: Handle first tool call
tool_call = response.tool_calls[0]
tool_name = tool_call["name"]
tool_args = tool_call["args"]
tool_call_id = tool_call["id"]
# Step 4: Call tool manually
tool_result = self.tool_map[tool_name].invoke(tool_args)
# Step 5: Send result back to LLM
tool_message = ToolMessage(content=str(tool_result), tool_call_id=tool_call_id)
chat_history.extend([response, tool_message])
# Step 6: Final LLM result
final_response = self.llm_with_tools.invoke(chat_history)
return final_response.content
调用测试
my_agent = ToolCallingAgent(llm)
print(my_agent.run("one plus 2"))
print(my_agent.run("one - 2"))
print(my_agent.run("three times two"))
更多推荐
所有评论(0)