pub struct LlmClient { /* private fields */ }Expand description
Unified LLM client.
This client provides:
- Unified interface for all LLM operations
- Automatic retry with exponential backoff
- Rate limiting and concurrency control
- JSON response parsing
- Error classification
- Graceful fallback on errors
§Example
use vectorless::llm::{LlmClient, LlmConfig};
let config = LlmConfig::new("gpt-4o-mini");
let client = LlmClient::new(config);
// Simple completion
let response = client.complete("You are helpful.", "Hello!").await?;
println!("Response: {}", response);
// JSON completion
#[derive(serde::Deserialize)]
struct Answer {
answer: String,
}
let answer: Answer = client.complete_json(
"You answer questions in JSON.",
"What is 2+2?"
).await?;Implementations§
Source§impl LlmClient
impl LlmClient
Sourcepub fn with_defaults() -> Self
pub fn with_defaults() -> Self
Create a client with default configuration.
Sourcepub fn with_concurrency(self, controller: ConcurrencyController) -> Self
pub fn with_concurrency(self, controller: ConcurrencyController) -> Self
Add concurrency control to the client.
§Example
use vectorless::llm::LlmClient;
use vectorless::throttle::{ConcurrencyController, ConcurrencyConfig};
let config = ConcurrencyConfig::new()
.with_max_concurrent_requests(10)
.with_requests_per_minute(500);
let client = LlmClient::for_model("gpt-4o-mini")
.with_concurrency(ConcurrencyController::new(config));Add concurrency control from an existing Arc.
Sourcepub fn with_fallback(self, chain: FallbackChain) -> Self
pub fn with_fallback(self, chain: FallbackChain) -> Self
Add fallback chain for error recovery.
§Example
use vectorless::llm::{LlmClient, FallbackChain, FallbackConfig};
let fallback = FallbackConfig::default();
let client = LlmClient::for_model("gpt-4o")
.with_fallback(FallbackChain::new(fallback));
assert!(client.fallback().is_some());Add fallback chain from an existing Arc.
Sourcepub fn concurrency(&self) -> Option<&ConcurrencyController>
pub fn concurrency(&self) -> Option<&ConcurrencyController>
Get the concurrency controller (if any).
Sourcepub fn fallback(&self) -> Option<&FallbackChain>
pub fn fallback(&self) -> Option<&FallbackChain>
Get the fallback chain (if any).
Sourcepub async fn complete(&self, system: &str, user: &str) -> LlmResult<String>
pub async fn complete(&self, system: &str, user: &str) -> LlmResult<String>
Complete a prompt with system and user messages.
This method includes:
- Automatic rate limiting (if configured)
- Automatic retry with exponential backoff
Sourcepub async fn complete_with_max_tokens(
&self,
system: &str,
user: &str,
max_tokens: u16,
) -> LlmResult<String>
pub async fn complete_with_max_tokens( &self, system: &str, user: &str, max_tokens: u16, ) -> LlmResult<String>
Complete a prompt with custom max tokens.
Sourcepub async fn complete_json<T: DeserializeOwned>(
&self,
system: &str,
user: &str,
) -> LlmResult<T>
pub async fn complete_json<T: DeserializeOwned>( &self, system: &str, user: &str, ) -> LlmResult<T>
Complete a prompt and parse the response as JSON.
This method handles:
- JSON extraction from markdown code blocks
- Bracket matching for nested JSON
§Example
#[derive(serde::Deserialize)]
struct TocEntry {
title: String,
page: usize,
}
let client = LlmClient::for_model("gpt-4o-mini");
let entries: Vec<TocEntry> = client.complete_json(
"Extract TOC entries as JSON array.",
"Chapter 1: Introduction ... 5"
).await?;Sourcepub async fn complete_json_with_max_tokens<T: DeserializeOwned>(
&self,
system: &str,
user: &str,
max_tokens: u16,
) -> LlmResult<T>
pub async fn complete_json_with_max_tokens<T: DeserializeOwned>( &self, system: &str, user: &str, max_tokens: u16, ) -> LlmResult<T>
Complete a prompt and parse the response as JSON with custom max tokens.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for LlmClient
impl !RefUnwindSafe for LlmClient
impl Send for LlmClient
impl Sync for LlmClient
impl Unpin for LlmClient
impl UnsafeUnpin for LlmClient
impl !UnwindSafe for LlmClient
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more