File size: 3,160 Bytes
66f3d3c 9b5b26a c19d193 6aae614 9b5b26a 66f3d3c 9b5b26a 66f3d3c 9b5b26a 66f3d3c 9b5b26a 66f3d3c 8c01ffb 66f3d3c 6aae614 ae7a494 66f3d3c e121372 66f3d3c 13d500a 8c01ffb 66f3d3c 8c01ffb 66f3d3c 8c01ffb 66f3d3c 9b5b26a 66f3d3c 8c01ffb 8fe992b 66f3d3c 8c01ffb 66f3d3c 861422e 8fe992b 66f3d3c 9b5b26a 66f3d3c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
import datetime
import requests
import pytz
import yaml
from tools.final_answer import FinalAnswerTool
from Gradio_UI import GradioUI
@tool
def get_current_time_in_timezone(timezone: str) -> str:
"""Get current time in specified timezone.
Args:
timezone: Valid timezone (e.g., 'America/New_York')
"""
try:
tz = pytz.timezone(timezone)
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
return f"Current time in {timezone}: {local_time}"
except Exception as e:
return f"Error: {str(e)}"
# Performance optimizations
final_answer = FinalAnswerTool()
# Try alternative model endpoint if main is overloaded
model = HfApiModel(
max_tokens=1024, # Reduced from 2096 to limit output length
temperature=0.3, # Reduced from 0.5 for more focused responses
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
custom_role_conversions=None,
)
# Fallback model configuration
fallback_model = HfApiModel(
max_tokens=1024,
temperature=0.3,
model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',
custom_role_conversions=None,
)
# Load image generation tool (only if needed)
# image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
# Optimized prompt templates
optimized_prompts = {
"system": """You are a helpful coding assistant. Be concise and direct.
Focus on the specific task. Avoid unnecessary explanations unless requested.""",
"user": """Task: {task}
Available tools: {tools}
Provide a clear, step-by-step solution."""
}
# Try to load custom prompts, fallback to optimized ones
try:
with open("prompts.yaml", 'r') as stream:
prompt_templates = yaml.safe_load(stream)
# Add length limits to existing prompts
if 'system' in prompt_templates:
prompt_templates['system'] = prompt_templates['system'][:500] # Limit system prompt
except FileNotFoundError:
prompt_templates = optimized_prompts
# Create agent with performance optimizations
agent = CodeAgent(
model=model,
tools=[final_answer, get_current_time_in_timezone],
max_steps=3, # Reduced from 6 to limit processing time
verbosity_level=0, # Reduced from 1 to minimize output
grammar=None,
planning_interval=2, # Add planning interval for better step management
name="OptimizedAgent",
description="Fast, efficient coding assistant",
prompt_templates=prompt_templates
)
# Add error handling and timeout
def launch_with_optimizations():
try:
# Try main model first
GradioUI(agent).launch(
share=False, # Disable sharing for better performance
server_name="127.0.0.1",
server_port=7860,
max_threads=4 # Limit concurrent threads
)
except Exception as e:
print(f"Main model failed: {e}")
print("Switching to fallback model...")
agent.model = fallback_model
GradioUI(agent).launch()
if __name__ == "__main__":
launch_with_optimizations() |