Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -11,64 +11,23 @@ import json
|
|
11 |
|
12 |
import chainlit as cl
|
13 |
|
|
|
|
|
14 |
from langchain import hub
|
15 |
from langchain_openai import OpenAI
|
16 |
from langchain.chains import LLMChain, APIChain
|
17 |
from langchain_core.prompts import PromptTemplate
|
18 |
from langchain.memory.buffer import ConversationBufferMemory
|
|
|
|
|
|
|
19 |
|
20 |
from api_docs_mck import api_docs_str
|
21 |
-
from faq_data import ansatte_faq_data, utleiere_faq_data
|
22 |
-
from personvernspolicy import personvernspolicy_data
|
23 |
|
24 |
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
|
25 |
|
26 |
-
FAQ_ANSATTE = [
|
27 |
-
"Hvordan registrerer jeg meg som bruker?",
|
28 |
-
"Når får jeg leieinstruks for min bestilling? Informasjon om nøkler etc.?",
|
29 |
-
"Det står barneseng og barnestol under fasiliteter, må dette forhåndsbestilles?",
|
30 |
-
"Kan jeg ta med hund eller katt?",
|
31 |
-
"Jeg har lagt inn en bestilling hva skjer videre?",
|
32 |
-
"Jeg har bestilt firmahytte, men kan ikke reise. Kan jeg endre navn på bestillingen til min kollega eller familiemedlem som vil reise i stedet for meg?",
|
33 |
-
"Kan jeg avbestille min reservasjon?",
|
34 |
-
"Jeg har bestilt utvask. Hva må jeg gjøre i tillegg til dette?",
|
35 |
-
"Jeg er medlem og eier en hytte! Kan jeg bli utleier i DaysOff?",
|
36 |
-
"Bestille opphold?"
|
37 |
-
]
|
38 |
-
|
39 |
-
FAQ_UTLEIERE = [
|
40 |
-
"Hva er betingelser for utleie?",
|
41 |
-
"Hvor lang tid har jeg på å bekrefte en bestilling?",
|
42 |
-
"Hvilke kanselleringsregler gjelder?",
|
43 |
-
"Hvem er kundene deres?",
|
44 |
-
"Kan jeg legge inn rabatterte priser for å lage egne kampanjer?",
|
45 |
-
"Når mottar jeg betaling for leie?",
|
46 |
-
"Jeg fikk en e-post om ny bestilling, men jeg finner den ikke i systemet?",
|
47 |
-
"Hvordan registrerer jeg opptatte perioder i kalenderen?",
|
48 |
-
"Jeg leier ut i andre kanaler. Hvordan kan jeg synkronisere kalenderne?"
|
49 |
-
]
|
50 |
-
|
51 |
-
PERSONVERNSPOLICY_QUESTIONS = [
|
52 |
-
"Hvilke personlige opplysninger samler vi inn?",
|
53 |
-
"Kan dere motta personlig informasjon fra tredjepart?",
|
54 |
-
"Hvordan bruker vi dine personlige opplysninger?",
|
55 |
-
"Med hvem deler vi dine personlige opplysninger?",
|
56 |
-
"Adferdsmessig annonsering?",
|
57 |
-
"Hvordan reagerer vi på « Spor ikke » forespørsler?",
|
58 |
-
"Hva er dine rettigheter?",
|
59 |
-
"Hvordan beskytter vi dataene dine?",
|
60 |
-
"Hvilke data brudd prosedyrer har vi på plass?",
|
61 |
-
"Hvem i vårt team har tilgang til dine data?",
|
62 |
-
"Endringer i denne policyen"
|
63 |
-
]
|
64 |
-
|
65 |
-
# If you do not know the answer, just reply truthfully that you do not have the answer rather than
|
66 |
-
# giving inaccurate or speculative information.
|
67 |
-
|
68 |
-
#(i) retrieving booking information for a given booking ID,
|
69 |
-
#(ii) answering frequently asked questions (FAQ) about DaysOff firmahytteordning for employees and employers,
|
70 |
-
#(iii) addressing questions related to Daysoff's personvernspolicy.
|
71 |
-
|
72 |
daysoff_assistant_template = """
|
73 |
You are a customer support assistant (’kundeservice AI assistent’) for Daysoff.
|
74 |
By default, you respond in Norwegian language, using a warm, direct, and professional tone.
|
@@ -116,6 +75,19 @@ api_response_prompt = PromptTemplate(
|
|
116 |
template=api_response_template
|
117 |
)
|
118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
@cl.on_chat_start
|
120 |
def setup_multiple_chains():
|
121 |
|
@@ -123,16 +95,29 @@ def setup_multiple_chains():
|
|
123 |
model='gpt-3.5-turbo-instruct',
|
124 |
temperature=0.7,
|
125 |
openai_api_key=OPENAI_API_KEY,
|
126 |
-
|
127 |
top_p=0.9,
|
128 |
frequency_penalty=0.1,
|
129 |
presence_penalty=0.1
|
130 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
llm_chain = LLMChain(llm=llm,
|
137 |
prompt=daysoff_assistant_prompt,
|
138 |
memory=conversation_memory
|
@@ -160,12 +145,6 @@ async def handle_message(message: cl.Message):
|
|
160 |
booking_pattern = r'\b[A-Z]{6}\d{6}\b'
|
161 |
endpoint_url = "https://670dccd0073307b4ee447f2f.mockapi.io/daysoff/api/V1/booking"
|
162 |
|
163 |
-
#faq_keywords = (
|
164 |
-
#[key for key in ansatte_faq_data.keys()] +
|
165 |
-
#[key for key in utleiere_faq_data.keys()] +
|
166 |
-
#[key for key in personvernspolicy_data.keys()]
|
167 |
-
#)
|
168 |
-
|
169 |
if re.search(booking_pattern, user_message):
|
170 |
bestillingskode = re.search(booking_pattern, user_message).group(0)
|
171 |
question = f"Retrieve information for booking ID {endpoint_url}?search={bestillingskode}"
|
@@ -178,13 +157,6 @@ async def handle_message(message: cl.Message):
|
|
178 |
},
|
179 |
callbacks=[cl.AsyncLangchainCallbackHandler()])
|
180 |
|
181 |
-
#if any(keyword in user_message for keyword in (FAQ_ANSATTE + FAQ_UTLEIERE + PERSONVERNSPOLICY_QUESTIONS)):
|
182 |
-
#elif any(keyword in user_message for keyword in faq_keywords):
|
183 |
-
#response = await api_chain.acall(user_message, callbacks=[cl.AsyncLangchainCallbackHandler()])
|
184 |
-
|
185 |
-
#else:
|
186 |
-
#response = await llm_chain.acall(user_message, callbacks=[cl.AsyncLangchainCallbackHandler()])
|
187 |
-
|
188 |
else:
|
189 |
response = await llm_chain.acall(user_message, callbacks=[cl.AsyncLangchainCallbackHandler()])
|
190 |
|
|
|
11 |
|
12 |
import chainlit as cl
|
13 |
|
14 |
+
#from tiktoken import encoding_for_model
|
15 |
+
|
16 |
from langchain import hub
|
17 |
from langchain_openai import OpenAI
|
18 |
from langchain.chains import LLMChain, APIChain
|
19 |
from langchain_core.prompts import PromptTemplate
|
20 |
from langchain.memory.buffer import ConversationBufferMemory
|
21 |
+
from langchain.memory import ConversationTokenBufferMemory
|
22 |
+
from langchain.memory import ConversationSummaryMemory
|
23 |
+
|
24 |
|
25 |
from api_docs_mck import api_docs_str
|
26 |
+
#from faq_data import ansatte_faq_data, utleiere_faq_data
|
27 |
+
#from personvernspolicy import personvernspolicy_data
|
28 |
|
29 |
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
daysoff_assistant_template = """
|
32 |
You are a customer support assistant (’kundeservice AI assistent’) for Daysoff.
|
33 |
By default, you respond in Norwegian language, using a warm, direct, and professional tone.
|
|
|
75 |
template=api_response_template
|
76 |
)
|
77 |
|
78 |
+
# ---------------------------------------------------------------------------------------------------------
|
79 |
+
# 100 tokens ≃ 75 words
|
80 |
+
# system prompt(s), total = 330 tokens
|
81 |
+
# average api response = 250-300 tokens (current)
|
82 |
+
# user input "reserved" = 400 tokens (300 words max. /English; Polish, Norwegian {..}?@tiktokenizer), could be reduc3d to 140 tokens ≃ 105 words
|
83 |
+
# model output (max_tokens) = 2048
|
84 |
+
|
85 |
+
# ConversationBufferMemory = maintains raw chat history; crucial for "nuanced" follow-ups (e.g. "nuanced" ~ for non-English inputs)
|
86 |
+
# ConversationTokenBufferMemory (max_token_limit) = 1318 (gives space in chat_history for approximately 10-15 exchanges, assuming ~100 tokens/exchange)
|
87 |
+
# ConversationSummaryMemory = scalable approach, especially useful for extended or complex interactions, caveat: loss of granular context
|
88 |
+
# ---------------------------------------------------------------------------------------------------------
|
89 |
+
|
90 |
+
|
91 |
@cl.on_chat_start
|
92 |
def setup_multiple_chains():
|
93 |
|
|
|
95 |
model='gpt-3.5-turbo-instruct',
|
96 |
temperature=0.7,
|
97 |
openai_api_key=OPENAI_API_KEY,
|
98 |
+
max_tokens=2048,
|
99 |
top_p=0.9,
|
100 |
frequency_penalty=0.1,
|
101 |
presence_penalty=0.1
|
102 |
)
|
103 |
+
|
104 |
+
# --ConversationBufferMemory
|
105 |
+
#conversation_memory = ConversationBufferMemory(memory_key="chat_history",
|
106 |
+
#max_len=30, # --retains only the last 30 exchanges
|
107 |
+
#return_messages=True,
|
108 |
+
)
|
109 |
|
110 |
+
# --ConversationTokenBufferMemory
|
111 |
+
conversation_memory = ConversationTokenBufferMemory(memory_key="chat_history",
|
112 |
+
max_token_limit=1318,
|
113 |
+
return_messages=True,
|
114 |
+
)
|
115 |
+
|
116 |
+
# --ConversationSummaryMemory
|
117 |
+
#conversation_memory = ConversationSummaryMemory(memory_key="chat_history",
|
118 |
+
return_messages=True,
|
119 |
+
)
|
120 |
+
|
121 |
llm_chain = LLMChain(llm=llm,
|
122 |
prompt=daysoff_assistant_prompt,
|
123 |
memory=conversation_memory
|
|
|
145 |
booking_pattern = r'\b[A-Z]{6}\d{6}\b'
|
146 |
endpoint_url = "https://670dccd0073307b4ee447f2f.mockapi.io/daysoff/api/V1/booking"
|
147 |
|
|
|
|
|
|
|
|
|
|
|
|
|
148 |
if re.search(booking_pattern, user_message):
|
149 |
bestillingskode = re.search(booking_pattern, user_message).group(0)
|
150 |
question = f"Retrieve information for booking ID {endpoint_url}?search={bestillingskode}"
|
|
|
157 |
},
|
158 |
callbacks=[cl.AsyncLangchainCallbackHandler()])
|
159 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
else:
|
161 |
response = await llm_chain.acall(user_message, callbacks=[cl.AsyncLangchainCallbackHandler()])
|
162 |
|