Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -310,7 +310,7 @@ class QueryModel(BaseModel):
|
|
310 |
}
|
311 |
}
|
312 |
|
313 |
-
def
|
314 |
logger.info(f"Received {data_type} assistant query: {query}")
|
315 |
messages = analyze_data(query, data_type)
|
316 |
|
@@ -326,8 +326,11 @@ def assistant_api(query, data_type,model = "openai/gpt-4o-mini"):
|
|
326 |
yield content
|
327 |
logger.info(f"Completed {data_type} assistant response for query: {query}")
|
328 |
logger.info(f"LLM Response: {full_response}")
|
|
|
|
|
329 |
|
330 |
-
|
|
|
331 |
|
332 |
@app.post("/news-assistant")
|
333 |
async def news_assistant(query: QueryModel, api_key: str = Depends(verify_api_key)):
|
@@ -335,7 +338,8 @@ async def news_assistant(query: QueryModel, api_key: str = Depends(verify_api_ke
|
|
335 |
News assistant endpoint that provides summaries and analysis of recent news based on user queries.
|
336 |
Requires API Key authentication via X-API-Key header.
|
337 |
"""
|
338 |
-
|
|
|
339 |
|
340 |
@app.post("/search-assistant")
|
341 |
async def search_assistant(query: QueryModel, api_key: str = Depends(verify_api_key)):
|
@@ -343,7 +347,8 @@ async def search_assistant(query: QueryModel, api_key: str = Depends(verify_api_
|
|
343 |
Search assistant endpoint that provides summaries and analysis of web search results based on user queries.
|
344 |
Requires API Key authentication via X-API-Key header.
|
345 |
"""
|
346 |
-
|
|
|
347 |
|
348 |
from pydantic import BaseModel, Field
|
349 |
import yaml
|
@@ -547,15 +552,13 @@ async def followup_agent(query: FollowupQueryModel, background_tasks: Background
|
|
547 |
logger.info(f"Completed followup agent response for query: {query.query}, send result: {result}")
|
548 |
|
549 |
return StreamingResponse(process_response(), media_type="text/event-stream")
|
550 |
-
|
551 |
@app.post("/v2/followup-tools-agent")
|
552 |
-
|
553 |
"""
|
554 |
Followup agent endpoint that provides helpful responses or generates clarifying questions based on user queries.
|
555 |
Requires API Key authentication via X-API-Key header.
|
556 |
"""
|
557 |
logger.info(f"Received followup agent query: {query.query}")
|
558 |
-
|
559 |
if query.conversation_id not in conversations:
|
560 |
conversations[query.conversation_id] = [
|
561 |
{"role": "system", "content": MULTI_AGENT_PROMPT}
|
@@ -566,19 +569,19 @@ async def followup_agent(query: FollowupQueryModel, background_tasks: Background
|
|
566 |
|
567 |
# Limit tokens in the conversation history
|
568 |
limited_conversation = conversations[query.conversation_id]
|
569 |
-
|
570 |
def process_response():
|
571 |
full_response = ""
|
572 |
for content in chat_with_llama_stream(limited_conversation, model=query.model_id):
|
573 |
full_response += content
|
574 |
yield content
|
575 |
-
|
576 |
logger.info(f"LLM RAW response for query: {query.query}: {full_response}")
|
577 |
response_content, interact, tools = parse_followup_and_tools(full_response)
|
578 |
|
579 |
result = {
|
580 |
"clarification": interact,
|
581 |
-
"tools":tools
|
582 |
}
|
583 |
|
584 |
yield "<json>" + json.dumps(result)
|
@@ -586,9 +589,19 @@ async def followup_agent(query: FollowupQueryModel, background_tasks: Background
|
|
586 |
# Add the assistant's response to the conversation history
|
587 |
conversations[query.conversation_id].append({"role": "assistant", "content": full_response})
|
588 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
589 |
background_tasks.add_task(update_db, query.user_id, query.conversation_id, query.query, full_response)
|
590 |
logger.info(f"Completed followup agent response for query: {query.query}, send result: {result}")
|
591 |
-
|
592 |
return StreamingResponse(process_response(), media_type="text/event-stream")
|
593 |
|
594 |
|
|
|
310 |
}
|
311 |
}
|
312 |
|
313 |
+
def search_assistant_api(query, data_type, model="openai/gpt-4o-mini"):
|
314 |
logger.info(f"Received {data_type} assistant query: {query}")
|
315 |
messages = analyze_data(query, data_type)
|
316 |
|
|
|
326 |
yield content
|
327 |
logger.info(f"Completed {data_type} assistant response for query: {query}")
|
328 |
logger.info(f"LLM Response: {full_response}")
|
329 |
+
|
330 |
+
return process_response
|
331 |
|
332 |
+
def create_streaming_response(generator):
|
333 |
+
return StreamingResponse(generator(), media_type="text/event-stream")
|
334 |
|
335 |
@app.post("/news-assistant")
|
336 |
async def news_assistant(query: QueryModel, api_key: str = Depends(verify_api_key)):
|
|
|
338 |
News assistant endpoint that provides summaries and analysis of recent news based on user queries.
|
339 |
Requires API Key authentication via X-API-Key header.
|
340 |
"""
|
341 |
+
response_generator = search_assistant_api(query.query, "news", model=query.model_id)
|
342 |
+
return create_streaming_response(response_generator)
|
343 |
|
344 |
@app.post("/search-assistant")
|
345 |
async def search_assistant(query: QueryModel, api_key: str = Depends(verify_api_key)):
|
|
|
347 |
Search assistant endpoint that provides summaries and analysis of web search results based on user queries.
|
348 |
Requires API Key authentication via X-API-Key header.
|
349 |
"""
|
350 |
+
response_generator = search_assistant_api(query.query, "web", model=query.model_id)
|
351 |
+
return create_streaming_response(response_generator)
|
352 |
|
353 |
from pydantic import BaseModel, Field
|
354 |
import yaml
|
|
|
552 |
logger.info(f"Completed followup agent response for query: {query.query}, send result: {result}")
|
553 |
|
554 |
return StreamingResponse(process_response(), media_type="text/event-stream")
|
|
|
555 |
@app.post("/v2/followup-tools-agent")
|
556 |
+
def followup_agent(query: FollowupQueryModel, background_tasks: BackgroundTasks, api_key: str = Depends(verify_api_key)):
|
557 |
"""
|
558 |
Followup agent endpoint that provides helpful responses or generates clarifying questions based on user queries.
|
559 |
Requires API Key authentication via X-API-Key header.
|
560 |
"""
|
561 |
logger.info(f"Received followup agent query: {query.query}")
|
|
|
562 |
if query.conversation_id not in conversations:
|
563 |
conversations[query.conversation_id] = [
|
564 |
{"role": "system", "content": MULTI_AGENT_PROMPT}
|
|
|
569 |
|
570 |
# Limit tokens in the conversation history
|
571 |
limited_conversation = conversations[query.conversation_id]
|
572 |
+
|
573 |
def process_response():
|
574 |
full_response = ""
|
575 |
for content in chat_with_llama_stream(limited_conversation, model=query.model_id):
|
576 |
full_response += content
|
577 |
yield content
|
578 |
+
|
579 |
logger.info(f"LLM RAW response for query: {query.query}: {full_response}")
|
580 |
response_content, interact, tools = parse_followup_and_tools(full_response)
|
581 |
|
582 |
result = {
|
583 |
"clarification": interact,
|
584 |
+
"tools": tools
|
585 |
}
|
586 |
|
587 |
yield "<json>" + json.dumps(result)
|
|
|
589 |
# Add the assistant's response to the conversation history
|
590 |
conversations[query.conversation_id].append({"role": "assistant", "content": full_response})
|
591 |
|
592 |
+
# Process tool if present
|
593 |
+
if tools and len(tools) > 0:
|
594 |
+
tool = tools[0] # Assume only one tool is present
|
595 |
+
if tool["name"] in ["news", "web"]:
|
596 |
+
search_query = tool["input"]
|
597 |
+
search_response = search_assistant_api(search_query, tool["name"], model=query.model_id)
|
598 |
+
|
599 |
+
for content in search_response():
|
600 |
+
yield content
|
601 |
+
|
602 |
background_tasks.add_task(update_db, query.user_id, query.conversation_id, query.query, full_response)
|
603 |
logger.info(f"Completed followup agent response for query: {query.query}, send result: {result}")
|
604 |
+
|
605 |
return StreamingResponse(process_response(), media_type="text/event-stream")
|
606 |
|
607 |
|