Spaces:
Sleeping
Sleeping
| # app.py | |
| import asyncio | |
| import gradio as gr | |
| from typing import List, Tuple | |
| from agents import FileSearchTool, Agent, ModelSettings, TResponseInputItem, Runner, RunConfig, trace | |
| import os | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| VECTOR_STORE_ID = os.getenv("VECTOR_STORE_ID") | |
| WORKFLOW_ID = os.getenv("WORKFLOW_ID") | |
| # --- Tools & Agent setup --- | |
| file_search = FileSearchTool( | |
| vector_store_ids=[VECTOR_STORE_ID] | |
| ) | |
| my_agent = Agent( | |
| name="My agent", | |
| instructions=( | |
| "Ante la pregunta del alumno sobre su calendario academico, consulta la herramienta de reglamentos_upn y por favor respondele como si fueras Yoda. " | |
| ), | |
| model="gpt-4.1-mini", | |
| tools=[file_search], | |
| model_settings=ModelSettings( | |
| temperature=1, | |
| top_p=1, | |
| max_tokens=2048, | |
| store=True | |
| ) | |
| ) | |
| def _to_items_from_history(history: List[Tuple[str, str]]) -> List[TResponseInputItem]: | |
| """Convert Gradio chat history to Agent format.""" | |
| items: List[TResponseInputItem] = [] | |
| for user_msg, assistant_msg in history: | |
| if user_msg: | |
| items.append({ | |
| "role": "user", | |
| "content": [{"type": "input_text", "text": user_msg}], | |
| }) | |
| if assistant_msg: | |
| items.append({ | |
| "role": "assistant", | |
| "content": [{"type": "output_text", "text": assistant_msg}], | |
| }) | |
| return items | |
| async def agent_reply(message: str, history: List[Tuple[str, str]]): | |
| """Handler for Gradio ChatInterface.""" | |
| conversation_items = _to_items_from_history(history) | |
| conversation_items.append({ | |
| "role": "user", | |
| "content": [{"type": "input_text", "text": message}], | |
| }) | |
| with trace("Gradio message"): | |
| result = await Runner.run( | |
| my_agent, | |
| input=conversation_items, | |
| run_config=RunConfig(trace_metadata={ | |
| "__trace_source__": "agent-builder", | |
| "workflow_id": WORKFLOW_ID | |
| }) | |
| ) | |
| return result.final_output_as(str) | |
| # --- Updated Gradio ChatInterface --- | |
| demo = gr.ChatInterface( | |
| fn=agent_reply, | |
| title="My agent (FileSearch-powered)", | |
| description=( | |
| "Este chat usa un agente con FileSearchTool. " | |
| "Escribe tu pregunta y el agente buscará primero en el file search antes de responder." | |
| ), | |
| examples=["¿Qué me puedes contar del reglamento?", "¿Puedo copiar"], | |
| theme="soft", | |
| submit_btn="Enviar" | |
| # Removed clear_btn as it's not a valid parameter | |
| ) | |
| # If you need to run the app | |
| if __name__ == "__main__": | |
| demo.launch() |