Update app.py
Browse files
app.py
CHANGED
@@ -21,10 +21,32 @@ from langchain.memory.buffer import ConversationBufferMemory
|
|
21 |
|
22 |
|
23 |
HUGGINGFACEHUB_API_TOKEN = os.environ.get("HUGGINGFACEHUB_API_TOKEN")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
daysoff_assistant_booking_template = """
|
26 |
-
You are a customer support assistant for Daysoff.no.
|
27 |
-
|
|
|
|
|
|
|
28 |
Chat History: {chat_history}
|
29 |
Question: {question}
|
30 |
Answer:
|
@@ -41,19 +63,20 @@ Your task is to construct the most efficient API URL to answer
|
|
41 |
the user's question, ensuring the
|
42 |
call is optimized to include only the necessary information.
|
43 |
Question: {question}
|
|
|
44 |
"""
|
45 |
api_url_prompt = PromptTemplate(input_variables=['api_docs', 'question'],
|
46 |
template=api_url_template)
|
47 |
|
48 |
api_response_template = """
|
49 |
With the API Documentation for Daysoff's official API: {api_docs}
|
50 |
-
|
51 |
and given this API URL: {api_url} for querying, here is the
|
52 |
response from Daysoff's API: {api_response}.
|
53 |
-
Please provide
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
Booking Info:
|
58 |
"""
|
59 |
|
@@ -62,11 +85,9 @@ api_response_prompt = PromptTemplate(
|
|
62 |
template=api_response_template
|
63 |
)
|
64 |
|
65 |
-
|
66 |
@cl.on_chat_start
|
67 |
def setup_multiple_chains():
|
68 |
-
|
69 |
-
|
70 |
llm = HuggingFaceEndpoint(
|
71 |
repo_id="google/gemma-2-2b-it",
|
72 |
huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN,
|
@@ -97,46 +118,47 @@ def setup_multiple_chains():
|
|
97 |
|
98 |
|
99 |
cl.user_session.set("api_chain", api_chain)
|
|
|
|
|
100 |
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
"bookingen",
|
107 |
-
"ordrenummer",
|
108 |
-
"reservation",
|
109 |
-
"rezerwacji",
|
110 |
-
"bookingreferanse",
|
111 |
-
"rezerwacja",
|
112 |
-
"logg inn",
|
113 |
-
"booket",
|
114 |
-
"reservation number",
|
115 |
-
"bestilling",
|
116 |
-
"order number",
|
117 |
-
"booking ID",
|
118 |
-
"identyfikacyjny płatności"
|
119 |
-
]
|
120 |
-
|
121 |
@cl.on_message
|
122 |
async def handle_message(message: cl.Message):
|
123 |
user_message = message.content.lower()
|
124 |
llm_chain = cl.user_session.get("llm_chain")
|
125 |
api_chain = cl.user_session.get("api_chain")
|
126 |
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
|
133 |
-
|
134 |
-
|
|
|
|
|
135 |
callbacks=[cl.AsyncLangchainCallbackHandler()])
|
|
|
136 |
else:
|
|
|
137 |
response = await llm_chain.acall(user_message,
|
138 |
callbacks=[cl.AsyncLangchainCallbackHandler()])
|
139 |
|
140 |
response_key = "output" if "output" in response else "text"
|
141 |
await cl.Message(response.get(response_key, "")).send()
|
142 |
-
return message.content
|
|
|
|
|
|
|
|
21 |
|
22 |
|
23 |
HUGGINGFACEHUB_API_TOKEN = os.environ.get("HUGGINGFACEHUB_API_TOKEN")
|
24 |
+
BOOKING_ID = r'\b[A-Z]{6}\d{6}\b'
|
25 |
+
BOOKING_KEYWORDS = [
|
26 |
+
"booking",
|
27 |
+
"bestillingsnummer",
|
28 |
+
"bookingen",
|
29 |
+
"ordrenummer",
|
30 |
+
"reservation",
|
31 |
+
"rezerwacji",
|
32 |
+
"bookingreferanse",
|
33 |
+
"rezerwacja",
|
34 |
+
"logg inn",
|
35 |
+
"booket",
|
36 |
+
"reservation number",
|
37 |
+
"bestilling",
|
38 |
+
"order number",
|
39 |
+
"booking ID",
|
40 |
+
"identyfikacyjny płatności"
|
41 |
+
]
|
42 |
+
|
43 |
|
44 |
daysoff_assistant_booking_template = """
|
45 |
+
You are a Norwegian customer support AI assistant for Daysoff.no. By default,
|
46 |
+
you respond in Norwegian to user question: {question}. In all other cases,
|
47 |
+
adapt to user's language and respond accordingly.
|
48 |
+
You can retrieving booking information for a given booking ID and provide
|
49 |
+
general helpful info about Daysoff's services."
|
50 |
Chat History: {chat_history}
|
51 |
Question: {question}
|
52 |
Answer:
|
|
|
63 |
the user's question, ensuring the
|
64 |
call is optimized to include only the necessary information.
|
65 |
Question: {question}
|
66 |
+
API URL:
|
67 |
"""
|
68 |
api_url_prompt = PromptTemplate(input_variables=['api_docs', 'question'],
|
69 |
template=api_url_template)
|
70 |
|
71 |
api_response_template = """
|
72 |
With the API Documentation for Daysoff's official API: {api_docs}
|
73 |
+
in mind, and IF {question} contains keywords from:{BOOKING_KEYWORDS},
|
74 |
and given this API URL: {api_url} for querying, here is the
|
75 |
response from Daysoff's API: {api_response}.
|
76 |
+
Please provide a summary that directly addresses the user's question,
|
77 |
+
omitting technical details like response format, and
|
78 |
+
focusing on delivering the answer with clarity and conciseness,
|
79 |
+
as if Daysoff's kundeservice is providing this information themselves.
|
80 |
Booking Info:
|
81 |
"""
|
82 |
|
|
|
85 |
template=api_response_template
|
86 |
)
|
87 |
|
|
|
88 |
@cl.on_chat_start
|
89 |
def setup_multiple_chains():
|
90 |
+
|
|
|
91 |
llm = HuggingFaceEndpoint(
|
92 |
repo_id="google/gemma-2-2b-it",
|
93 |
huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN,
|
|
|
118 |
|
119 |
|
120 |
cl.user_session.set("api_chain", api_chain)
|
121 |
+
|
122 |
+
import re
|
123 |
|
124 |
+
def extract_booking_id(user_message):
|
125 |
+
# Extract booking ID from user message
|
126 |
+
match = re.search(r'\b[A-Z]{6}\d{6}\b', user_message)
|
127 |
+
return match.group(0) if match else None
|
128 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
@cl.on_message
|
130 |
async def handle_message(message: cl.Message):
|
131 |
user_message = message.content.lower()
|
132 |
llm_chain = cl.user_session.get("llm_chain")
|
133 |
api_chain = cl.user_session.get("api_chain")
|
134 |
|
135 |
+
# booking_id = extract_booking_id(user_message)
|
136 |
+
|
137 |
+
|
138 |
+
#is_booking_query = any(
|
139 |
+
#re.search(keyword, user_message, re.IGNORECASE) # re.search(keyword, r'\b[A-Z]{6}\d{6}\b', user_message, re.IGNORECASE)
|
140 |
+
#for keyword in BOOKING_KEYWORDS + [BOOKING_ID]
|
141 |
+
#)
|
142 |
+
if any(keyword in user_message for keyword in ["BOOKING_KEYWORDS"]): # could be(?): if any(keyword in user_message for keyword in BOOKING_KEYWORDS + [BOOKING_ID]
|
143 |
+
|
144 |
+
# <f any of the keywords are in user_message>, use api_chain
|
145 |
+
response = await api_chain.acall(user_message,
|
146 |
+
callbacks=[cl.AsyncLangchainCallbackHandler()])
|
147 |
|
148 |
+
elif
|
149 |
+
# <or if the booking_id regex format is discovered in user message>, use api_chain
|
150 |
+
re.search(keyword, r'\b[A-Z]{6}\d{6}\b', user_message, re.IGNORECASE):
|
151 |
+
response = await api_chain.acall(user_message,
|
152 |
callbacks=[cl.AsyncLangchainCallbackHandler()])
|
153 |
+
|
154 |
else:
|
155 |
+
|
156 |
response = await llm_chain.acall(user_message,
|
157 |
callbacks=[cl.AsyncLangchainCallbackHandler()])
|
158 |
|
159 |
response_key = "output" if "output" in response else "text"
|
160 |
await cl.Message(response.get(response_key, "")).send()
|
161 |
+
return message.content
|
162 |
+
|
163 |
+
# await cl.Message(response.get(response_key, "")).send()
|
164 |
+
# content=f"Her er informasjonen for bestillingsnummer {booking_id}:\n{booking_info}").send()
|