Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -25,6 +25,7 @@ huggingface_token = os.environ.get("HUGGINGFACE_TOKEN")
|
|
25 |
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 |
print(f"ACCOUNT_ID: {ACCOUNT_ID}")
|
30 |
print(f"CLOUDFLARE_AUTH_TOKEN: {AUTH_TOKEN[:5]}..." if AUTH_TOKEN else "Not set")
|
@@ -251,49 +252,39 @@ def respond(message, history, model, temperature, num_calls, use_web_search):
|
|
251 |
logging.basicConfig(level=logging.DEBUG)
|
252 |
|
253 |
def get_response_from_cloudflare(query, num_calls=3, temperature=0.2):
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
logging.debug(f"
|
258 |
-
logging.debug(f"
|
259 |
-
|
260 |
prompt = f"Write a detailed and complete response that answers the following user question: '{query}'"
|
261 |
-
|
|
|
|
|
|
|
|
|
|
|
262 |
full_response = ""
|
263 |
for i in range(num_calls):
|
264 |
try:
|
265 |
-
response = requests.post(
|
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 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
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)}")
|
|
|
25 |
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 |
+
API_BASE_URL = "https://api.cloudflare.com/client/v4/accounts/a17f03e0f049ccae0c15cdcf3b9737ce/ai/run/"
|
29 |
|
30 |
print(f"ACCOUNT_ID: {ACCOUNT_ID}")
|
31 |
print(f"CLOUDFLARE_AUTH_TOKEN: {AUTH_TOKEN[:5]}..." if AUTH_TOKEN else "Not set")
|
|
|
252 |
logging.basicConfig(level=logging.DEBUG)
|
253 |
|
254 |
def get_response_from_cloudflare(query, num_calls=3, temperature=0.2):
|
255 |
+
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
256 |
+
model = "@cf/meta/llama-3.1-8b-instruct"
|
257 |
+
|
258 |
+
logging.debug(f"API_BASE_URL: {API_BASE_URL}")
|
259 |
+
logging.debug(f"API_TOKEN: {API_TOKEN[:5]}...")
|
260 |
+
|
261 |
prompt = f"Write a detailed and complete response that answers the following user question: '{query}'"
|
262 |
+
|
263 |
+
inputs = [
|
264 |
+
{"role": "system", "content": "You are a friendly assistant that helps answer questions."},
|
265 |
+
{"role": "user", "content": prompt}
|
266 |
+
]
|
267 |
+
|
268 |
full_response = ""
|
269 |
for i in range(num_calls):
|
270 |
try:
|
271 |
+
response = requests.post(f"{API_BASE_URL}{model}", headers=headers, json={"messages": inputs})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
272 |
|
273 |
logging.debug(f"Cloudflare API Response Status: {response.status_code}")
|
274 |
logging.debug(f"Cloudflare API Response Headers: {response.headers}")
|
275 |
logging.debug(f"Cloudflare API Response Content: {response.text[:500]}...") # First 500 characters
|
276 |
|
277 |
if response.status_code == 200:
|
278 |
+
json_response = response.json()
|
279 |
+
if 'result' in json_response and 'response' in json_response['result']:
|
280 |
+
chunk = json_response['result']['response']
|
281 |
+
full_response += chunk
|
282 |
+
yield full_response
|
283 |
+
else:
|
284 |
+
logging.error(f"Unexpected JSON structure: {json_response}")
|
285 |
+
yield "I apologize, but I received an unexpected response format. Please try again later."
|
|
|
|
|
|
|
|
|
286 |
else:
|
287 |
+
logging.error(f"HTTP Error: {response.status_code}, Response: {response.text}")
|
288 |
yield f"I apologize, but I encountered an HTTP error: {response.status_code}. Please try again later."
|
289 |
except Exception as e:
|
290 |
logging.error(f"Error in generating response from Cloudflare: {str(e)}")
|