llm-connector
Minimal Rust library for LLM protocol abstraction.
Supports 4 protocols: OpenAI, Anthropic, Aliyun, Ollama. No complex configuration - just pick a protocol and start chatting.
🚨 Having Authentication Issues?
Test your API keys right now:
This will tell you exactly what's wrong with your API keys! See Debugging & Troubleshooting for more details.
✨ Key Features
- 4 Protocol Support: OpenAI, Anthropic, Aliyun, Ollama
- No Hardcoded Models: Use any model name without restrictions
- Online Model Discovery: Fetch available models dynamically from API
- Enhanced Streaming Support: Real-time streaming responses with proper Anthropic event handling
- Ollama Model Management: Full CRUD operations for local models
- Unified Interface: Same API for all protocols
- Type-Safe: Full Rust type safety with async/await
Quick Start
Installation
Add to your Cargo.toml:
[]
= "0.3.0"
= { = "1", = ["full"] }
Optional features:
= { = "0.2.3", = ["streaming"] }
Basic Usage
use ;
async
Supported Protocols
1. OpenAI Protocol
Standard OpenAI API format.
// OpenAI
let client = openai;
// OpenAI-compatible endpoints (if needed)
let client = openai_compatible;
Features:
- ✅ No hardcoded models - use any model name
- ✅ Online model discovery via
fetch_models() - ✅ Works with OpenAI-compatible providers (DeepSeek, Zhipu, Moonshot, etc.)
Example Models: gpt-4, gpt-4-turbo, gpt-3.5-turbo, o1-preview, o1-mini
2. Anthropic Protocol
Claude Messages API with separate system messages.
let client = anthropic;
Models: claude-3-5-sonnet-20241022, claude-3-opus, claude-3-haiku
3. Aliyun Protocol (DashScope)
Custom protocol for Qwen models.
let client = aliyun;
Models: qwen-turbo, qwen-plus, qwen-max
4. Ollama Protocol (Local)
Local LLM server with no API key required.
// Default: localhost:11434
let client = ollama;
// Custom URL
let client = ollama_at;
Models: llama3.2, llama3.1, mistral, mixtral, qwen2.5, etc.
Features:
- ✅ Model listing via
/api/tags - ✅ Model management (pull, push, delete, show details)
- ✅ Local server support with custom URLs
- ✅ Enhanced error handling for Ollama-specific operations
Ollama Model Management
The library now provides comprehensive Ollama model management capabilities:
let client = ollama;
// List all installed models
let models = client.list_ollama_models.await?;
for model in models
// Pull a new model
client.pull_ollama_model.await?;
// Get detailed model information
let details = client.show_ollama_model.await?;
println!;
// Delete a model
client.delete_ollama_model.await?;
Supported Ollama Operations
- List Models:
list_ollama_models()- Get all locally installed models - Pull Models:
pull_ollama_model(name)- Download models from registry - Push Models:
push_ollama_model(name)- Upload models to registry - Delete Models:
delete_ollama_model(name)- Remove local models - Show Details:
show_ollama_model(name)- Get comprehensive model information
Enhanced Streaming Support
The library now includes improved streaming support for Anthropic with proper event state management:
use StreamExt;
let client = anthropic;
let request = ChatRequest ;
let mut stream = client.chat_stream.await?;
while let Some = stream.next.await
Enhanced Anthropic Streaming Features
- State Management: Proper handling of
message_start,content_block_delta,message_delta,message_stopevents - Event Processing: Correct parsing of complex Anthropic streaming responses
- Usage Tracking: Real-time token usage statistics during streaming
- Error Resilience: Robust error handling for streaming interruptions
Model Discovery
Fetch the latest available models from the API:
let client = openai;
// Fetch models online from the API
let models = client.fetch_models.await?;
println!;
Supported by:
- ✅ OpenAI Protocol (including OpenAI-compatible providers like DeepSeek, Zhipu, Moonshot)
- ✅ Anthropic Protocol (limited support - returns fallback endpoint)
- ✅ Ollama Protocol (full support via
/api/tags) - ❌ Aliyun Protocol (not supported)
Example Results:
- DeepSeek:
["deepseek-chat", "deepseek-reasoner"] - Zhipu:
["glm-4.5", "glm-4.5-air", "glm-4.6"] - Moonshot:
["moonshot-v1-32k", "kimi-latest", ...]
Recommendation:
- Cache
fetch_models()results to avoid repeated API calls - For protocols that don't support model listing, you can use any model name directly in your requests
Request Examples
OpenAI / OpenAI-compatible
let request = ChatRequest ;
Anthropic (requires max_tokens)
let request = ChatRequest ;
Aliyun (DashScope)
let request = ChatRequest ;
Ollama (Local)
let request = ChatRequest ;
Streaming (Optional Feature)
Enable streaming in your Cargo.toml:
= { = "0.2.3", = ["streaming"] }
use StreamExt;
let mut stream = client.chat_stream.await?;
while let Some = stream.next.await
Error Handling
use LlmConnectorError;
match client.chat.await
Configuration
Simple API Key (Recommended)
let client = openai;
Environment Variables
use env;
let api_key = var?;
let client = openai;
Protocol Information
let client = openai;
// Get protocol name
println!;
// Fetch models online (requires API call)
let models = client.fetch_models.await?;
println!;
Debugging & Troubleshooting
Test Your API Keys
Quickly test if your API keys are valid:
# Test all keys from keys.yaml
# Debug DeepSeek specifically
The test tool will:
- ✅ Validate API key format
- ✅ Test authentication with the provider
- ✅ Show exactly what's wrong if a key fails
- ✅ Provide specific fix instructions
Troubleshooting Guides
TROUBLESHOOTING.md- Comprehensive troubleshooting guideHOW_TO_TEST_YOUR_KEYS.md- How to test your API keysTEST_YOUR_DEEPSEEK_KEY.md- Quick start for DeepSeek users
Common Issues
Authentication Error:
❌ Authentication failed: Incorrect API key provided
Solutions:
- Verify your API key is correct (no extra spaces)
- Check if your account has credits
- Generate a new API key from your provider's dashboard
- Run
cargo run --example test_keys_yamlto diagnose
Recent Changes
v0.3.0 (Latest)
🚀 Major New Features:
- Complete Ollama Model Management: Full CRUD operations for local models
list_ollama_models()- List all installed modelspull_ollama_model()- Download models from registrypush_ollama_model()- Upload models to registrydelete_ollama_model()- Remove local modelsshow_ollama_model()- Get detailed model information
- Enhanced Anthropic Streaming: Proper event state management
- Correct handling of
message_start,content_block_delta,message_delta,message_stopevents - Real-time token usage tracking during streaming
- Improved error resilience and state management
- Correct handling of
🔧 Improvements:
- Expanded Model Discovery Support:
- Added Ollama model listing via
/api/tagsendpoint - Limited Anthropic model discovery support
- Added Ollama model listing via
- Enhanced Client Interface: New methods for Ollama model management
- Updated Examples: Added comprehensive model management and streaming examples
📚 Documentation:
- Complete rewrite of Ollama section with model management examples
- Enhanced streaming documentation with code examples
- Updated feature descriptions and supported operations
v0.2.3
🔧 Breaking Changes:
- Removed
supported_models()method - Usefetch_models()instead - Removed
supports_model()method - No longer needed
✨ New Features:
- Improved error messages - Removed confusing OpenAI URLs for other providers
- New debugging tools:
examples/test_keys_yaml.rs- Test all API keysexamples/debug_deepseek.rs- Debug DeepSeek authentication
- Comprehensive documentation:
TROUBLESHOOTING.md- Troubleshooting guideHOW_TO_TEST_YOUR_KEYS.md- Testing instructionsTEST_YOUR_DEEPSEEK_KEY.md- Quick start guide
Migration from v0.2.2:
// ❌ Old (no longer works)
let models = client.supported_models;
// ✅ New
let models = client.fetch_models.await?;
v0.2.2
✨ New Features:
- Added
fetch_models()for online model discovery - OpenAI protocol supports dynamic model fetching from
/v1/modelsendpoint - Works with OpenAI-compatible providers (DeepSeek, Zhipu, Moonshot, etc.)
Design Philosophy
Minimal by Design:
- Only 4 protocols to cover all major LLM providers
- No hardcoded model restrictions - use any model name
- No complex configuration files or registries
- Direct API usage with clear abstractions
Protocol-first:
- Group providers by API protocol, not by company
- OpenAI-compatible providers share one implementation
- Extensible through protocol adapters
Examples
Check out the examples/ directory:
# Test your API keys from keys.yaml
# Debug DeepSeek authentication
# Test online model fetching
# Simple fetch_models() demo
# Ollama model management (NEW!)
# Anthropic streaming (NEW! - requires streaming feature)
# Test with your API keys
Example Descriptions
test_keys_yaml.rs ⭐ New!
- Tests all API keys from your
keys.yamlfile - Validates API key format and authentication
- Provides specific troubleshooting for each error
- Run this first if you have authentication issues!
debug_deepseek.rs ⭐ New!
- Interactive debugging tool for DeepSeek API
- Validates API key format
- Tests model fetching and chat requests
- Provides detailed troubleshooting guidance
test_fetch_models.rs
- Tests
fetch_models()with all providers fromkeys.yaml - Shows which providers support online model listing
- Displays available models for each provider
fetch_models_simple.rs
- Simple demonstration of
fetch_models() - Shows how to fetch models from OpenAI-compatible providers
- Includes usage recommendations
ollama_model_management.rs ⭐ New!
- Demonstrates complete Ollama model management functionality
- Shows how to list, pull, delete, and get model details
- Includes error handling and practical usage examples
anthropic_streaming.rs ⭐ New!
- Shows enhanced Anthropic streaming with proper event handling
- Demonstrates real-time response streaming and usage tracking
- Includes both regular and streaming chat examples
test_with_keys.rs
- Comprehensive test with real API keys
- Tests chat completion for all providers
- Verifies API connectivity and responses
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT