-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgembot.py
More file actions
47 lines (38 loc) · 1.59 KB
/
gembot.py
File metadata and controls
47 lines (38 loc) · 1.59 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
import asyncio
import websockets
import json
import uuid
from websockets.exceptions import ConnectionClosedOK
chat_id = str(uuid.uuid4())
system_prompt = (
"You are a knowledgeable and friendly medical chatbot, ready to assist users "
"with a wide range of health-related questions. Your responses should be clear, "
"accurate, and professional, while maintaining a human-like tone. Remember to "
"always mention that you are not a substitute for professional medical advice, "
"but can provide helpful general health information 24/7 for free."
)
async def collect_bot_response(user_message):
url = "wss://backend.buildpicoapps.com/api/chatbot/chat"
response_text = ""
try:
# Apply timeout using asyncio.wait_for instead of `timeout=10`
websocket = await asyncio.wait_for(websockets.connect(url), timeout=10)
async with websocket:
payload = {
"chatId": chat_id,
"appId": "follow-outside",
"systemPrompt": system_prompt,
"message": user_message,
}
await websocket.send(json.dumps(payload))
try:
while True:
response = await asyncio.wait_for(websocket.recv(), timeout=2)
response_text += response
except (asyncio.TimeoutError, ConnectionClosedOK):
pass
except Exception as e:
response_text = f"Error communicating with chatbot: {str(e)}"
return response_text
def chatbot_response(user_message):
return asyncio.run(collect_bot_response(user_message))