forked from OpenHands/OpenHands
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfastapi_streaming_client.py
More file actions
229 lines (190 loc) Β· 8.11 KB
/
Copy pathfastapi_streaming_client.py
File metadata and controls
229 lines (190 loc) Β· 8.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/env python3
"""
Simple FastAPI client that calls the /join-conversation-stream endpoint
and prints responses from the server in real-time.
"""
import asyncio
import json
import os
import sys
import httpx
class StreamingClient:
def __init__(self, base_url: str = 'http://localhost:3000'):
self.base_url = base_url
self.client = None
async def __aenter__(self):
self.client = httpx.AsyncClient(timeout=300.0) # 5 minute timeout
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
if self.client:
await self.client.aclose()
async def _handle_event(self, event: dict):
"""Handle different types of events from the stream"""
event_type = event.get('type', 'unknown')
if event_type == 'connection':
status = event.get('status', '')
message = event.get('message', '')
if status == 'connected':
print(f'π {message}')
elif status == 'disconnected':
print(f'π {message}')
elif event_type == 'oh_event':
# This is the main socket.io event data
data = event.get('data', {})
print('\nπ¨ Socket Event:')
print(f" Type: {data.get('type', 'N/A')}")
print(f" Source: {data.get('source', 'N/A')}")
if 'content' in data:
print(f" Content: {data['content']}")
if 'message' in data:
print(f" Message: {data['message']}")
if 'observation' in data:
print(f" Observation: {data['observation']}")
if 'extras' in data and data['extras']:
extras = data['extras']
if 'agent_state' in extras:
print(f" Agent State: {extras['agent_state']}")
if extras['agent_state'] == 'awaiting_user_input':
print(
' π Agent is now awaiting user input - conversation completed!'
)
# Print full event data for debugging
print(f' Full Data: {json.dumps(data, indent=2)}')
print('-' * 30)
elif event_type == 'error':
error_type = event.get('error', 'Unknown')
message = event.get('message', '')
print(f'\nβ Error ({error_type}): {message}')
elif event_type == 'completion':
reason = event.get('reason', 'unknown')
status = event.get('status', 'finished')
print(f'\nπ Completion: {status} (reason: {reason})')
else:
print(f'\nβ Unknown event type: {event_type}')
print(f' Full Event: {json.dumps(event, indent=2)}')
async def stream_conversation(
self,
conversation_id: str,
api_key: str,
system_prompt: str = '',
user_prompt: str = '',
research_mode: str = 'deep_research',
):
"""
Stream conversation responses from the FastAPI endpoint
"""
params = {
'conversation_id': conversation_id,
'system_prompt': system_prompt,
'user_prompt': user_prompt,
'research_mode': research_mode,
}
endpoint = f'{self.base_url}/api/v1/integration/conversations/join-conversation'
print(f'π Connecting to: {endpoint}')
print(f'π Parameters: {params}')
print('=' * 50)
try:
async with self.client.stream(
'POST',
endpoint,
json=params,
headers={'Authorization': f'Bearer {api_key}'},
) as response:
print(f'β
Response Status: {response.status_code}')
if response.status_code != 200:
error_text = await response.aread()
print(f'β Error: {error_text.decode()}')
return
print('π Streaming events:')
print('-' * 50)
# Buffer to handle chunked JSON
buffer = ''
async for chunk in response.aiter_text():
buffer += chunk
# Process complete JSON objects from buffer
while buffer:
try:
# Try to decode JSON from the buffer
decoder = json.JSONDecoder()
event, idx = decoder.raw_decode(buffer)
# Successfully parsed a JSON object
await self._handle_event(event)
# Remove processed JSON from buffer
buffer = buffer[idx:].lstrip()
# Check for completion
if event.get('type') == 'completion':
status = event.get('status', 'finished')
if status == 'cancelled':
print(
f"\nπ« Stream cancelled: {event.get('message', 'Stream was cancelled')}"
)
elif status == 'finished':
print(
f"\nβ
Stream completed successfully with message: {event.get('message', 'Unknown message')}"
)
else:
print(
f"\nπ Stream ended with status '{status}': {event.get('message', 'No message')}"
)
return
elif event.get('type') == 'error':
print(
f"\nβ Stream ended with error: {event.get('message', 'Unknown error')}"
)
return
except json.JSONDecodeError:
# Incomplete JSON in buffer, wait for more data
break
# Handle any remaining buffer content
if buffer.strip():
print(f'β οΈ Unparsed buffer content: {buffer}')
except httpx.ConnectError:
print(f'β Failed to connect to {self.base_url}')
print('Make sure your FastAPI server is running!')
except httpx.TimeoutException:
print('β° Request timed out')
except Exception as e:
print(f'β Unexpected error: {e}')
async def main():
"""
Main function to run the streaming client
"""
# Example configuration - modify these values
config = {
'conversation_id': '4b03707134ee42b4abf613353f746b6c',
'api_key': os.getenv('API_KEY'),
'system_prompt': 'You are a helpful AI assistant specialized in software development. You are also a joke teller.',
'user_prompt': 'Tell me a joke.',
'research_mode': 'deep_research',
}
# FastAPI server URL
print('π Starting FastAPI Streaming Client')
async with StreamingClient(
os.getenv('API_BASE_URL') or 'http://localhost:3000'
) as client:
await client.stream_conversation(**config)
def print_usage():
"""Print usage information"""
print(
"""
FastAPI Streaming Client
This client connects to your FastAPI streaming endpoint and displays
real-time responses from the socket.io conversation.
Before running:
1. Start your socket.io server (usually on port 3000)
2. Start your FastAPI server with the streaming endpoint (usually on port 8000)
3. Update the configuration in this script with your actual values
Usage:
python fastapi_streaming_client.py
"""
)
if __name__ == '__main__':
if len(sys.argv) > 1 and sys.argv[1] in ['-h', '--help', 'help']:
print_usage()
else:
try:
asyncio.run(main())
except KeyboardInterrupt:
print('\nβΉοΈ Client stopped by user')
except Exception as e:
print(f'\nπ₯ Client error: {e}')