Upload agent.py
Browse files
agent.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import TypedDict, Annotated
|
| 2 |
+
from langgraph.graph.message import add_messages
|
| 3 |
+
from langchain_core.messages import AnyMessage
|
| 4 |
+
from langgraph.graph import START, StateGraph
|
| 5 |
+
from langgraph.prebuilt import ToolNode, tools_condition
|
| 6 |
+
from langchain_ollama import ChatOllama
|
| 7 |
+
from tools import search_tool, weather_info_tool, hub_stats_tool
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
+
load_dotenv()
|
| 10 |
+
|
| 11 |
+
llm = ChatOllama(model="mistral")
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
tools = [search_tool, weather_info_tool, hub_stats_tool]
|
| 15 |
+
chat_with_tools = llm.bind_tools(tools)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class AgentState(TypedDict):
|
| 19 |
+
messages: Annotated[list[AnyMessage], add_messages]
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def assistant(state: AgentState):
|
| 23 |
+
return {
|
| 24 |
+
"messages": [chat_with_tools.invoke(state["messages"])]
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
builder = StateGraph(AgentState)
|
| 29 |
+
builder.add_node("assistant", assistant)
|
| 30 |
+
builder.add_node("tools", ToolNode(tools))
|
| 31 |
+
builder.add_edge(START, "assistant")
|
| 32 |
+
builder.add_conditional_edges("assistant", tools_condition)
|
| 33 |
+
builder.add_edge("tools", "assistant")
|
| 34 |
+
alfred = builder.compile()
|