LLMBuilder

Struct LLMBuilder 

Source
pub struct LLMBuilder { /* private fields */ }
Expand description

re-export the llm types so downstream code can use the same structs/enums. Builder for configuring and instantiating LLM providers.

Provides a fluent interface for setting various configuration options like model selection, API keys, generation parameters, etc.

Implementations§

Source§

impl LLMBuilder

Source

pub fn new() -> LLMBuilder

Creates a new empty builder instance with default values.

Source

pub fn backend(self, backend: LLMBackend) -> LLMBuilder

Sets the backend provider to use.

Source

pub fn api_key(self, key: impl Into<String>) -> LLMBuilder

Sets the API key for authentication.

Source

pub fn base_url(self, url: impl Into<String>) -> LLMBuilder

Sets the base URL for API requests.

Source

pub fn model(self, model: impl Into<String>) -> LLMBuilder

Sets the model identifier to use.

Source

pub fn max_tokens(self, max_tokens: u32) -> LLMBuilder

Sets the maximum number of tokens to generate.

Source

pub fn temperature(self, temperature: f32) -> LLMBuilder

Sets the temperature for controlling response randomness (0.0-1.0).

Source

pub fn system(self, system: impl Into<String>) -> LLMBuilder

Sets the system prompt/context.

Source

pub fn reasoning_effort(self, reasoning_effort: ReasoningEffort) -> LLMBuilder

Sets the reasoning flag.

Source

pub fn reasoning(self, reasoning: bool) -> LLMBuilder

Sets the reasoning flag.

Source

pub fn reasoning_budget_tokens(self, reasoning_budget_tokens: u32) -> LLMBuilder

Sets the reasoning budget tokens.

Source

pub fn timeout_seconds(self, timeout_seconds: u64) -> LLMBuilder

Sets the request timeout in seconds.

Source

pub fn stream(self, _stream: bool) -> LLMBuilder

👎Deprecated: This method is deprecated and will be removed in a future release. Streaming is defined by the function used (e.g. chat_stream)

Enables or disables streaming responses, stub kept for compatibility.

§Deprecation

This method is deprecated and will be removed in a future release. Streaming is defined by the function called (e.g. chat_stream).

Source

pub fn top_p(self, top_p: f32) -> LLMBuilder

Sets the top-p (nucleus) sampling parameter.

Source

pub fn top_k(self, top_k: u32) -> LLMBuilder

Sets the top-k sampling parameter.

Source

pub fn embedding_encoding_format( self, embedding_encoding_format: impl Into<String>, ) -> LLMBuilder

Sets the encoding format for embeddings.

Source

pub fn embedding_dimensions(self, embedding_dimensions: u32) -> LLMBuilder

Sets the dimensions for embeddings.

Source

pub fn schema(self, schema: impl Into<StructuredOutputFormat>) -> LLMBuilder

Sets the JSON schema for structured output.

Source

pub fn validator<F>(self, f: F) -> LLMBuilder
where F: Fn(&str) -> Result<(), String> + Send + Sync + 'static,

Sets a validation function to verify LLM responses.

§Arguments
  • f - Function that takes a response string and returns Ok(()) if valid, or Err with error message if invalid
Source

pub fn validator_attempts(self, attempts: usize) -> LLMBuilder

Sets the number of retry attempts for validation failures.

§Arguments
  • attempts - Maximum number of times to retry generating a valid response
Source

pub fn function(self, function_builder: FunctionBuilder) -> LLMBuilder

Adds a function tool to the builder

Source

pub fn enable_parallel_tool_use(self, enable: bool) -> LLMBuilder

Enable parallel tool use

Source

pub fn tool_choice(self, choice: ToolChoice) -> LLMBuilder

Set tool choice. Note that if the choice is given as Tool(name), and that tool isn’t available, the builder will fail.

Source

pub fn disable_tools(self) -> LLMBuilder

Explicitly disable the use of tools, even if they are provided.

Source

pub fn api_version(self, api_version: impl Into<String>) -> LLMBuilder

Set the API version.

Source

pub fn deployment_id(self, deployment_id: impl Into<String>) -> LLMBuilder

Set the deployment id. Used in Azure OpenAI.

Source

pub fn voice(self, voice: impl Into<String>) -> LLMBuilder

Set the voice.

Enable web search

Source

pub fn openai_web_search_context_size( self, context_size: impl Into<String>, ) -> LLMBuilder

Set the web search context

Source

pub fn openai_web_search_user_location_type( self, location_type: impl Into<String>, ) -> LLMBuilder

Set the web search user location type

Source

pub fn openai_web_search_user_location_approximate_country( self, country: impl Into<String>, ) -> LLMBuilder

Set the web search user location approximate country

Source

pub fn openai_web_search_user_location_approximate_city( self, city: impl Into<String>, ) -> LLMBuilder

Set the web search user location approximate city

Source

pub fn openai_web_search_user_location_approximate_region( self, region: impl Into<String>, ) -> LLMBuilder

Set the web search user location approximate region

Source

pub fn resilient(self, enable: bool) -> LLMBuilder

Enables or disables the resilience wrapper (retry/backoff).

Source

pub fn resilient_attempts(self, attempts: usize) -> LLMBuilder

Sets the maximum number of attempts for resilience (including the first try).

Source

pub fn resilient_backoff( self, base_delay_ms: u64, max_delay_ms: u64, ) -> LLMBuilder

Sets the backoff bounds in milliseconds for resilience.

Source

pub fn resilient_jitter(self, jitter: bool) -> LLMBuilder

Enables or disables jitter for backoff delays.

Source

pub fn search_mode(self, mode: impl Into<String>) -> LLMBuilder

👎Deprecated: Renamed to xai_search_mode.
Source

pub fn xai_search_mode(self, mode: impl Into<String>) -> LLMBuilder

Sets the search mode for search-enabled providers.

Source

pub fn xai_search_source( self, source_type: impl Into<String>, excluded_websites: Option<Vec<String>>, ) -> LLMBuilder

Adds a search source with optional excluded websites.

Source

pub fn xai_max_search_results(self, max: u32) -> LLMBuilder

Sets the maximum number of search results.

Source

pub fn xai_search_date_range( self, from: impl Into<String>, to: impl Into<String>, ) -> LLMBuilder

Sets the date range for search results.

Source

pub fn xai_search_from_date(self, date: impl Into<String>) -> LLMBuilder

Sets the start date for search results (format: “YYYY-MM-DD”).

Source

pub fn xai_search_to_date(self, date: impl Into<String>) -> LLMBuilder

Sets the end date for search results (format: “YYYY-MM-DD”).

Source

pub fn memory(self, memory: impl MemoryProvider + 'static) -> LLMBuilder

Sets a custom memory provider for storing conversation history.

§Arguments
  • memory - A boxed memory provider implementing the MemoryProvider trait
§Examples
use llm::builder::{LLMBuilder, LLMBackend};
use llm::memory::SlidingWindowMemory;

let memory = SlidingWindowMemory::new(10);
let builder = LLMBuilder::new()
    .backend(LLMBackend::OpenAI)
    .memory(memory);
Source

pub fn sliding_memory(self, memory: SlidingWindowMemory) -> LLMBuilder

Sets a sliding window memory instance directly (convenience method).

§Arguments
  • memory - A SlidingWindowMemory instance
§Examples
use llm::builder::{LLMBuilder, LLMBackend};
use llm::memory::SlidingWindowMemory;

let memory = SlidingWindowMemory::new(10);
let builder = LLMBuilder::new()
    .backend(LLMBackend::OpenAI)
    .sliding_memory(memory);
Source

pub fn sliding_window_memory(self, window_size: usize) -> LLMBuilder

Sets up a sliding window memory with the specified window size.

This is a convenience method for creating a SlidingWindowMemory instance.

§Arguments
  • window_size - Maximum number of messages to keep in memory
§Examples
use llm::builder::{LLMBuilder, LLMBackend};

let builder = LLMBuilder::new()
    .backend(LLMBackend::OpenAI)
    .sliding_window_memory(5); // Keep last 5 messages
Source

pub fn sliding_window_with_strategy( self, window_size: usize, strategy: TrimStrategy, ) -> LLMBuilder

Sets up a sliding window memory with specified trim strategy.

§Arguments
  • window_size - Maximum number of messages to keep in memory
  • strategy - How to handle overflow when window is full
§Examples
use llm::builder::{LLMBuilder, LLMBackend};
use llm::memory::TrimStrategy;

let builder = LLMBuilder::new()
    .backend(LLMBackend::OpenAI)
    .sliding_window_with_strategy(5, TrimStrategy::Summarize);
Source

pub fn build(self) -> Result<Box<dyn LLMProvider>, LLMError>

Builds and returns a configured LLM provider instance.

§Errors

Returns an error if:

  • No backend is specified
  • Required backend feature is not enabled
  • Required configuration like API keys are missing

Trait Implementations§

Source§

impl Default for LLMBuilder

Source§

fn default() -> LLMBuilder

Returns the “default value” for a type. Read more

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, U> AsBindGroupShaderType<U> for T
where U: ShaderType, &'a T: for<'a> Into<U>,

Source§

fn as_bind_group_shader_type(&self, _images: &RenderAssets<GpuImage>) -> U

Return the T ShaderType for self. When used in AsBindGroup derives, it is safe to assume that all images in self exist.
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> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Converts Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Converts &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Converts &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSend for T
where T: Any + Send,

Source§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromWorld for T
where T: Default,

Source§

fn from_world(_world: &mut World) -> T

Creates Self using default().

Source§

impl<T, W> HasTypeWitness<W> for T
where W: MakeTypeWitness<Arg = T>, T: ?Sized,

Source§

const WITNESS: W = W::MAKE

A constant of the type witness
Source§

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

Source§

const TYPE_EQ: TypeEq<T, <T as Identity>::Type> = TypeEq::NEW

Proof that Self is the same type as Self::Type, provides methods for casting between Self and Self::Type.
Source§

type Type = T

The same type as Self, used to emulate type equality bounds (T == U) with associated type equality constraints (T: Identity<Type = U>).
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> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
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, 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> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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
Source§

impl<T> ConditionalSend for T
where T: Send,

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> Settings for T
where T: 'static + Send + Sync,

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,