camparchimedes commited on
Commit
eb4924a
ยท
verified ยท
1 Parent(s): c417e7d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -93
app.py CHANGED
@@ -95,6 +95,9 @@ async def set_starters():
95
  #------?-----@cl.Context
96
 
97
  @cl.on_chat_start
 
 
 
98
  async def setup():
99
  asyncio.create_task(keep_alive())
100
 
@@ -127,102 +130,112 @@ def setup_multiple_chains():
127
  )
128
 
129
  cl.user_session.set("llm_chain", llm_chain)
 
130
 
131
  @cl.on_message
132
  async def handle_message(message: cl.Message):
133
- user_message = message.content
134
- llm_chain = cl.user_session.get("llm_chain")
135
- #languages = cl.user_session.get("languages")
136
-
137
- if sys.version_info >= (3, 11):
138
- from asyncio import timeout
139
- else:
140
- from async_timeout import timeout
141
-
142
- booking_pattern = r'\b[A-Z]{6}\d{6}\b'
143
- match = re.search(booking_pattern, user_message)
144
-
145
- if match:
146
- bestillingskode = match.group()
147
- headers = {
148
- "Authorization": auth_token,
149
- "Content-Type": "application/json"
150
- }
151
- payload = {"booking_id": bestillingskode}
152
-
153
- try:
154
-
155
- response = await async_post_request(API_URL, headers, payload)
156
- response.raise_for_status()
157
- booking_data = response.json()
158
 
159
- if not booking_data:
160
- raise BookingNotFoundError("No booking data returned")
 
 
161
 
162
- if "error" in booking_data:
163
- raise APIResponseError(booking_data["error"])
164
-
165
- #table = (
166
- #"| ๐‘ญ๐’Š๐’†๐’๐’… | ๐—œ๐—ป๐—ณ๐—ผ |\n"
167
- #"|:----------------|:-------------------------------------------|\n"
168
- #f"| ๐™Š๐™ง๐™™๐™š๐™ง ๐™‰๐™ช๐™ข๐™—๐™š๐™ง | {booking_data.get('order_number', 'N/A')} |\n"
169
- #f"| ๐˜ผ๐™˜๐™˜๐™š๐™ฅ๐™ฉ๐™š๐™™ | {booking_data.get('accepted', 'N/A')} |\n"
170
- #f"| ๐™‚๐™ช๐™š๐™จ๐™ฉ๐™จ | {booking_data.get('guests', 'N/A')} |\n"
171
- #f"| ๐™๐™ž๐™ข๐™š ๐™๐™ง๐™ค๐™ข | {booking_data.get('time_from', 'N/A')} |\n"
172
- #f"| ๐™๐™ž๐™ข๐™š ๐™๐™ค | {booking_data.get('time_to', 'N/A')} |\n"
173
- #f"| ๐˜พ๐™–๐™ฃ๐™˜๐™š๐™ก๐™ก๐™š๐™™ | {'Yes' if booking_data.get('cancelled', 0) else 'No'} |\n"
174
- #f"| ๐˜ฟ๐™š๐™จ๐™˜๐™ง๐™ž๐™ฅ๐™ฉ๐™ž๐™ค๐™ฃ | {booking_data.get('description', 'N/A')} |\n"
175
- #f"| ๐˜ผ๐™ง๐™ง๐™ž๐™ซ๐™–๐™ก | {booking_data.get('arrival', 'N/A')} |\n"
176
- #f"| ๐˜ฟ๐™š๐™ฅ๐™–๐™ง๐™ฉ๐™ช๐™ง๐™š | {booking_data.get('departure', 'N/A')} |\n"
177
- #f"| ๐˜ฝ๐™š๐™™๐™™๐™ž๐™ฃ๐™œ | {booking_data.get('bedding', 'N/A')} |\n"
178
- #f"| ๐™‡๐™ค๐™˜๐™–๐™ฉ๐™ž๐™ค๐™ฃ | Lat: {booking_data.get('lat', 'N/A')}, Long: {booking_data.get('long', 'N/A')} |\n"
179
- #f"| ๐™‰๐™–๐™ข๐™š | {booking_data.get('name', 'N/A')} |\n"
180
- #f"| ๐˜พ๐™ค๐™ฃ๐™ฉ๐™–๐™˜๐™ฉ ๐™‰๐™–๐™ข๐™š | {booking_data.get('contactname', 'N/A')} |\n"
181
- #f"| ๐˜พ๐™ค๐™ฃ๐™ฉ๐™–๐™˜๐™ฉ ๐™€๐™ข๐™–๐™ž๐™ก| {booking_data.get('contactemail', 'N/A')} |\n"
182
- #f"| ๐˜พ๐™ค๐™ฃ๐™ฉ๐™–๐™˜๐™ฉ ๐™‹๐™๐™ค๐™ฃ๐™š| {booking_data.get('contactphone', 'N/A')} |\n"
183
- #f"| ๐˜พ๐™ค๐™ช๐™ฃ๐™ฉ๐™ง๐™ฎ ๐˜พ๐™ค๐™™๐™š | {booking_data.get('country_code', 'N/A')} |\n"
184
- #)
185
- #combined_message = f"### Informasjon om booking:\n\n{table}"
186
- #await cl.Message(content=combined_message).send()
187
-
188
- table = (
189
- "| ๐‘ญ๐’Š๐’†๐’๐’… | ๐—œ๐—ป๐—ณ๐—ผ |\n"
190
- "|:-----------|:---------------------|\n"
191
- f"| ๐™ฑ๐šŽ๐šœ๐š๐š’๐š•๐š•๐š’๐š—๐š๐šœ๐š”๐š˜๐š๐šŽ | {booking_data.get('booking_id', 'N/A')} |\n"
192
- f"| ๐™๐™ช๐™ก๐™ก ๐™‰๐™–๐™ข๐™š | {booking_data.get('full_name', 'N/A')} |\n"
193
- f"| ๐˜ผ๐™ข๐™ค๐™ช๐™ฃ๐™ฉ | {booking_data.get('amount', 0)} kr |\n"
194
- f"| ๐˜พ๐™๐™š๐™˜๐™ -๐™ž๐™ฃ | {booking_data.get('checkin', 'N/A')} |\n"
195
- f"| ๐˜พ๐™๐™š๐™˜๐™ -๐™ค๐™ช๐™ฉ | {booking_data.get('checkout', 'N/A')} |\n"
196
- f"| ๐˜ผ๐™™๐™™๐™ง๐™š๐™จ๐™จ | {booking_data.get('address', 'N/A')} |\n"
197
- f"| ๐™๐™จ๐™š๐™ง ๐™„๐˜ฟ | {booking_data.get('user_id', 0)} |\n"
198
- f"| ๐™„๐™ฃ๐™›๐™ค ๐™๐™š๐™ญ๐™ฉ | {booking_data.get('infotext', 'N/A')} |\n"
199
- f"| ๐™„๐™ฃ๐™˜๐™ก๐™ช๐™™๐™š๐™™ | {booking_data.get('included', 'N/A')} |"
200
- )
201
-
202
- combined_message = f"### Informasjon om booking:\n\n{table}"
203
- await cl.Message(content=combined_message).send()
204
-
205
- except (APIConnectionError, APIResponseError, BookingNotFoundError) as e:
206
- error_messages = {
207
- APIConnectionError: "Kunne ikke koble til bookingsystemet. Prรธv igjen senere.",
208
- APIResponseError: "Det oppstod en feil ved henting av bookingdata.",
209
- BookingNotFoundError: "Ingen booking funnet med denne koden."
210
  }
211
- await cl.Message(content=f"โŒ {error_messages[type(e)]}\n\nPrรธv igjen, kanskje du feilstavet eller glemte ett siffer eller en bokstav?\n\nHvis du ser denne feilmedlingen gjentatte ganger, vennligst kontakt kundeservice@daysoff.no sรฅ de kan hjelpe deg.").send()
212
- return None
213
- except requests.exceptions.RequestException as e:
214
- await cl.Message(content="En uventet feil oppstod. Vennligst kontakt kundeservice@daysoff.no").send()
215
- return None
216
-
217
- else:
218
- try:
219
- async with timeout(30):
220
- response = await llm_chain.ainvoke({
221
- "question": user_message,
222
- "chat_history": ""
223
- }, callbacks=[cl.AsyncLangchainCallbackHandler()])
224
- await cl.Message(content=response["text"]).send()
225
- except asyncio.TimeoutError:
226
- await cl.Message(content="Request timed out. Please try again.").send()
227
- except Exception as e:
228
- await cl.Message(content=f"Error: {str(e)}").send()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  #------?-----@cl.Context
96
 
97
  @cl.on_chat_start
98
+ async def start():
99
+ cl.user_session.set("socket_auth", True)
100
+
101
  async def setup():
102
  asyncio.create_task(keep_alive())
103
 
 
130
  )
131
 
132
  cl.user_session.set("llm_chain", llm_chain)
133
+
134
 
135
  @cl.on_message
136
  async def handle_message(message: cl.Message):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137
 
138
+ try:
139
+ if not cl.user_session.get("socket_auth"):
140
+ await cl.Message(content="Session authentication failed. Please refresh the page.").send()
141
+ return
142
 
143
+ user_message = message.content
144
+ llm_chain = cl.user_session.get("llm_chain")
145
+ #languages = cl.user_session.get("languages")
146
+
147
+ if sys.version_info >= (3, 11):
148
+ from asyncio import timeout
149
+ else:
150
+ from async_timeout import timeout
151
+
152
+ booking_pattern = r'\b[A-Z]{6}\d{6}\b'
153
+ match = re.search(booking_pattern, user_message)
154
+
155
+ if match:
156
+ bestillingskode = match.group()
157
+ headers = {
158
+ "Authorization": auth_token,
159
+ "Content-Type": "application/json"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
  }
161
+ payload = {"booking_id": bestillingskode}
162
+
163
+ try:
164
+
165
+ response = await async_post_request(API_URL, headers, payload)
166
+ response.raise_for_status()
167
+ booking_data = response.json()
168
+
169
+ if not booking_data:
170
+ raise BookingNotFoundError("No booking data returned")
171
+
172
+ if "error" in booking_data:
173
+ raise APIResponseError(booking_data["error"])
174
+
175
+ #table = (
176
+ #"| ๐‘ญ๐’Š๐’†๐’๐’… | ๐—œ๐—ป๐—ณ๐—ผ |\n"
177
+ #"|:----------------|:-------------------------------------------|\n"
178
+ #f"| ๐™Š๐™ง๐™™๐™š๐™ง ๐™‰๐™ช๐™ข๐™—๐™š๐™ง | {booking_data.get('order_number', 'N/A')} |\n"
179
+ #f"| ๐˜ผ๐™˜๐™˜๐™š๐™ฅ๐™ฉ๐™š๐™™ | {booking_data.get('accepted', 'N/A')} |\n"
180
+ #f"| ๐™‚๐™ช๐™š๐™จ๐™ฉ๐™จ | {booking_data.get('guests', 'N/A')} |\n"
181
+ #f"| ๐™๐™ž๐™ข๐™š ๐™๐™ง๐™ค๐™ข | {booking_data.get('time_from', 'N/A')} |\n"
182
+ #f"| ๐™๐™ž๐™ข๐™š ๐™๐™ค | {booking_data.get('time_to', 'N/A')} |\n"
183
+ #f"| ๐˜พ๐™–๐™ฃ๐™˜๐™š๐™ก๐™ก๐™š๐™™ | {'Yes' if booking_data.get('cancelled', 0) else 'No'} |\n"
184
+ #f"| ๐˜ฟ๐™š๐™จ๐™˜๐™ง๐™ž๐™ฅ๐™ฉ๐™ž๐™ค๐™ฃ | {booking_data.get('description', 'N/A')} |\n"
185
+ #f"| ๐˜ผ๐™ง๐™ง๐™ž๐™ซ๐™–๐™ก | {booking_data.get('arrival', 'N/A')} |\n"
186
+ #f"| ๐˜ฟ๐™š๐™ฅ๐™–๐™ง๐™ฉ๐™ช๐™ง๐™š | {booking_data.get('departure', 'N/A')} |\n"
187
+ #f"| ๐˜ฝ๐™š๐™™๐™™๐™ž๐™ฃ๐™œ | {booking_data.get('bedding', 'N/A')} |\n"
188
+ #f"| ๐™‡๐™ค๐™˜๐™–๐™ฉ๐™ž๐™ค๐™ฃ | Lat: {booking_data.get('lat', 'N/A')}, Long: {booking_data.get('long', 'N/A')} |\n"
189
+ #f"| ๐™‰๐™–๐™ข๐™š | {booking_data.get('name', 'N/A')} |\n"
190
+ #f"| ๐˜พ๐™ค๐™ฃ๐™ฉ๐™–๐™˜๐™ฉ ๐™‰๐™–๐™ข๐™š | {booking_data.get('contactname', 'N/A')} |\n"
191
+ #f"| ๐˜พ๐™ค๐™ฃ๐™ฉ๐™–๐™˜๐™ฉ ๐™€๐™ข๐™–๐™ž๐™ก| {booking_data.get('contactemail', 'N/A')} |\n"
192
+ #f"| ๐˜พ๐™ค๐™ฃ๐™ฉ๐™–๐™˜๐™ฉ ๐™‹๐™๐™ค๐™ฃ๐™š| {booking_data.get('contactphone', 'N/A')} |\n"
193
+ #f"| ๐˜พ๐™ค๐™ช๐™ฃ๐™ฉ๐™ง๐™ฎ ๐˜พ๐™ค๐™™๐™š | {booking_data.get('country_code', 'N/A')} |\n"
194
+ #)
195
+ #combined_message = f"### Informasjon om booking:\n\n{table}"
196
+ #await cl.Message(content=combined_message).send()
197
+
198
+ table = (
199
+ "| ๐‘ญ๐’Š๐’†๐’๐’… | ๐—œ๐—ป๐—ณ๐—ผ |\n"
200
+ "|:-----------|:---------------------|\n"
201
+ f"| ๐™ฑ๐šŽ๐šœ๐š๐š’๐š•๐š•๐š’๐š—๐š๐šœ๐š”๐š˜๐š๐šŽ | {booking_data.get('booking_id', 'N/A')} |\n"
202
+ f"| ๐™๐™ช๐™ก๐™ก ๐™‰๐™–๐™ข๐™š | {booking_data.get('full_name', 'N/A')} |\n"
203
+ f"| ๐˜ผ๐™ข๐™ค๐™ช๐™ฃ๐™ฉ | {booking_data.get('amount', 0)} kr |\n"
204
+ f"| ๐˜พ๐™๐™š๐™˜๐™ -๐™ž๐™ฃ | {booking_data.get('checkin', 'N/A')} |\n"
205
+ f"| ๐˜พ๐™๐™š๐™˜๐™ -๐™ค๐™ช๐™ฉ | {booking_data.get('checkout', 'N/A')} |\n"
206
+ f"| ๐˜ผ๐™™๐™™๐™ง๐™š๐™จ๐™จ | {booking_data.get('address', 'N/A')} |\n"
207
+ f"| ๐™๐™จ๐™š๐™ง ๐™„๐˜ฟ | {booking_data.get('user_id', 0)} |\n"
208
+ f"| ๐™„๐™ฃ๐™›๐™ค ๐™๐™š๐™ญ๐™ฉ | {booking_data.get('infotext', 'N/A')} |\n"
209
+ f"| ๐™„๐™ฃ๐™˜๐™ก๐™ช๐™™๐™š๐™™ | {booking_data.get('included', 'N/A')} |"
210
+ )
211
+
212
+ combined_message = f"### Informasjon om booking:\n\n{table}"
213
+ await cl.Message(content=combined_message).send()
214
+
215
+ except (APIConnectionError, APIResponseError, BookingNotFoundError) as e:
216
+ error_messages = {
217
+ APIConnectionError: "Kunne ikke koble til bookingsystemet. Prรธv igjen senere.",
218
+ APIResponseError: "Det oppstod en feil ved henting av bookingdata.",
219
+ BookingNotFoundError: "Ingen booking funnet med denne koden."
220
+ }
221
+ await cl.Message(content=f"โŒ {error_messages[type(e)]}\n\nPrรธv igjen, kanskje du feilstavet eller glemte ett siffer eller en bokstav?\n\nHvis du ser denne feilmedlingen gjentatte ganger, vennligst kontakt kundeservice@daysoff.no sรฅ de kan hjelpe deg.").send()
222
+ return None
223
+ except requests.exceptions.RequestException as e:
224
+ await cl.Message(content="En uventet feil oppstod. Vennligst kontakt kundeservice@daysoff.no").send()
225
+ return None
226
+
227
+ else:
228
+ try:
229
+ async with timeout(30):
230
+ response = await llm_chain.ainvoke({
231
+ "question": user_message,
232
+ "chat_history": ""
233
+ }, callbacks=[cl.AsyncLangchainCallbackHandler()])
234
+ await cl.Message(content=response["text"]).send()
235
+ except asyncio.TimeoutError:
236
+ await cl.Message(content="Request timed out. Please try again.").send()
237
+ except Exception as e:
238
+ await cl.Message(content=f"Error: {str(e)}").send()
239
+
240
+ except Exception as e:
241
+ await cl.Message(content="Connection error. WTF yo...").send()