作者:CSDN博客
State中除了消息列表(messages: Annotated[list, add_messages]),也可以加入其它字段,如下,加入name和birthday字段:- class State(TypedDict):
- messages: Annotated[list, add_messages]
- name: str
- birthday: str
复制代码 使用human_assistance 工具更新state- from langchain_core.messages import ToolMessage
- from langchain_core.tools import InjectedToolCallId, tool
- from langgraph.types import Command, interrupt
- @tool
- def human_assistance(
- name: str, birthday: str, tool_call_id: Annotated[str, InjectedToolCallId]
- ) -> str:
- """Request assistance from a human."""
- human_response = interrupt(
- {
- "question": "Is this correct?",
- "name": name,
- "birthday": birthday,
- },
- )
- # If the information is correct, update the state as-is.
- if human_response.get("correct", "").lower().startswith("y"):
- verified_name = name
- verified_birthday = birthday
- response = "Correct"
- # Otherwise, receive information from the human reviewer.
- else:
- verified_name = human_response.get("name", name)
- verified_birthday = human_response.get("birthday", birthday)
- response = f"Made a correction: {human_response}"
- # This time we explicitly update the state with a ToolMessage inside
- # the tool.
- state_update = {
- "name": verified_name,
- "birthday": verified_birthday,
- "messages": [ToolMessage(response, tool_call_id=tool_call_id)],
- }
- # We return a Command object in the tool to update our state.
- return Command(update=state_update)
复制代码 完整代码:- import os
- from typing import Annotated
- from typing_extensions import
复制代码 原文地址:https://blog.csdn.net/Mrhiuser/article/details/151395406 |