CoolFace
Apppublic

farsim/dr_aida

sourceHugging Facemitupdated 2y agoView on Hugging Face
0likes
chat.py119 linesDownload Raw Back to root
1
2
3
4# import re
5# import requests
6# import json
7
8# def chat_with_mistral(message, message_history=[]):
9#     url = "https://api.mistral.ai/v1/chat/completions"
10#     headers = {
11#         "Authorization": "Bearer Hu5xolTb35UTAAu9rKUv8RvrvxpHtsXp",
12#         "Content-Type": "application/json"
13#     }
14#     messages = [
15#         {"role": "system", "content": "You are a virtual doctor named Mistral, empathetic and helpful. Respond with short sentences and ask follow-up questions to better understand the user's symptoms."}
16#     ] + message_history + [
17#         {"role": "user", "content": message}
18#     ]
19#     data = {
20#         "model": "mistral-small",
21#         "messages": messages
22#     }
23#     response = requests.post(url, headers=headers, data=json.dumps(data))
24#     return response.json()["choices"][0]["message"]["content"]
25
26# def extract_name(message):
27#     match = re.search(r"my name is (\w+)", message, re.IGNORECASE)
28#     if match:
29#         return match.group(1)
30#     else:
31#         return None
32
33# message_history = []
34# name = None
35
36# while not name:
37#     message = input("Mistral Doctor: Hi, I'm Dr. Mistral. May I have your name please? ")
38#     name = extract_name(message)
39#     if name:
40#         message_history.append({"role": "user", "content": message})
41#         print("Mistral Doctor: Nice to meet you, " + name + ". How are you feeling today? ")
42#     else:
43#         print("Mistral Doctor: I'm sorry, I didn't catch your name. Could you please tell me your name? ")
44
45# symptoms = input()
46# message_history.append({"role": "user", "content": symptoms})
47
48# while symptoms.lower() != "exit":
49#     response = chat_with_mistral(symptoms, message_history)
50#     print("Mistral Doctor: " + response)
51#     message_history.append({"role": "assistant", "content": response})
52#     symptoms = input("User: ")
53
54
55import requests
56import json
57import re
58
59def chat_with_mistral(message, message_history=[]):
60    url = "https://api.mistral.ai/v1/chat/completions"
61    headers = {
62        "Authorization": "Bearer Hu5xolTb35UTAAu9rKUv8RvrvxpHtsXp",
63        "Content-Type": "application/json"
64    }
65    messages = [
66        {"role": "system", "content": "You are a virtual doctor named Mistral, empathetic and helpful. Respond with short sentences and ask follow-up questions to better understand the user's symptoms. Stop asking questions about symptoms when the user indicates that they have no more symptoms to report. If the user has not mentioned any symptoms, respond with 'I'm sorry, I didn't catch any symptoms in your message. Could you please tell me what symptoms you are experiencing?' If the user has no more symptoms to report, respond with 'Thank you for sharing the information. Let me check my system based on the symptoms you have mentioned and get back to you with more information soon.'"}
67    ] + message_history + [
68        {"role": "user", "content": message}
69    ]
70    data = {
71        "model": "mistral-small",
72        "messages": messages
73    }
74    response = requests.post(url, headers=headers, data=json.dumps(data))
75    return response.json()["choices"][0]["message"]["content"]
76
77def extract_name(message):
78    match = re.search(r"(?:my name is|this is|hi, this is|hello, this is|hey, i'm|i am|i'm|hello, i'm|hello, i am|hi, i am|hi, i'm|hey, this is|it's|it is|hello, my name is|hi, my name is|hey, my name is) (\w+)", message, re.IGNORECASE)
79    if match:
80        return match.group(1)
81    else:
82        return None
83
84message_history = []
85name = None
86
87# Prompt the user for their name once
88message = input("Mistral Doctor: Hi, I'm Dr. Mistral. May I have your name please? ")
89
90# Keep asking for the name until it is successfully extracted
91while not name:
92    name = extract_name(message)
93    if name:
94        message_history.append({"role": "user", "content": message})
95        print("Mistral Doctor: Nice to meet you, " + name + ". How are you feeling today?")
96    else:
97        print("Mistral Doctor: I'm sorry, I didn't catch your name. Could you please tell me your name?")
98        message = input("User: ")
99
100# Continue with the conversation
101symptoms = input("User: ")
102message_history.append({"role": "user", "content": symptoms})
103
104while symptoms.lower() != "no more symptoms":
105    response = chat_with_mistral(symptoms, message_history)
106    print("Mistral Doctor: " + response)
107    message_history.append({"role": "assistant", "content": response})
108    
109    symptoms = input("User: ")
110    if "no more symptoms" in symptoms.lower():
111        break
112    message_history.append({"role": "user", "content": symptoms})
113
114print("Mistral Doctor: Thank you for sharing the information. Let me check my system based on the symptoms you have mentioned and get back to you with more information soon.")
115
116
117
118
119