Jonathand2028's picture
Upload folder using huggingface_hub
873569a verified
from pydantic import BaseModel, Field
from agents import Agent
from openai import AsyncOpenAI
client = AsyncOpenAI()
HOW_MANY_SEARCHES = 1
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. Start by coming up " \
f"with up to 3 clarifying questions, if the search intent is not clear, to ask the user to help you better understand the query."
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=INSTRUCTIONS,
model="gpt-4o-mini",
output_type=WebSearchPlan,
)
async def get_clarification_questions(query: str) -> str:
"""Ask up to 3 clarification questions about the user's query."""
response = await client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "You are an assistant that detects ambiguity in requests and asks up to 3 clarifying questions."},
{"role": "user", "content": f"The user said: '{query}'. Ask your clarification questions if needed. If not, say: 'No clarification needed.'"}
]
)
return response.choices[0].message.content
async def refine_query_with_user_input(original: str, clarification_answer: str) -> str:
"""Refine the original query using the user's clarification answer."""
response = await client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are an assistant that improves the original request using the user's clarification."},
{"role": "user", "content": f"Original request: '{original}'\nClarification answer: '{clarification_answer}'\nRefined query:"}
]
)
return response.choices[0].message.content
## async def process_query(initial_query: str) -> WebSearchPlan:
## """Full pipeline from initial user input to executing the planner agent."""
## clarifications = await get_clarification_questions(initial_query)
## if clarifications.strip().lower() != "no clarification needed.":
## print("Clarifying questions:\n", clarifications)
## user_answer = input("Your answer to the above: ")
## refined_query = await refine_query_with_user_input(initial_query, user_answer)
## else:
## refined_query = initial_query
##
## search_plan = await planner_agent.run(refined_query)
## return search_plan