Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 3 |
+
from langgraph.graph import START, StateGraph, MessagesState
|
| 4 |
+
from langgraph.prebuilt import tools_condition
|
| 5 |
+
from langgraph.prebuilt import ToolNode
|
| 6 |
+
from langchain_core.messages import SystemMessage, HumanMessage
|
| 7 |
+
from tools import *
|
| 8 |
+
|
| 9 |
+
tools = [add, substract, multiply, divide, web_search]
|
| 10 |
+
|
| 11 |
+
class AgentState(TypedDict):
|
| 12 |
+
messages: Annotated[list[AnyMessage], add_messages]
|
| 13 |
+
|
| 14 |
+
def assistant(state: AgentState):
|
| 15 |
+
return {
|
| 16 |
+
"messages": [chat_with_tools.invoke(state["messages"])],
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
def build_agent():
|
| 20 |
+
llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash", temperature=0)
|
| 21 |
+
llm.bind_tools(tools)
|
| 22 |
+
|
| 23 |
+
## The graph
|
| 24 |
+
builder = StateGraph(AgentState)
|
| 25 |
+
|
| 26 |
+
# Define nodes: these do the work
|
| 27 |
+
builder.add_node("assistant", assistant)
|
| 28 |
+
builder.add_node("tools", ToolNode(tools))
|
| 29 |
+
|
| 30 |
+
# Define edges: these determine how the control flow moves
|
| 31 |
+
builder.add_edge(START, "assistant")
|
| 32 |
+
builder.add_conditional_edges(
|
| 33 |
+
"assistant",
|
| 34 |
+
# If the latest message requires a tool, route to tools
|
| 35 |
+
# Otherwise, provide a direct response
|
| 36 |
+
tools_condition,
|
| 37 |
+
)
|
| 38 |
+
builder.add_edge("tools", "assistant")
|
| 39 |
+
|
| 40 |
+
return builder.compile()
|