File size: 2,561 Bytes
6cc760e 9b5b26a 6cc760e 9b5b26a 6cc760e 9b5b26a 6cc760e c19d193 6cc760e 6aae614 6cc760e 9b5b26a 6cc760e 9b5b26a 6cc760e 9b5b26a 6cc760e 9b5b26a 6cc760e 9b5b26a 8c01ffb 6cc760e 6aae614 6cc760e e121372 6cc760e 29fad24 6cc760e 13d500a 8c01ffb 6cc760e 9b5b26a 8c01ffb 6cc760e 861422e 9b5b26a 6cc760e 8c01ffb 6cc760e 8fe992b 6cc760e 8c01ffb |
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 |
# 导入所需的模块和工具类
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() |