camparchimedes commited on
Commit
fb13605
Β·
verified Β·
1 Parent(s): 1606748

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -5
app.py CHANGED
@@ -1,5 +1,6 @@
1
  # ===========================================
2
- # title: daysoff-assistant-API-v2, table
 
3
  # file: app.py
4
  # ===========================================
5
 
@@ -83,7 +84,32 @@ daysoff_assistant_prompt = PromptTemplate(
83
 
84
  async def async_post_request(url, headers, data):
85
  return await asyncio.to_thread(requests.post, url, headers=headers, json=data)
86
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  @cl.on_chat_start # <β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€” the Chainlit context
88
  def setup_multiple_chains():
89
  llm = OpenAI(
@@ -115,6 +141,7 @@ def setup_multiple_chains():
115
  async def handle_message(message: cl.Message):
116
  user_message = message.content
117
  llm_chain = cl.user_session.get("llm_chain")
 
118
  booking_pattern = r'\b[A-Z]{6}\d{6}\b'
119
  match = re.search(booking_pattern, user_message)
120
 
@@ -127,12 +154,14 @@ async def handle_message(message: cl.Message):
127
  payload = {"booking_id": bestillingskode}
128
 
129
  try:
 
130
  response = await async_post_request(API_URL, headers, payload)
131
  response.raise_for_status()
132
  booking_data = response.json()
133
 
134
  if "booking_id" in booking_data:
135
  try:
 
136
  table = (
137
  "| π‘­π’Šπ’†π’π’… | π—œπ—»π—³π—Ό |\n"
138
  "|:-----------|:---------------------|\n"
@@ -148,25 +177,28 @@ async def handle_message(message: cl.Message):
148
  )
149
 
150
  # --send both as combined_message
151
- combined_message = f"### Informasjon for Bestillingskode:\n\n{table}"
152
  await cl.Message(content=combined_message).send()
153
-
154
  except Exception as e:
155
  await cl.Message(content=f"Error processing booking data: {str(e)}").send()
156
 
157
  else:
158
- await cl.Message(content="Booking not found.").send()
159
 
160
  except requests.exceptions.RequestException as e:
161
  await cl.Message(content=f"Request failed: {str(e)}").send()
162
 
163
  else:
164
  try:
 
165
  response = await llm_chain.ainvoke({
166
  "question": user_message,
167
  "chat_history": ""
168
  }, callbacks=[cl.AsyncLangchainCallbackHandler()])
169
 
 
 
170
  await cl.Message(content=response["text"]).send()
171
 
172
  except Exception as e:
 
1
  # ===========================================
2
+ # title: daysoff-assistant-API-v2,
3
+ # chainlit==1.1.404
4
  # file: app.py
5
  # ===========================================
6
 
 
84
 
85
  async def async_post_request(url, headers, data):
86
  return await asyncio.to_thread(requests.post, url, headers=headers, json=data)
87
+
88
+ @cl.set_starters
89
+ async def set_starters():
90
+ return [
91
+ cl.Starter(
92
+ label="Booking ID request",
93
+ message="Kan du gi meg info om en reservasjon?",
94
+ icon="/public/booking_id.svg",
95
+ ),
96
+ cl.Starter(
97
+ label="Metric Space Self-Identity Framework",
98
+ message="Explain the Metric Space Self-Identity Framework like I'm five years old.",
99
+ icon="/public/learn.svg",
100
+ ),
101
+ cl.Starter(
102
+ label="Python script for daily email reports",
103
+ message="Write a script to automate sending daily email reports in Python, and walk me through how I would set it up.",
104
+ icon="/public/terminal.svg",
105
+ ),
106
+ cl.Starter(
107
+ label="Morning routine ideation",
108
+ message="Can you help me create a personalized Yoga/pranayama/meditation morning routine that would help increase my productivity throughout the day? Start by asking me about my current habits and what activities energize me in the morning.",
109
+ icon="/public/idea.svg",
110
+ )
111
+ ]
112
+
113
  @cl.on_chat_start # <β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€” the Chainlit context
114
  def setup_multiple_chains():
115
  llm = OpenAI(
 
141
  async def handle_message(message: cl.Message):
142
  user_message = message.content
143
  llm_chain = cl.user_session.get("llm_chain")
144
+
145
  booking_pattern = r'\b[A-Z]{6}\d{6}\b'
146
  match = re.search(booking_pattern, user_message)
147
 
 
154
  payload = {"booking_id": bestillingskode}
155
 
156
  try:
157
+ # --async POST request
158
  response = await async_post_request(API_URL, headers, payload)
159
  response.raise_for_status()
160
  booking_data = response.json()
161
 
162
  if "booking_id" in booking_data:
163
  try:
164
+ # --markdown_table
165
  table = (
166
  "| π‘­π’Šπ’†π’π’… | π—œπ—»π—³π—Ό |\n"
167
  "|:-----------|:---------------------|\n"
 
177
  )
178
 
179
  # --send both as combined_message
180
+ combined_message = f"### Her er informasjon for {bestillingskode}:\n\n{table}"
181
  await cl.Message(content=combined_message).send()
182
+
183
  except Exception as e:
184
  await cl.Message(content=f"Error processing booking data: {str(e)}").send()
185
 
186
  else:
187
+ await cl.Message(content="Booking not found").send()
188
 
189
  except requests.exceptions.RequestException as e:
190
  await cl.Message(content=f"Request failed: {str(e)}").send()
191
 
192
  else:
193
  try:
194
+ # --invoke LLM w/ user_message
195
  response = await llm_chain.ainvoke({
196
  "question": user_message,
197
  "chat_history": ""
198
  }, callbacks=[cl.AsyncLangchainCallbackHandler()])
199
 
200
+ # Send the LLM response as a message
201
+ #await cl.Message(content=response.get("text")).send()
202
  await cl.Message(content=response["text"]).send()
203
 
204
  except Exception as e: