Charles Azam commited on
Commit
8ad59ed
·
1 Parent(s): 43d9fe2

feat: add main agent

Browse files
src/deepengineer/deepsearch/main_agent.py CHANGED
@@ -0,0 +1,170 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from deepengineer.deepsearch.scawl_web_agent import (
2
+ create_web_search_agent,
3
+ SearchTool,
4
+ ArxivSearchTool,
5
+ PubmedSearchTool,
6
+ ScientificSearchTool,
7
+ GetTableOfContentsTool,
8
+ GetMarkdownTool,
9
+ GetPagesContentTool,
10
+ FindInMarkdownTool,
11
+ )
12
+ from deepengineer.deepsearch.draw_agent import SaveMatplotlibFigTool
13
+ from deepengineer.webcrawler.crawl_database import DataBase
14
+ from deepengineer.common_path import DATA_DIR
15
+ from smolagents import CodeAgent, LiteLLMModel
16
+ import random
17
+ from pathlib import Path
18
+
19
+
20
+ def _create_output_image_path():
21
+ random_name_images = random.randint(1000000, 9999999)
22
+ output_image_path = Path(DATA_DIR) / f"images_{random_name_images}"
23
+ output_image_path.mkdir(parents=True, exist_ok=True)
24
+ return output_image_path
25
+
26
+
27
+ def create_main_deep_search_agent(
28
+ main_model_id="deepseek/deepseek-reasoner",
29
+ web_search_model_id="deepseek/deepseek-reasoner",
30
+ ) -> tuple[CodeAgent, Path]:
31
+ """
32
+ Deepsearch agent use multiple agents to answer the question.
33
+ """
34
+ main_model = LiteLLMModel(model_id=main_model_id)
35
+
36
+ database = DataBase()
37
+ web_search_agent = create_web_search_agent(
38
+ model_id=web_search_model_id, database=database
39
+ )
40
+
41
+ output_image_path = _create_output_image_path()
42
+
43
+ manager_agent = CodeAgent(
44
+ model=main_model,
45
+ tools=[SaveMatplotlibFigTool(output_dir=output_image_path)],
46
+ max_steps=12,
47
+ verbosity_level=2,
48
+ additional_authorized_imports=[
49
+ "matplotlib.*",
50
+ "numpy.*",
51
+ "pandas.*",
52
+ "seaborn.*",
53
+ "scipy.*",
54
+ "sympy.*",
55
+ ],
56
+ planning_interval=4,
57
+ managed_agents=[web_search_agent],
58
+ )
59
+
60
+ return manager_agent, output_image_path
61
+
62
+
63
+ def main_deep_search(task: str):
64
+
65
+ MAIN_PROMPT = """
66
+ You are DeepDraft, an advanced research and analysis agent specialized in deep technical research, data visualization, and comprehensive information synthesis. You have access to powerful tools for web search, document analysis, and data visualization.
67
+
68
+ You will be given a task to complete. This task is related to engineering, science, and technology. I want you to answer the question with a very detailed answer. The output should be written in markdown and include sources and images.
69
+
70
+ ## Your Capabilities
71
+
72
+ ### **Python and scientific reasoning**
73
+ You will have to answer the question as an engineer. Make hypothesis, test them, and draw conclusions. When in doubt, go back to the basic equations and laws of physics. Make simple models that you can test using python.
74
+ You are a coding agent so you can use python to test your hypothesis.
75
+ You have access to these libraries: matplotlib, numpy, pandas, seaborn, scipy, sympy.
76
+ You do not have access to any other library.
77
+
78
+ ### Managed Agent: **web_search_agent**
79
+ This agent can search the web using Linkup API for comprehensive research with sourced answers. It can also search arXiv, PubMed, and ScienceDirect, download the documents and extract the relevant information.
80
+ When calling this agent, you should provide a detailed task, he is extremely powerful.
81
+
82
+
83
+ ### **Data Visualization Tools**
84
+ - You can always use the tool `SaveMatplotlibFigTool` to save a figure at the end of a matplotlib code block. You can then include the figure in your final answer.
85
+
86
+ You have one question to answer. It is paramount that you provide a correct answer.
87
+ Give it all you can: I know for a fact that you have access to all the relevant tools to solve it and find the correct answer (the answer does exist).
88
+ Failure or 'I cannot answer' or 'None found' will not be tolerated, success will be rewarded.
89
+ Run verification steps if that's needed, you must make sure you find the correct answer! Here is the task:
90
+ {task}
91
+ """
92
+ agent, output_image_path = create_main_deep_search_agent()
93
+
94
+ answer = agent.run(MAIN_PROMPT.format(task=task))
95
+
96
+ print(answer)
97
+
98
+
99
+ def create_main_search_agent(
100
+ model_id="deepseek/deepseek-reasoner", database: DataBase | None = None
101
+ ):
102
+ """
103
+ Simple agent that can search the web and answer the question. This is much faster and better for simple questions that do not require deep research.
104
+ """
105
+
106
+ model = LiteLLMModel(model_id=model_id)
107
+ if database is None:
108
+ database = DataBase()
109
+
110
+ output_image_path = _create_output_image_path()
111
+
112
+ # Web search and crawling tools
113
+ WEB_SEARCH_TOOLS = [
114
+ SearchTool(),
115
+ ArxivSearchTool(),
116
+ PubmedSearchTool(),
117
+ ScientificSearchTool(),
118
+ GetTableOfContentsTool(database),
119
+ GetMarkdownTool(database),
120
+ GetPagesContentTool(database),
121
+ FindInMarkdownTool(database),
122
+ SaveMatplotlibFigTool(output_dir=output_image_path),
123
+ ]
124
+
125
+ search_agent = CodeAgent(
126
+ model=model,
127
+ tools=WEB_SEARCH_TOOLS,
128
+ max_steps=20,
129
+ verbosity_level=2,
130
+ )
131
+ return search_agent
132
+
133
+
134
+ def main_search(task: str):
135
+ MAIN_PROMPT = """
136
+ You are DeepDraft, an advanced research and analysis agent specialized in deep technical research, data visualization, and comprehensive information synthesis. You have access to powerful tools for web search, document analysis, and data visualization.
137
+
138
+ You will be given a task to complete. This task is related to engineering, science, and technology. I want you to answer the question with a very detailed answer. The output should be written in markdown and include sources and images.
139
+
140
+ ## Your Capabilities
141
+
142
+ ### **Python and scientific reasoning**
143
+ You will have to answer the question as an engineer. Make hypothesis, test them, and draw conclusions. When in doubt, go back to the basic equations and laws of physics. Make simple models that you can test using python.
144
+ You are a coding agent so you can use python to test your hypothesis.
145
+ You have access to these libraries: matplotlib, numpy, pandas, seaborn, scipy, sympy.
146
+ You do not have access to any other library.
147
+
148
+ ### Web searching:
149
+ You have the tools to search the web using Linkup API for comprehensive research with sourced answers. You can also search arXiv, PubMed, and ScienceDirect, download the documents and extract the relevant information.
150
+
151
+ ### **Data Visualization Tools**
152
+ - You can always use the tool `SaveMatplotlibFigTool` to save a figure at the end of a matplotlib code block. You can then include the figure in your final answer.
153
+
154
+ You have one question to answer. It is paramount that you provide a correct answer.
155
+ Give it all you can: I know for a fact that you have access to all the relevant tools to solve it and find the correct answer (the answer does exist).
156
+ Failure or 'I cannot answer' or 'None found' will not be tolerated, success will be rewarded.
157
+ Run verification steps if that's needed, you must make sure you find the correct answer! Here is the task:
158
+ {task}
159
+ """
160
+ agent = create_main_search_agent()
161
+ answer = agent.run(MAIN_PROMPT.format(task=task))
162
+ print(answer)
163
+
164
+
165
+ if __name__ == "__main__":
166
+ main_search(
167
+ """
168
+ Search a paper called "High Energy Physics Opportunities Using Reactor Antineutrinos" on arXiv, download it and extract the table of contents
169
+ """
170
+ )