update prompt
Browse files
agent.py
CHANGED
|
@@ -80,6 +80,14 @@ def web_search(keywords: str, max_results:int = 5) -> str:
|
|
| 80 |
# return ''
|
| 81 |
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
class AgentState(TypedDict):
|
| 84 |
messages: List[Union[HumanMessage, AIMessage, SystemMessage]]
|
| 85 |
|
|
@@ -126,6 +134,11 @@ def answer_message(state: AgentState) -> AgentState:
|
|
| 126 |
Do not include any thought process before answering the question, and only response exactly what was being asked of you.
|
| 127 |
If you are not able to provide an answer, use tools or state the limitation that you're facing instead.
|
| 128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
Example question: How many hours are there in a day?
|
| 130 |
Response: 24
|
| 131 |
""")]
|
|
@@ -165,51 +178,6 @@ def answer_message(state: AgentState) -> AgentState:
|
|
| 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():
|
| 215 |
agent_graph = StateGraph(AgentState)
|
|
@@ -217,12 +185,10 @@ 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)
|
|
|
|
| 80 |
# return ''
|
| 81 |
|
| 82 |
|
| 83 |
+
system_prompt = """
|
| 84 |
+
You are a helpful assistant tasked with answering questions using a set of tools.
|
| 85 |
+
Now, I will ask you a question. Report your thoughts, and finish your answer with the following template:
|
| 86 |
+
FINAL ANSWER: [YOUR FINAL ANSWER].
|
| 87 |
+
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. 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.
|
| 88 |
+
Your answer should only start with "FINAL ANSWER: ", then follows with the answer.
|
| 89 |
+
"""
|
| 90 |
+
|
| 91 |
class AgentState(TypedDict):
|
| 92 |
messages: List[Union[HumanMessage, AIMessage, SystemMessage]]
|
| 93 |
|
|
|
|
| 134 |
Do not include any thought process before answering the question, and only response exactly what was being asked of you.
|
| 135 |
If you are not able to provide an answer, use tools or state the limitation that you're facing instead.
|
| 136 |
|
| 137 |
+
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
|
| 138 |
+
If you are asked for a number, don't use comma to write your number, and don't use units such as $ or percent sign unless specified otherwise.
|
| 139 |
+
If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise.
|
| 140 |
+
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.
|
| 141 |
+
|
| 142 |
Example question: How many hours are there in a day?
|
| 143 |
Response: 24
|
| 144 |
""")]
|
|
|
|
| 178 |
messages.append(final_response)
|
| 179 |
return {"messages": messages}
|
| 180 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
def build_graph():
|
| 183 |
agent_graph = StateGraph(AgentState)
|
|
|
|
| 185 |
# Add nodes
|
| 186 |
agent_graph.add_node("read_message", read_message)
|
| 187 |
agent_graph.add_node("answer_message", answer_message)
|
|
|
|
| 188 |
|
| 189 |
# Add edges
|
| 190 |
agent_graph.add_edge(START, "read_message")
|
| 191 |
agent_graph.add_edge("read_message", "answer_message")
|
|
|
|
| 192 |
|
| 193 |
# Final edge
|
| 194 |
agent_graph.add_edge("answer_message", END)
|