Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -5,20 +5,33 @@ 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 |
-
from typing import TypedDict
|
| 9 |
|
| 10 |
tools = [add, multiply, divide, subtract, search_wikipedia]
|
| 11 |
|
| 12 |
def build_agent():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
llm = ChatGroq(model="qwen-qwq-32b", temperature=0)
|
| 14 |
chat_with_tools = llm.bind_tools(tools)
|
| 15 |
|
| 16 |
-
def assistant(state:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
return {
|
| 18 |
-
"messages": [
|
|
|
|
| 19 |
}
|
| 20 |
|
| 21 |
-
def enhancer(state:
|
| 22 |
sys_msg = """
|
| 23 |
You are a helpful assistant tasked with answering questions using a set of tools. You can reason through problems and take actions using available tools to solve complex tasks.
|
| 24 |
Follow this format strictly:
|
|
@@ -35,12 +48,13 @@ def build_agent():
|
|
| 35 |
If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
|
| 36 |
Your answer should only start with "FINAL ANSWER: ", then follows with the answer.
|
| 37 |
"""
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
| 41 |
|
| 42 |
## The graph
|
| 43 |
-
builder = StateGraph(
|
| 44 |
|
| 45 |
# Define nodes: these do the work
|
| 46 |
builder.add_node("enhancer", enhancer)
|
|
|
|
| 5 |
from langgraph.prebuilt import ToolNode
|
| 6 |
from langchain_core.messages import SystemMessage, HumanMessage
|
| 7 |
from tools import *
|
| 8 |
+
from typing import TypedDict, Optional
|
| 9 |
|
| 10 |
tools = [add, multiply, divide, subtract, search_wikipedia]
|
| 11 |
|
| 12 |
def build_agent():
|
| 13 |
+
|
| 14 |
+
class AgentState(TypedDict):
|
| 15 |
+
messages: list # chat history
|
| 16 |
+
wiki_table: Optional[str]
|
| 17 |
+
|
| 18 |
llm = ChatGroq(model="qwen-qwq-32b", temperature=0)
|
| 19 |
chat_with_tools = llm.bind_tools(tools)
|
| 20 |
|
| 21 |
+
def assistant(state: AgentState):
|
| 22 |
+
table = state.get("wiki_table")
|
| 23 |
+
context = []
|
| 24 |
+
|
| 25 |
+
if table:
|
| 26 |
+
context.append({"role": "system", "content": f"Here is a Wikipedia table you can use:\n\n{table}"})
|
| 27 |
+
|
| 28 |
+
response = chat_with_tools.invoke(context + state["messages"])
|
| 29 |
return {
|
| 30 |
+
"messages": [response],
|
| 31 |
+
"wiki_table": table,
|
| 32 |
}
|
| 33 |
|
| 34 |
+
def enhancer(state: AgentState):
|
| 35 |
sys_msg = """
|
| 36 |
You are a helpful assistant tasked with answering questions using a set of tools. You can reason through problems and take actions using available tools to solve complex tasks.
|
| 37 |
Follow this format strictly:
|
|
|
|
| 48 |
If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
|
| 49 |
Your answer should only start with "FINAL ANSWER: ", then follows with the answer.
|
| 50 |
"""
|
| 51 |
+
context = []
|
| 52 |
+
context.append({"role": "system", "content": sys_msg)
|
| 53 |
+
|
| 54 |
+
return context + state["messages"]
|
| 55 |
|
| 56 |
## The graph
|
| 57 |
+
builder = StateGraph(AgentState)
|
| 58 |
|
| 59 |
# Define nodes: these do the work
|
| 60 |
builder.add_node("enhancer", enhancer)
|