mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-04-25 16:36:21 +02:00
parent
2bcf375103
commit
3ba6a3238f
6 changed files with 970 additions and 0 deletions
67
dev-tools/tests/librarian/simple_text_download.py
Normal file
67
dev-tools/tests/librarian/simple_text_download.py
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
#!/usr/bin/env python3
|
||||
"""
|
||||
Minimal example: download a text document in tiny chunks via websocket API
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import base64
|
||||
import websockets
|
||||
|
||||
async def main():
|
||||
url = "ws://localhost:8088/api/v1/socket"
|
||||
|
||||
document_id = "test-chunked-doc-001"
|
||||
chunk_size = 10 # Tiny chunks!
|
||||
|
||||
request_id = 0
|
||||
|
||||
async def send_request(ws, request):
|
||||
nonlocal request_id
|
||||
request_id += 1
|
||||
msg = {
|
||||
"id": f"req-{request_id}",
|
||||
"service": "librarian",
|
||||
"request": request
|
||||
}
|
||||
await ws.send(json.dumps(msg))
|
||||
response = json.loads(await ws.recv())
|
||||
if "error" in response:
|
||||
raise Exception(response["error"])
|
||||
return response.get("response", {})
|
||||
|
||||
async with websockets.connect(url) as ws:
|
||||
|
||||
print(f"Fetching document: {document_id}")
|
||||
print(f"Chunk size: {chunk_size} bytes")
|
||||
print()
|
||||
|
||||
chunk_index = 0
|
||||
all_content = b""
|
||||
|
||||
while True:
|
||||
resp = await send_request(ws, {
|
||||
"operation": "stream-document",
|
||||
"user": "trustgraph",
|
||||
"document-id": document_id,
|
||||
"chunk-index": chunk_index,
|
||||
"chunk-size": chunk_size,
|
||||
})
|
||||
|
||||
chunk_data = base64.b64decode(resp["content"])
|
||||
total_chunks = resp["total-chunks"]
|
||||
total_bytes = resp["total-bytes"]
|
||||
|
||||
print(f"Chunk {chunk_index}: {chunk_data}")
|
||||
|
||||
all_content += chunk_data
|
||||
chunk_index += 1
|
||||
|
||||
if chunk_index >= total_chunks:
|
||||
break
|
||||
|
||||
print()
|
||||
print(f"Complete: {all_content}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
56
dev-tools/tests/librarian/simple_text_upload.py
Normal file
56
dev-tools/tests/librarian/simple_text_upload.py
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
#!/usr/bin/env python3
|
||||
"""
|
||||
Minimal example: upload a small text document via websocket API
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import base64
|
||||
import time
|
||||
import websockets
|
||||
|
||||
async def main():
|
||||
url = "ws://localhost:8088/api/v1/socket"
|
||||
|
||||
# Small text content
|
||||
content = b"AAAAAAAAAABBBBBBBBBBCCCCCCCCCC"
|
||||
|
||||
request_id = 0
|
||||
|
||||
async def send_request(ws, request):
|
||||
nonlocal request_id
|
||||
request_id += 1
|
||||
msg = {
|
||||
"id": f"req-{request_id}",
|
||||
"service": "librarian",
|
||||
"request": request
|
||||
}
|
||||
await ws.send(json.dumps(msg))
|
||||
response = json.loads(await ws.recv())
|
||||
if "error" in response:
|
||||
raise Exception(response["error"])
|
||||
return response.get("response", {})
|
||||
|
||||
async with websockets.connect(url) as ws:
|
||||
|
||||
print(f"Uploading {len(content)} bytes...")
|
||||
|
||||
resp = await send_request(ws, {
|
||||
"operation": "add-document",
|
||||
"document-metadata": {
|
||||
"id": "test-chunked-doc-001",
|
||||
"time": int(time.time()),
|
||||
"kind": "text/plain",
|
||||
"title": "My Test Document",
|
||||
"comments": "Small doc for chunk testing",
|
||||
"user": "trustgraph",
|
||||
"tags": ["test"],
|
||||
"metadata": [],
|
||||
},
|
||||
"content": base64.b64encode(content).decode("utf-8"),
|
||||
})
|
||||
|
||||
print("Done!")
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Loading…
Add table
Add a link
Reference in a new issue