forked from trpc-group/trpc-agent-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
106 lines (81 loc) · 3.27 KB
/
Copy pathagent.py
File metadata and controls
106 lines (81 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# Tencent is pleased to support the open source community by making tRPC-Agent-Python available.
#
# Copyright (C) 2026 Tencent. All rights reserved.
#
# tRPC-Agent-Python is licensed under Apache-2.0.
""" LangGraph calculator agent with cancellation support. """
from typing import Annotated
from typing_extensions import TypedDict
from langchain.chat_models import init_chat_model
from langchain_core.messages import BaseMessage
from langgraph.graph import START
from langgraph.graph import StateGraph
from langgraph.graph.message import add_messages
from langgraph.prebuilt import ToolNode
from langgraph.prebuilt import tools_condition
from trpc_agent_sdk.agents import LangGraphAgent
from trpc_agent_sdk.agents import langgraph_llm_node
from .config import get_model_config
from .tools import analyze_data
from .tools import calculate
# Define state structure
class State(TypedDict):
messages: Annotated[list[BaseMessage], add_messages]
def build_graph():
"""Build a LangGraph with cancellation support.
This graph demonstrates:
1. Cancellation during LLM streaming (checkpoint in agent)
2. Cancellation during tool execution (checkpoint in agent)
The LangGraphAgent has checkpoints at:
- Method entry
- Each chunk iteration during streaming
"""
# Initialize model
api_key, url, model_name = get_model_config()
model = init_chat_model(
model_name,
model_provider="openai",
api_key=api_key,
base_url=url,
)
tools = [calculate, analyze_data]
llm_with_tools = model.bind_tools(tools)
# Define LLM node with @langgraph_llm_node decorator
@langgraph_llm_node
def chatbot(state: State):
"""Chatbot node that can use tools."""
return {"messages": [llm_with_tools.invoke(state["messages"])]}
# Build graph
graph_builder = StateGraph(State)
graph_builder.add_node("chatbot", chatbot)
# Add tool node
tool_node = ToolNode(tools=tools)
graph_builder.add_node("tools", tool_node)
# Add edges
graph_builder.add_edge(START, "chatbot")
graph_builder.add_conditional_edges("chatbot", tools_condition)
graph_builder.add_edge("tools", "chatbot")
return graph_builder.compile()
def create_agent():
"""Create LangGraph agent with cancellation support.
This agent demonstrates cooperative cancellation at various checkpoints:
- At method entry (before graph execution)
- At each chunk iteration during streaming
- Tool execution can be cancelled when checkpoints are hit
The tools have delays (2-3 seconds) to simulate slow operations,
giving enough time to cancel during execution.
"""
graph = build_graph()
return LangGraphAgent(
name="calculator_agent_with_cancel",
description="A calculator and data analysis assistant that supports cancellation at any time.",
graph=graph,
instruction="""You are a helpful assistant that can:
1. Perform calculations using the calculate tool
2. Analyze data using the analyze_data tool
Your responses may be cancelled by the user at any time. This is normal behavior
and the cancellation mechanism works at various checkpoints including during
tool execution and LLM streaming.
Be professional and helpful in your responses.""",
)
root_agent = create_agent()