SolshineMisfit commited on
Commit
8e59d51
·
verified ·
1 Parent(s): 08a6143

Updated api llm endpoint to deepseek

Browse files
Files changed (1) hide show
  1. app.py +84 -50
app.py CHANGED
@@ -350,67 +350,100 @@ def get_current_time_in_timezone(timezone: str) -> str:
350
 
351
  final_answer = FinalAnswerTool()
352
 
353
- # Remove the huggingface_api_key parameter - it's not supported
354
- model = HfApiModel(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
355
  max_tokens=2096,
356
  temperature=0.5,
357
- model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud', # Using the backup endpoint
358
  custom_role_conversions=None
359
  )
360
 
361
- # Add fallback logic that only activates if the primary model fails
362
  def try_model_call_with_fallbacks(prompt):
363
- """Try to use the primary model first, fall back to alternatives if it fails."""
364
- # First attempt with primary model
365
  try:
366
- return model(prompt)
367
  except Exception as primary_error:
368
  print(f"Primary model call failed: {str(primary_error)}")
369
- print("Trying fallback models...")
370
 
371
- # List of fallback models
372
- fallbacks = [
373
- {
374
- "provider": "sambanova",
375
- "model_name": "Qwen/Qwen2.5-Coder-32B-Instruct",
376
- "display_name": "Qwen 2.5 Coder 32B"
377
- },
378
- {
379
- "provider": "hf-inference",
380
- "model_name": "deepseek-ai/DeepSeek-R1-Distill-Qwen-32B",
381
- "display_name": "DeepSeek R1 Distill Qwen 32B"
382
- }
383
- ]
384
-
385
- # Get API key
386
- api_key = os.getenv("HF_API_KEY") or os.getenv("HUGGINGFACE_API_KEY")
387
- if not api_key:
388
- raise ValueError("No Hugging Face API key found in environment variables")
389
-
390
- # Try each fallback model in sequence
391
- for fallback in fallbacks:
392
- try:
393
- print(f"Trying fallback model: {fallback['display_name']}")
394
- client = InferenceClient(provider=fallback["provider"], api_key=api_key)
395
- messages = [{"role": "user", "content": prompt}]
396
- completion = client.chat.completions.create(
397
- model=fallback["model_name"],
398
- messages=messages,
399
- max_tokens=2096,
400
- temperature=0.5
401
- )
402
- print(f"Successfully used fallback model: {fallback['display_name']}")
403
- return completion.choices[0].message.content
404
- except Exception as e:
405
- print(f"Fallback model {fallback['display_name']} failed: {str(e)}")
406
- continue
407
-
408
- # If all fallbacks fail, re-raise the original error
409
- raise primary_error
 
 
 
 
 
 
410
 
411
- # Monkey patch the model's __call__ method to use our fallback logic
412
- original_call = model.__call__
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: