camparchimedes commited on
Commit
62ea741
Β·
verified Β·
1 Parent(s): eead9e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -40
app.py CHANGED
@@ -17,9 +17,9 @@ from langchain_core.prompts import PromptTemplate
17
  from langchain.memory.buffer import ConversationBufferMemory
18
 
19
  # ---------------------------------------------------for backend looks, example file:----------------------------------
20
- with open('/home/user/.local/lib/python3.10/site-packages/socketio/async_server.py', 'r') as file:
21
- content = file.read()
22
- print("see line 640:", content)
23
  # ------------------------------------------------------the end--------------------------------------------------------
24
 
25
  load_dotenv()
@@ -31,6 +31,7 @@ API_URL = "https://aivisions.no/data/daysoff/api/v1/booking/"
31
  #If booking information is requested, and with
32
  #retrieved booking information: {table} in mind, provide a conversational answer.
33
  #If no booking information is requested, provide a conversational answer.
 
34
 
35
  daysoff_assistant_template = """
36
  You are a customer support assistant for Daysoff kundeservice and help users retrieve booking information associated with their booking IDs.
@@ -50,7 +51,6 @@ daysoff_assistant_prompt = PromptTemplate(
50
  template=daysoff_assistant_template,
51
  )
52
 
53
- # -- async wrapper for requests.post
54
  async def async_post_request(url, headers, data):
55
  return await asyncio.to_thread(requests.post, url, headers=headers, json=data)
56
 
@@ -97,19 +97,17 @@ def setup_multiple_chains():
97
  return_messages=True
98
  )
99
 
100
- #llm_chain = LLMChain(
101
- #llm=llm,
102
- #prompt=daysoff_assistant_prompt,
103
- #memory=conversation_memory,
104
- #)
105
- llm_chain = daysoff_assistant_prompt | llm | conversation_memory
106
-
107
  cl.user_session.set("llm_chain", llm_chain)
108
-
 
109
  @cl.on_message
110
  async def handle_message(message: cl.Message):
 
111
  user_message = message.content
112
  llm_chain = cl.user_session.get("llm_chain")
 
113
 
114
  booking_pattern = r'\b[A-Z]{6}\d{6}\b'
115
  match = re.search(booking_pattern, user_message)
@@ -123,38 +121,16 @@ async def handle_message(message: cl.Message):
123
  payload = {"booking_id": bestillingskode}
124
 
125
  try:
126
- # --async POST request
127
  response = await async_post_request(API_URL, headers, payload)
128
  response.raise_for_status()
129
  booking_data = response.json()
130
 
131
  if "booking_id" in booking_data:
132
  try:
133
- # --markdown_table
134
- #table = (
135
- #"| π‘­π’Šπ’†π’π’… | π—œπ—»π—³π—Ό |\n"
136
- #"|:-----------|:---------------------|\n"
137
- #f"| π™±πšŽπšœπšπš’πš•πš•πš’πš—πšπšœπš”πš˜πšπšŽ | {booking_data.get('booking_id', 'N/A')} |\n"
138
- #f"| 𝙁π™ͺ𝙑𝙑 π™‰π™–π™’π™š | {booking_data.get('full_name', 'N/A')} |\n"
139
- #f"| π˜Όπ™’π™€π™ͺ𝙣𝙩 | {booking_data.get('amount', 0)} kr |\n"
140
- #f"| π˜Ύπ™π™šπ™˜π™ -π™žπ™£ | {booking_data.get('checkin', 'N/A')} |\n"
141
- #f"| π˜Ύπ™π™šπ™˜π™ -𝙀π™ͺ𝙩 | {booking_data.get('checkout', 'N/A')} |\n"
142
- #f"| π˜Όπ™™π™™π™§π™šπ™¨π™¨ | {booking_data.get('address', 'N/A')} |\n"
143
- #f"| π™π™¨π™šπ™§ π™„π˜Ώ | {booking_data.get('user_id', 0)} |\n"
144
- #f"| 𝙄𝙣𝙛𝙀 π™π™šπ™­π™© | {booking_data.get('infotext', 'N/A')} |\n"
145
- #f"| π™„π™£π™˜π™‘π™ͺπ™™π™šπ™™ | {booking_data.get('included', 'N/A')} |"
146
- #)
147
-
148
- # --invoke LLM w/ table + user_message
149
  response = await llm_chain.arun({
150
- #"table": table,
151
- #"booking_data": booking_data,
152
  "question": user_message,
153
- "chat_history": ""
154
  }, callbacks=[cl.AsyncLangchainCallbackHandler()])
155
-
156
- # --send both as combined_message
157
- #combined_message = f"### Informasjon for Bestillingskode:\n\n{table}"
158
  await cl.Message(content=combined_message).send()
159
 
160
  except Exception as e:
@@ -168,17 +144,12 @@ async def handle_message(message: cl.Message):
168
 
169
  else:
170
  try:
171
- # --invoke LLM w/ user_message
172
  response = await llm_chain.arun({
173
  "question": user_message,
174
  "chat_history": ""
175
  }, callbacks=[cl.AsyncLangchainCallbackHandler()])
176
 
177
- # Send the LLM response as a message
178
- #await cl.Message(content=response.get("text")).send()
179
  await cl.Message(content=response["text"]).send()
180
 
181
  except Exception as e:
182
  await cl.Message(content=f"Error: {str(e)}").send()
183
-
184
-
 
17
  from langchain.memory.buffer import ConversationBufferMemory
18
 
19
  # ---------------------------------------------------for backend looks, example file:----------------------------------
20
+ #with open('/home/user/.local/lib/python3.10/site-packages/socketio/async_server.py', 'r') as file:
21
+ #content = file.read()
22
+ #print("see line 640:", content)
23
  # ------------------------------------------------------the end--------------------------------------------------------
24
 
25
  load_dotenv()
 
31
  #If booking information is requested, and with
32
  #retrieved booking information: {table} in mind, provide a conversational answer.
33
  #If no booking information is requested, provide a conversational answer.
34
+ #combined_message = f"### Informasjon for Bestillingskode:\n\n{table}"
35
 
36
  daysoff_assistant_template = """
37
  You are a customer support assistant for Daysoff kundeservice and help users retrieve booking information associated with their booking IDs.
 
51
  template=daysoff_assistant_template,
52
  )
53
 
 
54
  async def async_post_request(url, headers, data):
55
  return await asyncio.to_thread(requests.post, url, headers=headers, json=data)
56
 
 
97
  return_messages=True
98
  )
99
 
100
+ llm_chain = daysoff_assistant_prompt | llm
101
+ memory = conversation_memory
 
 
 
 
 
102
  cl.user_session.set("llm_chain", llm_chain)
103
+ cl.user_session.set("memory", memory)
104
+
105
  @cl.on_message
106
  async def handle_message(message: cl.Message):
107
+
108
  user_message = message.content
109
  llm_chain = cl.user_session.get("llm_chain")
110
+ memory = cl.user_session.get("memory")
111
 
112
  booking_pattern = r'\b[A-Z]{6}\d{6}\b'
113
  match = re.search(booking_pattern, user_message)
 
121
  payload = {"booking_id": bestillingskode}
122
 
123
  try:
 
124
  response = await async_post_request(API_URL, headers, payload)
125
  response.raise_for_status()
126
  booking_data = response.json()
127
 
128
  if "booking_id" in booking_data:
129
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
  response = await llm_chain.arun({
 
 
131
  "question": user_message,
132
+ "chat_history": memory
133
  }, callbacks=[cl.AsyncLangchainCallbackHandler()])
 
 
 
134
  await cl.Message(content=combined_message).send()
135
 
136
  except Exception as e:
 
144
 
145
  else:
146
  try:
 
147
  response = await llm_chain.arun({
148
  "question": user_message,
149
  "chat_history": ""
150
  }, callbacks=[cl.AsyncLangchainCallbackHandler()])
151
 
 
 
152
  await cl.Message(content=response["text"]).send()
153
 
154
  except Exception as e:
155
  await cl.Message(content=f"Error: {str(e)}").send()