Spaces:
Runtime error
Runtime error
Updated api llm endpoint to deepseek
Browse files
app.py
CHANGED
|
@@ -350,67 +350,100 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 350 |
|
| 351 |
final_answer = FinalAnswerTool()
|
| 352 |
|
| 353 |
-
#
|
| 354 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 355 |
max_tokens=2096,
|
| 356 |
temperature=0.5,
|
| 357 |
-
model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',
|
| 358 |
custom_role_conversions=None
|
| 359 |
)
|
| 360 |
|
| 361 |
-
#
|
| 362 |
def try_model_call_with_fallbacks(prompt):
|
| 363 |
-
"""Try to use the primary model first, fall back to
|
| 364 |
-
# First attempt with primary model
|
| 365 |
try:
|
| 366 |
-
return
|
| 367 |
except Exception as primary_error:
|
| 368 |
print(f"Primary model call failed: {str(primary_error)}")
|
| 369 |
-
print("Trying
|
| 370 |
|
| 371 |
-
#
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
{
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 410 |
|
| 411 |
-
#
|
| 412 |
-
|
| 413 |
-
model.__call__ = try_model_call_with_fallbacks
|
| 414 |
|
| 415 |
# Import tool from Hub
|
| 416 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
|
@@ -447,6 +480,7 @@ agent = CodeAgent(
|
|
| 447 |
# Add informative message about which search tool is being used
|
| 448 |
print(f"Agent initialized with {search_tool_name} as primary search tool")
|
| 449 |
print(f"Available tools: final_answer, Sonar_Web_Search_Tool, {search_tool_name}, get_current_time_in_timezone, image_generation_tool, Dataset_Creator_Tool, Check_Dataset_Validity, visit_webpage_tool")
|
|
|
|
| 450 |
|
| 451 |
# Note about working within token limits - add this comment
|
| 452 |
# When using with queries that might exceed token limits, consider:
|
|
|
|
| 350 |
|
| 351 |
final_answer = FinalAnswerTool()
|
| 352 |
|
| 353 |
+
# Define the primary DeepSeek model using InferenceClient
|
| 354 |
+
def primary_deepseek_model(prompt):
|
| 355 |
+
"""Use the DeepSeek-R1-Distill-Qwen-32B model as the primary endpoint."""
|
| 356 |
+
try:
|
| 357 |
+
print("Using primary model: DeepSeek-R1-Distill-Qwen-32B via HF Inference API")
|
| 358 |
+
# Get API key
|
| 359 |
+
api_key = os.getenv("HF_API_KEY") or os.getenv("HUGGINGFACE_API_KEY")
|
| 360 |
+
if not api_key:
|
| 361 |
+
raise ValueError("No Hugging Face API key found in environment variables")
|
| 362 |
+
|
| 363 |
+
client = InferenceClient(
|
| 364 |
+
provider="hf-inference",
|
| 365 |
+
api_key=api_key
|
| 366 |
+
)
|
| 367 |
+
|
| 368 |
+
messages = [{"role": "user", "content": prompt}]
|
| 369 |
+
completion = client.chat.completions.create(
|
| 370 |
+
model="deepseek-ai/DeepSeek-R1-Distill-Qwen-32B",
|
| 371 |
+
messages=messages,
|
| 372 |
+
max_tokens=2096,
|
| 373 |
+
temperature=0.5
|
| 374 |
+
)
|
| 375 |
+
print("Primary model call successful")
|
| 376 |
+
return completion.choices[0].message.content
|
| 377 |
+
except Exception as e:
|
| 378 |
+
print(f"Primary DeepSeek model failed: {str(e)}")
|
| 379 |
+
raise e
|
| 380 |
+
|
| 381 |
+
# Keep the original endpoint as a backup
|
| 382 |
+
backup_model = HfApiModel(
|
| 383 |
max_tokens=2096,
|
| 384 |
temperature=0.5,
|
| 385 |
+
model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',
|
| 386 |
custom_role_conversions=None
|
| 387 |
)
|
| 388 |
|
| 389 |
+
# Updated fallback mechanism that tries primary first, then original model, then additional fallbacks
|
| 390 |
def try_model_call_with_fallbacks(prompt):
|
| 391 |
+
"""Try to use the primary model first, fall back to original model, then try other fallbacks if both fail."""
|
| 392 |
+
# First attempt with primary DeepSeek model
|
| 393 |
try:
|
| 394 |
+
return primary_deepseek_model(prompt)
|
| 395 |
except Exception as primary_error:
|
| 396 |
print(f"Primary model call failed: {str(primary_error)}")
|
| 397 |
+
print("Trying backup model...")
|
| 398 |
|
| 399 |
+
# Second attempt with original backup model
|
| 400 |
+
try:
|
| 401 |
+
print("Using backup model: HfApiModel endpoint")
|
| 402 |
+
result = backup_model(prompt)
|
| 403 |
+
print("Backup model call successful")
|
| 404 |
+
return result
|
| 405 |
+
except Exception as backup_error:
|
| 406 |
+
print(f"Backup model call failed: {str(backup_error)}")
|
| 407 |
+
print("Trying additional fallback models...")
|
| 408 |
+
|
| 409 |
+
# List of additional fallback models
|
| 410 |
+
fallbacks = [
|
| 411 |
+
{
|
| 412 |
+
"provider": "sambanova",
|
| 413 |
+
"model_name": "Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 414 |
+
"display_name": "Qwen 2.5 Coder 32B"
|
| 415 |
+
}
|
| 416 |
+
# DeepSeek is now the primary model, so we removed it from fallbacks
|
| 417 |
+
]
|
| 418 |
+
|
| 419 |
+
# Get API key
|
| 420 |
+
api_key = os.getenv("HF_API_KEY") or os.getenv("HUGGINGFACE_API_KEY")
|
| 421 |
+
if not api_key:
|
| 422 |
+
raise ValueError("No Hugging Face API key found in environment variables")
|
| 423 |
+
|
| 424 |
+
# Try each fallback model in sequence
|
| 425 |
+
for fallback in fallbacks:
|
| 426 |
+
try:
|
| 427 |
+
print(f"Trying fallback model: {fallback['display_name']}")
|
| 428 |
+
client = InferenceClient(provider=fallback["provider"], api_key=api_key)
|
| 429 |
+
messages = [{"role": "user", "content": prompt}]
|
| 430 |
+
completion = client.chat.completions.create(
|
| 431 |
+
model=fallback["model_name"],
|
| 432 |
+
messages=messages,
|
| 433 |
+
max_tokens=2096,
|
| 434 |
+
temperature=0.5
|
| 435 |
+
)
|
| 436 |
+
print(f"Successfully used fallback model: {fallback['display_name']}")
|
| 437 |
+
return completion.choices[0].message.content
|
| 438 |
+
except Exception as e:
|
| 439 |
+
print(f"Fallback model {fallback['display_name']} failed: {str(e)}")
|
| 440 |
+
continue
|
| 441 |
+
|
| 442 |
+
# If all fallbacks fail, re-raise the original error
|
| 443 |
+
raise primary_error
|
| 444 |
|
| 445 |
+
# Create a model wrapper that uses our fallback logic
|
| 446 |
+
model = lambda prompt: try_model_call_with_fallbacks(prompt)
|
|
|
|
| 447 |
|
| 448 |
# Import tool from Hub
|
| 449 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
|
|
|
| 480 |
# Add informative message about which search tool is being used
|
| 481 |
print(f"Agent initialized with {search_tool_name} as primary search tool")
|
| 482 |
print(f"Available tools: final_answer, Sonar_Web_Search_Tool, {search_tool_name}, get_current_time_in_timezone, image_generation_tool, Dataset_Creator_Tool, Check_Dataset_Validity, visit_webpage_tool")
|
| 483 |
+
print(f"Using DeepSeek-R1-Distill-Qwen-32B as primary model, with HfApiModel as backup")
|
| 484 |
|
| 485 |
# Note about working within token limits - add this comment
|
| 486 |
# When using with queries that might exceed token limits, consider:
|