# 导入所需的模块和工具类
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
# 导入日期时间处理模块
import datetime
# 导入网络请求模块
import requests
# 导入时区处理模块
import pytz
# 导入YAML配置文件处理模块
import yaml
# 导入最终答案工具类
from tools.final_answer import FinalAnswerTool
# 导入Gradio用户界面类
from Gradio_UI import GradioUI

# 定义一个自定义工具函数,接收两个参数并返回字符串
def my_custom_tool(arg1:str, arg2:int)-> str:
    """A tool that does nothing yet 
    Args:
        arg1: the first argument
        arg2: the second argument
    """
    return "What magic will you build ?"

# 定义一个获取指定时区当前时间的工具函数
def get_current_time_in_timezone(timezone: str) -> str:
    """A tool that fetches the current local time in a specified timezone.
    Args:
        timezone: A string representing a 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"The current local time in {timezone} is: {local_time}"
    except Exception as e:
        return f"Error fetching time for timezone '{timezone}': {str(e)}"

# 创建最终答案工具实例
final_answer = FinalAnswerTool()

# 配置AI模型参数
model = HfApiModel(
    max_tokens=2096,        # 设置最大令牌数
    temperature=0.5,        # 设置温度参数
    model_id='Qwen/Qwen2.5-Coder-32B-Instruct',  # 设置模型ID
    custom_role_conversions=None,   # 设置自定义角色转换
)

# 从Hub加载图像生成工具
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)

# 读取YAML格式的提示模板文件
with open("prompts.yaml", 'r') as stream:
    prompt_templates = yaml.safe_load(stream)
    
# 创建代码智能体实例
agent = CodeAgent(
    model=model,            # 设置使用的模型
    tools=[final_answer],   # 设置要使用的工具列表
    max_steps=6,           # 设置最大步骤数
    verbosity_level=1,     # 设置详细程度级别
    grammar=None,          # 设置语法规则
    planning_interval=None, # 设置规划间隔
    name=None,             # 设置名称
    description=None,      # 设置描述
    prompt_templates=prompt_templates  # 设置提示模板
)

# 启动Gradio用户界面
GradioUI(agent).launch()