Deep_Research / app_deepResearch.py
Swimmersteve's picture
Upload folder using huggingface_hub
9d5750f verified
Raw History Blame Contribute Delete
8.5 kB
import os
import asyncio
from typing import Dict
import gradio as gr
import sendgrid
from sendgrid.helpers.mail import Email, Mail, Content, To
from dotenv import load_dotenv
from pydantic import BaseModel, Field
from agents import Agent, Runner, WebSearchTool, ModelSettings, function_tool, trace, gen_trace_id
load_dotenv(override=True)
# ── Planner Agent ─────────────────────────────────────────────────────────────
HOW_MANY_SEARCHES = 5
PLANNER_INSTRUCTIONS = (
f"You are a helpful research assistant. Given a query, come up with a set of web searches "
f"to perform to best answer the query. Output {HOW_MANY_SEARCHES} terms to query for."
)
class WebSearchItem(BaseModel):
reason: str = Field(description="Your reasoning for why this search is important to the query.")
query: str = Field(description="The search term to use for the web search.")
class WebSearchPlan(BaseModel):
searches: list[WebSearchItem] = Field(
description="A list of web searches to perform to best answer the query."
)
planner_agent = Agent(
name="PlannerAgent",
instructions=PLANNER_INSTRUCTIONS,
model="gpt-4o-mini",
output_type=WebSearchPlan,
)
# ── Search Agent ──────────────────────────────────────────────────────────────
SEARCH_INSTRUCTIONS = (
"You are a research assistant. Given a search term, you search the web for that term and "
"produce a concise summary of the results. The summary must 2-3 paragraphs and less than 300 "
"words. Capture the main points. Write succintly, no need to have complete sentences or good "
"grammar. This will be consumed by someone synthesizing a report, so its vital you capture the "
"essence and ignore any fluff. Do not include any additional commentary other than the summary itself."
)
search_agent = Agent(
name="Search agent",
instructions=SEARCH_INSTRUCTIONS,
tools=[WebSearchTool(search_context_size="low")],
model="gpt-4o-mini",
model_settings=ModelSettings(tool_choice="required"),
)
# ── Writer Agent ──────────────────────────────────────────────────────────────
WRITER_INSTRUCTIONS = (
"You are a senior researcher tasked with writing a cohesive report for a research query. "
"You will be provided with the original query, and some initial research done by a research assistant.\n"
"You should first come up with an outline for the report that describes the structure and "
"flow of the report. Then, generate the report and return that as your final output.\n"
"The final output should be in markdown format, and it should be lengthy and detailed. Aim "
"for 5-10 pages of content, at least 1000 words."
)
class ReportData(BaseModel):
short_summary: str = Field(description="A short 2-3 sentence summary of the findings.")
markdown_report: str = Field(description="The final report")
follow_up_questions: list[str] = Field(description="Suggested topics to research further")
writer_agent = Agent(
name="WriterAgent",
instructions=WRITER_INSTRUCTIONS,
model="gpt-4o-mini",
output_type=ReportData,
)
# ── Email Agent ───────────────────────────────────────────────────────────────
@function_tool
def send_email(subject: str, html_body: str) -> Dict[str, str]:
"""Send an email with the given subject and HTML body"""
sg = sendgrid.SendGridAPIClient(api_key=os.environ.get("SENDGRID_API_KEY"))
from_email = Email("steve@stevetang.net") # put your verified sender here
to_email = To("swimmersteve@gmail.com") # put your recipient here
content = Content("text/html", html_body)
mail = Mail(from_email, to_email, subject, content).get()
response = sg.client.mail.send.post(request_body=mail)
print("Email response", response.status_code)
return "success"
EMAIL_INSTRUCTIONS = (
"You are able to send a nicely formatted HTML email based on a detailed report. "
"You will be provided with a detailed report. You should use your tool to send one email, providing the "
"report converted into clean, well presented HTML with an appropriate subject line."
)
email_agent = Agent(
name="Email agent",
instructions=EMAIL_INSTRUCTIONS,
tools=[send_email],
model="gpt-4o-mini",
)
# ── Research Manager ──────────────────────────────────────────────────────────
class ResearchManager:
async def run(self, query: str):
"""Run the deep research process, yielding status updates and the final report."""
trace_id = gen_trace_id()
with trace("Research trace", trace_id=trace_id):
print(f"View trace: https://platform.openai.com/traces/trace?trace_id={trace_id}")
yield f"View trace: https://platform.openai.com/traces/trace?trace_id={trace_id}"
print("Starting research...")
search_plan = await self.plan_searches(query)
yield "Searches planned, starting to search..."
search_results = await self.perform_searches(search_plan)
yield "Searches complete, writing report..."
report = await self.write_report(query, search_results)
yield "Report written, sending email..."
await self.send_email(report)
yield "Email sent, research complete"
yield report.markdown_report
async def plan_searches(self, query: str) -> WebSearchPlan:
print("Planning searches...")
result = await Runner.run(planner_agent, f"Query: {query}")
print(f"Will perform {len(result.final_output.searches)} searches")
return result.final_output_as(WebSearchPlan)
async def perform_searches(self, search_plan: WebSearchPlan) -> list[str]:
print("Searching...")
num_completed = 0
tasks = [asyncio.create_task(self._search(item)) for item in search_plan.searches]
results = []
for task in asyncio.as_completed(tasks):
result = await task
if result is not None:
results.append(result)
num_completed += 1
print(f"Searching... {num_completed}/{len(tasks)} completed")
print("Finished searching")
return results
async def _search(self, item: WebSearchItem) -> str | None:
input_text = f"Search term: {item.query}\nReason for searching: {item.reason}"
try:
result = await Runner.run(search_agent, input_text)
return str(result.final_output)
except Exception:
return None
async def write_report(self, query: str, search_results: list[str]) -> ReportData:
print("Thinking about report...")
input_text = f"Original query: {query}\nSummarized search results: {search_results}"
result = await Runner.run(writer_agent, input_text)
print("Finished writing report")
return result.final_output_as(ReportData)
async def send_email(self, report: ReportData) -> ReportData:
print("Writing email...")
await Runner.run(email_agent, report.markdown_report)
print("Email sent")
return report
# ── Gradio UI ─────────────────────────────────────────────────────────────────
async def run(query: str):
async for chunk in ResearchManager().run(query):
yield chunk
with gr.Blocks(theme=gr.themes.Default(primary_hue="sky")) as ui:
gr.Markdown("# Deep Research")
query_textbox = gr.Textbox(label="What topic would you like to research?")
run_button = gr.Button("Run", variant="primary")
report = gr.Markdown(label="Report")
run_button.click(fn=run, inputs=query_textbox, outputs=report)
query_textbox.submit(fn=run, inputs=query_textbox, outputs=report)
if __name__ == "__main__":
ui.launch(inbrowser=True)