Spaces:
Sleeping
Sleeping
Upload 7 files
Browse files- app.py +30 -0
- email_agent.py +54 -0
- requirements.txt +12 -0
- research_manager.py +143 -0
- research_planner.py +32 -0
- research_writer.py +42 -0
- web_searcher.py +34 -0
app.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dotenv import load_dotenv
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
from research_manager import ResearchManager
|
| 6 |
+
|
| 7 |
+
load_dotenv(override=True)
|
| 8 |
+
|
| 9 |
+
async def run(query: str):
|
| 10 |
+
|
| 11 |
+
async for chunk in ResearchManager().run(query):
|
| 12 |
+
|
| 13 |
+
yield chunk
|
| 14 |
+
|
| 15 |
+
with gr.Blocks(theme=gr.themes.Default(primary_hue="sky")) as user_interface:
|
| 16 |
+
|
| 17 |
+
gr.Markdown("Digital Research Assistant")
|
| 18 |
+
|
| 19 |
+
query = gr.Textbox(label="Hi there, what would you like to research on today?")
|
| 20 |
+
|
| 21 |
+
execute_button = gr.Button("Execute", variant="primary")
|
| 22 |
+
|
| 23 |
+
research_report = gr.Markdown(label="Research Report")
|
| 24 |
+
|
| 25 |
+
execute_button.click(fn= run, inputs=query, outputs=research_report)
|
| 26 |
+
|
| 27 |
+
query.submit(fn = run, inputs = query, outputs = research_report)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
user_interface.launch()
|
email_agent.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
from typing import Dict
|
| 4 |
+
|
| 5 |
+
import sendgrid
|
| 6 |
+
|
| 7 |
+
from sendgrid import SendGridAPIClient
|
| 8 |
+
|
| 9 |
+
from sendgrid.helpers.mail import To, Email, Mail, Content
|
| 10 |
+
|
| 11 |
+
from agents import Agent, function_tool
|
| 12 |
+
|
| 13 |
+
from dotenv import load_dotenv
|
| 14 |
+
|
| 15 |
+
load_dotenv(override=True)
|
| 16 |
+
|
| 17 |
+
@function_tool
|
| 18 |
+
def send_email(subject: str, html_body: str) -> Dict[str, str]:
|
| 19 |
+
|
| 20 |
+
"""Send out a given email with the given subject and html body. """
|
| 21 |
+
|
| 22 |
+
sendgrid_client = SendGridAPIClient(api_key = os.environ.get("SENDGRID_API_KEY"))
|
| 23 |
+
|
| 24 |
+
to_email = To("faiaza037@gmail.com")
|
| 25 |
+
|
| 26 |
+
from_email = Email("faiazrex8@gmail.com")
|
| 27 |
+
|
| 28 |
+
content = Content("text/html", html_body)
|
| 29 |
+
|
| 30 |
+
mail = Mail(to_emails=to_email, from_email=from_email, subject=subject, html_content=content)
|
| 31 |
+
|
| 32 |
+
response = sendgrid_client.send(mail)
|
| 33 |
+
|
| 34 |
+
return {"status" : "success"}
|
| 35 |
+
|
| 36 |
+
email_agent_instructions = """
|
| 37 |
+
|
| 38 |
+
You send nice HTML formatted emails based on a detailed report as you will be provided a detailed report.
|
| 39 |
+
|
| 40 |
+
Use your available tool to send one email providing the report is converted to a clean, well structured HTML with appropriate subject line.
|
| 41 |
+
|
| 42 |
+
"""
|
| 43 |
+
|
| 44 |
+
email_agent = Agent(
|
| 45 |
+
|
| 46 |
+
name = "Email Agent",
|
| 47 |
+
|
| 48 |
+
instructions = email_agent_instructions,
|
| 49 |
+
|
| 50 |
+
tools = [send_email],
|
| 51 |
+
|
| 52 |
+
model = "gpt-4o"
|
| 53 |
+
|
| 54 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Core dependencies
|
| 2 |
+
requests>=2.31.0
|
| 3 |
+
python-dotenv>=1.0.0
|
| 4 |
+
gradio>=4.19.2
|
| 5 |
+
sendgrid>=6.11.0
|
| 6 |
+
openai>=1.12.0
|
| 7 |
+
openai-agents>=0.0.17
|
| 8 |
+
|
| 9 |
+
# Development dependencies
|
| 10 |
+
pytest>=7.4.0
|
| 11 |
+
black>=23.7.0
|
| 12 |
+
flake8>=6.1.0
|
research_manager.py
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
|
| 3 |
+
from typing import Dict
|
| 4 |
+
|
| 5 |
+
from agents import trace, gen_trace_id, Runner
|
| 6 |
+
|
| 7 |
+
from web_searcher import web_searcher
|
| 8 |
+
|
| 9 |
+
from research_planner import research_planner, WebSearchItem, WebSearchResultsList
|
| 10 |
+
|
| 11 |
+
from research_writer import research_writer, ReportFormat
|
| 12 |
+
|
| 13 |
+
from email_agent import email_agent
|
| 14 |
+
|
| 15 |
+
class ResearchManager:
|
| 16 |
+
|
| 17 |
+
async def run(self, query: str):
|
| 18 |
+
|
| 19 |
+
"""Runs the deep research process, yielding the status updates and final report."""
|
| 20 |
+
|
| 21 |
+
trace_id = gen_trace_id()
|
| 22 |
+
|
| 23 |
+
with trace("Research in progress", trace_id=trace_id):
|
| 24 |
+
|
| 25 |
+
print(f"View trace: https://platform.openai.com/traces/{trace_id}")
|
| 26 |
+
|
| 27 |
+
yield f"View trace: https://platform.openai.com/traces/{trace_id}"
|
| 28 |
+
|
| 29 |
+
print("Initializing research.....")
|
| 30 |
+
|
| 31 |
+
research_plan = await self.plan_search(query)
|
| 32 |
+
|
| 33 |
+
yield "Search planning completed, preparing for search"
|
| 34 |
+
|
| 35 |
+
search_results = await self.perform_search(research_plan)
|
| 36 |
+
|
| 37 |
+
yield "Searches complete, drafting report...."
|
| 38 |
+
|
| 39 |
+
research_report = await self.draft_research_report(query, search_results)
|
| 40 |
+
|
| 41 |
+
yield "Research report drafted. Sending email with the report attached."
|
| 42 |
+
|
| 43 |
+
await self.send_email(research_report)
|
| 44 |
+
|
| 45 |
+
yield "Email sent successfully! Research Task completed."
|
| 46 |
+
|
| 47 |
+
yield research_report.markdown_report
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
async def plan_search(self, query: str) -> WebSearchResultsList:
|
| 51 |
+
|
| 52 |
+
"""Plans search to be perfomed for the query"""
|
| 53 |
+
|
| 54 |
+
print("Planning searches")
|
| 55 |
+
|
| 56 |
+
results = await Runner.run(research_planner, f"Query:{query}")
|
| 57 |
+
|
| 58 |
+
print(f"Will perform {len(results.final_output.web_search_results)} searches")
|
| 59 |
+
|
| 60 |
+
return results.final_output_as(WebSearchResultsList)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
async def perform_search(self, research_plan: WebSearchResultsList)-> list[str]:
|
| 64 |
+
|
| 65 |
+
"""Runs the searches to perform for the query"""
|
| 66 |
+
|
| 67 |
+
print("Searching....")
|
| 68 |
+
|
| 69 |
+
num_completed = 0
|
| 70 |
+
|
| 71 |
+
tasks = [asyncio.create_task(self.search(item)) for item in research_plan.web_search_results]
|
| 72 |
+
|
| 73 |
+
results = []
|
| 74 |
+
|
| 75 |
+
for task in asyncio.as_completed(tasks):
|
| 76 |
+
|
| 77 |
+
result = await task
|
| 78 |
+
|
| 79 |
+
if result is not None:
|
| 80 |
+
|
| 81 |
+
results.append(result)
|
| 82 |
+
|
| 83 |
+
num_completed += 1
|
| 84 |
+
|
| 85 |
+
print(f"Searches completed: {num_completed}/{len(tasks)} completed.")
|
| 86 |
+
|
| 87 |
+
print("Search Complete!")
|
| 88 |
+
|
| 89 |
+
return results
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
async def search(self, item: WebSearchItem) -> str | None:
|
| 93 |
+
|
| 94 |
+
"""Performs a search for the query"""
|
| 95 |
+
|
| 96 |
+
input = f"Query : {item.query}, Reason for searching/querying: {item.query}"
|
| 97 |
+
|
| 98 |
+
try:
|
| 99 |
+
|
| 100 |
+
result = await Runner.run(web_searcher, input)
|
| 101 |
+
|
| 102 |
+
except Exception:
|
| 103 |
+
|
| 104 |
+
return None
|
| 105 |
+
|
| 106 |
+
async def draft_research_report(self, query:str, search_results: list[str]) -> ReportFormat:
|
| 107 |
+
|
| 108 |
+
"""Drafting a research report for the query"""
|
| 109 |
+
|
| 110 |
+
print("Preparing research report....")
|
| 111 |
+
|
| 112 |
+
input = f"Original query: {query}, Summarized searches: {search_results}"
|
| 113 |
+
|
| 114 |
+
result = await Runner.run(research_writer, input)
|
| 115 |
+
|
| 116 |
+
print("Draft of research report completed!")
|
| 117 |
+
|
| 118 |
+
return result.final_output_as(ReportFormat)
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
async def send_email(self, research_report: ReportFormat) -> None:
|
| 122 |
+
|
| 123 |
+
print("Sending email with research report attached....")
|
| 124 |
+
|
| 125 |
+
result = await Runner.run(email_agent, research_report.markdown_report)
|
| 126 |
+
|
| 127 |
+
print("Email sent sucessfully with research report attached!")
|
| 128 |
+
|
| 129 |
+
return research_report
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
|
research_planner.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydantic import BaseModel
|
| 2 |
+
|
| 3 |
+
from agents import Agent
|
| 4 |
+
|
| 5 |
+
how_many_searches = 3
|
| 6 |
+
|
| 7 |
+
research_planner_agent_instructions = f"You are a fellow and a very helpful research assistant. Given a query, come up with a set of web searches to best answer the query.Output {how_many_searches} terms to query for"
|
| 8 |
+
|
| 9 |
+
class WebSearchItem(BaseModel):
|
| 10 |
+
|
| 11 |
+
reason: str
|
| 12 |
+
"Your reasoning and thought process of why this search is important to the query."
|
| 13 |
+
|
| 14 |
+
query: str
|
| 15 |
+
"The search term for to use for web searches"
|
| 16 |
+
|
| 17 |
+
class WebSearchResultsList(BaseModel):
|
| 18 |
+
|
| 19 |
+
web_search_results : list[WebSearchItem]
|
| 20 |
+
|
| 21 |
+
"""List of relevant web links of the query search result."""
|
| 22 |
+
|
| 23 |
+
research_planner = Agent(
|
| 24 |
+
|
| 25 |
+
name = "Research Planner",
|
| 26 |
+
|
| 27 |
+
instructions = research_planner_agent_instructions,
|
| 28 |
+
|
| 29 |
+
output_type = WebSearchResultsList,
|
| 30 |
+
|
| 31 |
+
model = "gpt-4o"
|
| 32 |
+
)
|
research_writer.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydantic import BaseModel
|
| 2 |
+
|
| 3 |
+
from agents import Agent
|
| 4 |
+
|
| 5 |
+
research_writer_instructions = """
|
| 6 |
+
|
| 7 |
+
You are a well experienced research writer tasked to preparing a cohesive report for a research query.
|
| 8 |
+
|
| 9 |
+
You will be provided with the original query, and a few initial research draft done by a research assistant.
|
| 10 |
+
|
| 11 |
+
You must first come up with the outline of the report that talks about the structure and flow of the report.
|
| 12 |
+
|
| 13 |
+
Finally, generate the report and give that as your final output.
|
| 14 |
+
|
| 15 |
+
The final output should be in a markdown format, and it should be lengthy and detailed, yet easy to understand.
|
| 16 |
+
|
| 17 |
+
Deliver at least 5 to 7 pages of content of minimum 500 to 700 words.
|
| 18 |
+
|
| 19 |
+
"""
|
| 20 |
+
|
| 21 |
+
class ReportFormat(BaseModel):
|
| 22 |
+
|
| 23 |
+
summary: str
|
| 24 |
+
"A summary of the report generated"
|
| 25 |
+
|
| 26 |
+
markdown_report: str
|
| 27 |
+
"The final report"
|
| 28 |
+
|
| 29 |
+
follow_up_questions: list[str]
|
| 30 |
+
"Further relevant and associated research areas to discuss and explore further"
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
research_writer = Agent(
|
| 34 |
+
|
| 35 |
+
name = "Research Writer",
|
| 36 |
+
|
| 37 |
+
instructions = research_writer_instructions,
|
| 38 |
+
|
| 39 |
+
model = "gpt-4o",
|
| 40 |
+
|
| 41 |
+
output_type = ReportFormat
|
| 42 |
+
)
|
web_searcher.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from agents import Agent
|
| 2 |
+
|
| 3 |
+
from agents import WebSearchTool
|
| 4 |
+
|
| 5 |
+
from agents import ModelSettings
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
web_searcher_instructions = """
|
| 9 |
+
|
| 10 |
+
You are a very sucessful and award-winning research assistant.
|
| 11 |
+
|
| 12 |
+
Given a search term, you will always search the web for that term and generate a robust summary of the results.
|
| 13 |
+
|
| 14 |
+
The summary must be strictly only 2 to 3 paragraphs and not more than 400 words.
|
| 15 |
+
|
| 16 |
+
Capture only the essential and feasible points, write it in a very easy to understand way yet in extremely impacting tone.
|
| 17 |
+
|
| 18 |
+
Do not include any unneccasary information other than the summary itself
|
| 19 |
+
|
| 20 |
+
"""
|
| 21 |
+
|
| 22 |
+
web_searcher = Agent(
|
| 23 |
+
|
| 24 |
+
name = "Research Assistant",
|
| 25 |
+
|
| 26 |
+
instructions = web_searcher_instructions,
|
| 27 |
+
|
| 28 |
+
tools = [WebSearchTool(search_context_size = "low")],
|
| 29 |
+
|
| 30 |
+
model_settings = ModelSettings(tool_choice = "required"),
|
| 31 |
+
|
| 32 |
+
model = "gpt-4o"
|
| 33 |
+
)
|
| 34 |
+
|