传统方法使用 JSON 格式来指定工具名称和参数作为字符串,系统必须解析以确定要执行哪个工具。工具调用型 LLM 与直接生成并执行代码更有效。这是 smolagents 的核心原则。

为什么 LLM 可以生成 code ?因为高质量的 code 已经在 LLM 的训练集中了。

使用 Code 而不是JSON 的优势是:

  • 模块化地组合和重用
  • 对象管理,直接处理复杂的结构
  • 可以表达任何计算上的任务
  • 充分利用了code 生成的能力

CodeAgent 是特殊的 MultiStepAgent

实例

准备好 HF access token: xxx

使用 HF serveless API 时最好设置 HF_TOKEN,但是对于公共的 models 和 datasets,是 optional 的。HF 中免费的API调用次数是有限的。需要付费。

他需要强大的可追溯性以供未来的监控和分析

代理本质上是不可预测且难以检查的。他需要可以被追溯从而,供监控和分析。有工具可以做, OpenTelemetryLangfuse 用于跟踪和分析 Agents 的行为。

通过上述工具可以重新审视之前的 Agent 行为并进一步优化。

code

pip install smolagents -U
pip install duckduckgo-search
# 登陆 HF
from huggingface_hub import notebook_login
notebook_login()  # 非nodetbook时 使用 login()

# 使用  DuckDuckGoSearchTool 搜索内容
from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel
agent = CodeAgent(tools=[DuckDuckGoSearchTool()], model=InferenceClientModel())
# 默认使用 InferenceClientModel - Qwen/Qwen2.5-Coder-32B-Instruct 
# 这个过程会执行多个Step,因为可以前面的Step中 生成的code有语法错误,Agent会自己修改
agent.run("Search for the best music recommendations for a party at the Wayne's mansion.")
from smolagents import CodeAgent, InferenceClientModel
import numpy as np
import time
import datetime

# 代码执行有严格的安全措施 - 默认情况下会阻止 predefined safe list 之外的导入。
# 但可以通过将它们作为字符串传递给 additional_authorized_imports 来授权额外的导入
agent = CodeAgent(tools=[], model=InferenceClientModel(), additional_authorized_imports=['datetime'])

agent.run(
    """
    Alfred needs to prepare for the party. Here are the tasks:
    1. Prepare the drinks - 30 minutes
    2. Decorate the mansion - 60 minutes
    3. Set up the menu - 45 minutes
    4. Prepare the music and playlist - 45 minutes

    If we start right now, at what time will the party be ready?
    """
)

# push 到HF hub
# 改成你的 username/reponame
agent.push_to_hub('sergiopaniego/AlfredAgent')

# 加载 Agent,并让他工作
# 改成你的 username/reponame
alfred_agent = agent.from_hub('sergiopaniego/AlfredAgent', trust_remote_code=True)

alfred_agent.run("Give me the best playlist for a party at Wayne's mansion. \
                    The party idea is a 'villain masquerade' theme")  

总之, smolagents 专门用于编写和执行 Python 代码片段,提供沙盒执行以保障安全。它支持本地和基于 API 的语言模型,使其能够适应各种开发环境。

from smolagents import CodeAgent, DuckDuckGoSearchTool, FinalAnswerTool, InferenceClientModel, Tool, tool, VisitWebpageTool

@tool
def suggest_menu(occasion: str) -> str:
    """
    Suggests a menu based on the occasion.
    Args:
        occasion: The type of occasion for the party.
    """
    if occasion == "casual":
        return "Pizza, snacks, and drinks."
    elif occasion == "formal":
        return "3-course dinner with wine and dessert."
    elif occasion == "superhero":
        return "Buffet with high-energy and healthy food."
    else:
        return "Custom menu for the butler."

@tool
def catering_service_tool(query: str) -> str:
    """
    This tool returns the highest-rated catering service in Gotham City.
    
    Args:
        query: A search term for finding catering services.
    """
    # Example list of catering services and their ratings
    services = {
        "Gotham Catering Co.": 4.9,
        "Wayne Manor Catering": 4.8,
        "Gotham City Events": 4.7,
    }
    
    # Find the highest rated catering service (simulating search query filtering)
    best_service = max(services, key=services.get)
    
    return best_service

class SuperheroPartyThemeTool(Tool):
    name = "superhero_party_theme_generator"
    description = """
    This tool suggests creative superhero-themed party ideas based on a category.
    It returns a unique party theme idea."""
    
    inputs = {
        "category": {
            "type": "string",
            "description": "The type of superhero party (e.g., 'classic heroes', 'villain masquerade', 'futuristic Gotham').",
        }
    }
    
    output_type = "string"

    def forward(self, category: str):
        themes = {
            "classic heroes": "Justice League Gala: Guests come dressed as their favorite DC heroes with themed cocktails like 'The Kryptonite Punch'.",
            "villain masquerade": "Gotham Rogues' Ball: A mysterious masquerade where guests dress as classic Batman villains.",
            "futuristic Gotham": "Neo-Gotham Night: A cyberpunk-style party inspired by Batman Beyond, with neon decorations and futuristic gadgets."
        }
        
        return themes.get(category.lower(), "Themed party idea not found. Try 'classic heroes', 'villain masquerade', or 'futuristic Gotham'.")


# Alfred, the butler, preparing the menu for the party
agent = CodeAgent(
    tools=[
        DuckDuckGoSearchTool(), 
        VisitWebpageTool(),
        suggest_menu,
        catering_service_tool,
        SuperheroPartyThemeTool(),
	    FinalAnswerTool()
    ], 
    model=InferenceClientModel(),
    max_steps=10,
    verbosity_level=2
)

agent.run("Give me the best playlist for a party at the Wayne's mansion. The party idea is a 'villain masquerade' theme")

使用 OpenTelemetry and Langfuse 检测 【todo】

# !pip install smolagents[telemetry] \
# opentelemetry-sdk \
# opentelemetry-exporter-otlp \
# openinference-instrumentation-smolagents

import os
import base64
from google.colab import userdata

LANGFUSE_PUBLIC_KEY=userdata.get("LANGFUSE_PUBLIC_KEY")
LANGFUSE_SECRET_KEY=userdata.get("LANGFUSE_SECRET_KEY")
LANGFUSE_AUTH=base64.b64encode(f"{LANGFUSE_PUBLIC_KEY}:{LANGFUSE_SECRET_KEY}".encode()).decode()

os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = "https://cloud.langfuse.com/api/public/otel" # EU data region
# os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = "https://us.cloud.langfuse.com/api/public/otel" # US data region
os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = f"Authorization=Basic {LANGFUSE_AUTH}"


from smolagents import CodeAgent, InferenceClientModel

agent = CodeAgent(tools=[], model=InferenceClientModel())
alfred_agent = agent.from_hub('sergiopaniego/AlfredAgent', trust_remote_code=True)
alfred_agent.run("Give me best playlist for a party at the Wayne's mansion. The party idea is a 'villain masquerade' theme")