camparchimedes commited on
Commit
45e1c9f
·
verified ·
1 Parent(s): bced73b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -124
app.py CHANGED
@@ -1,26 +1,8 @@
1
 
2
  # ===========================================
3
- # ver01.01-5.workload-----app.py
4
  # ===========================================
5
 
6
- def print_scripts(file_path):
7
- try:
8
- with open(file_path, 'r') as file:
9
- print(f"\n--- Contents of {file_path} ---\n")
10
- print(file.read())
11
- print(f"--- End of {file_path} ---\n")
12
- except Exception as e:
13
- print(f"Could not read {file_path}: {e}")
14
-
15
-
16
- if __name__ == "__main__":
17
-
18
- file_1 = "/home/user/.local/lib/python3.10/site-packages/chainlit/socket.py"
19
- file_2 = "/home/user/.local/lib/python3.10/site-packages/socketio/async_server.py"
20
-
21
- print_scripts(file_1)
22
- print_scripts(file_2)
23
-
24
  import asyncio
25
  import concurrent.futures
26
  import os
@@ -40,12 +22,11 @@ from langchain.memory.buffer import ConversationBufferMemory
40
  from langchain.memory import ConversationTokenBufferMemory
41
  from langchain.memory import ConversationSummaryMemory
42
 
43
- #from api_docs_mck import api_docs_str
44
- from api_docs import api_docs_str, auth_token
45
 
46
  load_dotenv()
47
  OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
48
- auth = os.environ.get("CHAINLIT_AUTH_SECRET")
49
 
50
  daysoff_assistant_template = """
51
  #You are a customer support assistant (’kundeservice AI assistent’) for Daysoff.
@@ -92,16 +73,6 @@ api_response_prompt = PromptTemplate(
92
  template=api_response_template
93
  )
94
 
95
- #async def on_chat_start():
96
- #app_user = cl.user_session.get("user")
97
- #user_env = cl.user_session.get("env")
98
- #await cl.Message(f"Hello {app_user.identifier}").send()
99
-
100
- #@cl.on_chat_start
101
- #async def on_chat_start():
102
- #app_user = cl.user_session.get("user")
103
- #await cl.Message(f"Hello {app_user}").send()
104
-
105
  @cl.on_chat_start
106
  def setup_multiple_chains():
107
  llm = OpenAI(
@@ -127,86 +98,21 @@ def setup_multiple_chains():
127
 
128
  cl.user_session.set("llm_chain", llm_chain)
129
 
130
-
131
  api_chain = APIChain.from_llm_and_api_docs(
132
  llm=llm,
133
  api_docs=api_docs_str,
134
  api_url_prompt=api_url_prompt,
135
  api_response_prompt=api_response_prompt,
 
 
 
 
136
  verbose=True,
137
- limit_to_domains=None #["http://0.0.0.0:7860/"]
138
  )
139
 
140
  cl.user_session.set("api_chain", api_chain)
141
 
142
-
143
- """
144
- @cl.on_message
145
- async def handle_message(message: cl.Message):
146
- user_message = message.content #.lower()
147
- llm_chain = cl.user_session.get("llm_chain")
148
- api_chain = cl.user_session.get("api_chain")
149
-
150
- booking_pattern = r'\b[A-Z]{6}\d{6}\b'
151
- endpoint_url = "https://aivisions.no/data/daysoff/api/v1/booking/"
152
-
153
-
154
- #auth_token = f"Bearer {auth_token}
155
- auth_token = "abc123def456ghi789jkl012mno345pqr678stu901vwx234yz567"
156
-
157
-
158
- # --GET method
159
- if re.search(booking_pattern, user_message):
160
- bestillingskode = re.search(booking_pattern, user_message).group(0)
161
- question = f"Retrieve information for booking ID {endpoint_url}?search={bestillingskode}&auth_token={auth_token}"
162
- "
163
- response = await api_chain.acall(
164
- {
165
- "bestillingskode": bestillingskode,
166
- "question": question,
167
- "auth_token": auth_token
168
-
169
- },
170
- callbacks=[cl.AsyncLangchainCallbackHandler()])
171
-
172
- """
173
-
174
-
175
- """
176
- # --POST method, booking_id@body
177
- if re.search(booking_pattern, user_message):
178
- bestillingskode = re.search(booking_pattern, user_message).group(0)
179
- question = f"Retrieve information for booking ID {bestillingskode}"
180
-
181
- response = await api_chain.acall(
182
- {
183
- "url": endpoint_url,
184
- "method": "POST",
185
- "headers": {
186
- "Authorization": f"Bearer {auth_token}",
187
- "Content-Type": "application/json"
188
- },
189
- "body": {
190
- "booking_id": bestillingskode
191
- },
192
- "question": question
193
- },
194
- callbacks=[cl.AsyncLangchainCallbackHandler()]
195
- )
196
-
197
- else:
198
- response = await llm_chain.acall(user_message, callbacks=[cl.AsyncLangchainCallbackHandler()])
199
-
200
-
201
- response_key = "output" if "output" in response else "text"
202
- await cl.Message(response.get(response_key, "")).send()
203
- return message.content
204
-
205
- #endpoint_url = "https://670dccd0073307b4ee447f2f.mockapi.io/daysoff/api/V1/booking"
206
-
207
-
208
-
209
- """
210
  @cl.on_message
211
  async def handle_message(message: cl.Message):
212
  user_message = message.content
@@ -214,36 +120,38 @@ async def handle_message(message: cl.Message):
214
  api_chain = cl.user_session.get("api_chain")
215
 
216
  booking_pattern = r'\b[A-Z]{6}\d{6}\b'
217
- endpoint_url = "https://aivisions.no/data/daysoff/api/v1/booking/"
218
- auth_token = "abc123def456ghi789jkl012mno345pqr678stu901vwx234yz567"
 
 
 
219
 
 
220
 
 
 
 
 
221
 
222
  if re.search(booking_pattern, user_message):
223
  bestillingskode = re.search(booking_pattern, user_message).group(0)
224
- question = f"Retrieve information for booking ID {endpoint_url}?auth_token={auth_token}&booking_id={bestillingskode}"
225
-
226
- response = await api_chain.acall( # ~ainvoke
227
- {
228
- "bestillingskode": bestillingskode,
229
- "question": question
230
- },
231
- callbacks=[cl.AsyncLangchainCallbackHandler()])
 
 
 
 
 
232
 
233
  else:
234
  response = await llm_chain.acall(user_message, callbacks=[cl.AsyncLangchainCallbackHandler()])
235
 
236
  response_key = "output" if "output" in response else "text"
237
  await cl.Message(response.get(response_key, "")).send()
238
- return message.content
239
-
240
-
241
- # --concurrent execution@ThreadPoolExecutor, resolve the ’Could not reach the server' error?
242
- #loop = asyncio.get_running_loop()
243
- #with concurrent.futures.ThreadPoolExecutor() as pool:
244
- #await loop.run_in_executor(pool, lambda: asyncio.run(process_message()))
245
-
246
-
247
-
248
-
249
-
 
1
 
2
  # ===========================================
3
+ # "the-very-latest"-----app.py ✍🏽🎬
4
  # ===========================================
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  import asyncio
7
  import concurrent.futures
8
  import os
 
22
  from langchain.memory import ConversationTokenBufferMemory
23
  from langchain.memory import ConversationSummaryMemory
24
 
25
+ from api_docs import api_docs_str
 
26
 
27
  load_dotenv()
28
  OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
29
+ auth_token = os.getenv("DAYSOFF_API_TOKEN")
30
 
31
  daysoff_assistant_template = """
32
  #You are a customer support assistant (’kundeservice AI assistent’) for Daysoff.
 
73
  template=api_response_template
74
  )
75
 
 
 
 
 
 
 
 
 
 
 
76
  @cl.on_chat_start
77
  def setup_multiple_chains():
78
  llm = OpenAI(
 
98
 
99
  cl.user_session.set("llm_chain", llm_chain)
100
 
 
101
  api_chain = APIChain.from_llm_and_api_docs(
102
  llm=llm,
103
  api_docs=api_docs_str,
104
  api_url_prompt=api_url_prompt,
105
  api_response_prompt=api_response_prompt,
106
+ headers={
107
+ "Authorization": f"Bearer {auth_token}",
108
+ "Content-Type": "application/json"
109
+ },
110
  verbose=True,
111
+ limit_to_domains=["aivisions.no"]
112
  )
113
 
114
  cl.user_session.set("api_chain", api_chain)
115
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  @cl.on_message
117
  async def handle_message(message: cl.Message):
118
  user_message = message.content
 
120
  api_chain = cl.user_session.get("api_chain")
121
 
122
  booking_pattern = r'\b[A-Z]{6}\d{6}\b'
123
+ base_url = "https://aivisions.no/data/daysoff/api/v1/booking/"
124
+ headers = {
125
+ "Authorization": f"Bearer {auth_token}",
126
+ "Content-Type": "application/json"
127
+ }
128
 
129
+ #from langchain.requests import Requests
130
 
131
+ #requests = Requests(
132
+ #headers=headers,
133
+ #verify=True # @SSL verification
134
+ #)
135
 
136
  if re.search(booking_pattern, user_message):
137
  bestillingskode = re.search(booking_pattern, user_message).group(0)
138
+ question = f"Retrieve information for booking ID {bestillingskode}"
139
+ # ~ainvoke
140
+ response = await api_chain.acall(
141
+ {
142
+ "question": question,
143
+ "input": {
144
+ "url": base_url,
145
+ "method": "POST",
146
+ "headers": headers,
147
+ "body": {"booking_id": bestillingskode}
148
+ }
149
+ },
150
+ callbacks=[cl.AsyncLangchainCallbackHandler()]
151
 
152
  else:
153
  response = await llm_chain.acall(user_message, callbacks=[cl.AsyncLangchainCallbackHandler()])
154
 
155
  response_key = "output" if "output" in response else "text"
156
  await cl.Message(response.get(response_key, "")).send()
157
+ return message.content