Paperbag commited on
Commit
9df4794
·
1 Parent(s): 1be319c

trying to add thought process

Browse files
Files changed (2) hide show
  1. agent.py +52 -0
  2. requirements.txt +2 -2
agent.py CHANGED
@@ -90,6 +90,12 @@ def read_message(state: AgentState) -> AgentState:
90
  # Just pass the messages through to the next node
91
  return {"messages": messages}
92
 
 
 
 
 
 
 
93
  # def tool_message(state: AgentState) -> AgentState:
94
  # messages = state["messages"]
95
  # prompt = f"""
@@ -159,6 +165,50 @@ def answer_message(state: AgentState) -> AgentState:
159
  messages.append(final_response)
160
  return {"messages": messages}
161
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
162
 
163
 
164
  def build_graph():
@@ -167,10 +217,12 @@ def build_graph():
167
  # Add nodes
168
  agent_graph.add_node("read_message", read_message)
169
  agent_graph.add_node("answer_message", answer_message)
 
170
 
171
  # Add edges
172
  agent_graph.add_edge(START, "read_message")
173
  agent_graph.add_edge("read_message", "answer_message")
 
174
 
175
  # Final edge
176
  agent_graph.add_edge("answer_message", END)
 
90
  # Just pass the messages through to the next node
91
  return {"messages": messages}
92
 
93
+ def restart_required(state: AgentState) -> AgentState:
94
+ messages = state["messages"]
95
+ print(f"Processing question: {messages[-1].content if messages else ''}")
96
+ # Just pass the messages through to the next node
97
+ return {"messages": messages}
98
+
99
  # def tool_message(state: AgentState) -> AgentState:
100
  # messages = state["messages"]
101
  # prompt = f"""
 
165
  messages.append(final_response)
166
  return {"messages": messages}
167
 
168
+ def thought_process(state: AgentState) -> AgentState:
169
+ messages = state["messages"]
170
+ prompt = [SystemMessage(f"""
171
+ You are a GAIA question answering assistant.
172
+ Your task is to list down the steps and chain of thoughts to answer the question provided carefully.
173
+ Think carefully before answering the question.
174
+ The steps provided should simplify the thinking of the another person and help them to derive the answer.
175
+ Include steps that require the use of specific tools or state the limitation that you're facing.
176
+ """)]
177
+ messages = prompt + messages
178
+
179
+ # First pass: let model decide whether to call web_search
180
+ ai_msg = model_with_tools.invoke(messages)
181
+ messages.append(ai_msg)
182
+
183
+ # If the model didn't request any tools, its content is already the answer
184
+ tool_calls = getattr(ai_msg, "tool_calls", None) or []
185
+ if not tool_calls:
186
+ print(f"Final response: {ai_msg}")
187
+ return {"messages": messages}
188
+
189
+ # Execute requested tools and append their text output into the conversation
190
+ for tool_call in tool_calls:
191
+ name = tool_call["name"]
192
+ args = tool_call["args"]
193
+ tool = tools_by_name[name]
194
+ tool_result = tool.invoke(args) # this is a plain string from web_search
195
+ messages.append(HumanMessage(content=f"Tool result ({name}):\n{tool_result}"))
196
+
197
+ # Second pass: force a plain-text final answer (no tool calls expected)
198
+ final_instruction = HumanMessage(
199
+ content=(
200
+ "Using the tool results above, provide the list of steps answer now. "
201
+ "Do not call any tools. Respond with only the chain of thought."
202
+ )
203
+ )
204
+ messages.append(final_instruction)
205
+
206
+ final_response = model.invoke(messages)
207
+ print(f"Final response: {final_response}")
208
+
209
+ # Return messages including the final AIMessage so BasicAgent reads .content
210
+ messages.append(final_response)
211
+ return {"thoughts": messages}
212
 
213
 
214
  def build_graph():
 
217
  # Add nodes
218
  agent_graph.add_node("read_message", read_message)
219
  agent_graph.add_node("answer_message", answer_message)
220
+ agent_graph.add_node("thought_process", thought_process)
221
 
222
  # Add edges
223
  agent_graph.add_edge(START, "read_message")
224
  agent_graph.add_edge("read_message", "answer_message")
225
+ agent_graph.add_conditional_edges("answer_message", restart_required, {True:"thought_process", False:END})
226
 
227
  # Final edge
228
  agent_graph.add_edge("answer_message", END)
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
- gradio
2
  requests
3
  langchain
4
  langchain-community
@@ -9,7 +9,7 @@ langchain-groq
9
  langchain-tavily
10
  langchain-chroma
11
  langgraph
12
- huggingface_hub
13
  supabase
14
  arxiv
15
  pymupdf
 
1
+ gradio >=4.44.0
2
  requests
3
  langchain
4
  langchain-community
 
9
  langchain-tavily
10
  langchain-chroma
11
  langgraph
12
+ huggingface_hub >=0.26.0
13
  supabase
14
  arxiv
15
  pymupdf