GLM-OCR Quick Start: Complete Document Parsing Guide
Learn how to get started with GLM-OCR for document parsing. This guide covers API integration, Ollama local deployment, vLLM, and SGLang deployment options.
Overview
This guide will help you get started with GLM-OCR, covering multiple deployment options from cloud API to local deployment.
Prerequisites
Before you begin, ensure you have:
- An API key (for cloud API usage)
- Python 3.8+ (for SDK usage)
- Ollama installed (for local deployment)
- CUDA-compatible GPU (recommended for vLLM/SGLang)
Option 1: Cloud API
The fastest way to get started is using the cloud API.
cURL Example
curl --location --request POST 'https://api.z.ai/api/paas/v4/layout_parsing' \
--header 'Authorization: Bearer your-api-key' \
--header 'Content-Type: application/json' \
--data-raw '{
"model": "glm-ocr",
"file": "https://example.com/document.png"
}'Python Example
import requests
url = "https://api.z.ai/api/paas/v4/layout_parsing"
headers = {
"Authorization": "Bearer your-api-key",
"Content-Type": "application/json"
}
data = {
"model": "glm-ocr",
"file": "https://example.com/document.png"
}
response = requests.post(url, headers=headers, json=data)
print(response.json())Pricing
- $0.03 per million tokens (uniform for input and output)
- Approximately 1/10 the cost of traditional OCR solutions
- Process ~2000 A4 scanned images for $1
Option 2: Ollama Local Deployment
Ollama provides the simplest way to run GLM-OCR locally.
Installation
# Install Ollama (if not already installed)
curl -fsSL https://ollama.com/install.sh | sh
# Pull and run GLM-OCR
ollama run glm-ocrAvailable Models
| Model | Size | Context | Input |
|---|---|---|---|
| glm-ocr:latest | 2.2GB | 128K | Text, Image |
| glm-ocr:q8_0 | 1.6GB | 128K | Text, Image |
| glm-ocr:bf16 | 2.2GB | 128K | Text, Image |
Usage Examples
# Text Recognition
ollama run glm-ocr Text Recognition: ./document.png
# Table Recognition
ollama run glm-ocr Table Recognition: ./table.png
# Figure Recognition
ollama run glm-ocr Figure Recognition: ./chart.pngOption 3: vLLM Deployment
vLLM provides high-performance inference for production workloads.
Installation
pip install -U vllm --extra-index-url https://wheels.vllm.ai/nightly
pip install git+https://github.com/huggingface/transformers.gitStart Server
vllm serve zai-org/GLM-OCR --allowed-local-media-path / --port 8080API Call
from openai import OpenAI
client = OpenAI(
base_url="http://localhost:8080/v1",
api_key="not-needed"
)
response = client.chat.completions.create(
model="zai-org/GLM-OCR",
messages=[{
"role": "user",
"content": [
{"type": "image_url", "image_url": {"url": "file:///path/to/image.png"}},
{"type": "text", "text": "Text Recognition:"}
]
}]
)
print(response.choices[0].message.content)Option 4: SGLang Deployment
SGLang offers efficient serving with advanced batching.
Installation
pip install git+https://github.com/huggingface/transformers.git
pip install sglangStart Server
python -m sglang.launch_server --model zai-org/GLM-OCR --port 8080Option 5: Transformers Direct Usage
For full control over inference, use Transformers directly.
from transformers import AutoProcessor, AutoModelForImageTextToText
MODEL_PATH = "zai-org/GLM-OCR"
processor = AutoProcessor.from_pretrained(MODEL_PATH)
model = AutoModelForImageTextToText.from_pretrained(
MODEL_PATH,
torch_dtype="auto",
device_map="auto",
)
messages = [{
"role": "user",
"content": [
{"type": "image", "url": "test_image.png"},
{"type": "text", "text": "Text Recognition:"}
]
}]
inputs = processor.apply_chat_template(
messages,
tokenize=True,
add_generation_prompt=True,
return_dict=True,
return_tensors="pt"
).to(model.device)
generated_ids = model.generate(**inputs, max_new_tokens=8192)
output_text = processor.decode(
generated_ids[0][inputs["input_ids"].shape[1]:]
)
print(output_text)Task Types
GLM-OCR supports multiple recognition tasks:
| Task | Prompt | Description |
|---|---|---|
| Text Recognition | Text Recognition: | General text extraction |
| Table Recognition | Table Recognition: | Table structure parsing |
| Figure Recognition | Figure Recognition: | Chart/diagram analysis |
| Formula Recognition | Formula Recognition: | Math formula to LaTeX |
Performance Tips
- Batch Processing: Process multiple images in parallel for better throughput
- Image Quality: Higher resolution images yield better results
- GPU Memory: Use quantized models (q8_0) for limited GPU memory
- Context Length: GLM-OCR supports up to 128K context for long documents
Troubleshooting
Common Issues
Issue: Out of memory error
Solution: Use the quantized model glm-ocr:q8_0 or reduce batch size
Issue: Slow inference Solution: Ensure CUDA is properly configured and use vLLM for production
Issue: Poor recognition quality Solution: Ensure image resolution is sufficient and properly oriented
Next Steps
Author
Categories
More Posts
GLM-OCR Local Deployment: Ollama vs vLLM vs SGLang
Compare different local deployment options for GLM-OCR including Ollama, vLLM, and SGLang. Learn which option is best for your use case.
GLM-OCR: 0.9B Parameters Achieving OCR SOTA Performance
Introducing GLM-OCR, a lightweight professional OCR model with only 0.9B parameters that achieves state-of-the-art performance on OmniDocBench V1.5 with a score of 94.62.
Complex Table Recognition: How GLM-OCR Handles Merged Cells
Learn how GLM-OCR excels at recognizing complex tables with merged cells, multi-level headers, and diverse layouts commonly found in business documents.