Spaces:
Sleeping
Sleeping
José Ángel González
commited on
Commit
·
4b0a0b7
1
Parent(s):
7e7b35e
HfApiModel
Browse files- agents/react_agent.py +21 -22
agents/react_agent.py
CHANGED
@@ -1,17 +1,3 @@
|
|
1 |
-
"""
|
2 |
-
React-Agent based solution for the smolagents course.
|
3 |
-
|
4 |
-
Based on two pillars:
|
5 |
-
|
6 |
-
1) Avoid unnecessary logic inside the agent's react flow. Why we have to let the LLM to decide how to load/parse
|
7 |
-
specific files if we can do that beforehand and we can prepare it accordingly? Less prone to errors, shorter
|
8 |
-
reasoning paths, and more accurate.
|
9 |
-
|
10 |
-
2) Stronger LLMs are all you need. GPT-4o does not work very well with the system prompt of smolagents,
|
11 |
-
but GPT-4.5 yes! The sentence of "or your mom will die" in the steered system prompt is just for GPT-4o to work :)
|
12 |
-
|
13 |
-
"""
|
14 |
-
|
15 |
from smolagents import (
|
16 |
OpenAIServerModel,
|
17 |
CodeAgent,
|
@@ -20,6 +6,7 @@ from smolagents import (
|
|
20 |
PythonInterpreterTool,
|
21 |
VisitWebpageTool,
|
22 |
Tool,
|
|
|
23 |
)
|
24 |
from PIL import Image
|
25 |
import requests
|
@@ -90,16 +77,17 @@ AUTHORIZED_IMPORTS = ["json", "pandas", "numpy", "datetime", "requests", "bs4"]
|
|
90 |
|
91 |
class ReactAgent:
|
92 |
def __init__(self):
|
93 |
-
model = OpenAIServerModel(
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
)
|
98 |
self.agent = CodeAgent(
|
99 |
-
model=model,
|
|
|
100 |
tools=TOOLS,
|
101 |
additional_authorized_imports=AUTHORIZED_IMPORTS,
|
102 |
-
max_steps=
|
103 |
verbosity_level=2,
|
104 |
)
|
105 |
self.steer_system_prompt()
|
@@ -128,6 +116,17 @@ class ReactAgent:
|
|
128 |
prev_system_prompt = self.agent.system_prompt
|
129 |
prompt_prefix = prev_system_prompt.split("Now Begin!")[0].strip()
|
130 |
gaia_answer_rules = """\n\nYour 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."""
|
131 |
-
gaia_answer_rules += """ You must wrap
|
132 |
system_prompt = prompt_prefix + gaia_answer_rules + "\n\nNow Begin!"
|
133 |
self.agent.system_prompt = system_prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from smolagents import (
|
2 |
OpenAIServerModel,
|
3 |
CodeAgent,
|
|
|
6 |
PythonInterpreterTool,
|
7 |
VisitWebpageTool,
|
8 |
Tool,
|
9 |
+
HfApiModel
|
10 |
)
|
11 |
from PIL import Image
|
12 |
import requests
|
|
|
77 |
|
78 |
class ReactAgent:
|
79 |
def __init__(self):
|
80 |
+
#model = OpenAIServerModel(
|
81 |
+
# model_id="gpt-4o",
|
82 |
+
# api_key=os.environ["OPENAI_API_KEY"],
|
83 |
+
# temperature=0,
|
84 |
+
#)
|
85 |
self.agent = CodeAgent(
|
86 |
+
#model=model,
|
87 |
+
model=HfApiModel(),
|
88 |
tools=TOOLS,
|
89 |
additional_authorized_imports=AUTHORIZED_IMPORTS,
|
90 |
+
max_steps=20,
|
91 |
verbosity_level=2,
|
92 |
)
|
93 |
self.steer_system_prompt()
|
|
|
116 |
prev_system_prompt = self.agent.system_prompt
|
117 |
prompt_prefix = prev_system_prompt.split("Now Begin!")[0].strip()
|
118 |
gaia_answer_rules = """\n\nYour 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."""
|
119 |
+
gaia_answer_rules += """ You must wrap your final answer in the ```code``` block by using the `final_answer` tool or your mom will die."""
|
120 |
system_prompt = prompt_prefix + gaia_answer_rules + "\n\nNow Begin!"
|
121 |
self.agent.system_prompt = system_prompt
|
122 |
+
|
123 |
+
if __name__ == "__main__":
|
124 |
+
question4 = {
|
125 |
+
"task_id": "99c9cc74-fdc8-46c6-8f8d-3ce2d3bfeea3",
|
126 |
+
"question": 'Hi, I\'m making a pie but I could use some help with my shopping list. I have everything I need for the crust, but I\'m not sure about the filling. I got the recipe from my friend Aditi, but she left it as a voice memo and the speaker on my phone is buzzing so I can\'t quite make out what she\'s saying. Could you please listen to the recipe and list all of the ingredients that my friend described? I only want the ingredients for the filling, as I have everything I need to make my favorite pie crust. I\'ve attached the recipe as Strawberry pie.mp3.\n\nIn your response, please only list the ingredients, not any measurements. So if the recipe calls for "a pinch of salt" or "two cups of ripe strawberries" the ingredients on the list would be "salt" and "ripe strawberries".\n\nPlease format your response as a comma separated list of ingredients. Also, please alphabetize the ingredients.',
|
127 |
+
"Level": "1",
|
128 |
+
"file_name": "99c9cc74-fdc8-46c6-8f8d-3ce2d3bfeea3.mp3",
|
129 |
+
}
|
130 |
+
|
131 |
+
agent = ReactAgent()
|
132 |
+
response = agent(question4)
|