LLMKitClient

Struct LLMKitClient 

Source
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

Source

pub fn builder() -> ClientBuilder

Create a new client builder.

Source

pub fn provider(&self, name: &str) -> Option<Arc<dyn Provider>>

Get a provider by name.

Source

pub fn default_provider(&self) -> Option<Arc<dyn Provider>>

Get the default provider.

Source

pub fn providers(&self) -> Vec<&str>

List all registered providers.

Source

pub async fn complete( &self, request: CompletionRequest, ) -> Result<CompletionResponse>

Make a completion request.

The provider is determined from:

  1. Explicit provider in model string (e.g., “anthropic/claude-sonnet-4-20250514”)
  2. Model name prefix inference (e.g., “claude-” -> anthropic, “gpt-” -> openai)
  3. Default provider as fallback
Source

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().

Source

pub async fn complete_with_provider( &self, provider_name: &str, request: CompletionRequest, ) -> Result<CompletionResponse>

Complete a request using a specific provider.

Source

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.

Source

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().

Source

pub async fn count_tokens_with_provider( &self, provider_name: &str, request: TokenCountRequest, ) -> Result<TokenCountResult>

Count tokens using a specific provider.

Source

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().

Source

pub async fn create_batch_with_provider( &self, provider_name: &str, requests: Vec<BatchRequest>, ) -> Result<BatchJob>

Create a batch using a specific provider.

Source

pub async fn get_batch( &self, provider_name: &str, batch_id: &str, ) -> Result<BatchJob>

Get the status of a batch job.

Source

pub async fn get_batch_results( &self, provider_name: &str, batch_id: &str, ) -> Result<Vec<BatchResult>>

Get the results of a completed batch.

Source

pub async fn cancel_batch( &self, provider_name: &str, batch_id: &str, ) -> Result<BatchJob>

Cancel a batch job.

Source

pub async fn list_batches( &self, provider_name: &str, limit: Option<u32>, ) -> Result<Vec<BatchJob>>

List recent batch jobs for a provider.

Source

pub async fn embed( &self, request: EmbeddingRequest, ) -> Result<EmbeddingResponse>

Generate embeddings for text.

The provider is determined from:

  1. Explicit provider in model string (e.g., “openai/text-embedding-3-small”)
  2. Model name prefix inference (e.g., “text-embedding-” -> openai, “voyage-” -> voyage)
  3. 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());
Source

pub async fn embed_with_provider( &self, provider_name: &str, request: EmbeddingRequest, ) -> Result<EmbeddingResponse>

Generate embeddings using a specific provider.

Source

pub fn embedding_providers(&self) -> Vec<&str>

List all registered embedding providers.

Source

pub fn supports_embeddings(&self, provider_name: &str) -> bool

Check if a provider supports embeddings.

Source

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")?;
Source

pub async fn speech_with_provider( &self, provider_name: &str, request: SpeechRequest, ) -> Result<SpeechResponse>

Generate speech using a specific provider.

Source

pub fn speech_providers(&self) -> Vec<&str>

List all registered speech providers.

Source

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);
Source

pub async fn transcribe_with_provider( &self, provider_name: &str, request: TranscriptionRequest, ) -> Result<TranscriptionResponse>

Transcribe audio using a specific provider.

Source

pub fn transcription_providers(&self) -> Vec<&str>

List all registered transcription providers.

Source

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());
Source

pub async fn generate_image_with_provider( &self, provider_name: &str, request: ImageGenerationRequest, ) -> Result<ImageGenerationResponse>

Generate images using a specific provider.

Source

pub fn image_providers(&self) -> Vec<&str>

List all registered image providers.

Source

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);
Source

pub async fn generate_video_with_provider( &self, provider_name: &str, request: VideoGenerationRequest, ) -> Result<VideoGenerationResponse>

Generate video using a specific provider.

Source

pub async fn get_video_status( &self, provider_name: &str, job_id: &str, ) -> Result<VideoJobStatus>

Get the status of a video generation job.

Source

pub fn video_providers(&self) -> Vec<&str>

List all registered video providers.

Source

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);
Source

pub async fn rank_with_provider( &self, provider_name: &str, request: RankingRequest, ) -> Result<RankingResponse>

Rank documents using a specific provider.

Source

pub fn ranking_providers(&self) -> Vec<&str>

List all registered ranking providers.

Source

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());
}
Source

pub async fn moderate_with_provider( &self, provider_name: &str, request: ModerationRequest, ) -> Result<ModerationResponse>

Moderate content using a specific provider.

Source

pub fn moderation_providers(&self) -> Vec<&str>

List all registered moderation providers.

Source

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);
Source

pub async fn classify_with_provider( &self, provider_name: &str, request: ClassificationRequest, ) -> Result<ClassificationResponse>

Classify text using a specific provider.

Source

pub fn classification_providers(&self) -> Vec<&str>

List all registered classification providers.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more