vlapparov commited on
Commit
95c20b1
·
verified ·
1 Parent(s): bfa48a0

Create agent.py

Browse files
Files changed (1) hide show
  1. agent.py +134 -0
agent.py ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ from linkup_utils import LinkupSearchTool
3
+ from text_inspector_tool import TextInspectorTool
4
+ from text_web_browser import (
5
+ ArchiveSearchTool,
6
+ FinderTool,
7
+ FindNextTool,
8
+ PageDownTool,
9
+ PageUpTool,
10
+ SimpleTextBrowser,
11
+ VisitTool,
12
+ )
13
+ from visual_qa import visualizer
14
+
15
+ from smolagents import (
16
+ CodeAgent,
17
+ LiteLLMModel,
18
+ ToolCallingAgent,
19
+ )
20
+
21
+
22
+ AUTHORIZED_IMPORTS = [
23
+ "requests",
24
+ "zipfile",
25
+ "os",
26
+ "pandas",
27
+ "numpy",
28
+ "sympy",
29
+ "json",
30
+ "bs4",
31
+ "pubchempy",
32
+ "xml",
33
+ "yahoo_finance",
34
+ "Bio",
35
+ "sklearn",
36
+ "scipy",
37
+ "pydub",
38
+ "io",
39
+ "PIL",
40
+ "chess",
41
+ "PyPDF2",
42
+ "pptx",
43
+ "torch",
44
+ "datetime",
45
+ "fractions",
46
+ "csv",
47
+ ]
48
+
49
+ # load_dotenv(override=True)
50
+ # login(os.getenv("HF_TOKEN"))
51
+
52
+ append_answer_lock = threading.Lock()
53
+
54
+
55
+ def parse_args():
56
+ parser = argparse.ArgumentParser()
57
+ parser.add_argument(
58
+ "question", type=str, help="for example: 'How many studio albums did Mercedes Sosa release before 2007?'"
59
+ )
60
+ parser.add_argument("--model-id", type=str, default="o1")
61
+ return parser.parse_args()
62
+
63
+
64
+ custom_role_conversions = {"tool-call": "assistant", "tool-response": "user"}
65
+
66
+ user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0"
67
+
68
+ BROWSER_CONFIG = {
69
+ "viewport_size": 1024 * 5,
70
+ "downloads_folder": "downloads_folder",
71
+ "request_kwargs": {
72
+ "headers": {"User-Agent": user_agent},
73
+ "timeout": 300,
74
+ },
75
+ "serpapi_key": os.getenv("SERPAPI_API_KEY"),
76
+ }
77
+
78
+ os.makedirs(f"./{BROWSER_CONFIG['downloads_folder']}", exist_ok=True)
79
+
80
+
81
+ def create_agent(model_id="gpt-4o-mini"):
82
+ model_params = {
83
+ "model_id": model_id,
84
+ "custom_role_conversions": custom_role_conversions,
85
+ "max_completion_tokens": 8192,
86
+ }
87
+ if model_id == "o1":
88
+ model_params["reasoning_effort"] = "high"
89
+ model = LiteLLMModel(**model_params)
90
+
91
+ text_limit = 100000
92
+ browser = SimpleTextBrowser(**BROWSER_CONFIG)
93
+ WEB_TOOLS = [
94
+ LinkupSearchTool(),
95
+ VisitTool(browser),
96
+ PageUpTool(browser),
97
+ PageDownTool(browser),
98
+ FinderTool(browser),
99
+ FindNextTool(browser),
100
+ ArchiveSearchTool(browser),
101
+ TextInspectorTool(model, text_limit),
102
+ ]
103
+ text_webbrowser_agent = ToolCallingAgent(
104
+ model=model,
105
+ tools=WEB_TOOLS,
106
+ max_steps=20,
107
+ verbosity_level=2,
108
+ planning_interval=4,
109
+ name="search_agent",
110
+ description="""A team member that will search the internet to answer your question.
111
+ Ask him for all your questions that require browsing the web.
112
+ Note that this agent is using a powerful language model and it can do the search and analyse the results.
113
+ You should ask question in a way to let the language model to perform the best, i.e. provide as much context as possible and ask in a clear way.
114
+ Provide him as much context as possible, in particular if you need to search on a specific timeframe!
115
+ And don't hesitate to provide him with a complex search task, like finding a difference between two webpages.
116
+ Your request must be a real sentence, not a google search! Like "Find me this information (...)" rather than a few keywords.
117
+ """,
118
+ provide_run_summary=True,
119
+ )
120
+ text_webbrowser_agent.prompt_templates["managed_agent"]["task"] += """You can navigate to .txt online files.
121
+ If a non-html page is in another format, especially .pdf or a Youtube video, use tool 'inspect_file_as_text' to inspect it.
122
+ Additionally, if after some searching you find out that you need more information to answer the question, you can use `final_answer` with your request for clarification as argument to request for more information."""
123
+
124
+ manager_agent = CodeAgent(
125
+ model=model,
126
+ tools=[visualizer, TextInspectorTool(model, text_limit)],
127
+ max_steps=12,
128
+ verbosity_level=2,
129
+ additional_authorized_imports=AUTHORIZED_IMPORTS,
130
+ planning_interval=4,
131
+ managed_agents=[text_webbrowser_agent],
132
+ )
133
+
134
+ return manager_agent