Langgraph #3330
Unanswered
ThangQT2606
asked this question in
Q&A
Langgraph
#3330
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
tôi thực hiện đoạn mã langgraph như dưới đây nhưng kết quả nhận được thì không như mong đợi, mn có thể hỗ trợ tôi được không. Cảm ơn mọi người!
kết quả: Tôi không thể trả lời câu hỏi đó. Tôi không có khả năng thực hiện phép tính toán học.
đoạn mã của tôi:
llm = ChatGoogleGenerativeAI(
model="gemini-1.5-flash",
# model='gemini-2.0-flash-exp',
api_key=google_api_key,
temperature=0.2,
max_output_tokens=512,
convert_system_message_to_human=True
)
@tool
def add_numbers(input_text: str) -> str:
"""Use this tool to add two numbers."""
numbers = [int(s) for s in input_text.split() if s.isdigit()]
return sum(numbers)
from typing import Annotated
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages
from langgraph.checkpoint.memory import MemorySaver
from langgraph.prebuilt import ToolNode, tools_condition
class State(TypedDict):
# Messages have the type "list". The
add_messages
function# in the annotation defines how this state key should be updated
# (in this case, it appends messages to the list, rather than overwriting them)
messages: Annotated[list, add_messages]
memory = MemorySaver()
graph_builder = StateGraph(State)
llm_with_tools = llm.bind_tools([add_numbers])
print(llm_with_tools.invoke("thư viện onnx runtime"))
def chatbot(state: State):
message = llm_with_tools.invoke(state["messages"])
# return {"messages": agent.invoke(state["messages"])['output']}
return {"messages": [message]}
graph_builder.add_node("chatbot", chatbot)
tool_node = ToolNode(tools=[add_numbers])
graph_builder.add_node("tools", tool_node)
graph_builder.add_conditional_edges(
"chatbot",
tools_condition,
{"tools": "tools", END: END},
)
graph_builder.add_edge(START, "chatbot")
graph_builder.add_edge("tools", "chatbot")
graph_builder.add_edge("chatbot", END)
graph = graph_builder.compile(checkpointer=memory)
dưới đây là ảnh đồ thị
![image](https://private-user-images.githubusercontent.com/134129739/410337241-669cc05f-48e5-48ec-aec2-3466a3652077.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzg5MDA2NzgsIm5iZiI6MTczODkwMDM3OCwicGF0aCI6Ii8xMzQxMjk3MzkvNDEwMzM3MjQxLTY2OWNjMDVmLTQ4ZTUtNDhlYy1hZWMyLTM0NjZhMzY1MjA3Ny5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjA3JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIwN1QwMzUyNThaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1mMjNlOWRhM2E3ZTNjMjUyMzQwNDJmNDEzMWMxNjU5NDlkZWY3NGViMDM1NWUyNGYwODA0MTNiOGI0ZTJlOTAzJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.SyzTsFq2xbXdMB7j2PjvFohc-B1bYcow31Tx_CEg920)
user_input = "2 + 2 bằng mấy?"
config = {"configurable": {"thread_id": "1"}}
events = graph.stream(
{"messages": [{"role": "user", "content": user_input}]},
config,
stream_mode="values",
)
for event in events:
print("Event:", event)
if "messages" in event:
event["messages"][-1].pretty_print()
Beta Was this translation helpful? Give feedback.
All reactions