Update app.py
Browse files
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 |
-
|
| 160 |
-
|
|
|
|
|
|
|
| 161 |
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 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 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|