SolshineMisfit commited on
Commit
1733fa0
·
verified ·
1 Parent(s): 24432a6

Fixed Model references after refactor to r1 1776

Browse files
Files changed (1) hide show
  1. app.py +20 -26
app.py CHANGED
@@ -350,13 +350,10 @@ def get_current_time_in_timezone(timezone: str) -> str:
350
 
351
  final_answer = FinalAnswerTool()
352
 
353
- # Replace current model with Perplexity AI R1-1776 (128K context window)
354
 
355
- # Import additional necessary modules
356
- from huggingface_hub import InferenceClient
357
-
358
- # Keep the original model definition but don't use it
359
- original_model = model
360
 
361
  # Create a new model implementation that uses the larger context window model through InferenceClient
362
  class PerplexityR1Model:
@@ -397,12 +394,12 @@ class PerplexityR1Model:
397
  return completion.choices[0].message.content
398
  except Exception as e:
399
  print(f"Error calling Perplexity R1-1776: {str(e)}")
400
- # If we get an error with the large context model, try our aggressive context trimming
401
  if "context length" in str(e).lower() or "token limit" in str(e).lower():
402
- print("Context length error with R1-1776 - trimming context and retrying")
403
- # Use our existing context management function
404
- trimmed_prompt = manage_context(prompt, max_allowed_tokens=90000) # 90K to be safe
405
- messages = [{"role": "user", "content": trimmed_prompt}]
406
 
407
  try:
408
  completion = self.client.chat.completions.create(
@@ -414,19 +411,22 @@ class PerplexityR1Model:
414
  return completion.choices[0].message.content
415
  except Exception as retry_error:
416
  print(f"Error on retry: {str(retry_error)}")
417
- # Fall back to error message
418
- return f"ERROR: Model call failed even with reduced context. Please try a shorter query. Error: {str(retry_error)}"
419
  else:
420
- # For non-context errors, return error message
421
  return f"ERROR: {str(e)}"
422
 
423
- # Replace the model with our new implementation
424
  model = PerplexityR1Model(temperature=0.5, max_tokens=1500)
425
 
426
- # No need for complex context management or fallbacks now with the large context window
427
- # But keep the functions in place in case they're needed as fallbacks
 
 
 
 
428
 
429
- # Update the agent with the new model and more steps
430
  agent = CodeAgent(
431
  model=model,
432
  tools=[
@@ -439,8 +439,8 @@ agent = CodeAgent(
439
  Check_Dataset_Validity,
440
  visit_webpage_tool,
441
  ],
442
- max_steps=12, # Increase back to 12 since we have a large context window
443
- verbosity_level=1, # Increase to 1 since we have room
444
  grammar=None,
445
  planning_interval=2,
446
  name="Research Assistant",
@@ -452,12 +452,6 @@ agent = CodeAgent(
452
  # Add informative message about the model
453
  print("Using Perplexity R1-1776 model with 128K token context window")
454
 
455
- # Import tool from Hub
456
- image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
457
-
458
- with open("prompts.yaml", 'r') as stream:
459
- prompt_templates = yaml.safe_load(stream)
460
-
461
  # Add informative message about which search tool is being used
462
  print(f"Agent initialized with {search_tool_name} as primary search tool")
463
  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")
 
350
 
351
  final_answer = FinalAnswerTool()
352
 
353
+ # Create Perplexity R1 model implementation directly without referencing an undefined variable
354
 
355
+ # Import necessary modules (already imported above)
356
+ # from huggingface_hub import InferenceClient
 
 
 
357
 
358
  # Create a new model implementation that uses the larger context window model through InferenceClient
359
  class PerplexityR1Model:
 
394
  return completion.choices[0].message.content
395
  except Exception as e:
396
  print(f"Error calling Perplexity R1-1776: {str(e)}")
397
+ # For context length errors, try simple truncation
398
  if "context length" in str(e).lower() or "token limit" in str(e).lower():
399
+ print("Context length error with R1-1776 - truncating prompt and retrying")
400
+ # Simple truncation - take the last ~80K characters which should be under the limit
401
+ truncated_prompt = prompt[-80000:] if len(prompt) > 80000 else prompt
402
+ messages = [{"role": "user", "content": truncated_prompt}]
403
 
404
  try:
405
  completion = self.client.chat.completions.create(
 
411
  return completion.choices[0].message.content
412
  except Exception as retry_error:
413
  print(f"Error on retry: {str(retry_error)}")
414
+ return f"ERROR: Model call failed even with reduced context. Please try a shorter query."
 
415
  else:
416
+ # For non-context errors
417
  return f"ERROR: {str(e)}"
418
 
419
+ # Initialize our model with Perplexity R1-1776
420
  model = PerplexityR1Model(temperature=0.5, max_tokens=1500)
421
 
422
+ # Import tool from Hub - do this before using the tool in the agent
423
+ image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
424
+
425
+ # Load prompt templates before using them in the agent
426
+ with open("prompts.yaml", 'r') as stream:
427
+ prompt_templates = yaml.safe_load(stream)
428
 
429
+ # Initialize the agent with all required components already defined
430
  agent = CodeAgent(
431
  model=model,
432
  tools=[
 
439
  Check_Dataset_Validity,
440
  visit_webpage_tool,
441
  ],
442
+ max_steps=12,
443
+ verbosity_level=1,
444
  grammar=None,
445
  planning_interval=2,
446
  name="Research Assistant",
 
452
  # Add informative message about the model
453
  print("Using Perplexity R1-1776 model with 128K token context window")
454
 
 
 
 
 
 
 
455
  # Add informative message about which search tool is being used
456
  print(f"Agent initialized with {search_tool_name} as primary search tool")
457
  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")