Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
# ===========================================
|
2 |
-
#
|
|
|
3 |
# ===========================================
|
4 |
|
5 |
import json
|
@@ -15,18 +16,25 @@ from langchain.chains import LLMChain
|
|
15 |
from langchain_core.prompts import PromptTemplate
|
16 |
from langchain.memory.buffer import ConversationBufferMemory
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
load_dotenv()
|
19 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
20 |
auth_token = os.environ.get("DAYSOFF_API_TOKEN")
|
21 |
|
22 |
API_URL = "https://aivisions.no/data/daysoff/api/v1/booking/"
|
23 |
|
24 |
-
# You help users retrieve booking information associated with their booking IDs.Provide a conversational answer.
|
25 |
daysoff_assistant_template = """
|
26 |
You are a customer support assistant for Daysoff kundeservice and help users retrieve booking information associated with their booking IDs.
|
27 |
-
By default, you respond
|
28 |
-
|
29 |
-
the
|
|
|
30 |
Chat History: {chat_history}
|
31 |
Question: {question}
|
32 |
Answer:
|
@@ -40,10 +48,35 @@ daysoff_assistant_prompt = PromptTemplate(
|
|
40 |
async def async_post_request(url, headers, data):
|
41 |
return await asyncio.to_thread(requests.post, url, headers=headers, json=data)
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
@cl.on_chat_start
|
44 |
def setup_multiple_chains():
|
45 |
llm = OpenAI(
|
46 |
-
model="gpt-3.5-turbo-instruct",
|
47 |
temperature=0.7,
|
48 |
openai_api_key=OPENAI_API_KEY,
|
49 |
max_tokens=2048,
|
@@ -54,8 +87,6 @@ def setup_multiple_chains():
|
|
54 |
|
55 |
conversation_memory = ConversationBufferMemory(
|
56 |
memory_key="chat_history",
|
57 |
-
#input_key="question", # ?
|
58 |
-
#output_key="text", # ?
|
59 |
max_len=30,
|
60 |
return_messages=True
|
61 |
)
|
@@ -72,12 +103,12 @@ def setup_multiple_chains():
|
|
72 |
async def handle_message(message: cl.Message):
|
73 |
user_message = message.content
|
74 |
llm_chain = cl.user_session.get("llm_chain")
|
75 |
-
|
76 |
-
booking_pattern = r'\b[A-Z]{6}\d{6}\b'
|
77 |
match = re.search(booking_pattern, user_message)
|
78 |
|
79 |
if match:
|
80 |
-
|
81 |
bestillingskode = match.group()
|
82 |
headers = {
|
83 |
"Authorization": auth_token,
|
@@ -86,43 +117,52 @@ async def handle_message(message: cl.Message):
|
|
86 |
payload = {"booking_id": bestillingskode}
|
87 |
|
88 |
try:
|
89 |
-
|
90 |
response = await async_post_request(API_URL, headers, payload)
|
91 |
-
response.raise_for_status()
|
92 |
booking_data = response.json()
|
93 |
|
94 |
if "booking_id" in booking_data:
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
|
112 |
else:
|
113 |
-
await cl.Message(content="Booking not found
|
114 |
-
|
115 |
-
except requests.exceptions.RequestException as e:
|
116 |
-
await cl.Message(content=f"Request failed: {str(e)}").send()
|
117 |
else:
|
118 |
try:
|
119 |
response = await llm_chain.ainvoke({
|
120 |
"question": user_message,
|
121 |
"chat_history": ""
|
122 |
}, callbacks=[cl.AsyncLangchainCallbackHandler()])
|
123 |
-
|
124 |
await cl.Message(content=response["text"]).send()
|
125 |
-
|
126 |
except Exception as e:
|
127 |
await cl.Message(content=f"Error: {str(e)}").send()
|
128 |
|
|
|
1 |
# ===========================================
|
2 |
+
# title: daysoff-assistant-API-v2
|
3 |
+
# file: app.py
|
4 |
# ===========================================
|
5 |
|
6 |
import json
|
|
|
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 |
daysoff_assistant_template = """
|
33 |
You are a customer support assistant for Daysoff kundeservice and help users retrieve booking information associated with their booking IDs.
|
34 |
+
By default, you respond using Norwegian bokmรฅl.
|
35 |
+
Provide a conversational answer.
|
36 |
+
This way you directly address the user's question in a manner that reflects the professionalism and warmth
|
37 |
+
of a customer support representative (female).
|
38 |
Chat History: {chat_history}
|
39 |
Question: {question}
|
40 |
Answer:
|
|
|
48 |
async def async_post_request(url, headers, data):
|
49 |
return await asyncio.to_thread(requests.post, url, headers=headers, json=data)
|
50 |
|
51 |
+
@cl.set_starters
|
52 |
+
async def set_starters():
|
53 |
+
return [
|
54 |
+
cl.Starter(
|
55 |
+
label="Booking ID request",
|
56 |
+
message="Kan du gi meg info om en reservasjon?",
|
57 |
+
icon="/public/booking_id.svg",
|
58 |
+
),
|
59 |
+
cl.Starter(
|
60 |
+
label="Metric Space Self-Identity Framework",
|
61 |
+
message="Explain the Metric Space Self-Identity Framework like I'm five years old.",
|
62 |
+
icon="/public/learn.svg",
|
63 |
+
),
|
64 |
+
cl.Starter(
|
65 |
+
label="Python script for daily email reports",
|
66 |
+
message="Write a script to automate sending daily email reports in Python, and walk me through how I would set it up.",
|
67 |
+
icon="/public/terminal.svg",
|
68 |
+
),
|
69 |
+
cl.Starter(
|
70 |
+
label="Morning routine ideation",
|
71 |
+
message="Can you help me create a personalized Yoga/pranayama/meditation morning routine that would help increase my productivity throughout the day? Start by asking me about my current habits and what activities energize me in the morning.",
|
72 |
+
icon="/public/idea.svg",
|
73 |
+
)
|
74 |
+
]
|
75 |
+
|
76 |
@cl.on_chat_start
|
77 |
def setup_multiple_chains():
|
78 |
llm = OpenAI(
|
79 |
+
model="gpt-3.5-turbo-instruct",
|
80 |
temperature=0.7,
|
81 |
openai_api_key=OPENAI_API_KEY,
|
82 |
max_tokens=2048,
|
|
|
87 |
|
88 |
conversation_memory = ConversationBufferMemory(
|
89 |
memory_key="chat_history",
|
|
|
|
|
90 |
max_len=30,
|
91 |
return_messages=True
|
92 |
)
|
|
|
103 |
async def handle_message(message: cl.Message):
|
104 |
user_message = message.content
|
105 |
llm_chain = cl.user_session.get("llm_chain")
|
106 |
+
|
107 |
+
booking_pattern = r'\b[A-Z]{6}\d{6}\b'
|
108 |
match = re.search(booking_pattern, user_message)
|
109 |
|
110 |
if match:
|
111 |
+
|
112 |
bestillingskode = match.group()
|
113 |
headers = {
|
114 |
"Authorization": auth_token,
|
|
|
117 |
payload = {"booking_id": bestillingskode}
|
118 |
|
119 |
try:
|
120 |
+
|
121 |
response = await async_post_request(API_URL, headers, payload)
|
122 |
+
response.raise_for_status()
|
123 |
booking_data = response.json()
|
124 |
|
125 |
if "booking_id" in booking_data:
|
126 |
+
|
127 |
+
try:
|
128 |
+
table = (
|
129 |
+
"| ๐ญ๐๐๐๐
| ๐๐ป๐ณ๐ผ |\n"
|
130 |
+
"|:-----------|:---------------------|\n"
|
131 |
+
f"| ๐ฑ๐๐๐๐๐๐๐๐๐๐๐๐๐๐ | {booking_data.get('booking_id', 'N/A')} |\n"
|
132 |
+
f"| ๐๐ช๐ก๐ก ๐๐๐ข๐ | {booking_data.get('full_name', 'N/A')} |\n"
|
133 |
+
f"| ๐ผ๐ข๐ค๐ช๐ฃ๐ฉ | {booking_data.get('amount', 0)} kr |\n"
|
134 |
+
f"| ๐พ๐๐๐๐ -๐๐ฃ | {booking_data.get('checkin', 'N/A')} |\n"
|
135 |
+
f"| ๐พ๐๐๐๐ -๐ค๐ช๐ฉ | {booking_data.get('checkout', 'N/A')} |\n"
|
136 |
+
f"| ๐ผ๐๐๐ง๐๐จ๐จ | {booking_data.get('address', 'N/A')} |\n"
|
137 |
+
f"| ๐๐จ๐๐ง ๐๐ฟ | {booking_data.get('user_id', 0)} |\n"
|
138 |
+
f"| ๐๐ฃ๐๐ค ๐๐๐ญ๐ฉ | {booking_data.get('infotext', 'N/A')} |\n"
|
139 |
+
f"| ๐๐ฃ๐๐ก๐ช๐๐๐ | {booking_data.get('included', 'N/A')} |"
|
140 |
+
)
|
141 |
+
|
142 |
+
response = await llm_chain.ainvoke({
|
143 |
+
"table": table,
|
144 |
+
"question": user_message,
|
145 |
+
"chat_history": ""
|
146 |
+
}, callbacks=[cl.AsyncLangchainCallbackHandler()])
|
147 |
+
|
148 |
+
await cl.Message(content=table,["text"]).send()
|
149 |
+
|
150 |
+
except Exception as e:
|
151 |
+
except requests.exceptions.RequestException as e:
|
152 |
+
await cl.Message(content=f"Request failed: {str(e)}").send()
|
153 |
+
|
154 |
|
155 |
else:
|
156 |
+
await cl.Message(content="Booking information not found.").send()
|
|
|
|
|
|
|
157 |
else:
|
158 |
try:
|
159 |
response = await llm_chain.ainvoke({
|
160 |
"question": user_message,
|
161 |
"chat_history": ""
|
162 |
}, callbacks=[cl.AsyncLangchainCallbackHandler()])
|
163 |
+
|
164 |
await cl.Message(content=response["text"]).send()
|
165 |
+
|
166 |
except Exception as e:
|
167 |
await cl.Message(content=f"Error: {str(e)}").send()
|
168 |
|