Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,7 @@
|
|
4 |
# ===========================================
|
5 |
|
6 |
import asyncio
|
|
|
7 |
import os
|
8 |
import re
|
9 |
import time
|
@@ -78,11 +79,12 @@ api_response_prompt = PromptTemplate(
|
|
78 |
#user_env = cl.user_session.get("env")
|
79 |
#await cl.Message(f"Hello {app_user.identifier}").send()
|
80 |
|
|
|
|
|
|
|
|
|
|
|
81 |
@cl.on_chat_start
|
82 |
-
async def on_chat_start():
|
83 |
-
app_user = cl.user_session.get("user")
|
84 |
-
await cl.Message(f"Hello {app_user}").send()
|
85 |
-
|
86 |
def setup_multiple_chains():
|
87 |
llm = OpenAI(
|
88 |
model='gpt-3.5-turbo-instruct',
|
@@ -93,15 +95,6 @@ def setup_multiple_chains():
|
|
93 |
frequency_penalty=0.1,
|
94 |
presence_penalty=0.1
|
95 |
)
|
96 |
-
llm = OpenAI(
|
97 |
-
model='gpt-3.5-turbo-instruct',
|
98 |
-
temperature=0.7,
|
99 |
-
openai_api_key=OPENAI_API_KEY,
|
100 |
-
max_tokens=2048,
|
101 |
-
top_p=0.9,
|
102 |
-
frequency_penalty=0.1,
|
103 |
-
presence_penalty=0.1
|
104 |
-
)
|
105 |
|
106 |
conversation_memory = ConversationBufferMemory(memory_key="chat_history",
|
107 |
max_len=30,
|
@@ -123,21 +116,23 @@ def setup_multiple_chains():
|
|
123 |
api_url_prompt=api_url_prompt,
|
124 |
api_response_prompt=api_response_prompt,
|
125 |
verbose=True,
|
126 |
-
limit_to_domains=
|
127 |
)
|
128 |
|
129 |
cl.user_session.set("api_chain", api_chain)
|
130 |
|
|
|
|
|
131 |
@cl.on_message
|
132 |
async def handle_message(message: cl.Message):
|
133 |
user_message = message.content #.lower()
|
134 |
llm_chain = cl.user_session.get("llm_chain")
|
135 |
api_chain = cl.user_session.get("api_chain")
|
136 |
-
|
137 |
booking_pattern = r'\b[A-Z]{6}\d{6}\b'
|
138 |
endpoint_url = "https://aivisions.no/data/daysoff/api/v1/booking/"
|
139 |
|
140 |
-
|
141 |
auth_token = f"Bearer {auth_token}
|
142 |
|
143 |
# --GET method
|
@@ -154,8 +149,10 @@ async def handle_message(message: cl.Message):
|
|
154 |
},
|
155 |
callbacks=[cl.AsyncLangchainCallbackHandler()])
|
156 |
|
|
|
|
|
|
|
157 |
"""
|
158 |
-
|
159 |
# --POST method, booking_id@body
|
160 |
if re.search(booking_pattern, user_message):
|
161 |
bestillingskode = re.search(booking_pattern, user_message).group(0)
|
@@ -186,6 +183,54 @@ async def handle_message(message: cl.Message):
|
|
186 |
return message.content
|
187 |
|
188 |
#endpoint_url = "https://670dccd0073307b4ee447f2f.mockapi.io/daysoff/api/V1/booking"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
|
190 |
|
191 |
|
|
|
4 |
# ===========================================
|
5 |
|
6 |
import asyncio
|
7 |
+
import concurrent.futures
|
8 |
import os
|
9 |
import re
|
10 |
import time
|
|
|
79 |
#user_env = cl.user_session.get("env")
|
80 |
#await cl.Message(f"Hello {app_user.identifier}").send()
|
81 |
|
82 |
+
#@cl.on_chat_start
|
83 |
+
#async def on_chat_start():
|
84 |
+
#app_user = cl.user_session.get("user")
|
85 |
+
#await cl.Message(f"Hello {app_user}").send()
|
86 |
+
|
87 |
@cl.on_chat_start
|
|
|
|
|
|
|
|
|
88 |
def setup_multiple_chains():
|
89 |
llm = OpenAI(
|
90 |
model='gpt-3.5-turbo-instruct',
|
|
|
95 |
frequency_penalty=0.1,
|
96 |
presence_penalty=0.1
|
97 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
conversation_memory = ConversationBufferMemory(memory_key="chat_history",
|
100 |
max_len=30,
|
|
|
116 |
api_url_prompt=api_url_prompt,
|
117 |
api_response_prompt=api_response_prompt,
|
118 |
verbose=True,
|
119 |
+
limit_to_domains=["http://0.0.0.0:7860/"]
|
120 |
)
|
121 |
|
122 |
cl.user_session.set("api_chain", api_chain)
|
123 |
|
124 |
+
|
125 |
+
"""
|
126 |
@cl.on_message
|
127 |
async def handle_message(message: cl.Message):
|
128 |
user_message = message.content #.lower()
|
129 |
llm_chain = cl.user_session.get("llm_chain")
|
130 |
api_chain = cl.user_session.get("api_chain")
|
131 |
+
|
132 |
booking_pattern = r'\b[A-Z]{6}\d{6}\b'
|
133 |
endpoint_url = "https://aivisions.no/data/daysoff/api/v1/booking/"
|
134 |
|
135 |
+
|
136 |
auth_token = f"Bearer {auth_token}
|
137 |
|
138 |
# --GET method
|
|
|
149 |
},
|
150 |
callbacks=[cl.AsyncLangchainCallbackHandler()])
|
151 |
|
152 |
+
"""
|
153 |
+
|
154 |
+
|
155 |
"""
|
|
|
156 |
# --POST method, booking_id@body
|
157 |
if re.search(booking_pattern, user_message):
|
158 |
bestillingskode = re.search(booking_pattern, user_message).group(0)
|
|
|
183 |
return message.content
|
184 |
|
185 |
#endpoint_url = "https://670dccd0073307b4ee447f2f.mockapi.io/daysoff/api/V1/booking"
|
186 |
+
|
187 |
+
|
188 |
+
|
189 |
+
"""
|
190 |
+
|
191 |
+
|
192 |
+
@cl.on_message
|
193 |
+
async def handle_message(message: cl.Message):
|
194 |
+
user_message = message.content # Retrieve user message
|
195 |
+
llm_chain = cl.user_session.get("llm_chain") # Get LLM chain
|
196 |
+
api_chain = cl.user_session.get("api_chain") # Get API chain
|
197 |
+
|
198 |
+
booking_pattern = r'\b[A-Z]{6}\d{6}\b'
|
199 |
+
endpoint_url = "https://aivisions.no/data/daysoff/api/v1/booking/"
|
200 |
+
auth_token = "your_auth_token_here"
|
201 |
+
|
202 |
+
async def process_message():
|
203 |
+
if re.search(booking_pattern, user_message):
|
204 |
+
bestillingskode = re.search(booking_pattern, user_message).group(0)
|
205 |
+
question = f"Retrieve information for booking ID {bestillingskode}"
|
206 |
+
|
207 |
+
response = await api_chain.acall(
|
208 |
+
{
|
209 |
+
"url": endpoint_url,
|
210 |
+
"method": "POST",
|
211 |
+
"headers": {
|
212 |
+
"Authorization": f"Bearer {auth_token}",
|
213 |
+
"Content-Type": "application/json",
|
214 |
+
},
|
215 |
+
"body": { # JSON body sent in POST request
|
216 |
+
"booking_id": bestillingskode,
|
217 |
+
},
|
218 |
+
"question": question,
|
219 |
+
},
|
220 |
+
callbacks=[cl.AsyncLangchainCallbackHandler()],
|
221 |
+
)
|
222 |
+
else:
|
223 |
+
response = await llm_chain.acall(user_message, callbacks=[cl.AsyncLangchainCallbackHandler()])
|
224 |
+
|
225 |
+
response_key = "output" if "output" in response else "text"
|
226 |
+
await cl.Message(response.get(response_key, "")).send()
|
227 |
+
return message.content
|
228 |
+
|
229 |
+
# Concurrent execution using ThreadPoolExecutor
|
230 |
+
loop = asyncio.get_running_loop()
|
231 |
+
with concurrent.futures.ThreadPoolExecutor() as pool:
|
232 |
+
await loop.run_in_executor(pool, lambda: asyncio.run(process_message()))
|
233 |
+
|
234 |
|
235 |
|
236 |
|