#[non_exhaustive]pub struct Agent<M, P = ()>where
M: CompletionModel,
P: PromptHook<M>,{Show 16 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<Vec<(usize, Arc<dyn VectorStoreIndexDyn + Send + Sync>)>>,
pub tool_choice: Option<ToolChoice>,
pub default_max_turns: Option<usize>,
pub hook: Option<P>,
pub output_schema: Option<Schema>,
pub memory: Option<Arc<dyn ConversationMemory>>,
pub default_conversation_id: Option<String>,
}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_core::{
client::{CompletionClient, ProviderClient},
completion::Prompt,
providers::openai,
};
let openai = openai::Client::from_env()?;
let comedian_agent = openai
.agent(openai::GPT_5_2)
.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?;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<Vec<(usize, Arc<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.
memory: Option<Arc<dyn ConversationMemory>>Optional conversation memory backend that loads/saves history per conversation id.
default_conversation_id: Option<String>Optional default conversation id used when none is set per-request.
Trait Implementations§
Source§impl<M, P> Chat for Agent<M, P>where
M: CompletionModel + 'static,
P: PromptHook<M> + 'static,
impl<M, P> Chat for Agent<M, P>where
M: CompletionModel + 'static,
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<I, T>(
&self,
prompt: impl Into<Message> + WasmCompatSend,
chat_history: I,
) -> Result<CompletionRequestBuilder<M>, CompletionError>
async fn completion<I, T>( &self, prompt: impl Into<Message> + WasmCompatSend, chat_history: I, ) -> 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>
impl<M> DiscordExt for Agent<M>
async fn into_discord_bot(self, token: &str) -> Result<Client, DiscordBotError>
fn into_discord_bot_from_env( self, ) -> impl Future<Output = Result<Client, DiscordBotError>> + Send
Source§impl<M, P> Prompt for Agent<M, P>where
M: CompletionModel + 'static,
P: PromptHook<M> + 'static,
impl<M, P> Prompt for Agent<M, P>where
M: CompletionModel + 'static,
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 + 'static,
P: PromptHook<M> + 'static,
impl<M, P> Prompt for &Agent<M, P>where
M: CompletionModel + 'static,
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 as CompletionModel>::StreamingResponse: GetTokenUsage,
P: PromptHook<M> + 'static,
impl<M, P> StreamingChat<M, <M as CompletionModel>::StreamingResponse> for Agent<M, P>where
M: CompletionModel + 'static,
<M as CompletionModel>::StreamingResponse: GetTokenUsage,
P: PromptHook<M> + 'static,
Source§fn stream_chat<I, T>(
&self,
prompt: impl Into<Message> + WasmCompatSend,
chat_history: I,
) -> StreamingPromptRequest<M, P>
fn stream_chat<I, T>( &self, prompt: impl Into<Message> + WasmCompatSend, chat_history: I, ) -> 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<I, T>(
&self,
prompt: impl Into<Message> + WasmCompatSend,
chat_history: I,
) -> Result<CompletionRequestBuilder<M>, CompletionError>
async fn stream_completion<I, T>( &self, prompt: impl Into<Message> + WasmCompatSend, chat_history: I, ) -> Result<CompletionRequestBuilder<M>, CompletionError>
Source§impl<M, P> StreamingPrompt<M, <M as CompletionModel>::StreamingResponse> for Agent<M, P>where
M: CompletionModel + 'static,
<M as CompletionModel>::StreamingResponse: GetTokenUsage,
P: PromptHook<M> + 'static,
impl<M, P> StreamingPrompt<M, <M as CompletionModel>::StreamingResponse> for Agent<M, P>where
M: CompletionModel + 'static,
<M as CompletionModel>::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> Tool for Agent<M>where
M: CompletionModel + 'static,
impl<M> Tool for Agent<M>where
M: CompletionModel + 'static,
Source§const NAME: &'static str = "agent_tool"
const NAME: &'static str = "agent_tool"
ToolSet or other registration scope that dispatches tools by name.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 + 'static,
P: PromptHook<M> + 'static,
impl<M, P> TypedPrompt for Agent<M, P>where
M: CompletionModel + 'static,
P: PromptHook<M> + 'static,
Source§fn prompt_typed<T>(
&self,
prompt: impl Into<Message> + WasmCompatSend,
) -> TypedPromptRequest<T, Standard, M, P>
fn prompt_typed<T>( &self, prompt: impl Into<Message> + WasmCompatSend, ) -> TypedPromptRequest<T, Standard, 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_core::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<T: JsonSchema + DeserializeOwned + WasmCompatSend + 'static> = TypedPromptRequest<T, Standard, M, P>
type TypedRequest<T: JsonSchema + DeserializeOwned + WasmCompatSend + 'static> = TypedPromptRequest<T, Standard, M, P>
prompt_typed.Source§impl<M, P> TypedPrompt for &Agent<M, P>where
M: CompletionModel + 'static,
P: PromptHook<M> + 'static,
impl<M, P> TypedPrompt for &Agent<M, P>where
M: CompletionModel + 'static,
P: PromptHook<M> + 'static,
Source§type TypedRequest<T: JsonSchema + DeserializeOwned + WasmCompatSend + 'static> = TypedPromptRequest<T, Standard, M, P>
type TypedRequest<T: JsonSchema + DeserializeOwned + WasmCompatSend + 'static> = TypedPromptRequest<T, Standard, M, P>
prompt_typed.Source§fn prompt_typed<T>(
&self,
prompt: impl Into<Message> + WasmCompatSend,
) -> TypedPromptRequest<T, Standard, M, P>
fn prompt_typed<T>( &self, prompt: impl Into<Message> + WasmCompatSend, ) -> TypedPromptRequest<T, Standard, M, P>
Auto Trait Implementations§
impl<M, P = ()> !RefUnwindSafe for Agent<M, P>
impl<M, P = ()> !UnwindSafe for Agent<M, P>
impl<M, P> Freeze for Agent<M, P>where
P: Freeze,
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,
Blanket Implementations§
Source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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>
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<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> 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 moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
Source§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Source§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
Source§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
Source§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out indicating that a T is niched.Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.