-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
164 lines (127 loc) · 5.38 KB
/
app.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
from dotenv import load_dotenv
from openai import AsyncOpenAI
import os
import chainlit as cl
import json
from prompts import SYSTEM_PROMPT
from tools import TOOLS
cl.instrument_openai()
load_dotenv()
client = AsyncOpenAI(api_key=os.getenv('OPENAI_API_KEY'))
settings = {
"model": "gpt-3.5-turbo-1106",
"temperature": 0.2,
}
@cl.on_chat_start
async def on_chat_start():
cl.user_session.set("message_history", [
{"role": "system", "content": SYSTEM_PROMPT},
])
msg = cl.Message(
content="Hello! Thanks for contacting ABC Dental Clinic office. How can I help you today?")
await msg.send()
PATIENT_DATA = [
{
"first_name": "Peter",
"last_name": "Jausovec",
"phone_number": "123-456-7890",
"email": "[email protected]"
},
{
"first_name": "John",
"last_name": "Doe",
"phone_number": "555-5555",
"email": "[email protected]"
},
]
def save_patient_info(first_name: str, last_name: str, phone_number: str, email: str):
print(f"Saving patient information for {first_name} {last_name}")
# Check if the patient already exists
for patient in PATIENT_DATA:
if patient["first_name"].lower() == first_name.lower() and patient["last_name"].lower() == last_name.lower():
return "Patient already exists."
# Add the patient to the PATIENT_DATA list
PATIENT_DATA.append({
"first_name": first_name,
"last_name": last_name,
"phone_number": phone_number,
"email": email
})
return "Patient information saved."
def get_patient_info(first_name: str, last_name: str):
print(f"Getting patient information for {first_name} {last_name}")
# Check the PAITENT_DATA list for the patient
for patient in PATIENT_DATA:
if patient["first_name"].lower() == first_name.lower() and patient["last_name"].lower() == last_name.lower():
return patient
return "Patient not found."
def check_appointment_availability(date: str, time: str, appointment_type: str):
print(
f"Checking appointment availability for {date} and {appointment_type}")
# Pick 3 random days and times for demonstration
available_slots = [
{"date": "Monday", "time": "1 PM"},
{"date": "Tuesday", "time": "3 PM"},
{"date": "Friday", "time": "10 AM"},
]
# Check the schedule for availability
resp = f"Appointment is not available. Available slots: {available_slots}"
return resp
def schedule_appointment(date: str, appointment_type: str, time: str, first_name: str, last_name: str):
print(
f"Scheduling appointment for {date} and {appointment_type}, {time} for {first_name} {last_name}")
# Check the schedule for availability
return "Appointment is scheduled."
def create_appointment_reminder(reminder_type: str, date: str, appointment_type: str, time: str, first_name: str, last_name: str):
print(
f"Creating appointment reminder for {reminder_type} for {first_name} {last_name}")
# Check the schedule for availability
return "Appointment reminder created."
@cl.on_message
async def on_message(message: cl.Message):
message_history = cl.user_session.get("message_history")
message_history.append({"role": "user", "content": message.content})
response = await client.chat.completions.create(
messages=message_history,
tools=TOOLS,
tool_choice="auto",
**settings)
response_message = response.choices[0].message
message_history.append(response_message)
if response_message.tool_calls:
for tool_call in response_message.tool_calls:
function_name = tool_call.function.name
function_args = json.loads(tool_call.function.arguments)
function_response = None
if function_name == "get_patient_info":
function_response = get_patient_info(**function_args)
elif function_name == "save_patient_info":
function_response = save_patient_info(**function_args)
elif function_name == "check_appointment_availability":
function_response = check_appointment_availability(
**function_args)
elif function_name == "schedule_appointment":
function_response = schedule_appointment(**function_args)
elif function_name == "create_appointment_reminder":
function_response = create_appointment_reminder(
**function_args)
else:
print(f"Function {function_name} not found")
message_history.append({
"tool_call_id": tool_call.id,
"role": "tool",
"name": function_name,
"content": json.dumps(function_response)
})
print(f"Message history: {message_history}")
print(f"Function response: {function_response}")
response = await client.chat.completions.create(
messages=message_history,
# tools=TOOLS,
# tool_choice="auto",
**settings)
await cl.Message(content=response.choices[0].message.content).send()
elif response_message.role == "assistant":
message_history.append(
{"role": "assistant", "content": response.choices[0].message.content})
await cl.Message(content=response.choices[0].message.content).send()