camparchimedes commited on
Commit
c797445
·
verified ·
1 Parent(s): 243e7d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -49
app.py CHANGED
@@ -1,14 +1,24 @@
1
 
2
  # ===========================================
3
- # ver02.02-Zeta.workload-----app.py
4
  # ===========================================
5
 
 
 
 
 
 
 
 
 
 
 
 
6
  import asyncio
7
  import os
8
  import re
9
  import time
10
  import json
11
- import pandas as pd
12
 
13
  import chainlit as cl
14
 
@@ -23,25 +33,34 @@ from langchain.memory import ConversationTokenBufferMemory
23
  from langchain.memory import ConversationSummaryMemory
24
 
25
  from api_docs_mck import api_docs_str
 
 
 
26
 
27
 
28
  OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
29
 
 
 
 
30
  daysoff_assistant_template = """
31
  You are a customer support assistant (’kundeservice AI assistent’) for Daysoff.
32
  By default, you respond in Norwegian language, using a warm, direct, and professional tone.
33
- Your expertise is exclusively in retrieving booking information for a given booking ID and answering
34
- FAQs about Daysoff firmahytteorning and personvernspolicy.
35
- If a question is not about these topics, respond with
36
- "Jeg driver faktisk kun med henvendelser omkring bestillingsinformasjon og ofte-stilte-spørsmål i forbindelse
37
- med DaysOff firmahytteordning (inkludert personvernspolicyn). Gjelder det andre ting
38
- du nok kontakte kundeservice direkte kundeservice@daysoff.no😊"
 
 
 
39
  Chat History: {chat_history}
40
  Question: {question}
41
  Answer:
42
  """
43
  daysoff_assistant_prompt = PromptTemplate(
44
- input_variables=['chat_history', 'question'],
45
  template=daysoff_assistant_template
46
  )
47
 
@@ -57,6 +76,7 @@ API URL:
57
  api_url_prompt = PromptTemplate(input_variables=['api_docs', 'question'],
58
  template=api_url_template)
59
 
 
60
 
61
  api_response_template = """
62
  With the API Documentation for Daysoff's official API: {api_docs} in mind,
@@ -104,12 +124,14 @@ def setup_multiple_chains():
104
  conversation_memory = ConversationBufferMemory(memory_key="chat_history",
105
  max_len=30, # --retains only the last 30 exchanges
106
  return_messages=True,
107
- )
 
108
  # --ConversationTokenBufferMemory
109
  #conversation_memory = ConversationTokenBufferMemory(memory_key="chat_history",
110
  #max_token_limit=1318,
111
  #return_messages=True,
112
  #)
 
113
  # --ConversationSummaryMemory
114
  #conversation_memory = ConversationSummaryMemory(memory_key="chat_history",
115
  #return_messages=True,
@@ -125,6 +147,10 @@ def setup_multiple_chains():
125
  api_chain = APIChain.from_llm_and_api_docs(
126
  llm=llm,
127
  api_docs=api_docs_str,
 
 
 
 
128
  api_url_prompt=api_url_prompt,
129
  api_response_prompt=api_response_prompt,
130
  verbose=True,
@@ -133,62 +159,27 @@ def setup_multiple_chains():
133
 
134
  cl.user_session.set("api_chain", api_chain)
135
 
136
-
137
  @cl.on_message
138
  async def handle_message(message: cl.Message):
139
  user_message = message.content #.lower()
140
  llm_chain = cl.user_session.get("llm_chain")
141
  api_chain = cl.user_session.get("api_chain")
142
 
143
- # --regex
144
  booking_pattern = r'\b[A-Z]{6}\d{6}\b'
145
- # --endpoint
146
  endpoint_url = "https://670dccd0073307b4ee447f2f.mockapi.io/daysoff/api/V1/booking"
147
- # --dataframe
148
- personvernspolicy_df = pd.read_csv('personvernspolicy.csv')
149
- faq_df = pd.read_csv('faq.csv')
150
- # --dictionaries
151
- personvernspolicy_qa = personvernspolicy_df.to_dict(orient='records')
152
- faq_qa = faq_df.to_dict(orient='records')
153
- # --keywords
154
- personvernspolicy_keywords = personvernspolicy_df['question'].tolist()
155
- faq_keywords = faq_df['question'].tolist()
156
- all_keywords = personvernspolicy_keywords + faq_keywords
157
 
158
-
159
  if re.search(booking_pattern, user_message):
160
-
161
  bestillingskode = re.search(booking_pattern, user_message).group(0)
162
  question = f"Retrieve information for booking ID {endpoint_url}?search={bestillingskode}"
163
-
164
  response = await api_chain.acall(
165
  {
166
  "bestillingskode": bestillingskode,
167
  "question": question
 
168
  },
169
- callbacks=[cl.AsyncLangchainCallbackHandler()]
170
- )
171
-
172
- elif any(keyword.lower() in user_message.lower() for keyword in all_keywords):
173
-
174
- matched_entry = None
175
- for qa_entry in personvernspolicy_qa + faq_qa:
176
- if qa_entry['question'].lower() in user_message.lower():
177
- matched_entry = qa_entry
178
- break
179
-
180
- if matched_entry:
181
- question = f"User Question: {user_message}\nMatched FAQ:\n{matched_entry}"
182
- response = await llm_chain.acall(
183
- {
184
- "question": question
185
- },
186
- callbacks=[cl.AsyncLangchainCallbackHandler()]
187
- )
188
- else:
189
-
190
- response = await llm_chain.acall(user_message, callbacks=[cl.AsyncLangchainCallbackHandler()])
191
-
192
  else:
193
  response = await llm_chain.acall(user_message, callbacks=[cl.AsyncLangchainCallbackHandler()])
194
 
 
1
 
2
  # ===========================================
3
+ # ver01.01-5.workload-----app.py
4
  # ===========================================
5
 
6
+
7
+
8
+ #You are a customer support assistant (’kundeservice AI assistent’) for Daysoff.
9
+ #By default, you respond in Norwegian language, using a warm, direct, and professional tone.
10
+ #Your expertise is exclusively in retrieving booking information for a given booking ID assistance related to
11
+ #to this.
12
+ #You do not provide information outside of this scope. If a question is not about this topic, respond with
13
+ #"Ooops da, jeg driver faktisk kun med henvendelser omkring bestillingsinformasjon. Gjelder det andre henvendelser,
14
+ #må du nok kontakte kundeservice på [email protected]😊"
15
+
16
+
17
  import asyncio
18
  import os
19
  import re
20
  import time
21
  import json
 
22
 
23
  import chainlit as cl
24
 
 
33
  from langchain.memory import ConversationSummaryMemory
34
 
35
  from api_docs_mck import api_docs_str
36
+ from frequently_asked_questions import instruction_text, frequently_asked_questions
37
+ from personvernspolicy import instruction_text_priv, personvernspolicy_data
38
+ from frequently_asked_questions import instruction_text_faq, faq
39
 
40
 
41
  OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
42
 
43
+
44
+
45
+ # If you don't know the answer, just say that you don't know, don't try to make up an answer.
46
  daysoff_assistant_template = """
47
  You are a customer support assistant (’kundeservice AI assistent’) for Daysoff.
48
  By default, you respond in Norwegian language, using a warm, direct, and professional tone.
49
+ Your expertise is exclusively in retrieving booking information for a given booking id and answering
50
+ questions about firmahytteorning and personvernspolicy.
51
+ If a question does not involve booking information for a given booking id, ask: "Gjelder spørsmålet firmahytteordning?",
52
+ upon user confirmation, do your best to try to answer accordingly by referring to {instruction_text_faq} and {faq}.
53
+ If a query does not involve booking information for a given booking id or firmahytteordning, ask: "Gjelder spørsmålet personvernspolicy?"
54
+ upon user confirmation, do your best to provide a precise privacy-related response by referring to: {instruction_text_priv} and {personvernspolicy_data}.
55
+ If the query does not involve booking information for a given booking id, firmahytteordning or personvernspolicy,
56
+ respond with: "Jeg driver faktisk kun med henvendelser omkring bestillingsinformasjon og ofte-stilte-spørsmål i forbindelse
57
+ med DaysOff firmahytteordning (inkludert personvernspolicyn). Gjelder det andre henvendelser, må du nok kontakte kundeservice på [email protected]😊"
58
  Chat History: {chat_history}
59
  Question: {question}
60
  Answer:
61
  """
62
  daysoff_assistant_prompt = PromptTemplate(
63
+ input_variables=['chat_history', 'question', "instruction_text_faq", "faq", "instruction_text_priv", "personvernspolicy_data"],
64
  template=daysoff_assistant_template
65
  )
66
 
 
76
  api_url_prompt = PromptTemplate(input_variables=['api_docs', 'question'],
77
  template=api_url_template)
78
 
79
+ # If the response includes booking information, provide the information verbatim (do not summarize it.)
80
 
81
  api_response_template = """
82
  With the API Documentation for Daysoff's official API: {api_docs} in mind,
 
124
  conversation_memory = ConversationBufferMemory(memory_key="chat_history",
125
  max_len=30, # --retains only the last 30 exchanges
126
  return_messages=True,
127
+ )
128
+
129
  # --ConversationTokenBufferMemory
130
  #conversation_memory = ConversationTokenBufferMemory(memory_key="chat_history",
131
  #max_token_limit=1318,
132
  #return_messages=True,
133
  #)
134
+
135
  # --ConversationSummaryMemory
136
  #conversation_memory = ConversationSummaryMemory(memory_key="chat_history",
137
  #return_messages=True,
 
147
  api_chain = APIChain.from_llm_and_api_docs(
148
  llm=llm,
149
  api_docs=api_docs_str,
150
+ instruction_text_faq=instruction_text_faq,
151
+ faq=faq,
152
+ instruction_text_priv=instruction_text_priv,
153
+ personvernspolicy_data=personvernspolicy_data,
154
  api_url_prompt=api_url_prompt,
155
  api_response_prompt=api_response_prompt,
156
  verbose=True,
 
159
 
160
  cl.user_session.set("api_chain", api_chain)
161
 
 
162
  @cl.on_message
163
  async def handle_message(message: cl.Message):
164
  user_message = message.content #.lower()
165
  llm_chain = cl.user_session.get("llm_chain")
166
  api_chain = cl.user_session.get("api_chain")
167
 
 
168
  booking_pattern = r'\b[A-Z]{6}\d{6}\b'
 
169
  endpoint_url = "https://670dccd0073307b4ee447f2f.mockapi.io/daysoff/api/V1/booking"
 
 
 
 
 
 
 
 
 
 
170
 
 
171
  if re.search(booking_pattern, user_message):
 
172
  bestillingskode = re.search(booking_pattern, user_message).group(0)
173
  question = f"Retrieve information for booking ID {endpoint_url}?search={bestillingskode}"
174
+
175
  response = await api_chain.acall(
176
  {
177
  "bestillingskode": bestillingskode,
178
  "question": question
179
+
180
  },
181
+ callbacks=[cl.AsyncLangchainCallbackHandler()])
182
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
183
  else:
184
  response = await llm_chain.acall(user_message, callbacks=[cl.AsyncLangchainCallbackHandler()])
185