-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatBot_API.py
More file actions
63 lines (56 loc) · 2.25 KB
/
Copy pathChatBot_API.py
File metadata and controls
63 lines (56 loc) · 2.25 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from multiprocessing import Manager, Process
import traceback
from azure.ai.inference import ChatCompletionsClient
from azure.ai.inference.models import SystemMessage, UserMessage
from azure.core.credentials import AzureKeyCredential
# Config
ENDPOINT = "https://models.github.ai/inference"
MODEL = "openai/gpt-4.1"
TOKEN = "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"
# Initialize client
client = ChatCompletionsClient(
endpoint=ENDPOINT,
credential=AzureKeyCredential(TOKEN),
)
# System prompt
chat_history = [
SystemMessage(content=(
"You are MindMate, a compassionate and understanding mental health assistant. "
"You listen carefully and respond with empathy, support, and helpful advice. "
"Give short and concise message but with deepness, use bullet points like 1. 2. etc to show any steps. "
"Use proper formatting. Do not give very long answers. Use emojis to keep the tone soothing. "
"Do not use '*' symbols to bold text. In my text box, it won't bold the text so don't use asterisks at all in the response. "
"For answers with bullet points use this format: Bla Bla Bla:\n"
"1. Bla \n2. Bla \n3. Bla\n\nBla."
))
]
# Worker
def _bot_request_worker(user_input, return_dict):
try:
chat_history.append(UserMessage(content=user_input))
response = client.complete(
messages=chat_history,
temperature=0.7,
top_p=0.9,
max_tokens=1024,
model=MODEL,
)
bot_reply = response.choices[0].message.content.strip()
chat_history.append(SystemMessage(content=bot_reply))
return_dict["result"] = bot_reply
except Exception as e:
return_dict["error"] = str(e)
return_dict["trace"] = traceback.format_exc()
# Interface
def get_bot_response(user_input: str, timeout: int = 10) -> str:
manager = Manager()
return_dict = manager.dict()
p = Process(target=_bot_request_worker, args=(user_input, return_dict))
p.start()
p.join(timeout)
if p.is_alive():
p.terminate()
raise TimeoutError("Request timed out.")
if "error" in return_dict:
raise RuntimeError(f"Bot Error: {return_dict['error']}\n{return_dict['trace']}")
return return_dict.get("result", "")