Token Classification (NER)
This cookbook walks through building, deploying, and using a Named Entity Recognition (NER) model with Encoderfile. We'll use BERT fine-tuned for NER to identify people, organizations, and locations in text.
What You'll Learn
Export a token classification model to ONNX
Build a self-contained encoderfile binary
Deploy as a REST API server
Make predictions via HTTP
Use CLI for batch processing
Prerequisites
encoderfileCLI tool installed (Installation Guide)Python with
optimum[exporters]for ONNX exportcurlfor testing the API
Step 1: Export the Model
We'll use dslim/bert-base-NER, a BERT model fine-tuned for named entity recognition.
Export to ONNX
Step 2: Create Configuration
Create a YAML configuration file for building the encoderfile.
Configuration Options
name- Model identifier used in API responsespath- Directory containing ONNX model filesmodel_type- Must betoken_classificationfor NERoutput_path- Where to save the binary (optional)transform- Optional Lua script for post-processing
Step 3: Build the Binary
Build your self-contained encoderfile binary:
Build Output
You should see output like:
The resulting binary is completely self-contained - it includes:
ONNX model weights
Tokenizer
Full inference runtime
REST and gRPC servers
Step 4: Start the Server
Launch the encoderfile server:
The server is now running with both HTTP and gRPC endpoints.
Step 5: Make Predictions
Now let's test the NER model with different types of text.
Example 1: Basic Entity Recognition
Entities Found:
Mozilla →
B-ORG,I-ORG(Organization)San Francisco →
B-LOC(Location)CA →
B-LOC(Location)
The B- prefix indicates the beginning of an entity, I- indicates inside/continuation, and O means outside any entity.
Example 2: Multiple Sentences
Sentence 1:
Yvon → Person (PER)
Patagonia → Organization (ORG)
Sentence 2:
Eiffel Tower → Miscellaneous (MISC)
Paris → Location (LOC)
France → Location (LOC)
Step 6: CLI Inference
For batch processing or one-off predictions, use the CLI directly:
Single Input
Batch Processing
This saves all results to results.json for further processing.
Advanced Usage
Custom Ports
HTTP Only (Disable gRPC)
Production Deployment
Understanding the Output
Token Classification Labels
The model uses the IOB (Inside-Outside-Beginning) tagging scheme:
B-
Beginning of entity
B-PER for "Barack" in "Barack Obama"
I-
Inside/continuation
I-PER for "Obama" in "Barack Obama"
O
Outside any entity
O for "is" or "the"
Entity Types
PER
Person names
"John Smith", "Marie Curie"
ORG
Organizations
"Apple Inc.", "United Nations"
LOC
Locations
"Paris", "California", "Mount Everest"
MISC
Miscellaneous
"iPhone", "Nobel Prize"
Response Format
Troubleshooting
Unexpected Entity Recognition
Model Limitations
The model may struggle with:
Rare or domain-specific entities
Ambiguous contexts (e.g., "Washington" as person vs. location)
Non-English text
Very long sequences (>512 tokens)
Solution: Fine-tune on domain-specific data or use a specialized model.
Performance Optimization
If inference is slow:
Server Connection Issues
Next Steps
Transforms Guide - Learn about custom post-processing with Lua scripts
Transforms Reference - Complete transforms API documentation
API Reference - REST, gRPC, and MCP endpoint specifications
CLI Reference - Full documentation for build, serve, and infer commands
Summary
You've learned to:
✅ Export a token classification model to ONNX
✅ Build a self-contained encoderfile binary
✅ Deploy as a REST API server
✅ Make predictions via HTTP and CLI
✅ Understand NER output format
The encoderfile you built is production-ready and can be deployed anywhere without dependencies!
Last updated