Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import TypedDict, Annotated
|
| 2 |
+
from tools import add, substract, multiply, divide, DuckDuckGoSearchTool, WikipediaSearchTool, ArxivSearchTool, PubmedSearchTool
|
| 3 |
+
|
| 4 |
+
from langgraph.graph.message import add_messages
|
| 5 |
+
from langchain_core.messages import AnyMessage, HumanMessage, AIMessage
|
| 6 |
+
from langgraph.graph import StateGraph, START, END, MessagesState
|
| 7 |
+
from langgraph.prebuilt import ToolNode, tools_condition
|
| 8 |
+
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
# load the system prompt from the file
|
| 12 |
+
with open("system_prompt.txt", "r", encoding="utf-8") as f:
|
| 13 |
+
system_prompt = f.read()
|
| 14 |
+
|
| 15 |
+
# System message
|
| 16 |
+
sys_msg = SystemMessage(content=system_prompt)
|
| 17 |
+
|
| 18 |
+
# Making the agent
|
| 19 |
+
llm = HuggingFaceEndpoint(
|
| 20 |
+
repo_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 21 |
+
huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN,
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
chat = ChatHuggingFace(llm=llm, verbose=True)
|
| 25 |
+
tools = [add,
|
| 26 |
+
substract,
|
| 27 |
+
multiply,
|
| 28 |
+
divide,
|
| 29 |
+
DuckDuckGoSearchTool,
|
| 30 |
+
WikipediaSearchTool,
|
| 31 |
+
ArxivSearchTool,
|
| 32 |
+
PubmedSearchTool]
|
| 33 |
+
|
| 34 |
+
chat_with_tools = chat.bind_tools(tools)
|
| 35 |
+
|
| 36 |
+
## Defining our nodes
|
| 37 |
+
def assistant(state: MessagesState):
|
| 38 |
+
"""Assistant node"""
|
| 39 |
+
return {"messages": [sys_msg] + [chat_with_tools.invoke(state["messages"])]}
|
| 40 |
+
|
| 41 |
+
# Build graph / nodes
|
| 42 |
+
builder = StateGraph(State)
|
| 43 |
+
builder.add_node("assistant", chat) # Assistant
|
| 44 |
+
builder.add_node("tools", ToolNode(tools)) # Tools
|
| 45 |
+
|
| 46 |
+
# Logic / edges
|
| 47 |
+
builder.add_edge(START, "assistant")
|
| 48 |
+
builder.add_conditional_edges("assistant", tools_condition)
|
| 49 |
+
builder.add_edge("tools", "assistant")
|
| 50 |
+
|
| 51 |
+
graph = builder.compile()
|