Shreyas094 commited on
Commit
e6e13d7
·
verified ·
1 Parent(s): 0b6ac2f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -20
app.py CHANGED
@@ -26,6 +26,9 @@ llama_cloud_api_key = os.environ.get("LLAMA_CLOUD_API_KEY")
26
  ACCOUNT_ID = os.environ.get("CLOUDFARE_ACCOUNT_ID")
27
  AUTH_TOKEN = os.environ.get("CLOUDFLARE_AUTH_TOKEN")
28
 
 
 
 
29
  MODELS = [
30
  "mistralai/Mistral-7B-Instruct-v0.3",
31
  "mistralai/Mixtral-8x7B-Instruct-v0.1",
@@ -85,14 +88,6 @@ def update_vectors(files, parser):
85
 
86
  return f"Vector store updated successfully. Processed {total_chunks} chunks from {len(files)} files using {parser}."
87
 
88
- import os
89
- import json
90
- import requests
91
- from huggingface_hub import InferenceClient
92
-
93
- ACCOUNT_ID = "your-account-id" # Replace with your actual Cloudflare account ID
94
- AUTH_TOKEN = os.environ.get("CLOUDFLARE_AUTH_TOKEN")
95
-
96
  def generate_chunked_response(prompt, model, max_tokens=1000, num_calls=3, temperature=0.2, should_stop=False):
97
  print(f"Starting generate_chunked_response with {num_calls} calls")
98
  full_response = ""
@@ -253,10 +248,15 @@ def respond(message, history, model, temperature, num_calls, use_web_search):
253
  else:
254
  yield f"An error occurred with the {model} model: {str(e)}. Please try again or select a different model."
255
 
 
 
256
  def get_response_from_cloudflare(query, num_calls=3, temperature=0.2):
257
  ACCOUNT_ID = "your-account-id" # Replace with your actual Cloudflare account ID
258
  AUTH_TOKEN = os.environ.get("CLOUDFLARE_AUTH_TOKEN")
259
 
 
 
 
260
  prompt = f"Write a detailed and complete response that answers the following user question: '{query}'"
261
 
262
  full_response = ""
@@ -266,29 +266,38 @@ def get_response_from_cloudflare(query, num_calls=3, temperature=0.2):
266
  f"https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/ai/run/@cf/meta/llama-3.1-8b-instruct",
267
  headers={"Authorization": f"Bearer {AUTH_TOKEN}"},
268
  json={
269
- "stream": True,
270
  "messages": [
271
  {"role": "system", "content": "You are a friendly assistant"},
272
  {"role": "user", "content": prompt}
273
  ],
274
  "max_tokens": 1000,
275
  "temperature": temperature
276
- },
277
- stream=True
278
  )
279
 
280
- for line in response.iter_lines():
281
- if line:
282
- try:
283
- json_data = json.loads(line.decode('utf-8').split('data: ')[1])
284
- chunk = json_data.get('response', '')
 
 
 
 
285
  full_response += chunk
286
  yield full_response
287
- except (json.JSONDecodeError, IndexError) as e:
288
- print(f"Error parsing response: {str(e)}")
289
- continue
 
 
 
 
 
 
290
  except Exception as e:
291
- print(f"Error in generating response from Cloudflare: {str(e)}")
 
292
 
293
  if not full_response:
294
  yield "I apologize, but I couldn't generate a response at this time. Please try again later."
 
26
  ACCOUNT_ID = os.environ.get("CLOUDFARE_ACCOUNT_ID")
27
  AUTH_TOKEN = os.environ.get("CLOUDFLARE_AUTH_TOKEN")
28
 
29
+ print(f"ACCOUNT_ID: {ACCOUNT_ID}")
30
+ print(f"CLOUDFLARE_AUTH_TOKEN: {AUTH_TOKEN[:5]}..." if AUTH_TOKEN else "Not set")
31
+
32
  MODELS = [
33
  "mistralai/Mistral-7B-Instruct-v0.3",
34
  "mistralai/Mixtral-8x7B-Instruct-v0.1",
 
88
 
89
  return f"Vector store updated successfully. Processed {total_chunks} chunks from {len(files)} files using {parser}."
90
 
 
 
 
 
 
 
 
 
91
  def generate_chunked_response(prompt, model, max_tokens=1000, num_calls=3, temperature=0.2, should_stop=False):
92
  print(f"Starting generate_chunked_response with {num_calls} calls")
93
  full_response = ""
 
248
  else:
249
  yield f"An error occurred with the {model} model: {str(e)}. Please try again or select a different model."
250
 
251
+ logging.basicConfig(level=logging.DEBUG)
252
+
253
  def get_response_from_cloudflare(query, num_calls=3, temperature=0.2):
254
  ACCOUNT_ID = "your-account-id" # Replace with your actual Cloudflare account ID
255
  AUTH_TOKEN = os.environ.get("CLOUDFLARE_AUTH_TOKEN")
256
 
257
+ logging.debug(f"ACCOUNT_ID: {ACCOUNT_ID}")
258
+ logging.debug(f"CLOUDFLARE_AUTH_TOKEN: {AUTH_TOKEN[:5]}...") # Print only the first 5 characters for security
259
+
260
  prompt = f"Write a detailed and complete response that answers the following user question: '{query}'"
261
 
262
  full_response = ""
 
266
  f"https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/ai/run/@cf/meta/llama-3.1-8b-instruct",
267
  headers={"Authorization": f"Bearer {AUTH_TOKEN}"},
268
  json={
 
269
  "messages": [
270
  {"role": "system", "content": "You are a friendly assistant"},
271
  {"role": "user", "content": prompt}
272
  ],
273
  "max_tokens": 1000,
274
  "temperature": temperature
275
+ }
 
276
  )
277
 
278
+ logging.debug(f"Cloudflare API Response Status: {response.status_code}")
279
+ logging.debug(f"Cloudflare API Response Headers: {response.headers}")
280
+ logging.debug(f"Cloudflare API Response Content: {response.text[:500]}...") # First 500 characters
281
+
282
+ if response.status_code == 200:
283
+ try:
284
+ json_response = response.json()
285
+ if 'result' in json_response and 'response' in json_response['result']:
286
+ chunk = json_response['result']['response']
287
  full_response += chunk
288
  yield full_response
289
+ else:
290
+ logging.error(f"Unexpected JSON structure: {json_response}")
291
+ yield "I apologize, but I received an unexpected response format. Please try again later."
292
+ except json.JSONDecodeError as e:
293
+ logging.error(f"JSON Decode Error: {str(e)}")
294
+ yield "I apologize, but I couldn't parse the response. Please try again later."
295
+ else:
296
+ logging.error(f"HTTP Error: {response.status_code}")
297
+ yield f"I apologize, but I encountered an HTTP error: {response.status_code}. Please try again later."
298
  except Exception as e:
299
+ logging.error(f"Error in generating response from Cloudflare: {str(e)}")
300
+ yield f"I apologize, but an error occurred: {str(e)}. Please try again later."
301
 
302
  if not full_response:
303
  yield "I apologize, but I couldn't generate a response at this time. Please try again later."