()
| 100 | |
| 101 | |
| 102 | async def main(): |
| 103 | # Create an agent |
| 104 | agent = Agent( |
| 105 | name="Assistant", |
| 106 | instructions="Reply very concisely.", |
| 107 | ) |
| 108 | |
| 109 | print("=== Dapr Session Example ===") |
| 110 | print() |
| 111 | print("########################################################") |
| 112 | print("This example requires Dapr sidecar to be running") |
| 113 | print("########################################################") |
| 114 | print() |
| 115 | print( |
| 116 | "Start Dapr with: dapr run --app-id myapp --dapr-http-port 3500 --dapr-grpc-port 50001 --resources-path ./components" |
| 117 | ) # noqa: E501 |
| 118 | print() |
| 119 | |
| 120 | # Create a Dapr session instance with context manager for automatic cleanup |
| 121 | session_id = "dapr_conversation_123" |
| 122 | try: |
| 123 | # Use async with to automatically close the session on exit |
| 124 | async with DaprSession.from_address( |
| 125 | session_id, |
| 126 | state_store_name=DEFAULT_STATE_STORE, |
| 127 | dapr_address=f"localhost:{grpc_port}", |
| 128 | ) as session: |
| 129 | # Test Dapr connectivity |
| 130 | if not await ping_with_retry(session, timeout_seconds=5.0, interval_seconds=0.5): |
| 131 | print("Dapr sidecar is not available!") |
| 132 | print("Please start Dapr sidecar and try again.") |
| 133 | print( |
| 134 | "Command: dapr run --app-id myapp --dapr-http-port 3500 --dapr-grpc-port 50001 --resources-path ./components" |
| 135 | ) # noqa: E501 |
| 136 | return |
| 137 | |
| 138 | print("Connected to Dapr successfully!") |
| 139 | print(f"Session ID: {session_id}") |
| 140 | print(f"State Store: {DEFAULT_STATE_STORE}") |
| 141 | |
| 142 | # Clear any existing session data for a clean start |
| 143 | await session.clear_session() |
| 144 | print("Session cleared for clean demonstration.") |
| 145 | print("The agent will remember previous messages automatically.\n") |
| 146 | |
| 147 | # First turn |
| 148 | print("First turn:") |
| 149 | print("User: What city is the Golden Gate Bridge in?") |
| 150 | result = await Runner.run( |
| 151 | agent, |
| 152 | "What city is the Golden Gate Bridge in?", |
| 153 | session=session, |
| 154 | ) |
| 155 | print(f"Assistant: {result.final_output}") |
| 156 | print() |
| 157 | |
| 158 | # Second turn - the agent will remember the previous conversation |
| 159 | print("Second turn:") |
no test coverage detected