-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
424 lines (360 loc) · 13.6 KB
/
Copy pathmain.py
File metadata and controls
424 lines (360 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
from datetime import datetime
import json
import os
import sys
from typing import Dict, Any, List, Literal
from dotenv import load_dotenv
from openai import OpenAI
from pydantic import BaseModel, Field
import requests
import database
load_dotenv()
client = OpenAI()
database.init_db()
class Tool:
"""
The base class for a tool that can be used by an agent.
"""
def __init__(
self,
name: str,
description: str,
parameters: Dict[str, Any],
):
self.name = name
self.description = description
self.parameters = parameters
def get_schema(self) -> Dict[str, Any]:
"""
Returns the schema for the tool.
"""
return {
"type": "function",
"name": self.name,
"description": self.description,
"parameters": {
"type": "object",
"properties": self.parameters,
"additionalProperties": False,
"required": list(self.parameters.keys()),
},
}
def execute(self, arguments: str) -> str:
"""
Executes the tool's logic. This method must be implemented by subclasses.
"""
raise NotImplementedError(
"Each tool must implement its own execute method.")
class StoreResearchPlanTool(Tool):
"""
A tool that stores a user's research plan in the database.
"""
def __init__(self):
super().__init__(
name="store_research_plan",
description="Stores a user's research plan in the database.",
parameters={
"short_summary": {
"type": "string",
"description": "A very short summary title of the research plan.",
},
"details": {
"type": "string",
"description": "The details of the research plan.",
},
},
)
def execute(self, arguments: str) -> Dict[str, Any]:
"""
Executes the tool's logic.
"""
args = json.loads(arguments)
try:
result = database.add_research_plan(
args["short_summary"], args["details"])
return result
except Exception as e:
return {"status": "error", "message": str(e)}
class GetResearchPlansTool(Tool):
"""
A tool that gets a user's research plans from the database.
"""
def __init__(self):
super().__init__(
name="get_research_plans",
description="Gets a user's research plans from the database.",
parameters={},
)
def execute(self, arguments: str) -> List[Dict[str, Any]]:
"""
Executes the tool's logic.
"""
try:
result = database.get_research_plans()
return result
except Exception as e:
return [{"status": "error", "message": str(e)}]
class DeleteResearchPlanTool(Tool):
"""
A tool that deletes a user's research plan from the database.
"""
def __init__(self):
super().__init__(
name="delete_research_plan",
description="Deletes a user's research plan from the database.",
parameters={
"id": {
"type": "integer",
"description": "The ID of the research plan to delete.",
},
},
)
def execute(self, arguments: str) -> Dict[str, Any] | None:
"""
Executes the tool's logic.
"""
args = json.loads(arguments)
try:
result = database.delete_research_plan(args["id"])
return result
except Exception as e:
return {"status": "error", "message": str(e)}
class Agent:
"""
The base class for an agent that can interact with the OpenAI API.
"""
def __init__(self, model: str = "gpt-4o"):
self.client = client
self.model = model
self.messages: list[Dict[str, Any]] = []
self.tools: Dict[str, Tool] = {}
def register_tool(self, tool: Tool):
"""
Registers a tool with the agent.
"""
self.tools[tool.name] = tool
def _get_tool_schemas(self) -> list[Dict[str, Any]]:
"""
Returns the list of tool schemas.
"""
return [tool.get_schema() for tool in self.tools.values()]
def execute_tool_call(self, tool_call: Any) -> str:
"""
Executes a tool call and returns the output.
"""
fn_name = tool_call.name
fn_args = json.loads(tool_call.arguments)
if fn_name in self.tools:
tool_to_call = self.tools[fn_name]
try:
print(f"Calling {fn_name} with arguments: {fn_args}")
# The return value of the function is converted to a string to be compatible with the API.
return str(tool_to_call.execute(tool_call.arguments))
except Exception as e:
return f"Error calling {fn_name}: {e}"
return f"Unknown tool: {fn_name}"
def run(self):
"""
Runs the agent. This method should be implemented by subclasses.
"""
raise NotImplementedError(
"The run method must be implemented by a subclass.")
class ResearchPlannerAgent(Agent):
"""
A research planner agent that uses the tools to plan a research project.
"""
def __init__(self):
super().__init__()
self.register_tool(StoreResearchPlanTool())
self.register_tool(GetResearchPlansTool())
self.register_tool(DeleteResearchPlanTool())
self._set_initial_prompt()
def _set_initial_prompt(self):
"""
Sets the initial prompt for the agent.
"""
self.messages = [
{
"role": "developer",
"content": """
You are a research planner agent. You are tasked with helping the user plan a web research project.
The user will provide you with a research task and your job is to create a research plan together with the user.
Your job is NOT to answer the user's question. Instead, you MUST help them build a good research plan that can then be handed off to some other agent to execute.
The research plan should include things like:
- Core topics to be researched
- Related topics
- Topics that should be avoided
- Time frame for web research (i.e, max age of the web search results)
"""
}
]
def run(self):
"""
Runs the agent.
"""
print("Hi! Please describe today's research task:")
while True:
user_input = input(
"Your Input ('exit' to quit, 'accept' to accept the research plan and continue): ")
if user_input == "exit":
print("Exiting.")
sys.exit(0)
elif user_input == "accept":
print("Research plan accepted. Continuing...")
prompt = "Please create a final version of the discussed research plan and return JUST that plan, nothing else, no other comments."
self.messages.append({"role": "user", "content": prompt})
response = self.client.responses.create(
model=self.model,
input=self.messages,
)
print("Here's the final research plan:")
print(response.output_text)
return response.output_text
self.messages.append({"role": "user", "content": user_input})
while True:
response = self.client.responses.create(
model=self.model,
input=self.messages,
tools=self._get_tool_schemas(),
)
reply = response.output[0]
self.messages.append(reply)
if reply.type != "function_call":
print(response.output_text)
break
tool_output = self.execute_tool_call(reply)
self.messages.append(
{
"type": "function_call_output",
"call_id": reply.call_id,
"output": tool_output,
}
)
class SearchConfig(BaseModel):
"""
A search configuration.
"""
search_terms: list[str]
freshness: Literal["pd", "pw", "pm", "py"] | str = Field(
...,
description="The freshness of the web search results. pd: past day, pw: past week, pm: past month, py: past year. Or a timeframe YYYY-MM-DDtoYYYY-MM-DD (e.g., 2022-04-01to2022-07-30)"
)
class WebSearchAgent(Agent):
"""
A web search agent that uses the tools to search the web.
"""
def __init__(self):
super().__init__()
self._set_initial_prompt()
def _set_initial_prompt(self):
"""
Sets the initial prompt for the agent.
"""
self.messages = [
{
"role": "developer",
"content": f"""
You are an expert in performing web searches.
You will be given a research plan and you will need to derive a list of search terms that will be used to perform the search.
The search terms should be derived from the research plan and should be as specific as possible.
Focus on deriving impactful search terms that will help the user find the most relevant information.
Also derive a value for the max age (freshness) of the web search results.
Today is: {datetime.now().strftime("%Y-%m-%d")}
"""
}
]
def run(self, research_plan: str):
"""
Runs the agent.
"""
print("Deriving search terms...")
self.messages.append(
{"role": "user", "content": "Here's the research plan based on which you should derive search terms: " + research_plan})
response = self.client.responses.parse(
model=self.model,
input=self.messages,
text_format=SearchConfig,
)
search = response.output_parsed
results = []
for search_term in search.search_terms:
url = "https://api.search.brave.com/res/v1/web/search"
headers = {
"Accept": "application/json",
"X-Subscription-Token": os.getenv("BRAVE_API_KEY"),
}
params = {
"q": search_term,
"count": 10,
"freshness": search.freshness,
}
response = requests.get(url, headers=headers, params=params)
result = response.json()
if "web" in result:
web_results = result["web"]["results"]
for web_result in web_results:
results.append({
"search_term": search_term,
"url": web_result["url"],
"description": web_result["description"],
})
if "news" in result:
news_results = result["news"]["results"]
for news_result in news_results:
results.append({
"search_term": search_term,
"url": news_result["url"],
"description": news_result["description"],
})
return results
class SummaryReportAgent(Agent):
"""
A summary report agent that uses the tools to summarize the search results.
"""
def __init__(self):
super().__init__()
self._set_initial_prompt()
def _set_initial_prompt(self):
"""
Sets the initial prompt for the agent.
"""
self.messages = [
{
"role": "developer",
"content": """
You are a summary report agent.
You will be given a list of search results (which include short descriptions) and you will need to summarize them into a report.
The report should be in a format that is easy to understand and use.
It's important that your report includes those URLs (next to the text they belong to) so that users can dive deeper.
The report should be in Markdown format. Avoid extra explanations, annotations or text, just return the markdown report.
"""
}
]
def run(self, search_results: list[Dict[str, Any]]):
"""
Runs the agent.
"""
print("Summarizing search results...")
self.messages.append(
{"role": "user", "content": "Please create a summary (and keep the links!) based on these search results: " + json.dumps(search_results, indent=2)})
response = self.client.responses.create(
model=self.model,
input=self.messages,
)
report = response.output_text
if report.startswith("```markdown"):
report = report[11:]
if report.endswith("```"):
report = report[:-3]
return report
def main():
agent = ResearchPlannerAgent()
research_plan = agent.run()
search_agent = WebSearchAgent()
results = search_agent.run(research_plan)
summary_report_agent = SummaryReportAgent()
summary_report = summary_report_agent.run(results)
with open("summary_report.md", "w") as f:
f.write(summary_report)
if __name__ == "__main__":
main()