camparchimedes commited on
Commit
f79a832
ยท
verified ยท
1 Parent(s): 221140c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -39
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 "booking_id" in booking_data:
129
- try:
130
- # --markdown_table
131
- table = (
132
- "| ๐‘ญ๐’Š๐’†๐’๐’… | ๐—œ๐—ป๐—ณ๐—ผ |\n"
133
- "|:-----------|:---------------------|\n"
134
- f"| ๐™ฑ๐šŽ๐šœ๐š๐š’๐š•๐š•๐š’๐š—๐š๐šœ๐š”๐š˜๐š๐šŽ | {booking_data.get('booking_id', 'N/A')} |\n"
135
- f"| ๐™๐™ช๐™ก๐™ก ๐™‰๐™–๐™ข๐™š | {booking_data.get('full_name', 'N/A')} |\n"
136
- f"| ๐˜ผ๐™ข๐™ค๐™ช๐™ฃ๐™ฉ | {booking_data.get('amount', 0)} kr |\n"
137
- f"| ๐˜พ๐™๐™š๐™˜๐™ -๐™ž๐™ฃ | {booking_data.get('checkin', 'N/A')} |\n"
138
- f"| ๐˜พ๐™๐™š๐™˜๐™ -๐™ค๐™ช๐™ฉ | {booking_data.get('checkout', 'N/A')} |\n"
139
- f"| ๐˜ผ๐™™๐™™๐™ง๐™š๐™จ๐™จ | {booking_data.get('address', 'N/A')} |\n"
140
- f"| ๐™๐™จ๐™š๐™ง ๐™„๐˜ฟ | {booking_data.get('user_id', 0)} |\n"
141
- f"| ๐™„๐™ฃ๐™›๐™ค ๐™๐™š๐™ญ๐™ฉ | {booking_data.get('infotext', 'N/A')} |\n"
142
- f"| ๐™„๐™ฃ๐™˜๐™ก๐™ช๐™™๐™š๐™™ | {booking_data.get('included', 'N/A')} |"
143
- )
144
-
145
- # --send both as combined_message
146
- combined_message = f"### Informasjon for Bestillingskode:\n\n{table}"
147
- await cl.Message(content=combined_message).send()
148
-
149
- except Exception as e:
150
- await cl.Message(content=f"Error processing booking data: {str(e)}").send()
151
-
152
- else:
153
- await cl.Message(content="Booking not found").send()
154
-
 
 
 
 
 
155
  except requests.exceptions.RequestException as e:
156
- await cl.Message(content=f"Request failed: {str(e)}").send()
 
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()