pub struct LLMKitClient { /* private fields */ }Expand description
Main client for accessing LLM providers.
§Model Format
Models must be specified using the "provider/model" format (e.g., "openai/gpt-4o").
This ensures the client routes the request to the correct provider.
§Example
use llmkit::LLMKitClient;
let client = LLMKitClient::builder()
.with_anthropic_from_env()
.with_openai_from_env()
.with_baidu(api_key, secret_key)?
.with_alibaba(api_key)?
.build()?;
// Use explicit provider/model format for any provider
let request = CompletionRequest::new("anthropic/claude-sonnet-4-20250514", messages);
let response = client.complete(request).await?;
// Regional providers (Phase 2.3)
let request = CompletionRequest::new("baidu/ERNIE-Bot-Ultra", messages);
let response = client.complete(request).await?;
let request = CompletionRequest::new("alibaba/qwen-max", messages);
let response = client.complete(request).await?;Implementations§
Source§impl LLMKitClient
impl LLMKitClient
Sourcepub fn builder() -> ClientBuilder
pub fn builder() -> ClientBuilder
Create a new client builder.
Sourcepub fn default_provider(&self) -> Option<Arc<dyn Provider>>
pub fn default_provider(&self) -> Option<Arc<dyn Provider>>
Get the default provider.
Sourcepub async fn complete(
&self,
request: CompletionRequest,
) -> Result<CompletionResponse>
pub async fn complete( &self, request: CompletionRequest, ) -> Result<CompletionResponse>
Make a completion request.
The provider is determined from:
- Explicit provider in model string (e.g., “anthropic/claude-sonnet-4-20250514”)
- Model name prefix inference (e.g., “claude-” -> anthropic, “gpt-” -> openai)
- Default provider as fallback
Sourcepub async fn complete_stream(
&self,
request: CompletionRequest,
) -> Result<Pin<Box<dyn Stream<Item = Result<StreamChunk>> + Send>>>
pub async fn complete_stream( &self, request: CompletionRequest, ) -> Result<Pin<Box<dyn Stream<Item = Result<StreamChunk>> + Send>>>
Make a streaming completion request.
The provider is determined using the same logic as complete().
Sourcepub async fn complete_with_provider(
&self,
provider_name: &str,
request: CompletionRequest,
) -> Result<CompletionResponse>
pub async fn complete_with_provider( &self, provider_name: &str, request: CompletionRequest, ) -> Result<CompletionResponse>
Complete a request using a specific provider.
Sourcepub async fn complete_stream_with_provider(
&self,
provider_name: &str,
request: CompletionRequest,
) -> Result<Pin<Box<dyn Stream<Item = Result<StreamChunk>> + Send>>>
pub async fn complete_stream_with_provider( &self, provider_name: &str, request: CompletionRequest, ) -> Result<Pin<Box<dyn Stream<Item = Result<StreamChunk>> + Send>>>
Stream a completion using a specific provider.
Sourcepub async fn count_tokens(
&self,
request: TokenCountRequest,
) -> Result<TokenCountResult>
pub async fn count_tokens( &self, request: TokenCountRequest, ) -> Result<TokenCountResult>
Count tokens in a request.
This allows estimation of token counts before making a completion request, useful for cost estimation and context window management.
Note: Not all providers support token counting. Use supports_token_counting
on the provider to check support.
The provider is determined using the same logic as complete().
Sourcepub async fn count_tokens_with_provider(
&self,
provider_name: &str,
request: TokenCountRequest,
) -> Result<TokenCountResult>
pub async fn count_tokens_with_provider( &self, provider_name: &str, request: TokenCountRequest, ) -> Result<TokenCountResult>
Count tokens using a specific provider.
Sourcepub async fn create_batch(
&self,
requests: Vec<BatchRequest>,
) -> Result<BatchJob>
pub async fn create_batch( &self, requests: Vec<BatchRequest>, ) -> Result<BatchJob>
Create a batch of requests for asynchronous processing.
Batch processing can be significantly cheaper (up to 50% on some providers) and is ideal for non-time-sensitive workloads.
The provider is determined from the first request’s model name using the
same logic as complete().
Sourcepub async fn create_batch_with_provider(
&self,
provider_name: &str,
requests: Vec<BatchRequest>,
) -> Result<BatchJob>
pub async fn create_batch_with_provider( &self, provider_name: &str, requests: Vec<BatchRequest>, ) -> Result<BatchJob>
Create a batch using a specific provider.
Sourcepub async fn get_batch(
&self,
provider_name: &str,
batch_id: &str,
) -> Result<BatchJob>
pub async fn get_batch( &self, provider_name: &str, batch_id: &str, ) -> Result<BatchJob>
Get the status of a batch job.
Sourcepub async fn get_batch_results(
&self,
provider_name: &str,
batch_id: &str,
) -> Result<Vec<BatchResult>>
pub async fn get_batch_results( &self, provider_name: &str, batch_id: &str, ) -> Result<Vec<BatchResult>>
Get the results of a completed batch.
Sourcepub async fn cancel_batch(
&self,
provider_name: &str,
batch_id: &str,
) -> Result<BatchJob>
pub async fn cancel_batch( &self, provider_name: &str, batch_id: &str, ) -> Result<BatchJob>
Cancel a batch job.
Sourcepub async fn list_batches(
&self,
provider_name: &str,
limit: Option<u32>,
) -> Result<Vec<BatchJob>>
pub async fn list_batches( &self, provider_name: &str, limit: Option<u32>, ) -> Result<Vec<BatchJob>>
List recent batch jobs for a provider.
Sourcepub async fn embed(
&self,
request: EmbeddingRequest,
) -> Result<EmbeddingResponse>
pub async fn embed( &self, request: EmbeddingRequest, ) -> Result<EmbeddingResponse>
Generate embeddings for text.
The provider is determined from:
- Explicit provider in model string (e.g., “openai/text-embedding-3-small”)
- Model name prefix inference (e.g., “text-embedding-” -> openai, “voyage-” -> voyage)
- First available embedding provider as fallback
§Example
use llmkit::{LLMKitClient, EmbeddingRequest};
let client = LLMKitClient::builder()
.with_openai_from_env()
.build()?;
// Explicit provider
let request = EmbeddingRequest::new("openai/text-embedding-3-small", "Hello, world!");
let response = client.embed(request).await?;
// Or with inference (backward compatible)
let request = EmbeddingRequest::new("text-embedding-3-small", "Hello, world!");
let response = client.embed(request).await?;
println!("Embedding dimensions: {}", response.dimensions());Sourcepub async fn embed_with_provider(
&self,
provider_name: &str,
request: EmbeddingRequest,
) -> Result<EmbeddingResponse>
pub async fn embed_with_provider( &self, provider_name: &str, request: EmbeddingRequest, ) -> Result<EmbeddingResponse>
Generate embeddings using a specific provider.
Sourcepub fn embedding_providers(&self) -> Vec<&str>
pub fn embedding_providers(&self) -> Vec<&str>
List all registered embedding providers.
Sourcepub fn supports_embeddings(&self, provider_name: &str) -> bool
pub fn supports_embeddings(&self, provider_name: &str) -> bool
Check if a provider supports embeddings.
Sourcepub async fn speech(&self, request: SpeechRequest) -> Result<SpeechResponse>
pub async fn speech(&self, request: SpeechRequest) -> Result<SpeechResponse>
Generate speech from text.
The provider is determined from the model string in “provider/model” format.
§Example
use llmkit::{LLMKitClient, SpeechRequest};
let client = LLMKitClient::builder()
.with_elevenlabs_from_env()
.build()?;
let request = SpeechRequest::new("elevenlabs/eleven_monolingual_v1", "Hello, world!", "voice_id");
let response = client.speech(request).await?;
response.save("output.mp3")?;Sourcepub async fn speech_with_provider(
&self,
provider_name: &str,
request: SpeechRequest,
) -> Result<SpeechResponse>
pub async fn speech_with_provider( &self, provider_name: &str, request: SpeechRequest, ) -> Result<SpeechResponse>
Generate speech using a specific provider.
Sourcepub fn speech_providers(&self) -> Vec<&str>
pub fn speech_providers(&self) -> Vec<&str>
List all registered speech providers.
Sourcepub async fn transcribe(
&self,
request: TranscriptionRequest,
) -> Result<TranscriptionResponse>
pub async fn transcribe( &self, request: TranscriptionRequest, ) -> Result<TranscriptionResponse>
Transcribe audio to text.
The provider is determined from the model string in “provider/model” format.
§Example
use llmkit::{LLMKitClient, TranscriptionRequest, AudioInput};
let client = LLMKitClient::builder()
.with_deepgram_from_env()
.build()?;
let request = TranscriptionRequest::new(
"deepgram/nova-2",
AudioInput::file("audio.mp3"),
);
let response = client.transcribe(request).await?;
println!("Text: {}", response.text);Sourcepub async fn transcribe_with_provider(
&self,
provider_name: &str,
request: TranscriptionRequest,
) -> Result<TranscriptionResponse>
pub async fn transcribe_with_provider( &self, provider_name: &str, request: TranscriptionRequest, ) -> Result<TranscriptionResponse>
Transcribe audio using a specific provider.
Sourcepub fn transcription_providers(&self) -> Vec<&str>
pub fn transcription_providers(&self) -> Vec<&str>
List all registered transcription providers.
Sourcepub async fn generate_image(
&self,
request: ImageGenerationRequest,
) -> Result<ImageGenerationResponse>
pub async fn generate_image( &self, request: ImageGenerationRequest, ) -> Result<ImageGenerationResponse>
Generate images from a text prompt.
The provider is determined from the model string in “provider/model” format.
§Example
use llmkit::{LLMKitClient, ImageGenerationRequest};
let client = LLMKitClient::builder()
.with_stability_from_env()
.build()?;
let request = ImageGenerationRequest::new(
"stability/stable-diffusion-xl",
"A serene mountain landscape at sunset",
);
let response = client.generate_image(request).await?;
println!("Generated {} images", response.images.len());Sourcepub async fn generate_image_with_provider(
&self,
provider_name: &str,
request: ImageGenerationRequest,
) -> Result<ImageGenerationResponse>
pub async fn generate_image_with_provider( &self, provider_name: &str, request: ImageGenerationRequest, ) -> Result<ImageGenerationResponse>
Generate images using a specific provider.
Sourcepub fn image_providers(&self) -> Vec<&str>
pub fn image_providers(&self) -> Vec<&str>
List all registered image providers.
Sourcepub async fn generate_video(
&self,
request: VideoGenerationRequest,
) -> Result<VideoGenerationResponse>
pub async fn generate_video( &self, request: VideoGenerationRequest, ) -> Result<VideoGenerationResponse>
Start a video generation job.
Video generation is asynchronous - use get_video_status to poll for completion.
§Example
use llmkit::{LLMKitClient, VideoGenerationRequest};
let client = LLMKitClient::builder()
.with_runway_from_env()
.build()?;
let request = VideoGenerationRequest::new(
"runway/gen-3",
"A cat playing with a ball",
).with_duration(6);
let job = client.generate_video(request).await?;
println!("Job ID: {}", job.job_id);Sourcepub async fn generate_video_with_provider(
&self,
provider_name: &str,
request: VideoGenerationRequest,
) -> Result<VideoGenerationResponse>
pub async fn generate_video_with_provider( &self, provider_name: &str, request: VideoGenerationRequest, ) -> Result<VideoGenerationResponse>
Generate video using a specific provider.
Sourcepub async fn get_video_status(
&self,
provider_name: &str,
job_id: &str,
) -> Result<VideoJobStatus>
pub async fn get_video_status( &self, provider_name: &str, job_id: &str, ) -> Result<VideoJobStatus>
Get the status of a video generation job.
Sourcepub fn video_providers(&self) -> Vec<&str>
pub fn video_providers(&self) -> Vec<&str>
List all registered video providers.
Sourcepub async fn rank(&self, request: RankingRequest) -> Result<RankingResponse>
pub async fn rank(&self, request: RankingRequest) -> Result<RankingResponse>
Rank documents by relevance to a query.
§Example
use llmkit::{LLMKitClient, RankingRequest};
let client = LLMKitClient::builder()
.with_cohere_from_env()
.build()?;
let request = RankingRequest::new(
"cohere/rerank-english-v3.0",
"What is the capital of France?",
vec!["Paris is the capital", "Berlin is a city"],
);
let response = client.rank(request).await?;
println!("Top result: index {} with score {}", response.results[0].index, response.results[0].score);Sourcepub async fn rank_with_provider(
&self,
provider_name: &str,
request: RankingRequest,
) -> Result<RankingResponse>
pub async fn rank_with_provider( &self, provider_name: &str, request: RankingRequest, ) -> Result<RankingResponse>
Rank documents using a specific provider.
Sourcepub fn ranking_providers(&self) -> Vec<&str>
pub fn ranking_providers(&self) -> Vec<&str>
List all registered ranking providers.
Sourcepub async fn moderate(
&self,
request: ModerationRequest,
) -> Result<ModerationResponse>
pub async fn moderate( &self, request: ModerationRequest, ) -> Result<ModerationResponse>
Check content for policy violations.
§Example
use llmkit::{LLMKitClient, ModerationRequest};
let client = LLMKitClient::builder()
.with_openai_from_env()
.build()?;
let request = ModerationRequest::new(
"openai/omni-moderation-latest",
"Some user content to check",
);
let response = client.moderate(request).await?;
if response.flagged {
println!("Content was flagged for: {:?}", response.flagged_categories());
}Sourcepub async fn moderate_with_provider(
&self,
provider_name: &str,
request: ModerationRequest,
) -> Result<ModerationResponse>
pub async fn moderate_with_provider( &self, provider_name: &str, request: ModerationRequest, ) -> Result<ModerationResponse>
Moderate content using a specific provider.
Sourcepub fn moderation_providers(&self) -> Vec<&str>
pub fn moderation_providers(&self) -> Vec<&str>
List all registered moderation providers.
Sourcepub async fn classify(
&self,
request: ClassificationRequest,
) -> Result<ClassificationResponse>
pub async fn classify( &self, request: ClassificationRequest, ) -> Result<ClassificationResponse>
Classify text into one or more labels.
§Example
use llmkit::{LLMKitClient, ClassificationRequest};
let client = LLMKitClient::builder()
.with_cohere_from_env()
.build()?;
let request = ClassificationRequest::new(
"cohere/embed-english-v3.0",
"I love this product!",
vec!["positive", "negative", "neutral"],
);
let response = client.classify(request).await?;
println!("Predicted: {} ({:.2}%)", response.label().unwrap(), response.top().unwrap().score * 100.0);Sourcepub async fn classify_with_provider(
&self,
provider_name: &str,
request: ClassificationRequest,
) -> Result<ClassificationResponse>
pub async fn classify_with_provider( &self, provider_name: &str, request: ClassificationRequest, ) -> Result<ClassificationResponse>
Classify text using a specific provider.
Sourcepub fn classification_providers(&self) -> Vec<&str>
pub fn classification_providers(&self) -> Vec<&str>
List all registered classification providers.