Spaces:
Running
Running
Upload folder using huggingface_hub
Browse files- appagents/deep_research.py +23 -0
- appagents/email_agent.py +29 -0
- appagents/planner_agent.py +23 -0
- appagents/research_agent.py +6 -5
- appagents/research_manager.py +84 -0
- appagents/search_agent.py +17 -0
- appagents/writer_agent.py +27 -0
appagents/deep_research.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|
| 23 |
+
|
appagents/email_agent.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 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("ed@edwarddonner.com") # put your verified sender here
|
| 13 |
+
to_email = To("ed.donner@gmail.com") # put your recipient here
|
| 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 |
+
print("Email response", response.status_code)
|
| 18 |
+
return {"status": "success"}
|
| 19 |
+
|
| 20 |
+
INSTRUCTIONS = """You are able to send a nicely formatted HTML email based on a detailed report.
|
| 21 |
+
You will be provided with a detailed report. You should use your tool to send one email, providing the
|
| 22 |
+
report converted into clean, well presented HTML with an appropriate subject line."""
|
| 23 |
+
|
| 24 |
+
email_agent = Agent(
|
| 25 |
+
name="Email agent",
|
| 26 |
+
instructions=INSTRUCTIONS,
|
| 27 |
+
tools=[send_email],
|
| 28 |
+
model="gpt-4o-mini",
|
| 29 |
+
)
|
appagents/planner_agent.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydantic import BaseModel, Field
|
| 2 |
+
from agents import Agent
|
| 3 |
+
|
| 4 |
+
HOW_MANY_SEARCHES = 5
|
| 5 |
+
|
| 6 |
+
INSTRUCTIONS = f"You are a helpful research assistant. Given a query, come up with a set of web searches \
|
| 7 |
+
to perform to best answer the query. Output {HOW_MANY_SEARCHES} terms to query for."
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class WebSearchItem(BaseModel):
|
| 11 |
+
reason: str = Field(description="Your reasoning for why this search is important to the query.")
|
| 12 |
+
query: str = Field(description="The search term to use for the web search.")
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class WebSearchPlan(BaseModel):
|
| 16 |
+
searches: list[WebSearchItem] = Field(description="A list of web searches to perform to best answer the query.")
|
| 17 |
+
|
| 18 |
+
planner_agent = Agent(
|
| 19 |
+
name="PlannerAgent",
|
| 20 |
+
instructions=INSTRUCTIONS,
|
| 21 |
+
model="gpt-4o-mini",
|
| 22 |
+
output_type=WebSearchPlan,
|
| 23 |
+
)
|
appagents/research_agent.py
CHANGED
|
@@ -2,7 +2,7 @@ from tools.google_tools import GoogleTools
|
|
| 2 |
from tools.news_tools import NewsTools
|
| 3 |
from tools.yahoo_tools import FinanceTools
|
| 4 |
from tools.time_tools import TimeTools
|
| 5 |
-
from agents import Agent
|
| 6 |
|
| 7 |
class MarketResearchAgent:
|
| 8 |
"""
|
|
@@ -16,10 +16,11 @@ class MarketResearchAgent:
|
|
| 16 |
"""
|
| 17 |
tools = [
|
| 18 |
TimeTools.current_datetime,
|
| 19 |
-
GoogleTools.search,
|
| 20 |
-
|
|
|
|
| 21 |
FinanceTools.get_market_sentiment,
|
| 22 |
-
FinanceTools.get_history,
|
| 23 |
# NewsTools.top_headlines,
|
| 24 |
# NewsTools.search_news,
|
| 25 |
]
|
|
@@ -45,6 +46,6 @@ class MarketResearchAgent:
|
|
| 45 |
name="AI Assistant",
|
| 46 |
tools=tools,
|
| 47 |
instructions=instructions,
|
| 48 |
-
model="gpt-4o"
|
| 49 |
)
|
| 50 |
return agent
|
|
|
|
| 2 |
from tools.news_tools import NewsTools
|
| 3 |
from tools.yahoo_tools import FinanceTools
|
| 4 |
from tools.time_tools import TimeTools
|
| 5 |
+
from agents import Agent, WebSearchTool
|
| 6 |
|
| 7 |
class MarketResearchAgent:
|
| 8 |
"""
|
|
|
|
| 16 |
"""
|
| 17 |
tools = [
|
| 18 |
TimeTools.current_datetime,
|
| 19 |
+
# GoogleTools.search,
|
| 20 |
+
WebSearchTool(search_context_size="low"),
|
| 21 |
+
# FinanceTools.get_summary,
|
| 22 |
FinanceTools.get_market_sentiment,
|
| 23 |
+
# FinanceTools.get_history,
|
| 24 |
# NewsTools.top_headlines,
|
| 25 |
# NewsTools.search_news,
|
| 26 |
]
|
|
|
|
| 46 |
name="AI Assistant",
|
| 47 |
tools=tools,
|
| 48 |
instructions=instructions,
|
| 49 |
+
model="gpt-4o-mini"
|
| 50 |
)
|
| 51 |
return agent
|
appagents/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
|
appagents/search_agent.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from agents import Agent, WebSearchTool
|
| 2 |
+
from agents.model_settings import ModelSettings
|
| 3 |
+
|
| 4 |
+
INSTRUCTIONS = "You are a research assistant. Given a search term, you search the web for that term and \
|
| 5 |
+
produce a concise summary of the results. The summary must 2-3 paragraphs and less than 300 \
|
| 6 |
+
words. Capture the main points. Write succintly, no need to have complete sentences or good \
|
| 7 |
+
grammar. This will be consumed by someone synthesizing a report, so it's vital you capture the \
|
| 8 |
+
essence and ignore any fluff. Do not include any additional commentary other than the summary itself."
|
| 9 |
+
|
| 10 |
+
search_agent = Agent(
|
| 11 |
+
name="Search agent",
|
| 12 |
+
instructions=INSTRUCTIONS,
|
| 13 |
+
tools=[WebSearchTool(search_context_size="low")],
|
| 14 |
+
model="gpt-4o-mini",
|
| 15 |
+
model_settings=ModelSettings(tool_choice="required"),
|
| 16 |
+
)
|
| 17 |
+
|
appagents/writer_agent.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
writer_agent = Agent(
|
| 23 |
+
name="WriterAgent",
|
| 24 |
+
instructions=INSTRUCTIONS,
|
| 25 |
+
model="gpt-4o-mini",
|
| 26 |
+
output_type=ReportData,
|
| 27 |
+
)
|