Vertex AI OCR

Vertex AI OCR

Mistral이 제공하는 Vertex AI OCR의 문서·이미지에서 텍스트를 추출하는 기능을 LiteLLM에서 사용하는 방법을 알아봐요.

출처: 문서

본문

개요

속성 내용
설명 Vertex AI OCR은 Mistral이 제공하는 문서 인텔리전스로, PDF·이미지에서 텍스트 추출을 지원해요
LiteLLM 라우트 vertex_ai/
지원 연산 /ocr
공식 문서 Vertex AI ↗

Mistral이 제공하는 Vertex AI의 OCR 모델로 문서와 이미지에서 텍스트를 추출할 수 있어요.

빠른 시작

LiteLLM SDK

import litellm
import os

# Set environment variables
os.environ["VERTEXAI_PROJECT"] = "your-project-id"
os.environ["VERTEXAI_LOCATION"] = "us-central1"

# OCR with PDF URL
response = litellm.ocr(
    model="vertex_ai/mistral-ocr-2505",
    document={
        "type": "document_url",
        "document_url": "https://example.com/document.pdf"
    }
)

# Access extracted text
for page in response.pages:
    print(page.markdown)

LiteLLM PROXY

proxy_config.yaml:

model_list:
  - model_name: vertex-ocr
    litellm_params:
      model: vertex_ai/mistral-ocr-2505
      vertex_project: os.environ/VERTEXAI_PROJECT
      vertex_location: os.environ/VERTEXAI_LOCATION
      vertex_credentials: path/to/service-account.json  # Optional
    model_info:
      mode: ocr

Proxy 시작:

litellm --config proxy_config.yaml

Proxy를 통해 OCR 호출:

curl -X POST http://localhost:4000/ocr \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ***" \
  -d '{
    "model": "vertex-ocr",
    "document": {
      "type": "document_url",
      "document_url": "https://arxiv.org/pdf/2201.04234"
    }
  }'

인증

Vertex AI OCR은 여러 인증 방법을 지원해요:

서비스 계정 JSON

response = litellm.ocr(
    model="vertex_ai/mistral-ocr-2505",
    document={"type": "document_url", "document_url": "https://..."},
    vertex_project="your-project-id",
    vertex_location="us-central1",
    vertex_credentials="path/to/service-account.json"
)

Application Default Credentials

# Relies on GOOGLE_APPLICATION_CREDENTIALS environment variable
response = litellm.ocr(
    model="vertex_ai/mistral-ocr-2505",
    document={"type": "document_url", "document_url": "https://..."},
    vertex_project="your-project-id",
    vertex_location="us-central1"
)

문서 유형

Vertex AI OCR은 PDF와 이미지를 모두 지원해요.

PDF 문서

response = litellm.ocr(
    model="vertex_ai/mistral-ocr-2505",
    document={
        "type": "document_url",
        "document_url": "https://example.com/document.pdf"
    },
    vertex_project="your-project-id",
    vertex_location="us-central1"
)

이미지 문서

response = litellm.ocr(
    model="vertex_ai/mistral-ocr-2505",
    document={
        "type": "image_url",
        "image_url": "https://example.com/image.png"
    },
    vertex_project="your-project-id",
    vertex_location="us-central1"
)

Base64 인코딩 문서

import base64

# Read and encode PDF
with open("document.pdf", "rb") as f:
    pdf_base64 = base64.b64encode(f.read()).decode()

response = litellm.ocr(
    model="vertex_ai/mistral-ocr-2505", # This doesn't work for deepseek
    document={
        "type": "document_url",
        "document_url": f"data:application/pdf;base64,{pdf_base64}"
    },
    vertex_project="your-project-id",
    vertex_location="us-central1"
)

지원 파라미터

response = litellm.ocr(
    model="vertex_ai/mistral-ocr-2505",
    document={                           # Required: Document to process
        "type": "document_url",
        "document_url": "https://..."
    },
    vertex_project="your-project-id",   # Required: GCP project ID
    vertex_location="us-central1",       # Optional: Defaults to us-central1
    vertex_credentials="path/to/key.json", # Optional: Service account key
    include_image_base64=True,           # Optional: Include base64 images
    pages=[0, 1, 2],                     # Optional: Specific pages to process
    image_limit=10                       # Optional: Limit number of images
)

응답 형식

# Response has the following structure
response.pages          # List of pages with extracted text
response.model          # Model used
response.object         # "ocr"
response.usage_info     # Token usage information

# Access page content
for page in response.pages:
    print(f"Page {page.index}:")
    print(page.markdown)

비동기 지원

import litellm

response = await litellm.aocr(
    model="vertex_ai/mistral-ocr-2505",
    document={
        "type": "document_url",
        "document_url": "https://example.com/document.pdf"
    },
    vertex_project="your-project-id",
    vertex_location="us-central1"
)

비용 추적

LiteLLM은 Vertex AI OCR 비용을 자동으로 추적해요:

  • 페이지당 비용: $0.0005 (1,000페이지당 $1.50 기준)
response = litellm.ocr(
    model="vertex_ai/mistral-ocr-2505",
    document={"type": "document_url", "document_url": "https://..."},
    vertex_project="your-project-id"
)

# Access cost information
print(f"Cost: ${response._hidden_params.get('response_cost', 0)}")

중요 참고

URL 변환: Vertex AI Mistral OCR 엔드포인트는 인터넷 접근이 없어요. LiteLLM이 공개 URL을 Vertex AI로 보내기 전에 base64 data URI로 자동 변환해요.

리전 가용성: Mistral OCR은 여러 리전에서 사용 가능해요. vertex_location으로 데이터에 가까운 리전을 지정할 수 있어요:

  • us-central1 (기본값)
  • europe-west1
  • asia-southeast1

Deepseek OCR은 global 리전에서만 사용 가능해요.

지원 모델

  • mistral-ocr-2505 - Vertex AI의 최신 Mistral OCR 모델
  • deepseek-ocr-maas - Vertex AI의 최신 Deepseek OCR 모델

Vertex AI 제공사 접두사 vertex_ai/를 사용해요.

더 알아보기 (Learn more)

  • Vertex AI 공식 문서
  • Mistral OCR 문서