推理提供商文档

Vision Agents

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

Vision Agents

Vision Agents 是一个用于构建实时 AI 应用程序的 SDK,支持文本和视觉语言模型、流式响应以及视频理解。

概述

Vision Agents 原生支持 Hugging Face 推理提供商,通过统一的 API 访问数千种模型。该集成支持多种推理提供商,包括 Together AI、Groq、Cerebras、Replicate 和 Fireworks。

Hugging Face 插件提供两种实现方式

  • HuggingFace LLM:仅文本语言模型,支持流式响应和函数调用
  • HuggingFace VLM:视觉语言模型,支持自动视频帧缓冲,实现实时视频理解

先决条件

一个拥有 API 令牌 的 Hugging Face 账户(需要“调用推理提供商”权限)

将您的令牌导出为环境变量

export HF_TOKEN=your_huggingface_token

安装

使用 Hugging Face 插件安装 Vision Agents

uv add vision-agents[huggingface]

用法

LLM

初始化 LLM 时,请指定模型 ID,也可以选择指定提供商

from vision_agents.huggingface import HuggingFaceLLM

llm = HuggingFaceLLM(
      model="meta-llama/Meta-Llama-3-8B-Instruct",
      provider="auto"  # Optional: "fastest", "cheapest", "groq", "together", etc.
)

实例化 llm 后,您可以像这样使用它

response = await llm.simple_response("Hello, how are you?")
print(response.text)

您还可以注册 Python 函数作为工具供 LLM 调用

from vision_agents.plugins import huggingface

llm = huggingface.LLM(model="meta-llama/Meta-Llama-3-8B-Instruct")

@llm.register_function()
def get_weather(city: str) -> str:
    """Get the current weather for a city."""
    return f"The weather in {city} is sunny."

response = await llm.simple_response("What's the weather in Paris?")

正在寻找兼容的模型?查看 此表,了解支持的模型列表,包括提供商、定价、上下文长度、延迟、吞吐量等。

VLM

LLM 很好,但现在让我们使用 Vision Agents 将 VLM 插入视频流,以实现实时视频理解!

from vision_agents.plugins import huggingface, getstream, deepgram
from vision_agents.core import Agent, User

agent = Agent(
    edge=getstream.Edge(),
    agent_user=User(name="AI Assistant", id="agent"),
    instructions="You are a helpful visual assistant.",
    llm=huggingface.VLM(
        model="Qwen/Qwen2-VL-7B-Instruct",
        fps=1,
        frame_buffer_seconds=10,
    ),
)

定义 Agent 后,只需询问视频中有什么

response = await vlm.simple_response("What do you see?")
print(response.text)

资源

在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.