siddhartharya commited on
Commit
64190a2
·
verified ·
1 Parent(s): 3b9dc5a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -23
app.py CHANGED
@@ -186,53 +186,63 @@ def generate_summary_and_assign_category(bookmark):
186
  else:
187
  use_prior_knowledge = False
188
 
189
- # Prepare the prompt
190
  if use_prior_knowledge:
191
- # Construct prompt to use prior knowledge
192
  prompt = f"""
193
  You are a knowledgeable assistant with up-to-date information as of 2023.
194
 
195
- The user provided a URL: {bookmark.get('url')}
196
 
197
- Please provide:
198
- 1. A concise summary in **no more than two sentences** about this website.
199
- 2. Assign the most appropriate category from the list below for this website.
200
 
201
  Categories:
202
  {', '.join([f'"{cat}"' for cat in CATEGORIES])}
203
 
204
- Provide your response in the following format:
205
- Summary: [Your summary here]
206
- Category: [One of the categories]
207
  """
208
  else:
209
- # Construct the prompt with the extracted content
210
  prompt = f"""
211
- You are a helpful assistant that creates concise webpage summaries and assigns categories.
212
-
213
- Analyze the following webpage content:
214
 
 
215
  {content_text}
216
 
217
- Please provide:
218
- 1. A concise summary in **no more than two sentences** focusing on the main purpose or topic of the page and key information or features.
219
- 2. Assign the most appropriate category from the list below for this webpage. **Ensure the category directly reflects the content of the summary.**
220
 
221
  Categories:
222
  {', '.join([f'"{cat}"' for cat in CATEGORIES])}
223
 
224
- Provide your response in the following format:
225
- Summary: [Your summary here]
226
- Category: [One of the categories]
227
  """
228
 
 
 
 
 
 
 
 
 
 
 
 
 
 
229
  # Call the LLM via Groq Cloud API
230
  response = openai.ChatCompletion.create(
231
  model='llama-3.1-70b-versatile',
232
  messages=[
233
  {"role": "user", "content": prompt}
234
  ],
235
- max_tokens=200,
236
  temperature=0.5,
237
  )
238
  content = response['choices'][0]['message']['content'].strip()
@@ -266,7 +276,7 @@ Category: [One of the categories]
266
  bookmark['category'] = 'Reference and Knowledge Bases'
267
 
268
  logger.info("Successfully generated summary and assigned category")
269
- time.sleep(1) # Reduced sleep time
270
  break # Exit the retry loop upon success
271
 
272
  except openai.error.RateLimitError as e:
@@ -616,19 +626,37 @@ Bookmarks:
616
  Provide a concise and helpful response.
617
  """
618
 
 
 
 
 
 
 
 
 
 
 
 
 
 
619
  response = openai.ChatCompletion.create(
620
  model='llama-3.1-70b-versatile',
621
  messages=[
622
  {"role": "user", "content": prompt}
623
  ],
624
- max_tokens=500,
625
  temperature=0.7,
626
  )
627
  answer = response['choices'][0]['message']['content'].strip()
628
  logger.info("Chatbot response generated")
629
- time.sleep(1) # Reduced sleep time
630
  return answer
631
 
 
 
 
 
 
632
  except Exception as e:
633
  error_message = f"⚠️ Error processing your query: {str(e)}"
634
  logger.error(error_message, exc_info=True)
 
186
  else:
187
  use_prior_knowledge = False
188
 
189
+ # Shortened prompts
190
  if use_prior_knowledge:
 
191
  prompt = f"""
192
  You are a knowledgeable assistant with up-to-date information as of 2023.
193
 
194
+ URL: {bookmark.get('url')}
195
 
196
+ Provide:
197
+ 1. A concise summary (max two sentences) about this website.
198
+ 2. Assign the most appropriate category from the list below.
199
 
200
  Categories:
201
  {', '.join([f'"{cat}"' for cat in CATEGORIES])}
202
 
203
+ Format:
204
+ Summary: [Your summary]
205
+ Category: [One category]
206
  """
207
  else:
 
208
  prompt = f"""
209
+ You are an assistant that creates concise webpage summaries and assigns categories.
 
 
210
 
211
+ Content:
212
  {content_text}
213
 
214
+ Provide:
215
+ 1. A concise summary (max two sentences) focusing on the main topic.
216
+ 2. Assign the most appropriate category from the list below.
217
 
218
  Categories:
219
  {', '.join([f'"{cat}"' for cat in CATEGORIES])}
220
 
221
+ Format:
222
+ Summary: [Your summary]
223
+ Category: [One category]
224
  """
225
 
226
+ # Estimate tokens
227
+ def estimate_tokens(text):
228
+ return len(text) / 4 # Approximate token estimation
229
+
230
+ prompt_tokens = estimate_tokens(prompt)
231
+ max_tokens = 150 # Reduced from 200
232
+ total_tokens = prompt_tokens + max_tokens
233
+
234
+ # Calculate required delay
235
+ tokens_per_second = 6000 / 60 # 100 tokens per second
236
+ required_delay = total_tokens / tokens_per_second
237
+ sleep_time = max(required_delay, 1)
238
+
239
  # Call the LLM via Groq Cloud API
240
  response = openai.ChatCompletion.create(
241
  model='llama-3.1-70b-versatile',
242
  messages=[
243
  {"role": "user", "content": prompt}
244
  ],
245
+ max_tokens=int(max_tokens),
246
  temperature=0.5,
247
  )
248
  content = response['choices'][0]['message']['content'].strip()
 
276
  bookmark['category'] = 'Reference and Knowledge Bases'
277
 
278
  logger.info("Successfully generated summary and assigned category")
279
+ time.sleep(sleep_time)
280
  break # Exit the retry loop upon success
281
 
282
  except openai.error.RateLimitError as e:
 
626
  Provide a concise and helpful response.
627
  """
628
 
629
+ # Estimate tokens
630
+ def estimate_tokens(text):
631
+ return len(text) / 4 # Approximate token estimation
632
+
633
+ prompt_tokens = estimate_tokens(prompt)
634
+ max_tokens = 300 # Adjust as needed
635
+ total_tokens = prompt_tokens + max_tokens
636
+
637
+ # Calculate required delay
638
+ tokens_per_second = 6000 / 60 # 100 tokens per second
639
+ required_delay = total_tokens / tokens_per_second
640
+ sleep_time = max(required_delay, 1)
641
+
642
  response = openai.ChatCompletion.create(
643
  model='llama-3.1-70b-versatile',
644
  messages=[
645
  {"role": "user", "content": prompt}
646
  ],
647
+ max_tokens=int(max_tokens),
648
  temperature=0.7,
649
  )
650
  answer = response['choices'][0]['message']['content'].strip()
651
  logger.info("Chatbot response generated")
652
+ time.sleep(sleep_time)
653
  return answer
654
 
655
+ except openai.error.RateLimitError as e:
656
+ wait_time = int(e.headers.get("Retry-After", 5))
657
+ logger.warning(f"Rate limit reached. Waiting for {wait_time} seconds before retrying...")
658
+ time.sleep(wait_time)
659
+ return chatbot_response(user_query) # Retry after waiting
660
  except Exception as e:
661
  error_message = f"⚠️ Error processing your query: {str(e)}"
662
  logger.error(error_message, exc_info=True)