LLM Kit Azure
Azure OpenAI provider for LLM Kit - Complete integration with Azure OpenAI Service for chat, completions, embeddings, and image generation.
Note: This provider uses the standardized builder pattern. See the Quick Start section for the recommended usage.
Features
- Text Generation: Generate text using GPT-4, GPT-3.5-turbo, and other Azure OpenAI chat models
- Streaming: Stream responses in real-time for interactive applications
- Tool Calling: Support for function calling with Azure OpenAI models
- Text Embedding: Generate embeddings with text-embedding-ada-002 and other embedding models
- Image Generation: Create images using DALL-E 3 and other image generation models
- Multi-modal: Support for text and images in conversations
- Completion Models: Access to GPT-3.5-turbo-instruct and other completion models
- Azure-specific Authentication: Uses
api-keyheader for Azure authentication - Flexible URL Formats: Supports both v1 API and deployment-based URLs
- Multiple API Versions: Configure API version for different Azure OpenAI endpoints
Installation
Add this to your Cargo.toml:
[]
= "0.1"
= "0.1"
= "0.1"
= { = "1", = ["full"] }
Quick Start
Using the Client Builder (Recommended)
use AzureClient;
use LanguageModel;
async
Using Settings Directly (Alternative)
use ;
use LanguageModel;
async
Configuration
Environment Variables
Set your Azure OpenAI credentials as environment variables:
# Or use AZURE_BASE_URL
Using the Client Builder
use AzureClient;
// With resource name (most common)
let provider = new
.resource_name
.api_key
.api_version
.build;
// With custom base URL
let provider = new
.base_url
.api_key
.build;
// With custom headers
let provider = new
.resource_name
.api_key
.header
.build;
// With deployment-based URLs (legacy format)
let provider = new
.resource_name
.api_key
.use_deployment_based_urls
.build;
Using Settings Directly
use ;
let settings = new
.with_resource_name
.with_api_key
.with_api_version
.with_header;
let provider = new;
Builder Methods
The AzureClient builder supports:
.resource_name(name)- Set Azure OpenAI resource name.base_url(url)- Set custom base URL.api_key(key)- Set the API key.api_version(version)- Set API version (default: "v1").header(key, value)- Add a single custom header.headers(map)- Add multiple custom headers.use_deployment_based_urls(bool)- Use legacy deployment-based URL format.build()- Build the provider
Azure-Specific Features
URL Formats
Azure OpenAI supports two URL formats:
V1 API Format (Default)
https://{resource}.openai.azure.com/openai/v1{path}?api-version={version}
This is the recommended format.
Deployment-Based Format (Legacy)
https://{resource}.openai.azure.com/openai/deployments/{deployment}{path}?api-version={version}
Enable this format with .use_deployment_based_urls(true).
API Versions
Azure OpenAI supports multiple API versions. Common versions include:
"v1"- Default, recommended version"2023-05-15"- Stable release"2024-02-15-preview"- Preview features"2024-08-01-preview"- Latest preview
Set the API version using the builder:
let provider = new
.resource_name
.api_key
.api_version
.build;
Prerequisites
To use this provider, you need:
- An Azure subscription
- An Azure OpenAI resource
- Deployed models in your Azure OpenAI resource
- API key from Azure portal
Supported Models
Azure OpenAI supports various model types through deployments. You must first deploy models in your Azure OpenAI resource before using them.
Chat Models
GPT-4 and GPT-3.5 models for conversational AI:
gpt-4- Most capable modelgpt-4-turbo- Faster GPT-4 variantgpt-35-turbo- Fast and efficient- And other chat models available in your Azure deployment
Embedding Models
Text embedding models:
text-embedding-ada-002- Standard embedding modeltext-embedding-3-small- Smaller embedding modeltext-embedding-3-large- Larger embedding model
Image Models
Image generation models:
dall-e-3- Latest DALL-E modeldall-e-2- Previous generation
Completion Models
Text completion models:
gpt-35-turbo-instruct- Instruction-tuned completion model
Note: Model availability depends on your Azure OpenAI resource region and deployment. Use your deployment name (not the base model name) when creating models.
Usage Examples
Model Types
Chat Models
Use .chat_model() or .model() for conversational AI:
use AzureClient;
use ;
let provider = new
.resource_name
.api_key
.build;
// Use your deployment name
let model = provider.chat_model;
let result = new
.execute
.await?;
Completion Models
Use .completion_model() for text completion:
let model = provider.completion_model;
Embedding Models
Use .text_embedding_model() for embeddings:
let model = provider.text_embedding_model;
Image Models
Use .image_model() for image generation:
let model = provider.image_model;
Streaming
Stream responses for real-time output. See examples/stream.rs for a complete example.
Tool Calling
Azure OpenAI supports function calling for tool integration. See examples/chat_tool_calling.rs for a complete example.
Examples
See the examples/ directory for complete examples:
chat.rs- Basic chat completionstream.rs- Streaming responseschat_tool_calling.rs- Tool calling with chat modelsstream_tool_calling.rs- Streaming with tool callingtext_embedding.rs- Text embeddingsimage_generation.rs- Image generation with DALL-E
Run examples with:
Documentation
License
Licensed under Apache License, Version 2.0 (LICENSE)
Contributing
Contributions are welcome! Please see the Contributing Guide for more details.