引入可检查性以优化HuggingFace SmolAgents的性能

2025年02月18日 由 alex 发表 2901 0

为什么应该记录代理运行日志?

传统的预定义图表方法要求将用户请求匹配到最接近的意图,从而将运行限制在特定的流程路径上。


当用户偏离这些预定义路径时,用户体验会下降,这通常被视为超出了领域范围。


4


另一方面,AI代理能够实时动态生成链条,提供了更高的灵活性和自主性。


为了确保透明度,全面了解AI代理的决策链条和节点至关重要。映射AI代理路径的遥测数据有助于高效地识别延迟问题或错误。


原始日志可能难以解释,但交互式的可视化表示简化了探索和调试过程。


为了标准化仪器配置,HuggingFace使用OpenTelemetry。


OpenTelemetry允许你无缝集成仪器层,如常运行你的代理,并自动将所有内容记录到你的平台。


入门指南

首先,安装必要的软件包。在此示例中,HuggingFace使用了Arize AI的Phoenix,这是一个用于收集和检查日志的强大工具。


然而,任何OpenTelemetry兼容的平台都可以用于此目的。


pip install smolagents
pip install arize-phoenix opentelemetry-sdk opentelemetry-exporter-otlp openinference-instrumentation-smolagents


然后可以启动Phoenix服务器,并通过浏览器进行访问……


python -m phoenix.server.main serve


以下是终端中的视图……


5


与LangChain和LangSmith类似,需要在你的Python脚本中添加一小段代码,如下所示。这让我想起了LangChain在LangChain应用程序中添加LangSmith遥测数据的方法。


from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from openinference.instrumentation.smolagents import SmolagentsInstrumentor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor
endpoint = "http://0.0.0.0:6006/v1/traces"
trace_provider = TracerProvider()
trace_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter(endpoint)))
SmolagentsInstrumentor().instrument(tracer_provider=trace_provider)


下面是完整的AI代理代码,可以复制并粘贴到一个Python文件中。


from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from openinference.instrumentation.smolagents import SmolagentsInstrumentor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor
endpoint = "http://0.0.0.0:6006/v1/traces"
trace_provider = TracerProvider()
trace_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter(endpoint)))
SmolagentsInstrumentor().instrument(tracer_provider=trace_provider)
####
from typing import Optional
from smolagents import HfApiModel, LiteLLMModel, TransformersModel, tool
from smolagents.agents import CodeAgent, ToolCallingAgent

# Choose which inference type to use!
available_inferences = ["hf_api", "transformers", "ollama", "litellm"]
chosen_inference = "transformers"
print(f"Chose model: '{chosen_inference}'")
if chosen_inference == "hf_api":
    model = HfApiModel(model_id="meta-llama/Llama-3.3-70B-Instruct")
elif chosen_inference == "transformers":
    model = TransformersModel(model_id="HuggingFaceTB/SmolLM2-1.7B-Instruct", device_map="auto", max_new_tokens=1000)
elif chosen_inference == "ollama":
    model = LiteLLMModel(
        model_id="ollama_chat/llama3.2",
        api_base="http://localhost:11434",  # replace with remote open-ai compatible server if necessary
        api_key="your-api-key",  # replace with API key if necessary
        num_ctx=8192,  # ollama default is 2048 which will often fail horribly. 8192 works for easy tasks, more is better. Check https://huggingface.co/spaces/NyxKrage/LLM-Model-VRAM-Calculator to calculate how much VRAM this will need for the selected model.
    )
elif chosen_inference == "litellm":
    # For anthropic: change model_id below to 'anthropic/claude-3-5-sonnet-latest'
    model = LiteLLMModel(model_id="gpt-4o")

@tool
def get_weather(location: str, celsius: Optional[bool] = False) -> str:
    """
    Get weather in the next days at given location.
    Secretly this tool does not care about the location, it hates the weather everywhere.
    Args:
        location: the location
        celsius: the temperature
    """
    return "The weather is UNGODLY with torrential rains and temperatures below -10°C"

agent = ToolCallingAgent(tools=[get_weather], model=model)
print("ToolCallingAgent:", agent.run("What's the weather like in Paris?"))
agent = CodeAgent(tools=[get_weather], model=model)
print("CodeAgent:", agent.run("What's the weather like in Paris?"))


一旦运行了AI代理,追踪信息就会在遥测GUI中可见。请注意代理名称、链条以及运行中的llm(语言模型)和工具步骤,包括输入、输出、延迟等更多信息。


6


以下是更深入的视图,其中向模型输入的消息和系统均可见……


7


最后,遥测技术提供了对AI代理决策过程的实时可见性,有助于诊断延迟、错误或意外行为等问题。


通过映射AI代理的工作流,遥测技术使开发人员能够了解不同节点之间的交互方式,从而确保更顺畅、更高效的执行。


如果没有适当的日志记录和监控,调试AI代理可能会很具有挑战性,但遥测技术通过结构化洞察和可视化表示简化了故障排除过程。

文章来源:https://cobusgreyling.medium.com/introduce-inspectability-to-huggingface-smolagents-571bd3f8da4c
欢迎关注ATYUN官方公众号
商务合作及内容投稿请联系邮箱:bd@atyun.com
评论 登录
热门职位
Maluuba
20000~40000/月
Cisco
25000~30000/月 深圳市
PilotAILabs
30000~60000/年 深圳市
写评论取消
回复取消