47 lines
1.6 KiB
Python
47 lines
1.6 KiB
Python
import asyncio
|
|
import json
|
|
from websockets import connect
|
|
import httpx
|
|
|
|
async def test():
|
|
print("=== Test v3 Conversación ===\n")
|
|
|
|
async with httpx.AsyncClient() as client:
|
|
r = await client.post('http://localhost:3030/auth/login',
|
|
json={'username': 'admin', 'password': 'admin'})
|
|
token = r.json()['token']
|
|
|
|
async with connect('ws://localhost:3030/ws/chat') as ws:
|
|
await ws.send(json.dumps({'token': token}))
|
|
await ws.recv()
|
|
|
|
await ws.send(json.dumps({'type': 'create_session', 'name': 'conversacion'}))
|
|
created = json.loads(await ws.recv())
|
|
sid = created['session_id']
|
|
|
|
await ws.send(json.dumps({'type': 'connect_session', 'session_id': sid}))
|
|
await ws.recv()
|
|
|
|
async def send_and_wait(msg):
|
|
print(f"YO: {msg}")
|
|
await ws.send(json.dumps({'type': 'message', 'content': msg}))
|
|
response = ""
|
|
for _ in range(20):
|
|
try:
|
|
m = await asyncio.wait_for(ws.recv(), timeout=1.0)
|
|
data = json.loads(m)
|
|
if data.get('type') == 'output':
|
|
response = data.get('content', '')
|
|
if data.get('type') == 'done':
|
|
break
|
|
except asyncio.TimeoutError:
|
|
pass
|
|
print(f"CLAUDE: {response}\n")
|
|
return response
|
|
|
|
await send_and_wait("mi nombre es Pablo")
|
|
await send_and_wait("cuál es mi nombre?")
|
|
await send_and_wait("di hola")
|
|
|
|
asyncio.run(test())
|