Hugging Face Text Generation Inference (TGI) & vLLM Setup
Overview
Run Mistral and other Hugging Face models locally with an OpenAI-compatible API. This allows OpenRegister's LLPhant library and other OpenAI clients to use local models instead of cloud APIs.
Why Use TGI or vLLM?
Benefits
- ✅ OpenAI-Compatible API - Drop-in replacement for OpenAI
- ✅ Privacy-First - All data stays local
- ✅ Cost-Free - No API fees
- ✅ Fast - Optimized inference engines
- ✅ Flexible - Choose any Hugging Face model
Comparison
| Feature | TGI | vLLM | Ollama |
|---|---|---|---|
| OpenAI API | ✅ Yes (v1.4.0+) | ✅ Yes | ❌ No |
| Speed | ⚡⚡ Fast | ⚡⚡⚡ Very Fast | ⚡ Good |
| Models | Hugging Face | Hugging Face | Curated list |
| Setup | Medium | Medium | Easy |
| Memory | 8-16GB | 8-16GB | 8-16GB |
| Use Case | Production | High throughput | Simple setup |
Two Options
Option 1: Text Generation Inference (TGI) - Recommended
Pros:
- Official Hugging Face solution
- Well-maintained and documented
- Optimized for production
- Automatic quantization
Installation:
# Use the huggingface profile in docker-compose.dev.yml
docker-compose -f docker-compose.dev.yml --profile huggingface up -d tgi-mistral
Option 2: vLLM - Alternative
Pros:
- Faster inference
- Better throughput for multiple requests
- Full OpenAI API compatibility
- PagedAttention optimization
Installation:
# Use the huggingface profile in docker-compose.dev.yml
docker-compose -f docker-compose.dev.yml --profile huggingface up -d vllm-mistral
Quick Start
1. Start TGI with Mistral
cd /path/to/openregister
# Start TGI (choose ONE) - using huggingface profile
docker-compose -f docker-compose.dev.yml --profile huggingface up -d tgi-mistral
# OR start vLLM (if configured)
docker-compose -f docker-compose.dev.yml --profile huggingface up -d vllm-mistral
2. Wait for Model Download
First startup downloads the model (~15GB for Mistral 7B):
# Watch logs
docker logs -f openregister-tgi-mistral
# You'll see:
# Downloading model...
# Model loaded successfully
# Serving on port 80
Time: 5-10 minutes depending on internet speed
3. Test the API
TGI (port 8081):
curl http://localhost:8081/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "mistral-7b-instruct",
"messages": [
{"role": "user", "content": "Hello! What is the capital of France?"}
],
"max_tokens": 100
}'
vLLM (port 8082):
curl http://localhost:8082/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "mistral-7b-instruct",
"messages": [
{"role": "user", "content": "Hello! What is the capital of France?"}
],
"max_tokens": 100
}'
Integration with OpenRegister
Using with LLPhant (PHP)
LLPhant's OpenAI client can point to TGI/vLLM:
use LLPhant\Chat\OpenAIChat;
use OpenAI\Client;
// Create OpenAI client pointing to TGI/vLLM
$client = Client::factory()
->withBaseUri('http://tgi-mistral:80') // or http://vllm-mistral:8000
->withHttpHeader('Content-Type', 'application/json')
->make();
// Use with LLPhant
$chat = new OpenAIChat($client);
$response = $chat->generateText('What is the capital of France?');
Configuration in OpenRegister
PHP Configuration (config/llm_config.php):
return [
'llm' => [
'provider' => 'openai', // Use OpenAI-compatible client
'base_url' => 'http://tgi-mistral:80', // Local TGI
// OR
'base_url' => 'http://vllm-mistral:8000', // Local vLLM
'model' => 'mistral-7b-instruct',
'api_key' => 'dummy', // Not used for local, but required by some clients
'timeout' => 30,
'max_tokens' => 4096
]
];
Direct API Calls (without LLPhant)
// Simple curl-based API call
function callLocalLLM(string $prompt, string $baseUrl = 'http://tgi-mistral:80'): string
{
$data = [
'model' => 'mistral-7b-instruct',
'messages' => [
['role' => 'user', 'content' => $prompt]
],
'max_tokens' => 1000,
'temperature' => 0.7
];
$ch = curl_init($baseUrl . '/v1/chat/completions');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response, true);
return $result['choices'][0]['message']['content'] ?? '';
}
// Usage
$answer = callLocalLLM('Explain GDPR in simple terms');
echo $answer;
Available Models
Recommended Models for OpenRegister
| Model | Size | Use Case | Memory Required |
|---|---|---|---|
| Mistral-7B-Instruct-v0.2 | 7B | General purpose, RAG | 16GB |
| Mixtral-8x7B-Instruct | 47B | High quality, complex | 48GB+ |
| Llama-3-8B-Instruct | 8B | General purpose | 16GB |
| Phi-3-mini-instruct | 3.8B | Fast, lightweight | 8GB |
| Qwen2-7B-Instruct | 7B | Multilingual, code | 16GB |
Changing the Model
Edit docker-compose.dev.yml:
For TGI:
tgi-mistral:
environment:
# Change this line
- MODEL_ID=mistralai/Mixtral-8x7B-Instruct-v0.1
For vLLM (if configured):
vllm-mistral:
command:
- --model
- mistralai/Mixtral-8x7B-Instruct-v0.1 # Change this
Then restart:
docker-compose -f docker-compose.dev.yml --profile huggingface up -d --force-recreate tgi-mistral
OpenAI API Compatibility
Supported Endpoints
TGI (v1.4.0+):
- ✅
/v1/chat/completions- Chat completions - ✅
/v1/completions- Text completions - ✅
/health- Health check - ✅
/info- Model info
vLLM:
- ✅
/v1/chat/completions- Chat completions - ✅
/v1/completions- Text completions - ✅
/v1/models- List models - ✅
/health- Health check - ✅
/metrics- Prometheus metrics
Compatibility Notes
Works with: