Hugging Face smolagents 简介

使用 Hugging Face smolagents 的 AI Agents

Adel Nehme

VP of AI Curriculum, DataCamp

使用 Hugging Face smolagents 的 AI Agents

使用 Hugging Face smolagents 的 AI Agents

使用 Hugging Face smolagents 的 AI Agents

使用 Hugging Face smolagents 的 AI Agents

使用 Hugging Face smolagents 的 AI Agents

AI 代理定义

使用 Hugging Face smolagents 的 AI Agents

从聊天机器人到代理

聊天机器人:

  • 以自然语言回答
  • 被动、反应式

代理:

  • 可执行操作(网页搜索、读文件、分析数据等)
  • 主动推理以达成目标

代理遵循一个循环:

使用 Hugging Face smolagents 的 AI Agents

示例:竞品定价调研

仅需一个提示词即可完成!

使用 Hugging Face smolagents 的 AI Agents

什么是 smolagents?

  • 轻量级 Python 框架
  • 由 Hugging Face 开发

支持两类代理:

  • ToolCallingAgent:使用结构化函数调用
  • CodeAgent:编写并运行 Python 代码
使用 Hugging Face smolagents 的 AI Agents

函数调用的工作原理

Action 1: {"tool": "search_company", "company": "Competitor A"}
Action 2: {"tool": "get_pricing", "company": "Competitor A", "plan": "Basic"}
Action 3: {"tool": "get_pricing", "company": "Competitor A", "plan": "Pro"}
Action 4: {"tool": "search_company", "company": "Competitor B"}
Action 5: {"tool": "get_pricing", "company": "Competitor B", "plan": "Basic"}
使用 Hugging Face smolagents 的 AI Agents

代码代理的工作原理

competitors = ["Competitor A", "Competitor B", "Competitor C"]
pricing_data = {}

for company in competitors:
    company_info = search_company(company)
    plans = extract_pricing_plans(company_info)
    pricing_data[company] = plans

most_affordable_option = min(pricing_data, 
                      key=lambda x: pricing_data[x]['basic_plan'])
使用 Hugging Face smolagents 的 AI Agents

代码代理流程

研究表明,其成功率比函数调用方法高约 20%。

1 https://huggingface.co/papers/2402.01030
使用 Hugging Face smolagents 的 AI Agents

让我们练习!

使用 Hugging Face smolagents 的 AI Agents

Preparing Video For Download...