Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,206 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# ---------------------------------------------------for backend looks-------------------------------------------------
|
2 |
+
|
3 |
+
#with open('/usr/local/lib/python3.10/site-packages/transformers/utils/chat_template_utils.py', 'r') as file:
|
4 |
+
#content = file.read()
|
5 |
+
#print("base.py:", content)
|
6 |
+
# ------------------------------------------------------the end--------------------------------------------------------
|
7 |
+
|
8 |
+
|
9 |
+
|
10 |
+
# ===========================================
|
11 |
+
# ver1(get)_app.py
|
12 |
+
# ===========================================
|
13 |
+
|
14 |
+
import asyncio
|
15 |
+
import os
|
16 |
+
import re
|
17 |
+
import time
|
18 |
+
import json
|
19 |
+
|
20 |
+
import chainlit as cl
|
21 |
+
from dotenv import load_dotenv
|
22 |
+
|
23 |
+
from langchain import hub
|
24 |
+
from langchain_openai import OpenAI
|
25 |
+
from tiktoken import encoding_for_model
|
26 |
+
from langchain.chains import LLMChain, APIChain
|
27 |
+
from langchain_core.prompts import PromptTemplate
|
28 |
+
from langchain.memory.buffer import ConversationBufferMemory
|
29 |
+
#from langchain.memory import ConversationTokenBufferMemory
|
30 |
+
#from langchain.memory import ConversationSummaryMemory
|
31 |
+
|
32 |
+
|
33 |
+
from api_docs_mck import api_docs_str
|
34 |
+
|
35 |
+
load_dotenv()
|
36 |
+
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
|
37 |
+
|
38 |
+
#auth_token = os.environ.get("CHAINLIT_AUTH_SECRET")
|
39 |
+
#if not auth_token.startswith("Bearer "):
|
40 |
+
#auth_token = f"Bearer {auth_token}"
|
41 |
+
|
42 |
+
daysoff_assistant_template = """
|
43 |
+
#You are a customer support assistant (โkundeservice AI assistentโ) for Daysoff.
|
44 |
+
#By default, you respond in Norwegian language, using a warm, direct, and professional tone.
|
45 |
+
Your expertise is exclusively in retrieving booking information for a given booking ID assistance related to
|
46 |
+
to this.
|
47 |
+
You do not provide information outside of this scope. If a question is not about this topic, respond with
|
48 |
+
"Jeg driver faktisk kun med henvendelser omkring bestillingsinformasjon. Gjelder det andre henvendelser
|
49 |
+
mรฅ du nok kontakte kundeservice pรฅ [email protected]๐"
|
50 |
+
Chat History: {chat_history}
|
51 |
+
Question: {question}
|
52 |
+
Answer:
|
53 |
+
"""
|
54 |
+
daysoff_assistant_prompt = PromptTemplate(
|
55 |
+
input_variables=['chat_history', 'question'],
|
56 |
+
template=daysoff_assistant_template
|
57 |
+
)
|
58 |
+
|
59 |
+
api_url_template = """
|
60 |
+
Given the following API Documentation for Daysoff's official
|
61 |
+
booking information API: {api_docs}
|
62 |
+
Your task is to construct the most efficient API URL to answer
|
63 |
+
the user's question, ensuring the
|
64 |
+
call is optimized to include only the necessary information.
|
65 |
+
Question: {question}
|
66 |
+
API URL:
|
67 |
+
"""
|
68 |
+
api_url_prompt = PromptTemplate(input_variables=['api_docs', 'question'],
|
69 |
+
template=api_url_template)
|
70 |
+
|
71 |
+
api_response_template = """
|
72 |
+
With the API Documentation for Daysoff's official API: {api_docs} in mind,
|
73 |
+
and the specific user question: {question},
|
74 |
+
and given this API URL: {api_url} for querying,
|
75 |
+
and response from Daysoff's API: {api_response},
|
76 |
+
never refer the user to the API URL as your answer!
|
77 |
+
You should always provide a clear and concise summary (in Norwegian) of the booking information retrieved.
|
78 |
+
This way you directly address the user's question in a manner that reflects the professionalism and warmth
|
79 |
+
of a human customer service agent.
|
80 |
+
Summary:
|
81 |
+
"""
|
82 |
+
api_response_prompt = PromptTemplate(
|
83 |
+
input_variables=['api_docs', 'question', 'api_url', 'api_response'],
|
84 |
+
template=api_response_template
|
85 |
+
)
|
86 |
+
|
87 |
+
@cl.on_chat_start
|
88 |
+
def setup_multiple_chains():
|
89 |
+
llm = OpenAI(
|
90 |
+
model='gpt-3.5-turbo-instruct',
|
91 |
+
temperature=0.7,
|
92 |
+
openai_api_key=OPENAI_API_KEY,
|
93 |
+
max_tokens=2048,
|
94 |
+
top_p=0.9,
|
95 |
+
frequency_penalty=0.1,
|
96 |
+
presence_penalty=0.1
|
97 |
+
)
|
98 |
+
|
99 |
+
conversation_memory = ConversationBufferMemory(memory_key="chat_history",
|
100 |
+
max_len=30,
|
101 |
+
return_messages=True,
|
102 |
+
)
|
103 |
+
|
104 |
+
llm_chain = LLMChain(
|
105 |
+
llm=llm,
|
106 |
+
prompt=daysoff_assistant_prompt,
|
107 |
+
memory=conversation_memory
|
108 |
+
)
|
109 |
+
|
110 |
+
cl.user_session.set("llm_chain", llm_chain)
|
111 |
+
|
112 |
+
api_chain = APIChain.from_llm_and_api_docs(
|
113 |
+
llm=llm,
|
114 |
+
api_docs=api_docs_str,
|
115 |
+
api_url_prompt=api_url_prompt,
|
116 |
+
api_response_prompt=api_response_prompt,
|
117 |
+
verbose=True,
|
118 |
+
limit_to_domains=None
|
119 |
+
)
|
120 |
+
|
121 |
+
cl.user_session.set("api_chain", api_chain)
|
122 |
+
|
123 |
+
cl.on_message
|
124 |
+
async def handle_message(message: cl.Message):
|
125 |
+
user_message = message.content.lower()
|
126 |
+
llm_chain = cl.user_session.get("llm_chain")
|
127 |
+
api_chain = cl.user_session.get("api_chain")
|
128 |
+
|
129 |
+
base_url = "https://670dccd0073307b4ee447f2f.mockapi.io/daysoff/api/V1/booking"
|
130 |
+
booking_pattern = r'\b[A-Z]{6}\d{6}\b'
|
131 |
+
match = re.search(booking_pattern, user_message)
|
132 |
+
|
133 |
+
try:
|
134 |
+
|
135 |
+
if match:
|
136 |
+
bestillingskode = match.group()
|
137 |
+
question = f"Retrieve information for booking ID {base_url}?search={bestillingskode}"
|
138 |
+
|
139 |
+
response = await api_chain.acall(
|
140 |
+
{
|
141 |
+
"bestillingskode": bestillingskode,
|
142 |
+
"question": question
|
143 |
+
|
144 |
+
},
|
145 |
+
callbacks=[cl.AsyncLangchainCallbackHandler()])
|
146 |
+
|
147 |
+
booking_info = json.loads(response.get("output", "{}"))
|
148 |
+
|
149 |
+
formatted_response = f"""
|
150 |
+
Her er informasjon for bestillingskode: {bestillingskode}
|
151 |
+
|
152 |
+
| Felt | Detaljer |
|
153 |
+
|-------------|----------------------------------------|
|
154 |
+
| Navn: | {booking_info.get('Navn', 'N/A')} |
|
155 |
+
| Belรธp: | {booking_info.get('Belรธp', 'N/A')} NOK |
|
156 |
+
| Check-In: | {booking_info.get('Checkin', 'N/A')} |
|
157 |
+
| Check-Out: | {booking_info.get('Checkout', 'N/A')} |
|
158 |
+
| Addresse: | {booking_info.get('Addresse', 'N/A')} |
|
159 |
+
| Bruker ID: | {booking_info.get('Bruker ID', 'N/A')} |
|
160 |
+
| Viktig informasjon: | {booking_info.get('Viktig informasjon', 'N/A')} |
|
161 |
+
| Message: | {booking_info.get('Message', 'N/A')} |
|
162 |
+
"""
|
163 |
+
|
164 |
+
await cl.Message(content=formatted_response).send()
|
165 |
+
|
166 |
+
else:
|
167 |
+
await cl.Message("Jeg kan desverre ikke finne noen informasjon for det oppgitte bookingnummeret.").send()
|
168 |
+
|
169 |
+
else:
|
170 |
+
response = await llm_chain.acall(user_message, callbacks=[cl.AsyncLangchainCallbackHandler()])
|
171 |
+
|
172 |
+
except Exception as e:
|
173 |
+
response = {"output": "Jeg fรฅr desverre ikke fram noe informasjon akkurat nรฅ."}
|
174 |
+
|
175 |
+
response_key = "output" if "output" in response else "text"
|
176 |
+
|
177 |
+
return message.content
|
178 |
+
|
179 |
+
|
180 |
+
|
181 |
+
|
182 |
+
"""
|
183 |
+
if match:
|
184 |
+
bestillingskode = match.group()
|
185 |
+
question = f"Retrieve information for booking ID"
|
186 |
+
|
187 |
+
api_url = f"{base_url}?search={booking_id}"
|
188 |
+
|
189 |
+
response = await api_chain.acall(
|
190 |
+
{
|
191 |
+
"booking_id": bestillingskode,
|
192 |
+
"question": question,
|
193 |
+
"api_url": api_url
|
194 |
+
},
|
195 |
+
callbacks=[cl.AsyncLangchainCallbackHandler()])
|
196 |
+
else:
|
197 |
+
response = await llm_chain.acall(user_message,
|
198 |
+
callbacks=[cl.AsyncLangchainCallbackHandler()])
|
199 |
+
|
200 |
+
response_key = "output" if "output" in response else "text"
|
201 |
+
await cl.Message(response.get(response_key, "")).send()
|
202 |
+
return message.content
|
203 |
+
|
204 |
+
|
205 |
+
|
206 |
+
"""
|