跳到内容

HunyuanOCR 使用指南

简介

HunyuanOCR 是一个领先的端到端 OCR 专家 VLM,由 Hunyuan 的原生多模态架构提供支持。在本指南中,我们将演示如何使用 OpenAI 兼容的 API 服务器设置 HunyuanOCR 以进行在线 OCR 服务。

安装 vLLM

uv venv
source .venv/bin/activate
uv pip install -U vllm --torch-backend auto

部署 HunyuanOCR

vllm serve tencent/HunyuanOCR \
    --no-enable-prefix-caching \
    --mm-processor-cache-gb 0

使用 OpenAI API 客户端查询

from openai import OpenAI

client = OpenAI(
    api_key="EMPTY",
    base_url="https://:8000/v1",
    timeout=3600
)

messages = [
    {"role": "system", "content": ""},
    {
        "role": "user",
        "content": [
            {
                "type": "image_url",
                "image_url": {
                    "url": "https://hugging-face.cn/datasets/huggingface/documentation-images/resolve/main/chat-ui/tools-dark.png"
                }
            },
            {
                "type": "text",
                "text": (
                    "Extract all information from the main body of the document image "
                    "and represent it in markdown format, ignoring headers and footers."
                    "Tables should be expressed in HTML format, formulas in the document "
                    "should be represented using LaTeX format, and the parsing should be "
                    "organized according to the reading order."
                )
            }
        ]
    }
]

response = client.chat.completions.create(
    model="tencent/HunyuanOCR",
    messages=messages,
    temperature=0.0,
    extra_body={
        "top_k": 1,
        "repetition_penalty": 1.0
    },
)
print(f"Generated text: {response.choices[0].message.content}")

配置技巧

  • 为获得最佳 OCR 性能,请使用贪心采样(即 temperature=0.0)或低温度采样。
  • 与多轮聊天用例不同,我们不期望 OCR 任务能从前缀缓存或图像重用中显著受益,因此建议关闭这些功能以避免不必要的哈希和缓存。
  • 根据您的硬件能力,调整 max_num_batched_tokens 以获得更好的吞吐量性能。
  • 有关各种文档解析任务的更多面向应用的提示,请查看官方 HunyuanOCR 文档