-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.py
51 lines (43 loc) · 1.5 KB
/
chat.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
import asyncio
import json
import sys
from websockets.client import connect
# THIS ENDPOINT ALLOWS ALL USERS TO CHAT WITH MENTORS.
# ALSO NOTE THAT WHEN MENTOR IS SET TO ANONYMOUS EVEN ANONYMOUS USERS CAN CHAT
# Manager base url to be changed
ASGI_BASE_URL = "wss://asgi.data.<DOMAIN>"
# user details to be changed
TENANT = ""
USERNAME = ""
ACCESS_TOKEN = "" # this must be a tenant api key
async def chat_with_mentor():
"""
Chat with mentor.
"""
mentor = "" # This can mentor slug or mentor unique id.
data = {
"flow": {"name": mentor, "tenant": TENANT, "username": USERNAME},
"session_id": "", # session id generated forthis mentor and user.
"token": ACCESS_TOKEN,
"pathway": mentor,
"prompt": "Who is Rayana Barnawi",
}
ws = await connect(f"{ASGI_BASE_URL}/ws/langflow/")
await ws.send(json.dumps(data))
received_data = await ws.recv()
print(received_data)
print("websocket status", json.loads(received_data)["detail"])
print(f"Question: {data.get('prompt')}")
await ws.send(json.dumps(data))
while True:
try:
data_rec = await asyncio.wait_for(ws.recv(), timeout=4)
data_json = json.loads(data_rec)
data_to_write = data_json.get("data")
if data_to_write:
sys.stdout.write(data_to_write)
except asyncio.TimeoutError:
sys.stdout.write("\n")
break
loop = asyncio.get_event_loop()
loop.run_until_complete(chat_with_mentor())