camparchimedes
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -57,7 +57,6 @@ Polish Terms:
|
|
57 |
|
58 |
If there is no booking information available for a booking ID, just say so and politely suggest they contact **[email protected]**
|
59 |
to enquire further.
|
60 |
-
By default, you respond using Norwegian bokmรฅl.
|
61 |
|
62 |
Maintain a conversational and professional tone, **reflecting the warmth of
|
63 |
a female customer support representative archetype.**
|
@@ -72,10 +71,37 @@ daysoff_assistant_prompt = PromptTemplate(
|
|
72 |
template=daysoff_assistant_template,
|
73 |
)
|
74 |
|
75 |
-
# -- async wrapper for requests.post
|
76 |
-
async def async_post_request(url, headers, data):
|
77 |
-
return await asyncio.to_thread(requests.post, url, headers=headers, json=data)
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
@cl.on_chat_start
|
81 |
def setup_multiple_chains():
|
@@ -125,49 +151,48 @@ async def handle_message(message: cl.Message):
|
|
125 |
response.raise_for_status()
|
126 |
booking_data = response.json()
|
127 |
|
128 |
-
if
|
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 |
except requests.exceptions.RequestException as e:
|
156 |
-
await cl.Message(content=
|
|
|
157 |
|
158 |
else:
|
159 |
try:
|
160 |
-
# --invoke LLM w/ user_message
|
161 |
response = await llm_chain.ainvoke({
|
162 |
"question": user_message,
|
163 |
"chat_history": ""
|
164 |
}, callbacks=[cl.AsyncLangchainCallbackHandler()])
|
165 |
-
|
166 |
-
# Send the LLM response as a message
|
167 |
-
#await cl.Message(content=response.get("text")).send()
|
168 |
await cl.Message(content=response["text"]).send()
|
169 |
-
|
170 |
except Exception as e:
|
171 |
await cl.Message(content=f"Error: {str(e)}").send()
|
172 |
-
|
173 |
-
|
|
|
57 |
|
58 |
If there is no booking information available for a booking ID, just say so and politely suggest they contact **[email protected]**
|
59 |
to enquire further.
|
|
|
60 |
|
61 |
Maintain a conversational and professional tone, **reflecting the warmth of
|
62 |
a female customer support representative archetype.**
|
|
|
71 |
template=daysoff_assistant_template,
|
72 |
)
|
73 |
|
|
|
|
|
|
|
74 |
|
75 |
+
# =============================================================================================================
|
76 |
+
class APIConnectionError(Exception):
|
77 |
+
"""Raised when API connection fails"""
|
78 |
+
pass
|
79 |
+
|
80 |
+
class APIResponseError(Exception):
|
81 |
+
"""Raised when API returns invalid response"""
|
82 |
+
pass
|
83 |
+
|
84 |
+
class BookingNotFoundError(Exception):
|
85 |
+
"""Raised when booking ID is not found"""
|
86 |
+
pass
|
87 |
+
# =============================================================================================================
|
88 |
+
|
89 |
+
#async def async_post_request(url, headers, data):
|
90 |
+
#return await asyncio.to_thread(requests.post, url, headers=headers, json=data)
|
91 |
+
|
92 |
+
# =============================================================================================================
|
93 |
+
async def async_post_request(url, headers, data):
|
94 |
+
try:
|
95 |
+
response = await asyncio.to_thread(requests.post, url, headers=headers, json=data)
|
96 |
+
response.raise_for_status()
|
97 |
+
return response
|
98 |
+
except requests.ConnectionError:
|
99 |
+
raise APIConnectionError("Failed to connect to booking service")
|
100 |
+
except requests.Timeout:
|
101 |
+
raise APIConnectionError("Request timed out")
|
102 |
+
except requests.RequestException as e:
|
103 |
+
raise APIResponseError(f"API request failed: {str(e)}")
|
104 |
+
# =============================================================================================================
|
105 |
|
106 |
@cl.on_chat_start
|
107 |
def setup_multiple_chains():
|
|
|
151 |
response.raise_for_status()
|
152 |
booking_data = response.json()
|
153 |
|
154 |
+
if not booking_data:
|
155 |
+
raise BookingNotFoundError("No booking data returned")
|
156 |
+
|
157 |
+
if "error" in booking_data:
|
158 |
+
raise APIResponseError(booking_data["error"])
|
159 |
+
|
160 |
+
# --markdown_table
|
161 |
+
table = (
|
162 |
+
"| ๐ญ๐๐๐๐
| ๐๐ป๐ณ๐ผ |\n"
|
163 |
+
"|:-----------|:---------------------|\n"
|
164 |
+
f"| ๐ฑ๐๐๐๐๐๐๐๐๐๐๐๐๐๐ | {booking_data.get('booking_id', 'N/A')} |\n"
|
165 |
+
f"| ๐๐ช๐ก๐ก ๐๐๐ข๐ | {booking_data.get('full_name', 'N/A')} |\n"
|
166 |
+
f"| ๐ผ๐ข๐ค๐ช๐ฃ๐ฉ | {booking_data.get('amount', 0)} kr |\n"
|
167 |
+
f"| ๐พ๐๐๐๐ -๐๐ฃ | {booking_data.get('checkin', 'N/A')} |\n"
|
168 |
+
f"| ๐พ๐๐๐๐ -๐ค๐ช๐ฉ | {booking_data.get('checkout', 'N/A')} |\n"
|
169 |
+
f"| ๐ผ๐๐๐ง๐๐จ๐จ | {booking_data.get('address', 'N/A')} |\n"
|
170 |
+
f"| ๐๐จ๐๐ง ๐๐ฟ | {booking_data.get('user_id', 0)} |\n"
|
171 |
+
f"| ๐๐ฃ๐๐ค ๐๐๐ญ๐ฉ | {booking_data.get('infotext', 'N/A')} |\n"
|
172 |
+
f"| ๐๐ฃ๐๐ก๐ช๐๐๐ | {booking_data.get('included', 'N/A')} |"
|
173 |
+
)
|
174 |
+
|
175 |
+
combined_message = f"### Informasjon for Bestillingskode:\n\n{table}"
|
176 |
+
await cl.Message(content=combined_message).send()
|
177 |
+
|
178 |
+
except (APIConnectionError, APIResponseError, BookingNotFoundError) as e:
|
179 |
+
error_messages = {
|
180 |
+
APIConnectionError: "Kunne ikke koble til bookingsystemet. Prรธv igjen senere.",
|
181 |
+
APIResponseError: "Det oppstod en feil ved henting av bookingdata.",
|
182 |
+
BookingNotFoundError: "Ingen booking funnet med denne koden."
|
183 |
+
}
|
184 |
+
await cl.Message(content=f"โ {error_messages[type(e)]}\n\nKontakt [email protected] for assistanse.").send()
|
185 |
+
return None
|
186 |
except requests.exceptions.RequestException as e:
|
187 |
+
await cl.Message(content="En uventet feil oppstod. Vennligst kontakt [email protected]").send()
|
188 |
+
return None
|
189 |
|
190 |
else:
|
191 |
try:
|
|
|
192 |
response = await llm_chain.ainvoke({
|
193 |
"question": user_message,
|
194 |
"chat_history": ""
|
195 |
}, callbacks=[cl.AsyncLangchainCallbackHandler()])
|
|
|
|
|
|
|
196 |
await cl.Message(content=response["text"]).send()
|
|
|
197 |
except Exception as e:
|
198 |
await cl.Message(content=f"Error: {str(e)}").send()
|
|
|
|