Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files- README.md +3 -9
- __pycache__/email_agent.cpython-313.pyc +0 -0
- __pycache__/planner_agent.cpython-313.pyc +0 -0
- __pycache__/research_manager.cpython-313.pyc +0 -0
- __pycache__/search_agent.cpython-313.pyc +0 -0
- __pycache__/writer_agent.cpython-313.pyc +0 -0
- deep_research.py +22 -0
- email_agent.py +31 -0
- planner_agent.py +29 -0
- research_manager.py +84 -0
- search_agent.py +19 -0
- writer_agent.py +28 -0
README.md
CHANGED
|
@@ -1,12 +1,6 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
|
| 4 |
-
colorFrom: blue
|
| 5 |
-
colorTo: yellow
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 5.
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
---
|
| 11 |
-
|
| 12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Deep_Research
|
| 3 |
+
app_file: deep_research.py
|
|
|
|
|
|
|
| 4 |
sdk: gradio
|
| 5 |
+
sdk_version: 5.35.0
|
|
|
|
|
|
|
| 6 |
---
|
|
|
|
|
|
__pycache__/email_agent.cpython-313.pyc
ADDED
|
Binary file (1.8 kB). View file
|
|
|
__pycache__/planner_agent.cpython-313.pyc
ADDED
|
Binary file (1.16 kB). View file
|
|
|
__pycache__/research_manager.cpython-313.pyc
ADDED
|
Binary file (5.57 kB). View file
|
|
|
__pycache__/search_agent.cpython-313.pyc
ADDED
|
Binary file (994 Bytes). View file
|
|
|
__pycache__/writer_agent.cpython-313.pyc
ADDED
|
Binary file (1.49 kB). View file
|
|
|
deep_research.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
from research_manager import ResearchManager
|
| 4 |
+
|
| 5 |
+
load_dotenv(override=True)
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
async def run(query: str):
|
| 9 |
+
async for chunk in ResearchManager().run(query):
|
| 10 |
+
yield chunk
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
with gr.Blocks(theme=gr.themes.Default(primary_hue="sky")) as ui:
|
| 14 |
+
gr.Markdown("# Deep Research")
|
| 15 |
+
query_textbox = gr.Textbox(label="What topic would you like to research?")
|
| 16 |
+
run_button = gr.Button("Run", variant="primary")
|
| 17 |
+
report = gr.Markdown(label="Report")
|
| 18 |
+
|
| 19 |
+
run_button.click(fn=run, inputs=query_textbox, outputs=report)
|
| 20 |
+
query_textbox.submit(fn=run, inputs=query_textbox, outputs=report)
|
| 21 |
+
|
| 22 |
+
ui.launch(inbrowser=True)
|
email_agent.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from typing import Dict
|
| 3 |
+
|
| 4 |
+
import sendgrid
|
| 5 |
+
from sendgrid.helpers.mail import Email, Mail, Content, To
|
| 6 |
+
from agents import Agent, function_tool
|
| 7 |
+
|
| 8 |
+
@function_tool
|
| 9 |
+
def send_email(subject: str, html_body: str) -> Dict[str, str]:
|
| 10 |
+
""" Send out an email with the given subject and HTML body """
|
| 11 |
+
sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY'))
|
| 12 |
+
from_email = Email("jai.300012723027@csvtu.ac.in") # Change this to your verified email
|
| 13 |
+
to_email = To("ksharma9719@gmail.com") # Change this to your email
|
| 14 |
+
content = Content("text/html", html_body)
|
| 15 |
+
mail = Mail(from_email, to_email, subject, content).get()
|
| 16 |
+
response = sg.client.mail.send.post(request_body=mail)
|
| 17 |
+
return {"status": "success"}
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
INSTRUCTIONS = """
|
| 21 |
+
You are able to send a nicely formatted HTML email based on a detailed report.
|
| 22 |
+
You will be provided with detailed report. You should use your tool to send one email,
|
| 23 |
+
providing the report converted into clean, well presented HTML with an appropriate subject line.
|
| 24 |
+
"""
|
| 25 |
+
|
| 26 |
+
email_agent = Agent(
|
| 27 |
+
name = "Email Agent",
|
| 28 |
+
instructions = INSTRUCTIONS,
|
| 29 |
+
model='gpt-4o-mini',
|
| 30 |
+
tools=[send_email]
|
| 31 |
+
)
|
planner_agent.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydantic import BaseModel
|
| 2 |
+
from agents import Agent
|
| 3 |
+
|
| 4 |
+
HOW_MANY_SEARCHES = 3
|
| 5 |
+
|
| 6 |
+
INSTRUCTIONS = f"You are a helpful research assistant. Given a query, come up with \
|
| 7 |
+
a set of web searches to perform to best answer the query. Output {HOW_MANY_SEARCHES} terms to query for"
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class WebSearchItem(BaseModel):
|
| 11 |
+
reason: str
|
| 12 |
+
"""Your reasoning for why this search is important for this query."""
|
| 13 |
+
|
| 14 |
+
query: str
|
| 15 |
+
"""The search term to use for the web search."""
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class WebSearchPlan(BaseModel):
|
| 19 |
+
searches: list[WebSearchItem]
|
| 20 |
+
"""A list of web searches to perform to best answer the query."""
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
planner_agent = Agent(
|
| 25 |
+
name="Planner Agent",
|
| 26 |
+
instructions=INSTRUCTIONS,
|
| 27 |
+
model = 'gpt-4o-mini',
|
| 28 |
+
output_type = WebSearchPlan
|
| 29 |
+
)
|
research_manager.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from agents import Runner, trace, gen_trace_id
|
| 2 |
+
from search_agent import search_agent
|
| 3 |
+
from planner_agent import planner_agent, WebSearchItem, WebSearchPlan
|
| 4 |
+
from writer_agent import writer_agent, ReportData
|
| 5 |
+
from email_agent import email_agent
|
| 6 |
+
import asyncio
|
| 7 |
+
|
| 8 |
+
class ResearchManager:
|
| 9 |
+
|
| 10 |
+
async def run(self, query: str):
|
| 11 |
+
""" Run the deep research process, yielding the status updates and the final report"""
|
| 12 |
+
trace_id = gen_trace_id()
|
| 13 |
+
with trace("Research trace", trace_id=trace_id):
|
| 14 |
+
print(f"View trace: https://platform.openai.com/traces/trace?trace_id={trace_id}")
|
| 15 |
+
yield f"View trace: https://platform.openai.com/traces/trace?trace_id={trace_id}"
|
| 16 |
+
print("Starting research...")
|
| 17 |
+
search_plan = await self.plan_searches(query)
|
| 18 |
+
yield "Searches planned, starting to search..."
|
| 19 |
+
search_results = await self.perform_searches(search_plan)
|
| 20 |
+
yield "Searches complete, writing report..."
|
| 21 |
+
report = await self.write_report(query, search_results)
|
| 22 |
+
yield "Report written, sending email..."
|
| 23 |
+
await self.send_email(report)
|
| 24 |
+
yield "Email sent, research complete"
|
| 25 |
+
yield report.markdown_report
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
async def plan_searches(self, query: str) -> WebSearchPlan:
|
| 29 |
+
""" Plan the searches to perform for the query """
|
| 30 |
+
print("Planning searches...")
|
| 31 |
+
result = await Runner.run(
|
| 32 |
+
planner_agent,
|
| 33 |
+
f"Query: {query}",
|
| 34 |
+
)
|
| 35 |
+
print(f"Will perform {len(result.final_output.searches)} searches")
|
| 36 |
+
return result.final_output_as(WebSearchPlan)
|
| 37 |
+
|
| 38 |
+
async def perform_searches(self, search_plan: WebSearchPlan) -> list[str]:
|
| 39 |
+
""" Perform the searches to perform for the query """
|
| 40 |
+
print("Searching...")
|
| 41 |
+
num_completed = 0
|
| 42 |
+
tasks = [asyncio.create_task(self.search(item)) for item in search_plan.searches]
|
| 43 |
+
results = []
|
| 44 |
+
for task in asyncio.as_completed(tasks):
|
| 45 |
+
result = await task
|
| 46 |
+
if result is not None:
|
| 47 |
+
results.append(result)
|
| 48 |
+
num_completed += 1
|
| 49 |
+
print(f"Searching... {num_completed}/{len(tasks)} completed")
|
| 50 |
+
print("Finished searching")
|
| 51 |
+
return results
|
| 52 |
+
|
| 53 |
+
async def search(self, item: WebSearchItem) -> str | None:
|
| 54 |
+
""" Perform a search for the query """
|
| 55 |
+
input = f"Search term: {item.query}\nReason for searching: {item.reason}"
|
| 56 |
+
try:
|
| 57 |
+
result = await Runner.run(
|
| 58 |
+
search_agent,
|
| 59 |
+
input,
|
| 60 |
+
)
|
| 61 |
+
return str(result.final_output)
|
| 62 |
+
except Exception:
|
| 63 |
+
return None
|
| 64 |
+
|
| 65 |
+
async def write_report(self, query: str, search_results: list[str]) -> ReportData:
|
| 66 |
+
""" Write the report for the query """
|
| 67 |
+
print("Thinking about report...")
|
| 68 |
+
input = f"Original query: {query}\nSummarized search results: {search_results}"
|
| 69 |
+
result = await Runner.run(
|
| 70 |
+
writer_agent,
|
| 71 |
+
input,
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
print("Finished writing report")
|
| 75 |
+
return result.final_output_as(ReportData)
|
| 76 |
+
|
| 77 |
+
async def send_email(self, report: ReportData) -> None:
|
| 78 |
+
print("Writing email...")
|
| 79 |
+
result = await Runner.run(
|
| 80 |
+
email_agent,
|
| 81 |
+
report.markdown_report,
|
| 82 |
+
)
|
| 83 |
+
print("Email sent")
|
| 84 |
+
return report
|
search_agent.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from agents import Agent, WebSearchTool, ModelSettings
|
| 2 |
+
|
| 3 |
+
INSTRUCTIONS = """
|
| 4 |
+
You are a research assistant. Given a search term, you search the web for
|
| 5 |
+
that term and produce a concise summary of the results. The summary must be
|
| 6 |
+
2-3 paragraphs and less than 300 words. Capture the main points. Write succintly,
|
| 7 |
+
no need to have the complete sentence or good grammar. This will be consumed nt someone
|
| 8 |
+
synthesizing a report, so it's vital you capture the essence and ignore any fluff.
|
| 9 |
+
Do not include any addtional commentary other than the summary itself.
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
search_agent = Agent(
|
| 14 |
+
name="Search Agent",
|
| 15 |
+
instructions=INSTRUCTIONS,
|
| 16 |
+
model = 'gpt-4o-mini',
|
| 17 |
+
tools=[WebSearchTool(search_context_size="low")],
|
| 18 |
+
model_settings=ModelSettings(tool_choice="required")
|
| 19 |
+
)
|
writer_agent.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydantic import BaseModel, Field
|
| 2 |
+
from agents import Agent
|
| 3 |
+
|
| 4 |
+
INSTRUCTIONS = (
|
| 5 |
+
"You are a senior researcher tasked with writing a cohesive report for a research query. "
|
| 6 |
+
"You will be provided with the original query, and some initial research done by a research assistant.\n"
|
| 7 |
+
"You should first come up with an outline for the report that describes the structure and "
|
| 8 |
+
"flow of the report. Then, generate the report and return that as your final output.\n"
|
| 9 |
+
"The final output should be in markdown format, and it should be lengthy and detailed. Aim "
|
| 10 |
+
"for 5-10 pages of content, at least 1000 words."
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class ReportData(BaseModel):
|
| 15 |
+
short_summary: str = Field(description="A short 2-3 sentence summary of the findings.")
|
| 16 |
+
|
| 17 |
+
markdown_report: str = Field(description="The final report.")
|
| 18 |
+
|
| 19 |
+
follow_up_questions: list[str] = Field(description="Suggested topics to research further.")
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
writer_agent = Agent(
|
| 24 |
+
name ="Writer Agent",
|
| 25 |
+
instructions = INSTRUCTIONS,
|
| 26 |
+
model = 'gpt-4o-mini',
|
| 27 |
+
output_type = ReportData
|
| 28 |
+
)
|