Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
# ===========================================
|
| 2 |
-
# title: daysoff-assistant-API-v2
|
| 3 |
# file: app.py
|
|
|
|
| 4 |
# ===========================================
|
| 5 |
|
| 6 |
import json
|
|
@@ -16,29 +17,20 @@ from langchain.chains import LLMChain
|
|
| 16 |
from langchain_core.prompts import PromptTemplate
|
| 17 |
from langchain.memory.buffer import ConversationBufferMemory
|
| 18 |
|
| 19 |
-
# ---------------------------------------------------for backend looks, example file:----------------------------------
|
| 20 |
-
|
| 21 |
-
#with open('/usr/local/lib/python3.10/site-packages/transformers/utils/chat_template_utils.py', 'r') as file:
|
| 22 |
-
#content = file.read()
|
| 23 |
-
#print("base.py:", content)
|
| 24 |
-
# ------------------------------------------------------the end--------------------------------------------------------
|
| 25 |
-
|
| 26 |
load_dotenv()
|
| 27 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 28 |
auth_token = os.environ.get("DAYSOFF_API_TOKEN")
|
| 29 |
|
| 30 |
API_URL = "https://aivisions.no/data/daysoff/api/v1/booking/"
|
| 31 |
-
|
| 32 |
-
# Provide a conversational answer.
|
| 33 |
-
# This way you directly address the user's question in a manner that reflects the professionalism and warmth
|
| 34 |
-
# of a customer support representative (female).
|
| 35 |
-
# If users have more in-depth queries about their booking information, politely suggest they contact **kundeservice@daysoff.no** to enquire further.
|
| 36 |
daysoff_assistant_template = """
|
| 37 |
You are a customer support assistant for Daysoff ('Daysoff Kundeservice AI Support') who helps users retrieve booking information based on their bookingnummer.
|
| 38 |
-
You should concisely use the term ’bookingnummer’. Maintain a
|
| 39 |
representative archetype.** By default, you answer in **Norwegian**.
|
|
|
|
| 40 |
Chat History: {chat_history}
|
| 41 |
Question: {question}
|
|
|
|
| 42 |
Answer:
|
| 43 |
"""
|
| 44 |
daysoff_assistant_prompt = PromptTemplate(
|
|
@@ -46,7 +38,6 @@ daysoff_assistant_prompt = PromptTemplate(
|
|
| 46 |
template=daysoff_assistant_template,
|
| 47 |
)
|
| 48 |
|
| 49 |
-
# =============================================================================================================
|
| 50 |
class APIConnectionError(Exception):
|
| 51 |
"""Raised when API connection fails"""
|
| 52 |
pass
|
|
@@ -58,12 +49,7 @@ class APIResponseError(Exception):
|
|
| 58 |
class BookingNotFoundError(Exception):
|
| 59 |
"""Raised when booking ID is not found"""
|
| 60 |
pass
|
| 61 |
-
# =============================================================================================================
|
| 62 |
-
|
| 63 |
-
#async def async_post_request(url, headers, data):
|
| 64 |
-
#return await asyncio.to_thread(requests.post, url, headers=headers, json=data)
|
| 65 |
|
| 66 |
-
# =============================================================================================================
|
| 67 |
async def async_post_request(url, headers, data):
|
| 68 |
try:
|
| 69 |
response = await asyncio.to_thread(requests.post, url, headers=headers, json=data)
|
|
@@ -75,7 +61,6 @@ async def async_post_request(url, headers, data):
|
|
| 75 |
raise APIConnectionError("Request timed out")
|
| 76 |
except requests.RequestException as e:
|
| 77 |
raise APIResponseError(f"API request failed: {str(e)}")
|
| 78 |
-
# =============================================================================================================
|
| 79 |
|
| 80 |
@cl.on_chat_start
|
| 81 |
def setup_multiple_chains():
|
|
@@ -120,7 +105,6 @@ async def handle_message(message: cl.Message):
|
|
| 120 |
payload = {"booking_id": bestillingskode}
|
| 121 |
|
| 122 |
try:
|
| 123 |
-
# --async POST request
|
| 124 |
response = await async_post_request(API_URL, headers, payload)
|
| 125 |
response.raise_for_status()
|
| 126 |
booking_data = response.json()
|
|
@@ -153,9 +137,9 @@ async def handle_message(message: cl.Message):
|
|
| 153 |
error_messages = {
|
| 154 |
APIConnectionError: "Kunne ikke koble til bookingsystemet. Prøv igjen senere.",
|
| 155 |
APIResponseError: "Det oppstod en feil ved henting av bookingdata.",
|
| 156 |
-
BookingNotFoundError: "Ingen booking funnet med
|
| 157 |
}
|
| 158 |
-
await cl.Message(content=f"❌ {error_messages[type(e)]}\n\nPrøv igjen, kanskje du feilstavet eller glemte ett siffer?\n\nHvis du ser denne feilmedlingen gjentatte ganger,
|
| 159 |
return None
|
| 160 |
except requests.exceptions.RequestException as e:
|
| 161 |
await cl.Message(content="En uventet feil oppstod. Vennligst kontakt kundeservice@daysoff.no").send()
|
|
|
|
| 1 |
# ===========================================
|
| 2 |
+
# title: daysoff-assistant-API-v2 | API error handling | no custom starters
|
| 3 |
# file: app.py
|
| 4 |
+
# NOTE: chainlit==0.7.500
|
| 5 |
# ===========================================
|
| 6 |
|
| 7 |
import json
|
|
|
|
| 17 |
from langchain_core.prompts import PromptTemplate
|
| 18 |
from langchain.memory.buffer import ConversationBufferMemory
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
load_dotenv()
|
| 21 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 22 |
auth_token = os.environ.get("DAYSOFF_API_TOKEN")
|
| 23 |
|
| 24 |
API_URL = "https://aivisions.no/data/daysoff/api/v1/booking/"
|
| 25 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
daysoff_assistant_template = """
|
| 27 |
You are a customer support assistant for Daysoff ('Daysoff Kundeservice AI Support') who helps users retrieve booking information based on their bookingnummer.
|
| 28 |
+
You should concisely use the term ’bookingnummer’. Maintain a friendly and professional tone, **reflecting the warmth of a female customer support
|
| 29 |
representative archetype.** By default, you answer in **Norwegian**.
|
| 30 |
+
============================
|
| 31 |
Chat History: {chat_history}
|
| 32 |
Question: {question}
|
| 33 |
+
============================
|
| 34 |
Answer:
|
| 35 |
"""
|
| 36 |
daysoff_assistant_prompt = PromptTemplate(
|
|
|
|
| 38 |
template=daysoff_assistant_template,
|
| 39 |
)
|
| 40 |
|
|
|
|
| 41 |
class APIConnectionError(Exception):
|
| 42 |
"""Raised when API connection fails"""
|
| 43 |
pass
|
|
|
|
| 49 |
class BookingNotFoundError(Exception):
|
| 50 |
"""Raised when booking ID is not found"""
|
| 51 |
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
|
|
|
| 53 |
async def async_post_request(url, headers, data):
|
| 54 |
try:
|
| 55 |
response = await asyncio.to_thread(requests.post, url, headers=headers, json=data)
|
|
|
|
| 61 |
raise APIConnectionError("Request timed out")
|
| 62 |
except requests.RequestException as e:
|
| 63 |
raise APIResponseError(f"API request failed: {str(e)}")
|
|
|
|
| 64 |
|
| 65 |
@cl.on_chat_start
|
| 66 |
def setup_multiple_chains():
|
|
|
|
| 105 |
payload = {"booking_id": bestillingskode}
|
| 106 |
|
| 107 |
try:
|
|
|
|
| 108 |
response = await async_post_request(API_URL, headers, payload)
|
| 109 |
response.raise_for_status()
|
| 110 |
booking_data = response.json()
|
|
|
|
| 137 |
error_messages = {
|
| 138 |
APIConnectionError: "Kunne ikke koble til bookingsystemet. Prøv igjen senere.",
|
| 139 |
APIResponseError: "Det oppstod en feil ved henting av bookingdata.",
|
| 140 |
+
BookingNotFoundError: "Ingen booking funnet med dette bookingnummeret."
|
| 141 |
}
|
| 142 |
+
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 ta kontakt kundeservice@daysoff.no").send()
|
| 143 |
return None
|
| 144 |
except requests.exceptions.RequestException as e:
|
| 145 |
await cl.Message(content="En uventet feil oppstod. Vennligst kontakt kundeservice@daysoff.no").send()
|