wolf1997 commited on
Commit
36bbe56
·
verified ·
1 Parent(s): 554ebd9

Delete main_agent.py

Browse files
Files changed (1) hide show
  1. main_agent.py +0 -208
main_agent.py DELETED
@@ -1,208 +0,0 @@
1
- from pydantic_ai import Agent, RunContext
2
- from pydantic_ai.common_tools.tavily import tavily_search_tool
3
- from pydantic_ai.messages import ModelMessage
4
- from dotenv import load_dotenv
5
- import os
6
- from pydantic import Field, BaseModel
7
- from typing import Dict, List, Any
8
- from deep_research import Deep_research_engine
9
- from pydantic_ai.models.gemini import GeminiModel
10
- from pydantic_ai.providers.google_gla import GoogleGLAProvider
11
- from dataclasses import dataclass
12
- from typing import Optional
13
- from spire.doc import Document,FileFormat
14
- from spire.doc.common import *
15
- import requests
16
- from table_maker import table_maker_engine
17
- from PIL import Image
18
- from io import BytesIO, StringIO
19
- import tempfile
20
- import pandas as pd
21
-
22
- load_dotenv()
23
- tavily_key=os.getenv('tavily_key')
24
- google_api_key=os.getenv('google_api_key')
25
- pse=os.getenv('pse')
26
-
27
- llm=GeminiModel('gemini-2.0-flash', provider=GoogleGLAProvider(api_key=google_api_key))
28
-
29
-
30
- @dataclass
31
- class Deps:
32
- deep_search_results:dict
33
- quick_search_results:list[str]
34
- table_data:dict
35
-
36
-
37
-
38
-
39
- async def deep_research_agent(ctx:RunContext[Deps], query:str):
40
- """
41
- This function is used to do a deep research on the web for information on a complex query, generates a report or a paper.
42
- Args:
43
- query (str): The query to search for
44
- Returns:
45
- str: The result of the search
46
- """
47
- deepsearch=Deep_research_engine()
48
- res=await deepsearch.chat(query)
49
- ctx.deps.deep_search_results=res
50
- ctx.deps.table_data=res.get('table')
51
- return str(res)
52
-
53
- quick_search_agent=Agent(llm,tools=[tavily_search_tool(tavily_key)])
54
- async def quick_research_agent(ctx: RunContext[Deps], query:str):
55
- """
56
- This function is used to do a quick search on the web for information on a given query.
57
- Args:
58
- query (str): The query to search for
59
- Returns:
60
- str: The result of the search
61
- """
62
- res=await quick_search_agent.run(query)
63
- ctx.deps.quick_search_results.append(res.data)
64
- return str(res.data)
65
-
66
-
67
- def google_image_search(query:str):
68
- """Search for images using Google Custom Search API
69
- args: query
70
- return: image url
71
- """
72
- # Define the API endpoint for Google Custom Search
73
- url = "https://www.googleapis.com/customsearch/v1"
74
-
75
- params = {
76
- "q": query,
77
- "cx": pse,
78
- "key": google_api_key,
79
- "searchType": "image", # Search for images
80
- "num": 1 # Number of results to fetch
81
- }
82
-
83
- # Make the request to the Google Custom Search API
84
- response = requests.get(url, params=params)
85
- data = response.json()
86
-
87
- # Check if the response contains image results
88
- if 'items' in data:
89
- # Extract the first image result
90
- image_url = data['items'][0]['link']
91
- return image_url
92
-
93
-
94
-
95
- async def research_editor_tool(ctx: RunContext[Deps], query:str):
96
- """
97
- Use this tool to edit the deep search result to make it more accurate following the query's instructions.
98
- This tool can modify paragraphs, image_url. For image_url, you need to give the query to search for the image.
99
- Args:
100
- query (str): The query containing instructions for editing the deep search result
101
- Returns:
102
- str: The edited and improved deep search result
103
- """
104
- @dataclass
105
- class edit_route:
106
- paragraph_number:Optional[int] = Field(default_factory=None, description='the number of the paragraph to edit, if the paragraph is not needed to be edited, return None')
107
- route: str = Field(description='the route to the content to edit, either paragraphs, image_url')
108
-
109
-
110
-
111
- paper_dict={'title':ctx.deps.deep_search_results.get('title'),
112
- 'image_url':ctx.deps.deep_search_results.get('image_url') if ctx.deps.deep_search_results.get('image_url') else 'None',
113
- 'paragraphs_title':{num:paragraph.get('title') for num,paragraph in enumerate(ctx.deps.deep_search_results.get('paragraphs'))},
114
- 'table':ctx.deps.deep_search_results.get('table') if ctx.deps.deep_search_results.get('table') else 'None',
115
- 'references':ctx.deps.deep_search_results.get('references')}
116
-
117
- route_agent=Agent(llm,result_type=edit_route, system_prompt="you decide the route to the content to edit based on the query's instructions and the paper_dict, either paragraphs, image_url")
118
- route=await route_agent.run(f'query:{query}, paper_dict:{paper_dict}')
119
- contents=ctx.deps.deep_search_results
120
-
121
-
122
- @dataclass
123
- class Research_edits:
124
- edits:str = Field(description='the edits')
125
- editor_agent=Agent(llm,tools=[google_image_search],result_type=Research_edits, system_prompt="you are an editor, you are given a query, some content to edit, and maybe a quick search result (optional), you need to edit the deep search result to make it more accurate following the query's instructions, return only the edited content, no comments")
126
- if route.data.route=='paragraphs':
127
- content=contents.get('paragraphs')[route.data.paragraph_number]['content']
128
- res=await editor_agent.run(f'query:{query}, content:{content}, quick_search_results:{ctx.deps.quick_search_results if ctx.deps.quick_search_results else "None"}')
129
- ctx.deps.deep_search_results['paragraphs'][route.data.paragraph_number]['content']=res.data.edits
130
- if route.data.route=='image_url':
131
- content=contents.get('image_url')
132
- res=await editor_agent.run(f'query:{query}, content:{content}, quick_search_results:{ctx.deps.quick_search_results if ctx.deps.quick_search_results else "None"}')
133
- ctx.deps.deep_search_results['image_url']=res.data.edits
134
-
135
-
136
- return str(ctx.deps.deep_search_results)
137
-
138
-
139
- async def Table_agent(ctx: RunContext[Deps], query:str):
140
- """
141
- Use this tool to create a table, edit a table or add a table to the deep search result. the add table to paper route is used to create and add a table to the deep search result.
142
- Args:
143
- query (str): The query to create a table, edit a table or add a table to the deep search result
144
- Returns:
145
- dict: The table
146
- """
147
- @dataclass
148
- class route:
149
- route: str = Field(description='the route to the content to edit, either create_table, edit_table, or add_table_to_paper')
150
- route_agent=Agent(llm,result_type=route, system_prompt="you decide the route to the content to edit based on the query's instructions, return only the route, either create_table, edit_table, or add_table_to_paper")
151
- route=await route_agent.run(f'query:{query}')
152
-
153
-
154
- if route.data.route=='create_table':
155
- table_maker=table_maker_engine()
156
- table=await table_maker.chat(query)
157
- ctx.deps.table_data=table
158
- return str(table)
159
-
160
- if route.data.route=='edit_table':
161
- table=ctx.deps.table_data
162
- class Table_row(BaseModel):
163
- data: List[str] = Field(description='the data of the row')
164
- class Table(BaseModel):
165
- rows: List[Table_row] = Field(description='the rows of the table')
166
- columns: List[str] = Field(description='the columns of the table')
167
-
168
- table_editor=Agent(llm, result_type=Table, system_prompt="edit the table based on the query's instructions, the research results (if any) and the quick search results(if any)")
169
- generated_table=await table_editor.run(f'query:{query}, table:{table}, research:{ctx.deps.deep_search_results if ctx.deps.deep_search_results else "None"}, quick_search_results:{ctx.deps.quick_search_results if ctx.deps.quick_search_results else "None"}')
170
- ctx.deps.table_data={'data':[row.data for row in generated_table.data.rows], 'columns':generated_table.data.columns}
171
- return str(ctx.deps.table_data)
172
-
173
- if route.data.route=='add_table_to_paper':
174
- class Table_row(BaseModel):
175
- data: List[str] = Field(description='the data of the row')
176
- class Table(BaseModel):
177
- rows: List[Table_row] = Field(description='the rows of the table')
178
- columns: List[str] = Field(description='the columns of the table')
179
- table_creator=Agent(llm, result_type=Table, system_prompt="create a table based on the query's instructions, the research results (if any) and the quick search results(if any)")
180
- generated_table=await table_creator.run(f'query:{query}, research:{ctx.deps.deep_search_results if ctx.deps.deep_search_results else "None"}, quick_search_results:{ctx.deps.quick_search_results if ctx.deps.quick_search_results else "None"}')
181
- ctx.deps.deep_search_results['table']={'data':[row.data for row in generated_table.data.rows], 'columns':generated_table.data.columns}
182
- ctx.deps.table_data=ctx.deps.deep_search_results['table']
183
- return str(ctx.deps.deep_search_results)
184
-
185
- @dataclass
186
- class Message_state:
187
- messages: list[ModelMessage]
188
-
189
-
190
-
191
- class Main_agent:
192
- def __init__(self):
193
- self.agent=Agent(llm, system_prompt="you are a research assistant, you are given a query, leverage what tool(s) to use, make suggestions to the user about the tools to use, \
194
- never show the output of the tools, except for the table, notify the user about what next step they can take, inform the user about the table,\
195
- and the table's editable nature either in the chat or in the files section",
196
- tools=[deep_research_agent,research_editor_tool,quick_research_agent,Table_agent])
197
- self.deps=Deps( deep_search_results=[], quick_search_results=[], table_data={})
198
- self.memory=Message_state(messages=[])
199
-
200
- async def chat(self, query:str):
201
- result = await self.agent.run(query,deps=self.deps, message_history=self.memory.messages)
202
- self.memory.messages=result.all_messages()
203
- return result.data
204
-
205
- def reset(self):
206
- self.memory.messages=[]
207
- self.deps=Deps( deep_search_results=[], quick_search_results=[], table_data={})
208
-