-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_blocknote_streaming.py
More file actions
264 lines (220 loc) · 10.2 KB
/
test_blocknote_streaming.py
File metadata and controls
264 lines (220 loc) · 10.2 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/usr/bin/env python3
"""Test script for BlockNote AI streaming integration."""
import requests
import json
import sys
def test_blocknote_streaming():
"""Test the BlockNote AI streaming integration."""
# Test data similar to what BlockNote sends with streaming enabled
test_request = {
"model": "mistral:latest",
"temperature": 0,
"messages": [
{
"role": "system",
"content": "You're manipulating a text document using HTML blocks. Make sure to follow the json schema provided. When referencing ids they MUST be EXACTLY the same (including the trailing $)."
},
{
"role": "system",
"content": "[{\"id\":\"e77d39f6-597d-46bb-83c3-2aba55e519f3$\",\"block\":\"<h3 data-level=\\\"3\\\">Planets of the solar system</h3>\"},{\"id\":\"82ec1e48-07ee-4cfa-85e5-da9bf669cbf2$\",\"block\":\"<p></p>\"},{\"cursor\":true}]"
},
{
"role": "user",
"content": "List the planets of the solar system"
}
],
"tool_choice": {
"type": "function",
"function": {
"name": "json"
}
},
"tools": [
{
"type": "function",
"function": {
"name": "json",
"description": "Respond with a JSON object.",
"parameters": {
"type": "object",
"properties": {
"operations": {
"type": "array",
"items": {
"anyOf": [
{
"type": "object",
"description": "Update a block",
"properties": {
"type": {"type": "string", "enum": ["update"]},
"id": {"type": "string"},
"block": {"type": "string"}
},
"required": ["type", "id", "block"]
}
]
}
}
},
"required": ["operations"]
}
}
}
],
"stream": True
}
url = "https://haystack.pmflex.one/haystack/v1/chat/completions"
print("Testing BlockNote AI streaming integration...")
print(f"Sending streaming request to: {url}")
try:
response = requests.post(
url,
json=test_request,
headers={"Content-Type": "application/json"},
stream=True,
timeout=30
)
print(f"Response status: {response.status_code}")
if response.status_code == 200:
print("✅ Streaming response received!")
# Parse streaming response
chunks = []
tool_call_id = None
arguments_buffer = ""
for line in response.iter_lines():
if line:
line_str = line.decode('utf-8')
if line_str.startswith('data: '):
data_str = line_str[6:] # Remove 'data: ' prefix
if data_str == '[DONE]':
print("✅ Stream completed with [DONE]")
break
try:
chunk_data = json.loads(data_str)
chunks.append(chunk_data)
# Check for tool calls in delta
if (chunk_data.get("choices") and
len(chunk_data["choices"]) > 0 and
chunk_data["choices"][0].get("delta", {}).get("tool_calls")):
tool_calls = chunk_data["choices"][0]["delta"]["tool_calls"]
for tool_call in tool_calls:
if tool_call.get("id"):
tool_call_id = tool_call["id"]
print(f"✅ Tool call started: {tool_call_id}")
if tool_call.get("function", {}).get("arguments"):
arguments_buffer += tool_call["function"]["arguments"]
except json.JSONDecodeError as e:
print(f"⚠️ Failed to parse chunk: {e}")
print(f"Raw chunk: {data_str}")
print(f"\n✅ Received {len(chunks)} chunks")
if tool_call_id:
print(f"✅ Tool call ID: {tool_call_id}")
# Try to parse the accumulated arguments
if arguments_buffer:
try:
args = json.loads(arguments_buffer)
print("✅ Streamed arguments are valid JSON:")
print(json.dumps(args, indent=2))
# Check if it has the expected structure
if "operations" in args and isinstance(args["operations"], list):
print(f"✅ Found {len(args['operations'])} operations")
for i, op in enumerate(args["operations"]):
print(f" Operation {i+1}: {op.get('type', 'unknown')}")
else:
print("❌ Missing or invalid 'operations' field")
except json.JSONDecodeError as e:
print(f"❌ Streamed arguments are not valid JSON: {e}")
print(f"Raw arguments: {arguments_buffer}")
else:
print("❌ No arguments received in stream")
else:
print("❌ No tool call ID found in stream")
return True
else:
print(f"❌ Request failed with status {response.status_code}")
print("Response:", response.text)
return False
except requests.exceptions.ConnectionError:
print("❌ Could not connect to the server. Make sure it's running on http://localhost:8000")
return False
except requests.exceptions.Timeout:
print("❌ Request timed out")
return False
except Exception as e:
print(f"❌ Unexpected error: {e}")
return False
def test_regular_streaming():
"""Test regular streaming (non-tool call)."""
regular_request = {
"model": "mistral:latest",
"messages": [
{
"role": "user",
"content": "Write a short poem about AI"
}
],
"stream": True
}
url = "https://haystack.pmflex.one/haystack/v1/chat/completions"
print("\n" + "="*50)
print("Testing regular streaming...")
try:
response = requests.post(
url,
json=regular_request,
headers={"Content-Type": "application/json"},
stream=True,
timeout=30
)
if response.status_code == 200:
print("✅ Regular streaming works!")
content_buffer = ""
chunk_count = 0
for line in response.iter_lines():
if line:
line_str = line.decode('utf-8')
if line_str.startswith('data: '):
data_str = line_str[6:]
if data_str == '[DONE]':
print("✅ Stream completed")
break
try:
chunk_data = json.loads(data_str)
chunk_count += 1
if (chunk_data.get("choices") and
len(chunk_data["choices"]) > 0 and
chunk_data["choices"][0].get("delta", {}).get("content")):
content = chunk_data["choices"][0]["delta"]["content"]
content_buffer += content
except json.JSONDecodeError:
pass # Skip invalid chunks
print(f"✅ Received {chunk_count} chunks")
if content_buffer:
print(f"Content preview: {content_buffer[:100]}...")
return True
else:
print("❌ No content received")
return False
else:
print(f"❌ Regular streaming failed with status {response.status_code}")
return False
except Exception as e:
print(f"❌ Regular streaming test failed: {e}")
return False
if __name__ == "__main__":
print("BlockNote AI Streaming Integration Test")
print("="*50)
# Test BlockNote streaming
blocknote_success = test_blocknote_streaming()
# Test regular streaming
regular_success = test_regular_streaming()
print("\n" + "="*50)
print("Test Results:")
print(f"BlockNote Streaming: {'✅ PASS' if blocknote_success else '❌ FAIL'}")
print(f"Regular Streaming: {'✅ PASS' if regular_success else '❌ FAIL'}")
if blocknote_success and regular_success:
print("\n🎉 All streaming tests passed!")
sys.exit(0)
else:
print("\n❌ Some streaming tests failed.")
sys.exit(1)