This repository has been archived by the owner on Oct 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathhitl_client.py
63 lines (51 loc) · 1.86 KB
/
hitl_client.py
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
import asyncio
import os
from typing import Dict
import aiohttp
from pydantic import BaseModel, ValidationError
class Response(BaseModel):
result: str
error: str
stdout: str
class HumanPrompt(BaseModel):
prompt: str
async def hitl_client(url: str, name: str, question: str, envs: Dict = {}):
async with aiohttp.ClientSession() as session:
async with session.ws_connect(f'{url}/{name}') as ws:
print(f'Connected to {url}/{name}.')
await ws.send_json(
{
"question": question,
"envs": envs if envs else {},
}
)
async for msg in ws:
if msg.type == aiohttp.WSMsgType.TEXT:
if msg.data == 'close cmd':
await ws.close()
break
else:
try:
response = Response.parse_raw(msg.data)
print(response.result, end='')
except ValidationError:
try:
prompt = HumanPrompt.parse_raw(msg.data)
answer = input(prompt.prompt + '\n')
await ws.send_str(answer)
except ValidationError:
print(f'Unknown message: {msg.data}')
elif msg.type == aiohttp.WSMsgType.ERROR:
print('ws connection closed with exception %s' % ws.exception())
else:
print(msg)
asyncio.run(
hitl_client(
url='wss://langchain-1da55ad36a-websocket.wolf.jina.ai',
name='hitl',
question='What is Eric Zhu\'s birthday?',
envs={
'OPENAI_API_KEY': os.environ['OPENAI_API_KEY'],
},
)
)