GenerateTextBuilder

Struct GenerateTextBuilder 

Source
pub struct GenerateTextBuilder<M, P> { /* private fields */ }
Expand description

Builder for text generation.

This builder allows configuring the model, prompt, tools, and other settings before executing the generation.

Implementations§

Source§

impl GenerateTextBuilder<(), ()>

Source

pub fn new() -> Self

Creates a new builder in the initial state with default configuration.

The returned builder requires both a model and prompt to be set before generation can be executed.

Source§

impl<M, P> GenerateTextBuilder<M, P>

Source

pub fn tools(self, tools: Vec<Arc<dyn Tool>>) -> Self

Configures tools that the language model can invoke during generation.

When tools are provided, the model gains the ability to call them to retrieve information, perform actions, or integrate with external systems. The model must support tool use for this to have effect.

§Arguments
  • tools - A vector of tool implementations available to the model.
Source

pub fn max_steps(self, max_steps: u32) -> Self

Sets the maximum number of sequential generation steps.

A step is a single invocation of the language model. This controls how many times the model will be called in succession. Useful for tool use scenarios where the model needs multiple rounds to complete a task.

§Arguments
  • max_steps - The maximum number of model invocations to allow.
Source

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

Sets the sampling temperature for controlling output randomness.

Temperature controls the randomness of the model’s outputs. Valid ranges are typically 0.0 to 2.0, though this depends on the model. Lower values (closer to 0.0) make output more deterministic and focused, while higher values produce more diverse and creative output.

§Arguments
  • temperature - The temperature value to use.
Source

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

Sets the maximum number of tokens to generate in the output.

This controls the maximum length of the model’s response. Generation will stop when the model reaches this limit. The exact interpretation of “tokens” depends on the tokenizer used by the model provider.

§Arguments
  • max_tokens - The maximum number of tokens to generate.
Source

pub fn retry_policy(self, retry_policy: RetryPolicy) -> Self

Sets the retry policy for handling transient API failures.

The retry policy defines how the SDK should handle temporary failures such as rate limiting or service unavailability. Permanent errors are not retried regardless of policy.

§Arguments
  • retry_policy - The retry policy to apply.
Source§

impl<P> GenerateTextBuilder<(), P>

Source

pub fn model<Mod: LanguageModel + 'static>( self, model: Mod, ) -> GenerateTextBuilder<Arc<dyn LanguageModel>, P>

Sets the language model to use for generation.

This method is required before calling execute() or execute_async(). The model is wrapped in an Arc to allow thread-safe shared ownership.

§Arguments
  • model - An implementation of LanguageModel that will perform the generation.
§Returns

A new builder with the model configured, transitioning to a state where only a prompt is required.

Source§

impl<M> GenerateTextBuilder<M, ()>

Source

pub fn prompt( self, prompt: impl Into<String>, ) -> GenerateTextBuilder<M, Vec<Message>>

Sets the prompt from a string.

This convenience method converts the string into a user message and stores it as the conversation prompt. Equivalent to calling messages() with a single user message.

§Arguments
  • prompt - A string that will be used as the user message.
§Returns

A new builder with the prompt configured, transitioning to a state where the builder is ready for execution if a model has been set.

Source

pub fn messages( self, messages: Vec<Message>, ) -> GenerateTextBuilder<M, Vec<Message>>

Sets the prompt from a vector of conversation messages.

This method allows providing a full conversation history, including previous user messages, assistant responses, and tool results. The messages will be sent to the model in order.

§Arguments
  • messages - A vector of messages representing the conversation history.
§Returns

A new builder with the prompt configured, transitioning to a state where the builder is ready for execution if a model has been set.

Source§

impl<M, P> GenerateTextBuilder<M, P>

Source

pub fn on_preliminary_tool_result( self, callback: Arc<dyn Fn(ToolResultPart) -> BoxFuture<'static, ()> + Send + Sync>, ) -> Self

Set a callback for preliminary tool results.

This is useful when you want to receive updates about tool execution (e.g., partial outputs from long-running tools) while the tool is still running, even in a non-streaming generation context.

Source§

impl GenerateTextBuilder<Arc<dyn LanguageModel>, Vec<Message>>

Source

pub async fn execute(self) -> Result<GenerateTextResult>

Execute the text generation.

This will send the prompt to the model, handle tool calls if tools are provided, and return the final result.

§Returns

A Result containing GenerateTextResult on success, or a GenerateError on failure.

Trait Implementations§

Source§

impl Default for GenerateTextBuilder<(), ()>

Source§

fn default() -> Self

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

impl IntoFuture for GenerateTextBuilder<Arc<dyn LanguageModel>, Vec<Message>>

Source§

type Output = Result<GenerateTextResult, Box<dyn Error + Sync + Send>>

The output that the future will produce on completion.
Source§

type IntoFuture = Pin<Box<dyn Future<Output = <GenerateTextBuilder<Arc<dyn LanguageModel>, Vec<Message>> as IntoFuture>::Output> + Send>>

Which kind of future are we turning this into?
Source§

fn into_future(self) -> Self::IntoFuture

Creates a future from a value. Read more

Auto Trait Implementations§

§

impl<M, P> Freeze for GenerateTextBuilder<M, P>
where M: Freeze, P: Freeze,

§

impl<M, P> !RefUnwindSafe for GenerateTextBuilder<M, P>

§

impl<M, P> Send for GenerateTextBuilder<M, P>
where M: Send, P: Send,

§

impl<M, P> Sync for GenerateTextBuilder<M, P>
where M: Sync, P: Sync,

§

impl<M, P> Unpin for GenerateTextBuilder<M, P>
where M: Unpin, P: Unpin,

§

impl<M, P> !UnwindSafe for GenerateTextBuilder<M, P>

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, 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<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