Skip to main content

LanguageModelRequestBuilder

Struct LanguageModelRequestBuilder 

Source
pub struct LanguageModelRequestBuilder<M: LanguageModel, State = ModelStage> { /* private fields */ }
Expand description

A type-state builder for constructing LanguageModelRequest instances.

This builder uses phantom types to enforce a specific construction order, ensuring that required fields (like the model) are set before optional ones.

§Type Parameters

  • M - The language model type.
  • State - The current builder state, determining available methods.

Implementations§

Source§

impl<M: LanguageModel> LanguageModelRequestBuilder<M, ModelStage>

Methods available in the ModelStage state.

Source

pub fn model(self, model: M) -> LanguageModelRequestBuilder<M, SystemStage>

Sets the language model for the request.

This is the first required step in building a request.

§Parameters
  • model - The language model instance to use.
§Returns

The builder in the SystemStage state.

Source§

impl<M: LanguageModel> LanguageModelRequestBuilder<M, SystemStage>

Methods available in the SystemStage state.

Source

pub fn system( self, system: impl Into<String>, ) -> LanguageModelRequestBuilder<M, ConversationStage>

Sets an optional system prompt for the request.

The system prompt provides context or instructions to the model.

§Parameters
  • system - The system prompt text.
§Returns

The builder in the ConversationStage state.

Source

pub fn prompt( self, prompt: impl Into<String>, ) -> LanguageModelRequestBuilder<M, OptionsStage>

Sets a simple text prompt for the request.

This skips the system prompt and goes directly to options.

§Parameters
  • prompt - The user prompt text.
§Returns

The builder in the OptionsStage state.

Source

pub fn messages( self, messages: Messages, ) -> LanguageModelRequestBuilder<M, OptionsStage>

Sets conversation messages for the request.

This allows for multi-turn conversations with the model.

§Parameters
  • messages - Messages instances representing the conversation.
§Returns

The builder in the OptionsStage state.

Source§

impl<M: LanguageModel> LanguageModelRequestBuilder<M, ConversationStage>

Methods available in the ConversationStage state.

Source

pub fn prompt( self, prompt: impl Into<String>, ) -> LanguageModelRequestBuilder<M, OptionsStage>

Sets a simple text prompt for the request.

This method allows setting a user prompt. The prompt represents the user’s input to the language model.

§Parameters
  • prompt - The user prompt text.
§Returns

The builder in the OptionsStage state.

Source

pub fn messages( self, messages: Messages, ) -> LanguageModelRequestBuilder<M, OptionsStage>

Sets conversation messages for the request.

This method allows providing a full conversation history as a vector of messages, enabling multi-turn conversations with the language model.

§Parameters
  • messages - Messages instances representing the conversation.
§Returns

The builder in the OptionsStage state.

Source§

impl<M: LanguageModel> LanguageModelRequestBuilder<M, OptionsStage>

Methods available in the OptionsStage state.

Source

pub fn schema<T: JsonSchema>(self) -> Self

Sets the output schema for structured generation.

This method configures the language model to generate output that conforms to the provided JSON schema. The schema is derived from the given type T.

§Type Parameters
  • T - A type that implements JsonSchema, used to generate the output schema.
§Returns

The builder with the schema configured.

Source

pub fn seed(self, seed: impl Into<u32>) -> Self

Sets a seed for deterministic generation.

§Parameters
  • seed - The random seed value.
§Returns

The builder with the seed set.

Source

pub fn temperature(self, temperature: impl Into<u32>) -> Self

Sets the temperature for generation randomness (0-100, scaled to 0.0-1.0).

Higher values increase creativity, lower values increase determinism.

§Parameters
  • temperature - The temperature value (0-100).
§Returns

The builder with the temperature set.

Source

pub fn top_p(self, top_p: impl Into<u32>) -> Self

Sets the top-p (nucleus) sampling parameter (0-100, scaled to 0.0-1.0).

§Parameters
  • top_p - The top-p value (0-100).
§Returns

The builder with top-p set.

Source

pub fn top_k(self, top_k: impl Into<u32>) -> Self

Sets the top-k sampling parameter.

§Parameters
  • top_k - The top-k value.
§Returns

The builder with top-k set.

Source

pub fn stop_sequences(self, stop_sequences: impl Into<Vec<String>>) -> Self

Sets stop sequences that halt generation.

§Parameters
  • stop_sequences - A list of strings that stop generation when encountered.
§Returns

The builder with stop sequences set.

Source

pub fn max_retries(self, max_retries: impl Into<u32>) -> Self

Sets the maximum number of retries for failed requests.

§Parameters
  • max_retries - The maximum retry count.
§Returns

The builder with max retries set.

Source

pub fn frequency_penalty(self, frequency_penalty: impl Into<f32>) -> Self

Sets the frequency penalty to reduce repetition.

§Parameters
  • frequency_penalty - The penalty value.
§Returns

The builder with frequency penalty set.

Source

pub fn with_tool(self, tool: Tool) -> Self
where M: ToolCallSupport,

Adds a tool to the request.

§Arguments
  • tool - The tool to add.
§Returns

The builder with the tool added.

Source

pub fn stop_when<F>(self, hook: F) -> Self
where F: Fn(&LanguageModelOptions) -> bool + Send + Sync + 'static,

Sets a condition to stop the generation loop.

§Parameters
  • hook - A function that returns true when generation should stop.
§Returns

The builder with the stop condition set.

Source

pub fn on_step_start<F>(self, hook: F) -> Self
where F: Fn(&mut LanguageModelOptions) + Send + Sync + 'static,

Sets a hook to run at the start of each generation step.

§Parameters
  • hook - A function called before each step.
§Returns

The builder with the hook set.

Source

pub fn on_step_finish<F>(self, hook: F) -> Self
where F: Fn(&LanguageModelOptions) + Send + Sync + 'static,

Sets a hook to run at the end of each generation step.

§Parameters
  • hook - A function called after each step.
§Returns

The builder with the hook set.

Source

pub fn reasoning_effort( self, reasoning_effort: impl Into<ReasoningEffort>, ) -> Self

Sets the reasoning effort level.

§Parameters
  • reasoning_effort - The effort level.
§Returns

The builder with reasoning effort set.

Source

pub fn build(self) -> LanguageModelRequest<M>

Builds the LanguageModelRequest.

This method consumes the builder and returns the configured request.

§Returns

The constructed LanguageModelRequest.

Methods from Deref<Target = LanguageModelOptions>§

Source

pub fn messages(&self) -> Messages

Returns a vector of all messages in the conversation.

Source

pub fn step(&self, index: usize) -> Option<Step>

Returns the step with the given index, if it exists.

Source

pub fn last_step(&self) -> Option<Step>

Returns the most recent step, if any.

Source

pub fn steps(&self) -> Vec<Step>

Returns all steps in chronological order.

Source

pub fn usage(&self) -> Usage

Calculates the total token usage across all steps.

Source

pub fn content(&self) -> Option<&LanguageModelResponseContentType>

Returns the content of the last assistant message, excluding reasoning.

Source

pub fn text(&self) -> Option<String>

Returns the text content of the last assistant message.

Source

pub fn tool_results(&self) -> Option<Vec<ToolResultInfo>>

Extracts all tool results from the conversation.

Source

pub fn tool_calls(&self) -> Option<Vec<ToolCallInfo>>

Extracts all tool calls from the conversation.

Source

pub fn stop_reason(&self) -> Option<StopReason>

Returns the reason why generation stopped.

Trait Implementations§

Source§

impl<M: LanguageModel, State> Deref for LanguageModelRequestBuilder<M, State>

Source§

fn deref(&self) -> &Self::Target

Dereferences to the underlying LanguageModelOptions.

This allows direct access to the options fields during building.

Source§

type Target = LanguageModelOptions

The resulting type after dereferencing.
Source§

impl<M: LanguageModel, State> DerefMut for LanguageModelRequestBuilder<M, State>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences to the underlying LanguageModelOptions.

This allows direct mutation of the options fields during building.

Auto Trait Implementations§

§

impl<M, State = ModelStage> !RefUnwindSafe for LanguageModelRequestBuilder<M, State>

§

impl<M, State = ModelStage> !UnwindSafe for LanguageModelRequestBuilder<M, State>

§

impl<M, State> Freeze for LanguageModelRequestBuilder<M, State>
where M: Freeze,

§

impl<M, State> Send for LanguageModelRequestBuilder<M, State>
where State: Send,

§

impl<M, State> Sync for LanguageModelRequestBuilder<M, State>
where State: Sync,

§

impl<M, State> Unpin for LanguageModelRequestBuilder<M, State>
where M: Unpin, State: Unpin,

§

impl<M, State> UnsafeUnpin for LanguageModelRequestBuilder<M, State>
where M: UnsafeUnpin,

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: Sized + 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: Sized + 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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