ContentBuilder

Struct ContentBuilder 

Source
pub struct ContentBuilder {
    pub contents: Vec<Content>,
    /* private fields */
}
Expand description

Builder for content generation requests

Fields§

§contents: Vec<Content>

Implementations§

Source§

impl ContentBuilder

Source

pub fn with_system_prompt(self, text: impl Into<String>) -> Self

Sets the system prompt for the request.

This is an alias for with_system_instruction().

Source

pub fn with_system_instruction(self, text: impl Into<String>) -> Self

Sets the system instruction for the request.

System instructions are used to provide high-level guidance to the model, such as setting a persona, providing context, or defining the desired output format.

Source

pub fn with_user_message(self, text: impl Into<String>) -> Self

Adds a user message to the conversation history.

Source

pub fn with_model_message(self, text: impl Into<String>) -> Self

Adds a model message to the conversation history.

Source

pub fn with_inline_data( self, data: impl Into<String>, mime_type: impl Into<String>, ) -> Self

Adds inline data (e.g., an image) to the request.

The data should be base64-encoded.

Source

pub fn with_function_response<Response>( self, name: impl Into<String>, response: Response, ) -> Result<Self, Error>
where Response: Serialize,

Adds a function response to the request using a Serialize response.

This is used to provide the model with the result of a function call it has requested.

Source

pub fn with_function_response_str( self, name: impl Into<String>, response: impl Into<String>, ) -> Result<Self, Error>

Adds a function response to the request using a JSON string.

This is a convenience method that parses the string into a serde_json::Value.

Source

pub fn with_message(self, message: Message) -> Self

Adds a Message to the conversation history.

Source

pub fn with_cached_content(self, cached_content: &CachedContentHandle) -> Self

Uses cached content for this request.

This allows reusing previously cached system instructions and conversation history, which can reduce latency and cost.

Source

pub fn with_messages(self, messages: impl IntoIterator<Item = Message>) -> Self

Adds multiple messages to the conversation history.

Source

pub fn with_generation_config(self, config: GenerationConfig) -> Self

Sets the generation configuration for the request.

Source

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

Sets the temperature for the request.

Temperature controls the randomness of the output. Higher values (e.g., 1.0) produce more creative results, while lower values (e.g., 0.2) produce more deterministic results.

Source

pub fn with_top_p(self, top_p: f32) -> Self

Sets the top-p value for the request.

Top-p is a sampling method that selects the next token from a cumulative probability distribution. It can be used to control the diversity of the output.

Source

pub fn with_top_k(self, top_k: i32) -> Self

Sets the top-k value for the request.

Top-k is a sampling method that selects the next token from the k most likely candidates.

Source

pub fn with_max_output_tokens(self, max_output_tokens: i32) -> Self

Sets the maximum number of output tokens for the request.

Source

pub fn with_candidate_count(self, candidate_count: i32) -> Self

Sets the number of candidate responses to generate.

Source

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

Sets the stop sequences for the request.

The model will stop generating text when it encounters one of these sequences.

Source

pub fn with_response_mime_type(self, mime_type: impl Into<String>) -> Self

Sets the response MIME type for the request.

This can be used to request structured output, such as JSON.

Source

pub fn with_response_schema(self, schema: Value) -> Self

Sets the response schema for structured output.

When used with a JSON MIME type, this schema will be used to validate the model’s output.

Source

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

Adds a tool to the request.

Tools allow the model to interact with external systems, such as APIs or databases.

Source

pub fn with_function(self, function: FunctionDeclaration) -> Self

Adds a function declaration as a tool.

This is a convenience method for creating a Tool from a FunctionDeclaration.

Source

pub fn with_function_calling_mode(self, mode: FunctionCallingMode) -> Self

Sets the function calling mode for the request.

Source

pub fn with_thinking_config(self, thinking_config: ThinkingConfig) -> Self

Sets the thinking configuration for the request (Gemini 2.5 series only).

Source

pub fn with_thinking_budget(self, budget: i32) -> Self

Sets the thinking budget for the request (Gemini 2.5 series only).

A budget of -1 enables dynamic thinking.

Source

pub fn with_dynamic_thinking(self) -> Self

Enables dynamic thinking, which allows the model to decide its own thinking budget (Gemini 2.5 series only).

Note: This only enables the capability. To receive thoughts in the response, you must also call [.with_thoughts_included(true)](Self::with_thoughts_included).

Source

pub fn with_thoughts_included(self, include: bool) -> Self

Includes thought summaries in the response (Gemini 2.5 series only).

This requires with_dynamic_thinking() or with_thinking_budget() to be enabled.

Source

pub fn with_audio_output(self) -> Self

Enables audio output (text-to-speech).

Source

pub fn with_speech_config(self, speech_config: SpeechConfig) -> Self

Sets the speech configuration for text-to-speech generation.

Source

pub fn with_voice(self, voice_name: impl Into<String>) -> Self

Sets a single voice for text-to-speech generation.

Source

pub fn with_multi_speaker_config( self, speakers: Vec<SpeakerVoiceConfig>, ) -> Self

Sets multi-speaker configuration for text-to-speech generation.

Source

pub fn build(self) -> GenerateContentRequest

Builds the GenerateContentRequest.

Source

pub async fn execute(self) -> Result<GenerationResponse, ClientError>

Executes the content generation request.

Source

pub async fn execute_stream( self, ) -> Result<impl TryStream<Ok = GenerationResponse, Error = ClientError> + Send, ClientError>

Executes the content generation request as a stream.

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

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