pub struct Chat {
pub system_prompt: String,
pub max_output_tokens: usize,
pub history: Vec<Message>,
pub tools: Option<Vec<LlmToolInfo>>,
pub tool_choice: Option<ToolChoice>,
/* private fields */
}Expand description
The main Chat client that users will interact with. All methods return a new instance rather than mutating the existing one, following the immutable builder pattern.
Fields§
§system_prompt: String§max_output_tokens: usize§history: Vec<Message>§tools: Option<Vec<LlmToolInfo>>§tool_choice: Option<ToolChoice>Implementations§
Source§impl Chat
impl Chat
Sourcepub fn with_system_prompt(self, prompt: impl Into<String>) -> Self
pub fn with_system_prompt(self, prompt: impl Into<String>) -> Self
Sets system prompt and returns a new instance
Sourcepub fn with_max_output_tokens(self, n: usize) -> Self
pub fn with_max_output_tokens(self, n: usize) -> Self
Sets max output tokens and returns a new instance
Sourcepub fn with_history(self, history: Vec<Message>) -> Self
pub fn with_history(self, history: Vec<Message>) -> Self
Sets history and returns a new instance
Sourcepub fn add_message(self, msg: Message) -> Self
pub fn add_message(self, msg: Message) -> Self
Adds a message to the conversation history and returns a new instance
Sourcepub fn push_message(self, msg: Message) -> Self
pub fn push_message(self, msg: Message) -> Self
Alias for add_message for backward compatibility
Sourcepub fn tokens_used(&self) -> usize
pub fn tokens_used(&self) -> usize
Gets the current token count
Sourcepub fn with_tool(self, tool: impl ToolDefinition) -> Result<Self>
pub fn with_tool(self, tool: impl ToolDefinition) -> Result<Self>
Add a tool and returns a new instance with the tool added
Sourcepub fn with_tools(self, tools: Vec<LlmToolInfo>) -> Self
pub fn with_tools(self, tools: Vec<LlmToolInfo>) -> Self
Add multiple tools at once and return a new instance with the tools added
Sourcepub fn with_tool_choice(self, choice: ToolChoice) -> Self
pub fn with_tool_choice(self, choice: ToolChoice) -> Self
Sets the tool choice strategy and returns a new instance
This method allows configuring how the model should choose tools:
ToolChoice::Auto- Model can choose whether to use a tool (default)ToolChoice::Any- Model must use one of the available toolsToolChoice::None- Model must not use any toolsToolChoice::Specific(name)- Model must use the specified tool
Different providers implement this with slightly different terminology:
- OpenAI/Mistral use “auto”, “required”, “none”
- Anthropic uses “auto”, “any”, “none”
- Gemini uses function_calling_config with modes
The library transparently handles these differences, providing a consistent API regardless of which provider you’re using.
§Examples
use language_barrier_core::{Chat, tool::ToolChoice};
// Require using a tool
let chat = Chat::default()
.with_tool_choice(ToolChoice::Any);
// Specify a tool by name
let chat = Chat::default()
.with_tool_choice(ToolChoice::Specific("weather_tool".to_string()));
// Disable tools for this conversation
let chat = Chat::default()
.with_tool_choice(ToolChoice::None);Sourcepub fn without_tool_choice(self) -> Self
pub fn without_tool_choice(self) -> Self
Removes tool choice configuration and returns a new instance
This resets to the default behavior, where the model can choose whether to use tools.
Sourcepub fn most_recent_message(&self) -> Option<&Message>
pub fn most_recent_message(&self) -> Option<&Message>
Return the most recent message in the chat.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Chat
impl RefUnwindSafe for Chat
impl Send for Chat
impl Sync for Chat
impl Unpin for Chat
impl UnwindSafe for Chat
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)