pub struct Context {
pub system_prompt: Option<String>,
pub messages: Vec<Message>,
pub tools: Vec<Tool>,
}Expand description
Shared conversation context. Conversation context for LLM interactions.
Fields§
§system_prompt: Option<String>System prompt sent with each request
messages: Vec<Message>Conversation history
tools: Vec<Tool>Available tools for this context
Implementations§
Source§impl Context
impl Context
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new empty context
§Examples
use oxi_ai::Context;
let mut ctx = Context::new();
ctx.set_system_prompt("You are a helpful assistant.");Sourcepub fn with_system_prompt(self, prompt: impl Into<String>) -> Self
pub fn with_system_prompt(self, prompt: impl Into<String>) -> Self
Create a context with a system prompt
Sourcepub fn add_message(&mut self, message: Message)
pub fn add_message(&mut self, message: Message)
Add a message to the context
§Examples
use oxi_ai::{Context, Message, UserMessage};
let mut ctx = Context::new();
ctx.add_message(Message::User(UserMessage::new("Hello!")));
assert_eq!(ctx.len(), 1);Sourcepub fn last_message(&self) -> Option<&Message>
pub fn last_message(&self) -> Option<&Message>
Get the last message
§Examples
use oxi_ai::{Context, Message, UserMessage};
let mut ctx = Context::new();
ctx.add_message(Message::User(UserMessage::new("First")));
ctx.add_message(Message::User(UserMessage::new("Second")));
assert!(ctx.last_message().is_some());Sourcepub fn set_system_prompt(&mut self, prompt: impl Into<String>)
pub fn set_system_prompt(&mut self, prompt: impl Into<String>)
Set the system prompt
Sourcepub fn clear_system_prompt(&mut self)
pub fn clear_system_prompt(&mut self)
Clear the system prompt
Sourcepub fn set_tools(&mut self, tools: Vec<Tool>)
pub fn set_tools(&mut self, tools: Vec<Tool>)
Set available tools
§Examples
use oxi_ai::{Context, Tool};
let mut ctx = Context::new();
let tool = Tool::new(
"search",
"Search the web",
serde_json::json!({"type": "object", "properties": {}}),
);
ctx.set_tools(vec![tool]);
assert_eq!(ctx.tools.len(), 1);Sourcepub fn system_prompt(&self) -> Option<&str>
pub fn system_prompt(&self) -> Option<&str>
Get the system prompt for this context.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Context
impl<'de> Deserialize<'de> for Context
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations§
impl Freeze for Context
impl RefUnwindSafe for Context
impl Send for Context
impl Sync for Context
impl Unpin for Context
impl UnsafeUnpin for Context
impl UnwindSafe for Context
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
Mutably borrows from an owned value. Read more