pub struct Agent { /* private fields */ }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.
Default hooks attached with AgentBuilder::add_hook
are used for every prompt request, plus any added on the request or runner.
§Example
use rig_agent::prelude::*;
use rig_core::{client::ProviderClient, 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?;Implementations§
Source§impl Agent
impl Agent
Sourcepub fn description(&self) -> Option<&str>
pub fn description(&self) -> Option<&str>
Returns the configured agent description.
Sourcepub fn runner(&self, prompt: impl Into<Message>) -> AgentRunner
pub fn runner(&self, prompt: impl Into<Message>) -> AgentRunner
Build a hook-aware AgentRunner for this agent, seeded with the
agent’s default hook stack. Attach more hooks with
AgentRunner::add_hook, then call AgentRunner::run.
Sourcepub fn model_handle(&self) -> &ModelHandle
pub fn model_handle(&self) -> &ModelHandle
Returns the agent’s current default model handle.
Sourcepub fn set_model_handle(&mut self, model: ModelHandle)
pub fn set_model_handle(&mut self, model: ModelHandle)
Replace the default model used by runners created after this call.
Existing runners retain their model snapshot, and replacing one cloned agent does not mutate another clone. Model-selection hooks may replace the captured default at each model-call boundary.
Sourcepub fn set_model<M>(&mut self, model: M)where
M: CompletionModel + 'static,
pub fn set_model<M>(&mut self, model: M)where
M: CompletionModel + 'static,
Erase and install a typed completion model as this agent’s new default.
Sourcepub fn with_model_handle(self, model: ModelHandle) -> Self
pub fn with_model_handle(self, model: ModelHandle) -> Self
Return this agent with a replacement default model handle.
Model-selection hooks may replace this default for individual calls.
Sourcepub fn with_model<M>(self, model: M) -> Selfwhere
M: CompletionModel + 'static,
pub fn with_model<M>(self, model: M) -> Selfwhere
M: CompletionModel + 'static,
Return this agent with an erased typed model as its new default.
Sourcepub async fn tool_definitions(
&self,
prompt: Option<String>,
) -> Result<Vec<ToolDefinition>, ToolServerError>
pub async fn tool_definitions( &self, prompt: Option<String>, ) -> Result<Vec<ToolDefinition>, ToolServerError>
Resolve the provider-facing tool definitions available for a prompt.
This read-only view does not expose tool dispatch. Agent execution and
tool lifecycle hooks remain owned by Self::runner.
Trait Implementations§
Source§impl DiscordExt for Agent
Available on crate feature discord-bot only.
impl DiscordExt for Agent
discord-bot only.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 From<Agent> for DynamicTool
impl From<Agent> for DynamicTool
Source§impl Prompt for Agent
impl Prompt for Agent
Source§fn prompt(
&self,
prompt: impl Into<Message> + WasmCompatSend,
) -> PromptRequest<Standard>
fn prompt( &self, prompt: impl Into<Message> + WasmCompatSend, ) -> PromptRequest<Standard>
Source§impl Prompt for &Agent
impl Prompt for &Agent
Source§fn prompt(
&self,
prompt: impl Into<Message> + WasmCompatSend,
) -> PromptRequest<Standard>
fn prompt( &self, prompt: impl Into<Message> + WasmCompatSend, ) -> PromptRequest<Standard>
Source§impl StreamingChat for Agent
impl StreamingChat for Agent
Source§fn stream_chat<I, T>(
&self,
prompt: impl Into<Message> + WasmCompatSend,
chat_history: I,
) -> StreamingPromptRequest
fn stream_chat<I, T>( &self, prompt: impl Into<Message> + WasmCompatSend, chat_history: I, ) -> StreamingPromptRequest
Source§impl StreamingPrompt for Agent
impl StreamingPrompt for Agent
Source§fn stream_prompt(
&self,
prompt: impl Into<Message> + WasmCompatSend,
) -> StreamingPromptRequest
fn stream_prompt( &self, prompt: impl Into<Message> + WasmCompatSend, ) -> StreamingPromptRequest
prompt.Source§impl TypedPrompt for Agent
impl TypedPrompt for Agent
Source§fn prompt_typed<T>(
&self,
prompt: impl Into<Message> + WasmCompatSend,
) -> TypedPromptRequest<T, Standard>
fn prompt_typed<T>( &self, prompt: impl Into<Message> + WasmCompatSend, ) -> TypedPromptRequest<T, Standard>
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> = TypedPromptRequest<T, Standard>
where
T: JsonSchema + DeserializeOwned + WasmCompatSend + 'static
type TypedRequest<T> = TypedPromptRequest<T, Standard> where T: JsonSchema + DeserializeOwned + WasmCompatSend + 'static
Source§impl TypedPrompt for &Agent
impl TypedPrompt for &Agent
Source§type TypedRequest<T> = TypedPromptRequest<T, Standard>
where
T: JsonSchema + DeserializeOwned + WasmCompatSend + 'static
type TypedRequest<T> = TypedPromptRequest<T, Standard> where T: JsonSchema + DeserializeOwned + WasmCompatSend + 'static
Source§fn prompt_typed<T>(
&self,
prompt: impl Into<Message> + WasmCompatSend,
) -> TypedPromptRequest<T, Standard>
fn prompt_typed<T>( &self, prompt: impl Into<Message> + WasmCompatSend, ) -> TypedPromptRequest<T, Standard>
T.