pub struct ContentBuilder {
pub contents: Vec<Content>,
/* private fields */
}
Expand description
Builder for content generation requests
Fields§
§contents: Vec<Content>
Implementations§
Source§impl ContentBuilder
impl ContentBuilder
Sourcepub fn with_system_prompt(self, text: impl Into<String>) -> Self
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()
.
Sourcepub fn with_system_instruction(self, text: impl Into<String>) -> Self
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.
Sourcepub fn with_user_message(self, text: impl Into<String>) -> Self
pub fn with_user_message(self, text: impl Into<String>) -> Self
Adds a user message to the conversation history.
Sourcepub fn with_model_message(self, text: impl Into<String>) -> Self
pub fn with_model_message(self, text: impl Into<String>) -> Self
Adds a model message to the conversation history.
Sourcepub fn with_inline_data(
self,
data: impl Into<String>,
mime_type: impl Into<String>,
) -> Self
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.
Sourcepub fn with_function_response<Response>(
self,
name: impl Into<String>,
response: Response,
) -> Result<Self, Error>where
Response: Serialize,
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.
Sourcepub fn with_function_response_str(
self,
name: impl Into<String>,
response: impl Into<String>,
) -> Result<Self, Error>
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
.
Sourcepub fn with_message(self, message: Message) -> Self
pub fn with_message(self, message: Message) -> Self
Adds a Message
to the conversation history.
Sourcepub fn with_cached_content(self, cached_content: &CachedContentHandle) -> Self
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.
Sourcepub fn with_messages(self, messages: impl IntoIterator<Item = Message>) -> Self
pub fn with_messages(self, messages: impl IntoIterator<Item = Message>) -> Self
Adds multiple messages to the conversation history.
Sourcepub fn with_generation_config(self, config: GenerationConfig) -> Self
pub fn with_generation_config(self, config: GenerationConfig) -> Self
Sets the generation configuration for the request.
Sourcepub fn with_temperature(self, temperature: f32) -> Self
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.
Sourcepub fn with_top_p(self, top_p: f32) -> Self
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.
Sourcepub fn with_top_k(self, top_k: i32) -> Self
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.
Sourcepub fn with_max_output_tokens(self, max_output_tokens: i32) -> Self
pub fn with_max_output_tokens(self, max_output_tokens: i32) -> Self
Sets the maximum number of output tokens for the request.
Sourcepub fn with_candidate_count(self, candidate_count: i32) -> Self
pub fn with_candidate_count(self, candidate_count: i32) -> Self
Sets the number of candidate responses to generate.
Sourcepub fn with_stop_sequences(self, stop_sequences: Vec<String>) -> Self
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.
Sourcepub fn with_response_mime_type(self, mime_type: impl Into<String>) -> Self
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.
Sourcepub fn with_response_schema(self, schema: Value) -> Self
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.
Sourcepub fn with_tool(self, tool: Tool) -> Self
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.
Sourcepub fn with_function(self, function: FunctionDeclaration) -> Self
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
.
Sourcepub fn with_function_calling_mode(self, mode: FunctionCallingMode) -> Self
pub fn with_function_calling_mode(self, mode: FunctionCallingMode) -> Self
Sets the function calling mode for the request.
Sourcepub fn with_thinking_config(self, thinking_config: ThinkingConfig) -> Self
pub fn with_thinking_config(self, thinking_config: ThinkingConfig) -> Self
Sets the thinking configuration for the request (Gemini 2.5 series only).
Sourcepub fn with_thinking_budget(self, budget: i32) -> Self
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.
Sourcepub fn with_dynamic_thinking(self) -> Self
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)
.
Sourcepub fn with_thoughts_included(self, include: bool) -> Self
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.
Sourcepub fn with_audio_output(self) -> Self
pub fn with_audio_output(self) -> Self
Enables audio output (text-to-speech).
Sourcepub fn with_speech_config(self, speech_config: SpeechConfig) -> Self
pub fn with_speech_config(self, speech_config: SpeechConfig) -> Self
Sets the speech configuration for text-to-speech generation.
Sourcepub fn with_voice(self, voice_name: impl Into<String>) -> Self
pub fn with_voice(self, voice_name: impl Into<String>) -> Self
Sets a single voice for text-to-speech generation.
Sourcepub fn with_multi_speaker_config(
self,
speakers: Vec<SpeakerVoiceConfig>,
) -> Self
pub fn with_multi_speaker_config( self, speakers: Vec<SpeakerVoiceConfig>, ) -> Self
Sets multi-speaker configuration for text-to-speech generation.
Sourcepub fn build(self) -> GenerateContentRequest
pub fn build(self) -> GenerateContentRequest
Builds the GenerateContentRequest
.
Sourcepub async fn execute(self) -> Result<GenerationResponse, ClientError>
pub async fn execute(self) -> Result<GenerationResponse, ClientError>
Executes the content generation request.
Sourcepub async fn execute_stream(
self,
) -> Result<impl TryStream<Ok = GenerationResponse, Error = ClientError> + Send, ClientError>
pub async fn execute_stream( self, ) -> Result<impl TryStream<Ok = GenerationResponse, Error = ClientError> + Send, ClientError>
Executes the content generation request as a stream.