Charles Azam commited on
Commit
bd41508
·
1 Parent(s): 915df4b

feat: add support for images

Browse files
src/deepengineer/backend/fast_api_app.py CHANGED
@@ -17,7 +17,7 @@ class AgentRequest(BaseModel):
17
  async def run_agent(request: AgentRequest):
18
  task = request.task
19
  # Run the agent synchronously in a background thread
20
- result = await anyio.to_thread.run_sync(main_search, task)
21
  return JSONResponse(content={"result": result})
22
 
23
 
 
17
  async def run_agent(request: AgentRequest):
18
  task = request.task
19
  # Run the agent synchronously in a background thread
20
+ result, output_image_path = await anyio.to_thread.run_sync(main_search, task)
21
  return JSONResponse(content={"result": result})
22
 
23
 
src/deepengineer/backend/gradio_tools.py CHANGED
@@ -25,7 +25,7 @@ def run_agent_stream(user_input: str):
25
  done = threading.Event()
26
 
27
  def _worker():
28
- answer_container["text"] = main_search(user_input, log_queue)
29
  done.set()
30
 
31
  threading.Thread(target=_worker, daemon=True).start()
 
25
  done = threading.Event()
26
 
27
  def _worker():
28
+ answer_container["text"], output_image_path = main_search(user_input, log_queue)
29
  done.set()
30
 
31
  threading.Thread(target=_worker, daemon=True).start()
src/deepengineer/deepsearch/main_agent.py CHANGED
@@ -18,87 +18,16 @@ from pathlib import Path
18
  import queue
19
 
20
 
21
- def _create_output_image_path():
22
  random_name_images = random.randint(1000000, 9999999)
23
  output_image_path = Path(DATA_DIR) / f"images_{random_name_images}"
24
  output_image_path.mkdir(parents=True, exist_ok=True)
25
  return output_image_path
26
 
27
 
28
- def create_main_deep_search_agent(
29
- main_model_id="deepseek/deepseek-reasoner",
30
- web_search_model_id="deepseek/deepseek-reasoner",
31
- ) -> tuple[CodeAgent, Path]:
32
- """
33
- Deepsearch agent use multiple agents to answer the question.
34
- """
35
- main_model = LiteLLMModel(model_id=main_model_id)
36
-
37
- database = DataBase()
38
- web_search_agent = create_web_search_agent(
39
- model_id=web_search_model_id, database=database
40
- )
41
-
42
- output_image_path = _create_output_image_path()
43
-
44
- manager_agent = CodeAgent(
45
- model=main_model,
46
- tools=[SaveMatplotlibFigTool(output_dir=output_image_path)],
47
- max_steps=12,
48
- verbosity_level=2,
49
- additional_authorized_imports=[
50
- "matplotlib.*",
51
- "numpy.*",
52
- "pandas.*",
53
- "seaborn.*",
54
- "scipy.*",
55
- "sympy.*",
56
- ],
57
- planning_interval=4,
58
- managed_agents=[web_search_agent],
59
- )
60
-
61
- return manager_agent, output_image_path
62
-
63
-
64
- def main_deep_search(task: str):
65
-
66
- MAIN_PROMPT = """
67
- 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.
68
-
69
- 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.
70
-
71
- ## Your Capabilities
72
-
73
- ### **Python and scientific reasoning**
74
- 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.
75
- You are a coding agent so you can use python to test your hypothesis.
76
- You have access to these libraries: matplotlib, numpy, pandas, seaborn, scipy, sympy.
77
- You do not have access to any other library.
78
-
79
- ### Managed Agent: **web_search_agent**
80
- 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.
81
- When calling this agent, you should provide a detailed task, he is extremely powerful.
82
-
83
-
84
- ### **Data Visualization Tools**
85
- - 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.
86
-
87
- You have one question to answer. It is paramount that you provide a correct answer.
88
- 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).
89
- Failure or 'I cannot answer' or 'None found' will not be tolerated, success will be rewarded.
90
- Run verification steps if that's needed, you must make sure you find the correct answer! Here is the task:
91
- {task}
92
- """
93
- agent, output_image_path = create_main_deep_search_agent()
94
-
95
- answer = agent.run(MAIN_PROMPT.format(task=task))
96
-
97
- print(answer)
98
-
99
 
100
  def create_main_search_agent(
101
- model_id="deepseek/deepseek-reasoner", database: DataBase | None = None, log_queue: queue.Queue | None = None
102
  ):
103
  """
104
  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.
@@ -107,8 +36,9 @@ def create_main_search_agent(
107
  model = LiteLLMModel(model_id=model_id)
108
  if database is None:
109
  database = DataBase()
110
-
111
- output_image_path = _create_output_image_path()
 
112
 
113
  # Web search and crawling tools
114
  WEB_SEARCH_TOOLS = [
@@ -140,6 +70,7 @@ def create_main_search_agent(
140
 
141
 
142
  def main_search(task: str, log_queue: queue.Queue | None = None):
 
143
  MAIN_PROMPT = """
144
  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.
145
 
@@ -165,9 +96,10 @@ Failure or 'I cannot answer' or 'None found' will not be tolerated, success will
165
  Run verification steps if that's needed, you must make sure you find the correct answer! Here is the task:
166
  {task}
167
  """
168
- agent = create_main_search_agent(model_id="mistral/mistral-medium-latest", log_queue=log_queue)
169
  answer = agent.run(MAIN_PROMPT.format(task=task))
170
- return answer
 
171
 
172
 
173
  if __name__ == "__main__":
 
18
  import queue
19
 
20
 
21
+ def create_output_image_path():
22
  random_name_images = random.randint(1000000, 9999999)
23
  output_image_path = Path(DATA_DIR) / f"images_{random_name_images}"
24
  output_image_path.mkdir(parents=True, exist_ok=True)
25
  return output_image_path
26
 
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  def create_main_search_agent(
30
+ model_id="deepseek/deepseek-reasoner", database: DataBase | None = None, log_queue: queue.Queue | None = None, output_image_path: Path | None = None,
31
  ):
32
  """
33
  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.
 
36
  model = LiteLLMModel(model_id=model_id)
37
  if database is None:
38
  database = DataBase()
39
+
40
+ output_image_path = output_image_path or DATA_DIR / "images"
41
+ output_image_path.mkdir(parents=True, exist_ok=True)
42
 
43
  # Web search and crawling tools
44
  WEB_SEARCH_TOOLS = [
 
70
 
71
 
72
  def main_search(task: str, log_queue: queue.Queue | None = None):
73
+ output_image_path = create_output_image_path()
74
  MAIN_PROMPT = """
75
  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.
76
 
 
96
  Run verification steps if that's needed, you must make sure you find the correct answer! Here is the task:
97
  {task}
98
  """
99
+ agent = create_main_search_agent(model_id="mistral/mistral-medium-latest", log_queue=log_queue, output_image_path=output_image_path)
100
  answer = agent.run(MAIN_PROMPT.format(task=task))
101
+
102
+ return answer, output_image_path
103
 
104
 
105
  if __name__ == "__main__":
src/deepengineer/deepsearch/main_deep_search.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """work in progress"""
2
+ from pathlib import Path
3
+ import random
4
+ from smolagents import CodeAgent, LiteLLMModel
5
+ from deepengineer.webcrawler.crawl_database import DataBase
6
+ from deepengineer.deepsearch.scawl_web_agent import create_web_search_agent
7
+ from deepengineer.deepsearch.draw_agent import SaveMatplotlibFigTool
8
+ from deepengineer.common_path import DATA_DIR
9
+
10
+ def _create_output_image_path(image_folder_suffix: int | None = None):
11
+ output_image_path = Path(DATA_DIR) / f"images_{image_folder_suffix}"
12
+ output_image_path.mkdir(parents=True, exist_ok=True)
13
+ return output_image_path
14
+
15
+ def create_main_deep_search_agent(
16
+ main_model_id="deepseek/deepseek-reasoner",
17
+ web_search_model_id="deepseek/deepseek-reasoner",
18
+ ) -> tuple[CodeAgent, Path]:
19
+ """
20
+ Deepsearch agent use multiple agents to answer the question.
21
+ """
22
+ main_model = LiteLLMModel(model_id=main_model_id)
23
+
24
+ database = DataBase()
25
+ web_search_agent = create_web_search_agent(
26
+ model_id=web_search_model_id, database=database
27
+ )
28
+
29
+
30
+ image_folder_suffix = random.randint(1000000, 9999999)
31
+ output_image_path = _create_output_image_path(image_folder_suffix=image_folder_suffix)
32
+
33
+ manager_agent = CodeAgent(
34
+ model=main_model,
35
+ tools=[SaveMatplotlibFigTool(output_dir=output_image_path)],
36
+ max_steps=12,
37
+ verbosity_level=2,
38
+ additional_authorized_imports=[
39
+ "matplotlib.*",
40
+ "numpy.*",
41
+ "pandas.*",
42
+ "seaborn.*",
43
+ "scipy.*",
44
+ "sympy.*",
45
+ ],
46
+ planning_interval=4,
47
+ managed_agents=[web_search_agent],
48
+ )
49
+
50
+ return manager_agent, output_image_path
51
+
52
+
53
+ def main_deep_search(task: str):
54
+
55
+ MAIN_PROMPT = """
56
+ 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.
57
+
58
+ 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.
59
+
60
+ ## Your Capabilities
61
+
62
+ ### **Python and scientific reasoning**
63
+ 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.
64
+ You are a coding agent so you can use python to test your hypothesis.
65
+ You have access to these libraries: matplotlib, numpy, pandas, seaborn, scipy, sympy.
66
+ You do not have access to any other library.
67
+
68
+ ### Managed Agent: **web_search_agent**
69
+ 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.
70
+ When calling this agent, you should provide a detailed task, he is extremely powerful.
71
+
72
+
73
+ ### **Data Visualization Tools**
74
+ - 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.
75
+
76
+ You have one question to answer. It is paramount that you provide a correct answer.
77
+ 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).
78
+ Failure or 'I cannot answer' or 'None found' will not be tolerated, success will be rewarded.
79
+ Run verification steps if that's needed, you must make sure you find the correct answer! Here is the task:
80
+ {task}
81
+ """
82
+ agent, output_image_path = create_main_deep_search_agent()
83
+
84
+ answer = agent.run(MAIN_PROMPT.format(task=task))
85
+
86
+ print(answer)