pub struct Agent { /* private fields */ }Expand description
The agent: manages conversation state, context pipeline, and the prompt → LLM → tool → LLM loop.
use orion_core::{Agent, AgentConfig, ContextConfig, InferenceParams};
let mut agent = Agent::new(AgentConfig {
system_prompt: "You are a coding assistant.".into(),
inference_params: InferenceParams {
max_tokens: 4096,
temperature: 0.4,
context_size: 8192,
n_threads: 6,
},
context_config: ContextConfig {
max_context_tokens: 8192,
max_response_tokens: 4096,
..Default::default()
},
..Default::default()
});
// Change settings on the fly.
agent.set_system_prompt("You are a pirate.");
agent.set_inference_params(InferenceParams { temperature: 1.2, ..Default::default() });
agent.clear();Implementations§
Source§impl Agent
impl Agent
Sourcepub fn new(config: AgentConfig) -> Self
pub fn new(config: AgentConfig) -> Self
Create an agent with the given config and the default ChatML template.
Sourcepub fn with_template(
config: AgentConfig,
template: Arc<dyn ChatTemplate>,
) -> Self
pub fn with_template( config: AgentConfig, template: Arc<dyn ChatTemplate>, ) -> Self
Create an agent with an explicit chat template.
Sourcepub fn config(&self) -> &AgentConfig
pub fn config(&self) -> &AgentConfig
The agent’s current configuration.
Sourcepub fn template(&self) -> &dyn ChatTemplate
pub fn template(&self) -> &dyn ChatTemplate
The chat template currently in use.
Sourcepub fn set_system_prompt(&mut self, prompt: impl Into<String>)
pub fn set_system_prompt(&mut self, prompt: impl Into<String>)
Replace the system prompt used for subsequent prompts.
Sourcepub fn set_inference_params(&mut self, params: InferenceParams)
pub fn set_inference_params(&mut self, params: InferenceParams)
Replace the inference parameters used for subsequent generations.
Sourcepub fn set_context_config(&mut self, config: ContextConfig)
pub fn set_context_config(&mut self, config: ContextConfig)
Replace the context-management configuration.
Sourcepub fn set_prune_strategy(&mut self, strategy: PruneStrategy)
pub fn set_prune_strategy(&mut self, strategy: PruneStrategy)
Select the strategy used when the conversation overflows the budget.
Sourcepub fn set_pinned(&mut self, message_id: &str, pinned: bool) -> bool
pub fn set_pinned(&mut self, message_id: &str, pinned: bool) -> bool
Pin or unpin a message by id. Pinned messages always survive context pruning. Returns whether a message with that id was found.
Sourcepub fn set_template(&mut self, template: Arc<dyn ChatTemplate>)
pub fn set_template(&mut self, template: Arc<dyn ChatTemplate>)
Swap the chat template at runtime (e.g. after detecting the model family).
Sourcepub fn set_tools(&mut self, tools: Vec<Box<dyn Tool>>)
pub fn set_tools(&mut self, tools: Vec<Box<dyn Tool>>)
Register the tools the agent may invoke during a prompt.
Available only with the tools feature (enabled by default).
Sourcepub fn replace_messages(&mut self, messages: Vec<Message>)
pub fn replace_messages(&mut self, messages: Vec<Message>)
Replace the entire conversation (e.g. when restoring a saved session).
Advances the internal id counter past any restored msg-N ids so newly
generated ids don’t collide with restored ones.
Sourcepub fn abort_flag(&self) -> Arc<AtomicBool>
pub fn abort_flag(&self) -> Arc<AtomicBool>
Clone of the shared abort flag, for wiring cancellation into a backend.
Sourcepub async fn prompt(
&mut self,
text: impl Into<String>,
backend: Arc<dyn LlmBackend>,
tx: UnboundedSender<AgentEvent>,
) -> CoreResult<()>
pub async fn prompt( &mut self, text: impl Into<String>, backend: Arc<dyn LlmBackend>, tx: UnboundedSender<AgentEvent>, ) -> CoreResult<()>
Run a prompt through the agent loop.
Accepts an event sender so the caller can consume events concurrently while generation is in progress. This enables real-time token streaming to the UI.
Flow:
- Adds the user message.
- Generates an assistant response (prune + template + LLM call),
streaming tokens via
tx. - If tools are registered and the response contains tool calls, runs each tool, appends a tool-result message, and loops back to the LLM.
- Repeats until the model returns a tool-free answer or the
max_tool_iterationsguard trips. - Emits lifecycle events for every step and updates conversation state.
Sourcepub fn prompt_stream(
&mut self,
text: impl Into<String>,
backend: Arc<dyn LlmBackend>,
) -> (UnboundedReceiver<AgentEvent>, impl Future<Output = CoreResult<()>> + '_)
pub fn prompt_stream( &mut self, text: impl Into<String>, backend: Arc<dyn LlmBackend>, ) -> (UnboundedReceiver<AgentEvent>, impl Future<Output = CoreResult<()>> + '_)
Convenience wrapper over prompt that creates the event
channel for you.
Returns the event receiver plus a future that drives generation. Poll the
future (e.g. with tokio::join!) while draining the receiver — the two
run concurrently so tokens stream as they’re produced:
let mut agent = Agent::new(AgentConfig::default());
let backend: Arc<dyn LlmBackend> = Arc::new(MockBackend);
let (mut rx, run) = agent.prompt_stream("Hello", backend);
let (result, reply) = tokio::join!(run, async move {
let mut reply = String::new();
while let Some(event) = rx.recv().await {
if let AgentEvent::MessageDelta { delta, .. } = event {
reply.push_str(&delta);
}
}
reply
});
result.unwrap();
assert_eq!(reply, "Hi!");