()
| 18 | |
| 19 | |
| 20 | async def main(): |
| 21 | # Create an agent |
| 22 | agent = Agent( |
| 23 | name="Assistant", |
| 24 | instructions="Reply very concisely.", |
| 25 | ) |
| 26 | |
| 27 | print("=== Redis Session Example ===") |
| 28 | redis_url = os.environ.get("REDIS_URL", DEFAULT_REDIS_URL) |
| 29 | print(f"This example uses Redis at {redis_url}") |
| 30 | print("Set REDIS_URL to use a different Redis server.") |
| 31 | print() |
| 32 | |
| 33 | # Create a Redis session instance |
| 34 | session_id = "redis_conversation_123" |
| 35 | try: |
| 36 | session = RedisSession.from_url( |
| 37 | session_id, |
| 38 | url=redis_url, |
| 39 | ) |
| 40 | |
| 41 | # Test Redis connectivity |
| 42 | if not await session.ping(): |
| 43 | print("Redis server is not available!") |
| 44 | print("Please start Redis server and try again.") |
| 45 | return |
| 46 | |
| 47 | print("Connected to Redis successfully!") |
| 48 | print(f"Session ID: {session_id}") |
| 49 | |
| 50 | # Clear any existing session data for a clean start |
| 51 | await session.clear_session() |
| 52 | print("Session cleared for clean demonstration.") |
| 53 | print("The agent will remember previous messages automatically.\n") |
| 54 | |
| 55 | # First turn |
| 56 | print("First turn:") |
| 57 | print("User: What city is the Golden Gate Bridge in?") |
| 58 | result = await Runner.run( |
| 59 | agent, |
| 60 | "What city is the Golden Gate Bridge in?", |
| 61 | session=session, |
| 62 | ) |
| 63 | print(f"Assistant: {result.final_output}") |
| 64 | print() |
| 65 | |
| 66 | # Second turn - the agent will remember the previous conversation |
| 67 | print("Second turn:") |
| 68 | print("User: What state is it in?") |
| 69 | result = await Runner.run(agent, "What state is it in?", session=session) |
| 70 | print(f"Assistant: {result.final_output}") |
| 71 | print() |
| 72 | |
| 73 | # Third turn - continuing the conversation |
| 74 | print("Third turn:") |
| 75 | print("User: What's the population of that state?") |
| 76 | result = await Runner.run( |
| 77 | agent, |
no test coverage detected