pub struct CompletionRequestBuilder<M: CompletionModel> { /* private fields */ }Expand description
Builder struct for constructing a completion request.
Example usage:
use rig::{
providers::openai::{Client, self},
completion::CompletionRequestBuilder,
};
let openai = Client::new("your-openai-api-key");
let model = openai.completion_model(openai::GPT_4O).build();
// Create the completion request and execute it separately
let request = CompletionRequestBuilder::new(model, "Who are you?".to_string())
.preamble("You are Marvin from the Hitchhiker's Guide to the Galaxy.".to_string())
.temperature(0.5)
.build();
let response = model.completion(request)
.await
.expect("Failed to get completion response");Alternatively, you can execute the completion request directly from the builder:
use rig::{
providers::openai::{Client, self},
completion::CompletionRequestBuilder,
};
let openai = Client::new("your-openai-api-key");
let model = openai.completion_model(openai::GPT_4O).build();
// Create the completion request and execute it directly
let response = CompletionRequestBuilder::new(model, "Who are you?".to_string())
.preamble("You are Marvin from the Hitchhiker's Guide to the Galaxy.".to_string())
.temperature(0.5)
.send()
.await
.expect("Failed to get completion response");Note: It is usually unnecessary to create a completion request builder directly. Instead, use the CompletionModel::completion_request method.
Implementations§
Source§impl<M: CompletionModel> CompletionRequestBuilder<M>
impl<M: CompletionModel> CompletionRequestBuilder<M>
pub fn new(model: M, prompt: impl Into<Message>) -> Self
Sourcepub fn model(self, model: impl Into<String>) -> Self
pub fn model(self, model: impl Into<String>) -> Self
Overrides the model used for this request.
Sourcepub fn model_opt(self, model: Option<String>) -> Self
pub fn model_opt(self, model: Option<String>) -> Self
Overrides the model used for this request.
pub fn without_preamble(self) -> Self
Sourcepub fn message(self, message: Message) -> Self
pub fn message(self, message: Message) -> Self
Adds a message to the chat history for the completion request.
Sourcepub fn messages(self, messages: Vec<Message>) -> Self
pub fn messages(self, messages: Vec<Message>) -> Self
Adds a list of messages to the chat history for the completion request.
Sourcepub fn documents(self, documents: Vec<Document>) -> Self
pub fn documents(self, documents: Vec<Document>) -> Self
Adds a list of documents to the completion request.
Sourcepub fn tool(self, tool: ToolDefinition) -> Self
pub fn tool(self, tool: ToolDefinition) -> Self
Adds a tool to the completion request.
Sourcepub fn tools(self, tools: Vec<ToolDefinition>) -> Self
pub fn tools(self, tools: Vec<ToolDefinition>) -> Self
Adds a list of tools to the completion request.
Sourcepub fn additional_params(self, additional_params: Value) -> Self
pub fn additional_params(self, additional_params: Value) -> Self
Adds additional parameters to the completion request.
This can be used to set additional provider-specific parameters. For example,
Cohere’s completion models accept a connectors parameter that can be used to
specify the data connectors used by Cohere when executing the completion
(see examples/cohere_connectors.rs).
Sourcepub fn additional_params_opt(self, additional_params: Option<Value>) -> Self
pub fn additional_params_opt(self, additional_params: Option<Value>) -> Self
Sets the additional parameters for the completion request.
This can be used to set additional provider-specific parameters. For example,
Cohere’s completion models accept a connectors parameter that can be used to
specify the data connectors used by Cohere when executing the completion
(see examples/cohere_connectors.rs).
Sourcepub fn temperature(self, temperature: f64) -> Self
pub fn temperature(self, temperature: f64) -> Self
Sets the temperature for the completion request.
Sourcepub fn temperature_opt(self, temperature: Option<f64>) -> Self
pub fn temperature_opt(self, temperature: Option<f64>) -> Self
Sets the temperature for the completion request.
Sourcepub fn max_tokens(self, max_tokens: u64) -> Self
pub fn max_tokens(self, max_tokens: u64) -> Self
Sets the max tokens for the completion request. Note: This is required if using Anthropic
Sourcepub fn max_tokens_opt(self, max_tokens: Option<u64>) -> Self
pub fn max_tokens_opt(self, max_tokens: Option<u64>) -> Self
Sets the max tokens for the completion request. Note: This is required if using Anthropic
Sourcepub fn tool_choice(self, tool_choice: ToolChoice) -> Self
pub fn tool_choice(self, tool_choice: ToolChoice) -> Self
Sets the thing.
Sourcepub fn output_schema(self, schema: Schema) -> Self
pub fn output_schema(self, schema: Schema) -> Self
Sets the output schema for structured output. When set, providers that support
native structured outputs will constrain the model’s response to match this schema.
NOTE: For direct type conversion, you may want to use Agent::prompt_typed() - using this method
with Agent::prompt() will still output a String at the end, it’ll just be compatible with whatever
type you want to use here. This method is primarily an escape hatch for agents being used as tools
to still be able to leverage structured outputs.
Sourcepub fn output_schema_opt(self, schema: Option<Schema>) -> Self
pub fn output_schema_opt(self, schema: Option<Schema>) -> Self
Sets the output schema for structured output from an optional value.
NOTE: For direct type conversion, you may want to use Agent::prompt_typed() - using this method
with Agent::prompt() will still output a String at the end, it’ll just be compatible with whatever
type you want to use here. This method is primarily an escape hatch for agents being used as tools
to still be able to leverage structured outputs.
Sourcepub fn build(self) -> CompletionRequest
pub fn build(self) -> CompletionRequest
Builds the completion request.
Sourcepub async fn send(
self,
) -> Result<CompletionResponse<M::Response>, CompletionError>
pub async fn send( self, ) -> Result<CompletionResponse<M::Response>, CompletionError>
Sends the completion request to the completion model provider and returns the completion response.
Sourcepub async fn stream<'a>(
self,
) -> Result<StreamingCompletionResponse<M::StreamingResponse>, CompletionError>where
<M as CompletionModel>::StreamingResponse: 'a,
Self: 'a,
pub async fn stream<'a>(
self,
) -> Result<StreamingCompletionResponse<M::StreamingResponse>, CompletionError>where
<M as CompletionModel>::StreamingResponse: 'a,
Self: 'a,
Stream the completion request
Auto Trait Implementations§
impl<M> Freeze for CompletionRequestBuilder<M>where
M: Freeze,
impl<M> RefUnwindSafe for CompletionRequestBuilder<M>where
M: RefUnwindSafe,
impl<M> Send for CompletionRequestBuilder<M>
impl<M> Sync for CompletionRequestBuilder<M>
impl<M> Unpin for CompletionRequestBuilder<M>where
M: Unpin,
impl<M> UnsafeUnpin for CompletionRequestBuilder<M>where
M: UnsafeUnpin,
impl<M> UnwindSafe for CompletionRequestBuilder<M>where
M: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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