Update app.py
Browse files
app.py
CHANGED
@@ -36,8 +36,8 @@ auth_token = os.getenv("DAYSOFF_API_TOKEN")
|
|
36 |
class EnhancedRequestsPostTool(RequestsPostTool):
|
37 |
def __init__(self, requests_wrapper, llm, api_docs, api_url_prompt, api_response_prompt):
|
38 |
super().__init__(requests_wrapper=requests_wrapper, allow_dangerous_requests=True)
|
39 |
-
self.url_chain = LLMChain(llm=llm, prompt=api_url_prompt)
|
40 |
-
self.response_chain = LLMChain(llm=llm, prompt=api_response_prompt)
|
41 |
self.api_docs = api_docs
|
42 |
|
43 |
async def ainvoke(self, input_data, callbacks=None):
|
@@ -58,7 +58,7 @@ class EnhancedRequestsPostTool(RequestsPostTool):
|
|
58 |
"api_response": api_response
|
59 |
})
|
60 |
|
61 |
-
return formatted_response
|
62 |
|
63 |
|
64 |
daysoff_assistant_template = """
|
@@ -148,8 +148,6 @@ def setup_multiple_chains():
|
|
148 |
cl.user_session.set("llm_chain", llm_chain)
|
149 |
cl.user_session.set("post_tool", post_tool)
|
150 |
|
151 |
-
|
152 |
-
|
153 |
|
154 |
@cl.on_message
|
155 |
async def handle_message(message: cl.Message):
|
@@ -157,49 +155,12 @@ async def handle_message(message: cl.Message):
|
|
157 |
llm_chain = cl.user_session.get("llm_chain")
|
158 |
post_tool = cl.user_session.get("post_tool")
|
159 |
|
160 |
-
# Match booking pattern
|
161 |
booking_pattern = r'\b[A-Z]{6}\d{6}\b'
|
162 |
endpoint_url = "https://aivisions.no/data/daysoff/api/v1/booking/"
|
163 |
|
164 |
match = re.search(booking_pattern, user_message)
|
165 |
if match:
|
166 |
-
bestillingskode = match.group(
|
167 |
-
post_data = {
|
168 |
-
"url": endpoint_url,
|
169 |
-
"body": {
|
170 |
-
"booking_id": bestillingskode
|
171 |
-
}
|
172 |
-
}
|
173 |
-
try:
|
174 |
-
response = await post_tool.acall(
|
175 |
-
json.dumps(post_data),
|
176 |
-
callbacks=[cl.AsyncLangchainCallbackHandler()]
|
177 |
-
)
|
178 |
-
response_key = "output" if "output" in response else "text"
|
179 |
-
await cl.Message(content=response.get(response_key, "")).send()
|
180 |
-
except Exception as e:
|
181 |
-
await cl.Message(content=f"Error: {str(e)}").send()
|
182 |
-
else:
|
183 |
-
response = await llm_chain.ainvoke(
|
184 |
-
{"chat_history": "", "question": user_message},
|
185 |
-
callbacks=[cl.AsyncLangchainCallbackHandler()]
|
186 |
-
)
|
187 |
-
await cl.Message(content=response).send()
|
188 |
-
|
189 |
-
"""
|
190 |
-
async def handle_message(message: cl.Message):
|
191 |
-
user_message = message.content
|
192 |
-
llm_chain = cl.user_session.get("llm_chain")
|
193 |
-
cl.user_session.set("post_tool", post_tool)
|
194 |
-
|
195 |
-
# --------------------------------AD VAL.II.7--------------------------------
|
196 |
-
booking_pattern = r'\b[A-Z]{6}\d{6}\b'
|
197 |
-
endpoint_url = "https://aivisions.no/data/daysoff/api/v1/booking/"
|
198 |
-
|
199 |
-
match = re.search(booking_pattern, user_message)
|
200 |
-
if match:
|
201 |
-
bestillingskode = match.group(0)
|
202 |
-
|
203 |
post_data = {
|
204 |
"url": endpoint_url,
|
205 |
"body": {
|
@@ -211,19 +172,16 @@ async def handle_message(message: cl.Message):
|
|
211 |
json.dumps(post_data),
|
212 |
callbacks=[cl.AsyncLangchainCallbackHandler()]
|
213 |
)
|
214 |
-
|
215 |
-
|
216 |
else:
|
217 |
-
response = await llm_chain.ainvoke(
|
218 |
-
user_message,
|
219 |
-
callbacks=[cl.AsyncLangchainCallbackHandler()]
|
220 |
)
|
|
|
221 |
|
222 |
response_key = "output" if "output" in response else "text"
|
223 |
|
224 |
-
|
225 |
await cl.Message(response.get(response_key, "")).send()
|
226 |
return message.content
|
227 |
|
228 |
|
229 |
-
"""
|
|
|
36 |
class EnhancedRequestsPostTool(RequestsPostTool):
|
37 |
def __init__(self, requests_wrapper, llm, api_docs, api_url_prompt, api_response_prompt):
|
38 |
super().__init__(requests_wrapper=requests_wrapper, allow_dangerous_requests=True)
|
39 |
+
self.url_chain = LLMChain(llm=llm, prompt=api_url_prompt)
|
40 |
+
self.response_chain = LLMChain(llm=llm, prompt=api_response_prompt)
|
41 |
self.api_docs = api_docs
|
42 |
|
43 |
async def ainvoke(self, input_data, callbacks=None):
|
|
|
58 |
"api_response": api_response
|
59 |
})
|
60 |
|
61 |
+
return formatted_response
|
62 |
|
63 |
|
64 |
daysoff_assistant_template = """
|
|
|
148 |
cl.user_session.set("llm_chain", llm_chain)
|
149 |
cl.user_session.set("post_tool", post_tool)
|
150 |
|
|
|
|
|
151 |
|
152 |
@cl.on_message
|
153 |
async def handle_message(message: cl.Message):
|
|
|
155 |
llm_chain = cl.user_session.get("llm_chain")
|
156 |
post_tool = cl.user_session.get("post_tool")
|
157 |
|
|
|
158 |
booking_pattern = r'\b[A-Z]{6}\d{6}\b'
|
159 |
endpoint_url = "https://aivisions.no/data/daysoff/api/v1/booking/"
|
160 |
|
161 |
match = re.search(booking_pattern, user_message)
|
162 |
if match:
|
163 |
+
bestillingskode = match.group()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
164 |
post_data = {
|
165 |
"url": endpoint_url,
|
166 |
"body": {
|
|
|
172 |
json.dumps(post_data),
|
173 |
callbacks=[cl.AsyncLangchainCallbackHandler()]
|
174 |
)
|
175 |
+
|
|
|
176 |
else:
|
177 |
+
response = await llm_chain.ainvoke(
|
178 |
+
user_message, # {"chat_history": "", "question": user_message}
|
|
|
179 |
)
|
180 |
+
await cl.Message(content=response).send()
|
181 |
|
182 |
response_key = "output" if "output" in response else "text"
|
183 |
|
|
|
184 |
await cl.Message(response.get(response_key, "")).send()
|
185 |
return message.content
|
186 |
|
187 |
|
|