Neurolingua commited on
Commit
04f308f
1 Parent(s): d876bf1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -30
app.py CHANGED
@@ -206,49 +206,41 @@ def initialize_chroma():
206
  initialize_chroma()
207
 
208
  def query_rag(query_text: str):
209
- try:
210
- # Ensure query_text is a string
211
- if not isinstance(query_text, str):
212
- raise ValueError("Query text must be a string.")
213
-
214
- # Initialize the embedding function and Chroma DB
215
- embedding_function = get_embedding_function()
216
- db = Chroma(persist_directory=CHROMA_PATH, embedding_function=embedding_function)
217
-
218
- # Perform similarity search
219
- results = db.similarity_search_with_score(query_text, k=5)
220
-
221
- # Extract and clean context text
222
- context_texts = [doc.page_content for doc, _score in results]
223
- if not all(isinstance(text, str) for text in context_texts):
224
- raise ValueError("All context texts must be strings.")
225
-
226
- context_text = "\n\n---\n\n".join(context_texts)
227
-
228
- # Create prompt
229
  prompt_template = ChatPromptTemplate.from_template(PROMPT_TEMPLATE)
230
  prompt = prompt_template.format(context=context_text, question=query_text)
231
 
232
- # Generate response using AI71
233
  response = ''
234
  for chunk in AI71(AI71_API_KEY).chat.completions.create(
235
  model="tiiuae/falcon-180b-chat",
236
  messages=[
237
  {"role": "system", "content": "You are the best agricultural assistant. Remember to give a response in not more than 2 sentences."},
238
- {"role": "user", "content": f'Answer the following query based on the given context: {prompt}'},
239
  ],
240
  stream=True,
241
  ):
242
  if chunk.choices[0].delta.content:
243
  response += chunk.choices[0].delta.content
244
-
245
- # Return cleaned response
246
- return response.replace("###", '').replace('\nUser:', '')
247
-
248
- except Exception as e:
249
- # Log the error and return a user-friendly message
250
- print(f"Error in query_rag: {e}")
251
- return "Sorry, there was an error processing your query."
252
 
253
  def download_file(url, extension):
254
  try:
 
206
  initialize_chroma()
207
 
208
  def query_rag(query_text: str):
209
+ embedding_function = get_embedding_function()
210
+ db = Chroma(persist_directory=CHROMA_PATH, embedding_function=embedding_function)
211
+
212
+ # Check if the query is related to a PDF
213
+ if "from pdf" in query_text.lower() or "in pdf" in query_text.lower():
214
+ # Provide some context about handling PDFs
215
+ response_text = "I see you're asking about a PDF-related query. Let me check the context from the PDF."
216
+ else:
217
+ # Regular RAG functionality
218
+ response_text = "Your query is not related to PDFs. Please make sure your question is clear."
219
+
220
+ results = db.similarity_search_with_score(query_text, k=5)
221
+
222
+ if not results:
223
+ response_text = "Sorry, I couldn't find any relevant information."
224
+ else:
225
+ context_text = "\n\n---\n\n".join([doc.page_content for doc, _score in results])
 
 
 
226
  prompt_template = ChatPromptTemplate.from_template(PROMPT_TEMPLATE)
227
  prompt = prompt_template.format(context=context_text, question=query_text)
228
 
 
229
  response = ''
230
  for chunk in AI71(AI71_API_KEY).chat.completions.create(
231
  model="tiiuae/falcon-180b-chat",
232
  messages=[
233
  {"role": "system", "content": "You are the best agricultural assistant. Remember to give a response in not more than 2 sentences."},
234
+ {"role": "user", "content": f'''Answer the following query based on the given context: {prompt}'''},
235
  ],
236
  stream=True,
237
  ):
238
  if chunk.choices[0].delta.content:
239
  response += chunk.choices[0].delta.content
240
+
241
+ response_text = response.replace("###", '').replace('\nUser:', '')
242
+
243
+ return response_text
 
 
 
 
244
 
245
  def download_file(url, extension):
246
  try: