#[non_exhaustive]pub struct Agent<M, P = ()>where
M: CompletionModel,
P: PromptHook<M>,{Show 14 fields
pub name: Option<String>,
pub description: Option<String>,
pub model: Arc<M>,
pub preamble: Option<String>,
pub static_context: Vec<Document>,
pub temperature: Option<f64>,
pub max_tokens: Option<u64>,
pub additional_params: Option<Value>,
pub tool_server_handle: ToolServerHandle,
pub dynamic_context: Arc<RwLock<Vec<(usize, Box<dyn VectorStoreIndexDyn + Send + Sync>)>>>,
pub tool_choice: Option<ToolChoice>,
pub default_max_turns: Option<usize>,
pub hook: Option<P>,
pub output_schema: Option<Schema>,
}Expand description
Struct representing an LLM agent. An agent is an LLM model combined with a preamble (i.e.: system prompt) and a static set of context documents and tools. All context documents and tools are always provided to the agent when prompted.
The optional type parameter P represents a default hook that will be used for all
prompt requests unless overridden via .with_hook() on the request.
§Example
use rig::{completion::Prompt, providers::openai};
let openai = openai::Client::from_env();
let comedian_agent = openai
.agent("gpt-4o")
.preamble("You are a comedian here to entertain the user using humour and jokes.")
.temperature(0.9)
.build();
let response = comedian_agent.prompt("Entertain me!")
.await
.expect("Failed to prompt the agent");Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.name: Option<String>Name of the agent used for logging and debugging
description: Option<String>Agent description. Primarily useful when using sub-agents as part of an agent workflow and converting agents to other formats.
model: Arc<M>Completion model (e.g.: OpenAI’s gpt-3.5-turbo-1106, Cohere’s command-r)
preamble: Option<String>System prompt
static_context: Vec<Document>Context documents always available to the agent
temperature: Option<f64>Temperature of the model
max_tokens: Option<u64>Maximum number of tokens for the completion
additional_params: Option<Value>Additional parameters to be passed to the model
tool_server_handle: ToolServerHandle§dynamic_context: Arc<RwLock<Vec<(usize, Box<dyn VectorStoreIndexDyn + Send + Sync>)>>>List of vector store, with the sample number
tool_choice: Option<ToolChoice>Whether or not the underlying LLM should be forced to use a tool before providing a response.
default_max_turns: Option<usize>Default maximum depth for recursive agent calls
hook: Option<P>Default hook for this agent, used when no per-request hook is provided
output_schema: Option<Schema>Optional JSON Schema for structured output. When set, providers that support native structured outputs will constrain the model’s response to match this schema.
Trait Implementations§
Source§impl<M, P> Chat for Agent<M, P>where
M: CompletionModel,
P: PromptHook<M> + 'static,
impl<M, P> Chat for Agent<M, P>where
M: CompletionModel,
P: PromptHook<M> + 'static,
Source§impl<M, P> Completion<M> for Agent<M, P>where
M: CompletionModel,
P: PromptHook<M>,
impl<M, P> Completion<M> for Agent<M, P>where
M: CompletionModel,
P: PromptHook<M>,
Source§async fn completion(
&self,
prompt: impl Into<Message> + WasmCompatSend,
chat_history: Vec<Message>,
) -> Result<CompletionRequestBuilder<M>, CompletionError>
async fn completion( &self, prompt: impl Into<Message> + WasmCompatSend, chat_history: Vec<Message>, ) -> Result<CompletionRequestBuilder<M>, CompletionError>
prompt and chat_history.
This function is meant to be called by the user to further customize the
request at prompt time before sending it. Read moreSource§impl<M> DiscordExt for Agent<M>
Available on crate feature discord-bot only.
impl<M> DiscordExt for Agent<M>
discord-bot only.Source§impl<M, P> Prompt for &Agent<M, P>where
M: CompletionModel,
P: PromptHook<M> + 'static,
impl<M, P> Prompt for &Agent<M, P>where
M: CompletionModel,
P: PromptHook<M> + 'static,
Source§fn prompt(
&self,
prompt: impl Into<Message> + WasmCompatSend,
) -> PromptRequest<'_, Standard, M, P>
fn prompt( &self, prompt: impl Into<Message> + WasmCompatSend, ) -> PromptRequest<'_, Standard, M, P>
Source§impl<M, P> Prompt for Agent<M, P>where
M: CompletionModel,
P: PromptHook<M> + 'static,
impl<M, P> Prompt for Agent<M, P>where
M: CompletionModel,
P: PromptHook<M> + 'static,
Source§fn prompt(
&self,
prompt: impl Into<Message> + WasmCompatSend,
) -> PromptRequest<'_, Standard, M, P>
fn prompt( &self, prompt: impl Into<Message> + WasmCompatSend, ) -> PromptRequest<'_, Standard, M, P>
Source§impl<M, P> StreamingChat<M, <M as CompletionModel>::StreamingResponse> for Agent<M, P>where
M: CompletionModel + 'static,
M::StreamingResponse: GetTokenUsage,
P: PromptHook<M> + 'static,
impl<M, P> StreamingChat<M, <M as CompletionModel>::StreamingResponse> for Agent<M, P>where
M: CompletionModel + 'static,
M::StreamingResponse: GetTokenUsage,
P: PromptHook<M> + 'static,
Source§fn stream_chat(
&self,
prompt: impl Into<Message> + WasmCompatSend,
chat_history: Vec<Message>,
) -> StreamingPromptRequest<M, P>
fn stream_chat( &self, prompt: impl Into<Message> + WasmCompatSend, chat_history: Vec<Message>, ) -> StreamingPromptRequest<M, P>
Source§impl<M, P> StreamingCompletion<M> for Agent<M, P>where
M: CompletionModel,
P: PromptHook<M>,
impl<M, P> StreamingCompletion<M> for Agent<M, P>where
M: CompletionModel,
P: PromptHook<M>,
Source§async fn stream_completion(
&self,
prompt: impl Into<Message> + WasmCompatSend,
chat_history: Vec<Message>,
) -> Result<CompletionRequestBuilder<M>, CompletionError>
async fn stream_completion( &self, prompt: impl Into<Message> + WasmCompatSend, chat_history: Vec<Message>, ) -> Result<CompletionRequestBuilder<M>, CompletionError>
Source§impl<M, P> StreamingPrompt<M, <M as CompletionModel>::StreamingResponse> for Agent<M, P>where
M: CompletionModel + 'static,
M::StreamingResponse: GetTokenUsage,
P: PromptHook<M> + 'static,
impl<M, P> StreamingPrompt<M, <M as CompletionModel>::StreamingResponse> for Agent<M, P>where
M: CompletionModel + 'static,
M::StreamingResponse: GetTokenUsage,
P: PromptHook<M> + 'static,
Source§fn stream_prompt(
&self,
prompt: impl Into<Message> + WasmCompatSend,
) -> StreamingPromptRequest<M, P>
fn stream_prompt( &self, prompt: impl Into<Message> + WasmCompatSend, ) -> StreamingPromptRequest<M, P>
Source§impl<M: CompletionModel> Tool for Agent<M>
impl<M: CompletionModel> Tool for Agent<M>
Source§type Error = PromptError
type Error = PromptError
Source§async fn definition(&self, _prompt: String) -> ToolDefinition
async fn definition(&self, _prompt: String) -> ToolDefinition
Source§impl<M, P> TypedPrompt for &Agent<M, P>where
M: CompletionModel,
P: PromptHook<M> + 'static,
impl<M, P> TypedPrompt for &Agent<M, P>where
M: CompletionModel,
P: PromptHook<M> + 'static,
Source§type TypedRequest<'a, T> = TypedPromptRequest<'a, T, M, P>
where
Self: 'a,
T: JsonSchema + DeserializeOwned + WasmCompatSend + 'a
type TypedRequest<'a, T> = TypedPromptRequest<'a, T, M, P> where Self: 'a, T: JsonSchema + DeserializeOwned + WasmCompatSend + 'a
prompt_typed.Source§fn prompt_typed<T>(
&self,
prompt: impl Into<Message> + WasmCompatSend,
) -> TypedPromptRequest<'_, T, M, P>
fn prompt_typed<T>( &self, prompt: impl Into<Message> + WasmCompatSend, ) -> TypedPromptRequest<'_, T, M, P>
Source§impl<M, P> TypedPrompt for Agent<M, P>where
M: CompletionModel,
P: PromptHook<M> + 'static,
impl<M, P> TypedPrompt for Agent<M, P>where
M: CompletionModel,
P: PromptHook<M> + 'static,
Source§fn prompt_typed<T>(
&self,
prompt: impl Into<Message> + WasmCompatSend,
) -> TypedPromptRequest<'_, T, M, P>
fn prompt_typed<T>( &self, prompt: impl Into<Message> + WasmCompatSend, ) -> TypedPromptRequest<'_, T, M, P>
Send a prompt and receive a typed structured response.
The JSON schema for T is automatically generated and sent to the provider.
Providers that support native structured outputs will constrain the model’s
response to match this schema.
§Example
use rig::prelude::*;
use schemars::JsonSchema;
use serde::Deserialize;
#[derive(Debug, Deserialize, JsonSchema)]
struct WeatherForecast {
city: String,
temperature_f: f64,
conditions: String,
}
let agent = client.agent("gpt-4o").build();
// Type inferred from variable
let forecast: WeatherForecast = agent
.prompt_typed("What's the weather in NYC?")
.await?;
// Or explicit turbofish syntax
let forecast = agent
.prompt_typed::<WeatherForecast>("What's the weather in NYC?")
.max_turns(3)
.await?;Source§type TypedRequest<'a, T> = TypedPromptRequest<'a, T, M, P>
where
Self: 'a,
T: JsonSchema + DeserializeOwned + WasmCompatSend + 'a
type TypedRequest<'a, T> = TypedPromptRequest<'a, T, M, P> where Self: 'a, T: JsonSchema + DeserializeOwned + WasmCompatSend + 'a
prompt_typed.Auto Trait Implementations§
impl<M, P> Freeze for Agent<M, P>where
P: Freeze,
impl<M, P = ()> !RefUnwindSafe for Agent<M, P>
impl<M, P> Send for Agent<M, P>
impl<M, P> Sync for Agent<M, P>
impl<M, P> Unpin for Agent<M, P>where
P: Unpin,
impl<M, P> UnsafeUnpin for Agent<M, P>where
P: UnsafeUnpin,
impl<M, P = ()> !UnwindSafe for Agent<M, P>
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> CloneableStorage for T
impl<T> CloneableStorage for T
fn clone_storage(&self) -> Box<dyn CloneableStorage>
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