|
| 1 | +import asyncio |
| 2 | +import logging |
| 3 | +import os |
| 4 | +import random |
| 5 | +import uuid |
| 6 | +from typing import Annotated |
| 7 | + |
| 8 | +from agent_framework import Agent, tool |
| 9 | +from agent_framework.openai import OpenAIChatClient |
| 10 | +from agent_framework.redis import RedisHistoryProvider |
| 11 | +from azure.identity.aio import DefaultAzureCredential, get_bearer_token_provider |
| 12 | +from dotenv import load_dotenv |
| 13 | +from pydantic import Field |
| 14 | +from rich import print |
| 15 | +from rich.logging import RichHandler |
| 16 | + |
| 17 | +# Configurar logging |
| 18 | +handler = RichHandler(show_path=False, rich_tracebacks=True, show_level=False) |
| 19 | +logging.basicConfig(level=logging.WARNING, handlers=[handler], force=True, format="%(message)s") |
| 20 | +logger = logging.getLogger(__name__) |
| 21 | +logger.setLevel(logging.INFO) |
| 22 | + |
| 23 | +# Configurar cliente de OpenAI según el entorno |
| 24 | +load_dotenv(override=True) |
| 25 | +API_HOST = os.getenv("API_HOST", "github") |
| 26 | +REDIS_URL = os.getenv("REDIS_URL", "redis://localhost:6379") |
| 27 | + |
| 28 | +async_credential = None |
| 29 | +if API_HOST == "azure": |
| 30 | + async_credential = DefaultAzureCredential() |
| 31 | + token_provider = get_bearer_token_provider(async_credential, "https://cognitiveservices.azure.com/.default") |
| 32 | + client = OpenAIChatClient( |
| 33 | + base_url=f"{os.environ['AZURE_OPENAI_ENDPOINT']}/openai/v1/", |
| 34 | + api_key=token_provider, |
| 35 | + model_id=os.environ["AZURE_OPENAI_CHAT_DEPLOYMENT"], |
| 36 | + ) |
| 37 | +elif API_HOST == "github": |
| 38 | + client = OpenAIChatClient( |
| 39 | + base_url="https://models.github.ai/inference", |
| 40 | + api_key=os.environ["GITHUB_TOKEN"], |
| 41 | + model_id=os.getenv("GITHUB_MODEL", "openai/gpt-5-mini"), |
| 42 | + ) |
| 43 | +else: |
| 44 | + client = OpenAIChatClient( |
| 45 | + api_key=os.environ["OPENAI_API_KEY"], model_id=os.environ.get("OPENAI_MODEL", "gpt-5-mini") |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +@tool |
| 50 | +def get_weather( |
| 51 | + city: Annotated[str, Field(description="The city to get the weather for.")], |
| 52 | +) -> str: |
| 53 | + """Devuelve datos del clima para una ciudad.""" |
| 54 | + logger.info(f"Getting weather for {city}") |
| 55 | + conditions = ["soleado", "nublado", "lluvioso", "tormentoso"] |
| 56 | + return f"El clima en {city} está {conditions[random.randint(0, 3)]} con una máxima de {random.randint(10, 30)}°C." |
| 57 | + |
| 58 | + |
| 59 | +async def example_persistent_session() -> None: |
| 60 | + """Una sesión con Redis persiste el historial de conversación incluso tras reinicios.""" |
| 61 | + print("\n[bold]=== Sesión persistente en Redis ===[/bold]") |
| 62 | + |
| 63 | + session_id = str(uuid.uuid4()) |
| 64 | + |
| 65 | + # Fase 1: Iniciar una conversación con un proveedor de historial en Redis |
| 66 | + print("[dim]--- Fase 1: Iniciando conversación ---[/dim]") |
| 67 | + redis_provider = RedisHistoryProvider(source_id="redis_chat", redis_url=REDIS_URL) |
| 68 | + |
| 69 | + agent = Agent( |
| 70 | + client=client, |
| 71 | + instructions="Eres un agente de clima útil.", |
| 72 | + tools=[get_weather], |
| 73 | + context_providers=[redis_provider], |
| 74 | + ) |
| 75 | + |
| 76 | + session = agent.create_session(session_id=session_id) |
| 77 | + |
| 78 | + print("[blue]Usuario:[/blue] ¿Cómo está el clima en Tokio?") |
| 79 | + response = await agent.run("¿Cómo está el clima en Tokio?", session=session) |
| 80 | + print(f"[green]Agente:[/green] {response.text}") |
| 81 | + |
| 82 | + print("\n[blue]Usuario:[/blue] ¿Y París?") |
| 83 | + response = await agent.run("¿Y París?", session=session) |
| 84 | + print(f"[green]Agente:[/green] {response.text}") |
| 85 | + |
| 86 | + # Fase 2: Simular un reinicio de la app — reconectar usando el mismo session_id en Redis |
| 87 | + print("\n[dim]--- Fase 2: Reanudando después del 'reinicio' ---[/dim]") |
| 88 | + redis_provider2 = RedisHistoryProvider(source_id="redis_chat", redis_url=REDIS_URL) |
| 89 | + |
| 90 | + agent2 = Agent( |
| 91 | + client=client, |
| 92 | + instructions="Eres un agente de clima útil.", |
| 93 | + tools=[get_weather], |
| 94 | + context_providers=[redis_provider2], |
| 95 | + ) |
| 96 | + |
| 97 | + session2 = agent2.create_session(session_id=session_id) |
| 98 | + |
| 99 | + print("[blue]Usuario:[/blue] ¿Cuál de las ciudades por las que pregunté tuvo mejor clima?") |
| 100 | + response = await agent2.run("¿Cuál de las ciudades por las que pregunté tuvo mejor clima?", session=session2) |
| 101 | + print(f"[green]Agente:[/green] {response.text}") |
| 102 | + print("[dim]Nota: El agente recordó la conversación de la Fase 1 gracias a la persistencia en Redis.[/dim]") |
| 103 | + |
| 104 | + |
| 105 | +async def main() -> None: |
| 106 | + """Ejecuta los ejemplos de Redis para demostrar patrones de almacenamiento persistente.""" |
| 107 | + # Verificar conectividad con Redis |
| 108 | + import redis as redis_client |
| 109 | + |
| 110 | + r = redis_client.from_url(REDIS_URL) |
| 111 | + try: |
| 112 | + r.ping() |
| 113 | + except Exception as e: |
| 114 | + print(f"[red]No se puede conectar a Redis en {REDIS_URL}: {e}[/red]") |
| 115 | + print( |
| 116 | + "[red]Asegúrate de que Redis esté corriendo (por ejemplo, con el dev container" |
| 117 | + " o con 'docker run -p 6379:6379 redis:7-alpine').[/red]" |
| 118 | + ) |
| 119 | + return |
| 120 | + finally: |
| 121 | + r.close() |
| 122 | + |
| 123 | + print("[dim]Conexión a Redis verificada.[/dim]") |
| 124 | + |
| 125 | + await example_persistent_session() |
| 126 | + |
| 127 | + if async_credential: |
| 128 | + await async_credential.close() |
| 129 | + |
| 130 | + |
| 131 | +if __name__ == "__main__": |
| 132 | + asyncio.run(main()) |
0 commit comments