Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import asyncio
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from typing import List, Tuple
|
| 5 |
+
from agents import FileSearchTool, Agent, ModelSettings, TResponseInputItem, Runner, RunConfig, trace
|
| 6 |
+
import os
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
|
| 9 |
+
load_dotenv()
|
| 10 |
+
|
| 11 |
+
VECTOR_STORE_ID = os.getenv("VECTOR_STORE_ID")
|
| 12 |
+
WORKFLOW_ID = os.getenv("WORKFLOW_ID")
|
| 13 |
+
|
| 14 |
+
# --- Tools & Agent setup ---
|
| 15 |
+
file_search = FileSearchTool(
|
| 16 |
+
vector_store_ids=[VECTOR_STORE_ID]
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
my_agent = Agent(
|
| 20 |
+
name="My agent",
|
| 21 |
+
instructions=(
|
| 22 |
+
"Ante la pregunta del usuario, siempre busca en el file search la informaci贸n "
|
| 23 |
+
"y utiliza la informaci贸n recopilada para generar una respuesta."
|
| 24 |
+
),
|
| 25 |
+
model="gpt-4.1-mini",
|
| 26 |
+
tools=[file_search],
|
| 27 |
+
model_settings=ModelSettings(
|
| 28 |
+
temperature=1,
|
| 29 |
+
top_p=1,
|
| 30 |
+
max_tokens=2048,
|
| 31 |
+
store=True
|
| 32 |
+
)
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
def _to_items_from_history(history: List[Tuple[str, str]]) -> List[TResponseInputItem]:
|
| 36 |
+
"""Convert Gradio chat history to Agent format."""
|
| 37 |
+
items: List[TResponseInputItem] = []
|
| 38 |
+
for user_msg, assistant_msg in history:
|
| 39 |
+
if user_msg:
|
| 40 |
+
items.append({
|
| 41 |
+
"role": "user",
|
| 42 |
+
"content": [{"type": "input_text", "text": user_msg}],
|
| 43 |
+
})
|
| 44 |
+
if assistant_msg:
|
| 45 |
+
items.append({
|
| 46 |
+
"role": "assistant",
|
| 47 |
+
"content": [{"type": "output_text", "text": assistant_msg}],
|
| 48 |
+
})
|
| 49 |
+
return items
|
| 50 |
+
|
| 51 |
+
async def agent_reply(message: str, history: List[Tuple[str, str]]):
|
| 52 |
+
"""Handler for Gradio ChatInterface."""
|
| 53 |
+
conversation_items = _to_items_from_history(history)
|
| 54 |
+
conversation_items.append({
|
| 55 |
+
"role": "user",
|
| 56 |
+
"content": [{"type": "input_text", "text": message}],
|
| 57 |
+
})
|
| 58 |
+
|
| 59 |
+
with trace("Gradio message"):
|
| 60 |
+
result = await Runner.run(
|
| 61 |
+
my_agent,
|
| 62 |
+
input=conversation_items,
|
| 63 |
+
run_config=RunConfig(trace_metadata={
|
| 64 |
+
"__trace_source__": "agent-builder",
|
| 65 |
+
"workflow_id": WORKFLOW_ID
|
| 66 |
+
})
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
return result.final_output_as(str)
|
| 70 |
+
|
| 71 |
+
# --- Updated Gradio ChatInterface ---
|
| 72 |
+
demo = gr.ChatInterface(
|
| 73 |
+
fn=agent_reply,
|
| 74 |
+
title="My agent (FileSearch-powered)",
|
| 75 |
+
description=(
|
| 76 |
+
"Este chat usa un agente con FileSearchTool. "
|
| 77 |
+
"Escribe tu pregunta y el agente buscar谩 primero en el file search antes de responder."
|
| 78 |
+
),
|
| 79 |
+
# only valid arguments for Gradio 4.x
|
| 80 |
+
examples=["驴Qu茅 informaci贸n hay sobre el proyecto X?", "Resume el documento m谩s reciente."],
|
| 81 |
+
theme="soft",
|
| 82 |
+
submit_btn="Enviar",
|
| 83 |
+
clear_btn="Limpiar",
|
| 84 |
+
)
|