Shreyas094 commited on
Commit
774efea
·
verified ·
1 Parent(s): 234e2ce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -6
app.py CHANGED
@@ -238,7 +238,7 @@ def respond(message, history, model, temperature, num_calls, use_web_search):
238
  else:
239
  context_str = "No documents available."
240
 
241
- for partial_response in get_response_from_cloudflare(prompt="", context=context_str, query=message, num_calls=num_calls, temperature=temperature):
242
  first_line = partial_response.split('\n')[0] if partial_response else ''
243
  logging.info(f"Generated Response (first line): {first_line}")
244
  yield partial_response
@@ -259,16 +259,26 @@ def respond(message, history, model, temperature, num_calls, use_web_search):
259
 
260
  logging.basicConfig(level=logging.DEBUG)
261
 
262
- def get_response_from_cloudflare(prompt, context, query, num_calls=3, temperature=0.2):
263
  headers = {
264
  "Authorization": f"Bearer {API_TOKEN}",
265
  "Content-Type": "application/json"
266
  }
267
  model = "@cf/meta/llama-3.1-8b-instruct"
268
 
 
 
 
 
 
 
 
 
 
 
269
  inputs = [
270
- {"role": "system", "content": "Write a detailed and complete research document that fulfills the following user request: '{query}'"},
271
- {"role": "user", "content": f"Context: {context}\n\nQuestion: {query}"}
272
  ]
273
 
274
  payload = {
@@ -315,7 +325,7 @@ After writing the document, please provide a list of sources used in your respon
315
 
316
  if model == "@cf/meta/llama-3.1-8b-instruct":
317
  # Use Cloudflare API
318
- for response in get_response_from_cloudflare(prompt="", context=context, query=query, num_calls=num_calls, temperature=temperature):
319
  yield response, "" # Yield streaming response without sources
320
  else:
321
  # Use Hugging Face API
@@ -348,7 +358,7 @@ def get_response_from_pdf(query, model, num_calls=3, temperature=0.2):
348
 
349
  if model == "@cf/meta/llama-3.1-8b-instruct":
350
  # Use Cloudflare API with the retrieved context
351
- for response in get_response_from_cloudflare(prompt="", context=context_str, query=query, num_calls=num_calls, temperature=temperature):
352
  yield response
353
  else:
354
  # Use Hugging Face API
 
238
  else:
239
  context_str = "No documents available."
240
 
241
+ for partial_response in get_response_from_cloudflare(prompt="", context=context_str, query=message, num_calls=num_calls, temperature=temperature, search_type="pdf"):
242
  first_line = partial_response.split('\n')[0] if partial_response else ''
243
  logging.info(f"Generated Response (first line): {first_line}")
244
  yield partial_response
 
259
 
260
  logging.basicConfig(level=logging.DEBUG)
261
 
262
+ def get_response_from_cloudflare(prompt, context, query, num_calls=3, temperature=0.2, search_type="pdf"):
263
  headers = {
264
  "Authorization": f"Bearer {API_TOKEN}",
265
  "Content-Type": "application/json"
266
  }
267
  model = "@cf/meta/llama-3.1-8b-instruct"
268
 
269
+ if search_type == "pdf":
270
+ instruction = f"""Using the following context:
271
+ {context}
272
+ Write a detailed and complete research document that fulfills the following user request: '{query}'"""
273
+ else: # web search
274
+ instruction = f"""Using the following context:
275
+ {context}
276
+ Write a detailed and complete research document that fulfills the following user request: '{query}'
277
+ After writing the document, please provide a list of sources used in your response."""
278
+
279
  inputs = [
280
+ {"role": "system", "content": instruction},
281
+ {"role": "user", "content": query}
282
  ]
283
 
284
  payload = {
 
325
 
326
  if model == "@cf/meta/llama-3.1-8b-instruct":
327
  # Use Cloudflare API
328
+ for response in get_response_from_cloudflare(prompt="", context=context, query=query, num_calls=num_calls, temperature=temperature, search_type="web"):
329
  yield response, "" # Yield streaming response without sources
330
  else:
331
  # Use Hugging Face API
 
358
 
359
  if model == "@cf/meta/llama-3.1-8b-instruct":
360
  # Use Cloudflare API with the retrieved context
361
+ for response in get_response_from_cloudflare(prompt="", context=context_str, query=query, num_calls=num_calls, temperature=temperature, search_type="pdf"):
362
  yield response
363
  else:
364
  # Use Hugging Face API