wt002 commited on
Commit
dd95935
·
verified ·
1 Parent(s): db22cb0

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +3 -40
agent.py CHANGED
@@ -36,37 +36,13 @@ def calculate(expression: str) -> str:
36
  python_repl = PythonREPL()
37
  return python_repl.run(expression)
38
 
39
- def build_graph(tools: list, agent_executor: AgentExecutor) -> StateGraph:
40
- """Build and return the compiled workflow graph"""
41
- workflow = StateGraph(AgentState)
42
-
43
- def run_agent(state: AgentState) -> Dict[str, Any]:
44
- response = agent_executor.invoke({"messages": state["messages"]})
45
- return {"messages": [response["output"]]}
46
-
47
- def should_continue(state: AgentState) -> str:
48
- last_message = state["messages"][-1]
49
- return "continue" if last_message.additional_kwargs.get("tool_calls") else "end"
50
-
51
- workflow.add_node("agent", run_agent)
52
- workflow.add_node("tools", ToolNode(tools))
53
- workflow.set_entry_point("agent")
54
- workflow.add_conditional_edges(
55
- "agent",
56
- should_continue,
57
- {"continue": "tools", "end": END}
58
- )
59
- workflow.add_edge("tools", "agent")
60
-
61
- return workflow.compile()
62
-
63
  class AIAgent:
64
  def __init__(self, model_name: str = "gpt-3.5-turbo"):
65
  self.tools = [wikipedia_search, web_search, calculate]
66
  self.llm = ChatOpenAI(model=model_name, temperature=0.7)
67
  self.agent_executor = self._build_agent_executor()
68
- self.workflow = build_graph(self.tools, self.agent_executor) # Using the standalone function
69
-
70
  def _build_agent_executor(self) -> AgentExecutor:
71
  """Build the agent executor"""
72
  prompt = ChatPromptTemplate.from_messages([
@@ -135,17 +111,4 @@ class AIAgent:
135
  if hasattr(msg, 'additional_kwargs') and 'tool_calls' in msg.additional_kwargs:
136
  for call in msg.additional_kwargs['tool_calls']:
137
  steps.append(f"Used {call['function']['name']}: {call['function']['arguments']}")
138
- return steps
139
-
140
- if __name__ == "__main__":
141
- agent = AIAgent()
142
- response = agent("What's the capital of France?")
143
- print("Response:", response["response"])
144
- if response["sources"]:
145
- print("\nSources:")
146
- for source in response["sources"]:
147
- print("-", source)
148
- if response["steps"]:
149
- print("\nSteps:")
150
- for step in response["steps"]:
151
- print("-", step)
 
36
  python_repl = PythonREPL()
37
  return python_repl.run(expression)
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  class AIAgent:
40
  def __init__(self, model_name: str = "gpt-3.5-turbo"):
41
  self.tools = [wikipedia_search, web_search, calculate]
42
  self.llm = ChatOpenAI(model=model_name, temperature=0.7)
43
  self.agent_executor = self._build_agent_executor()
44
+ self.workflow = self._build_workflow() # Correct attribute name
45
+
46
  def _build_agent_executor(self) -> AgentExecutor:
47
  """Build the agent executor"""
48
  prompt = ChatPromptTemplate.from_messages([
 
111
  if hasattr(msg, 'additional_kwargs') and 'tool_calls' in msg.additional_kwargs:
112
  for call in msg.additional_kwargs['tool_calls']:
113
  steps.append(f"Used {call['function']['name']}: {call['function']['arguments']}")
114
+ return steps