Struct Chat

Source
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

Source

pub fn with_system_prompt(self, prompt: impl Into<String>) -> Self

Sets system prompt and returns a new instance

Source

pub fn with_max_output_tokens(self, n: usize) -> Self

Sets max output tokens and returns a new instance

Source

pub fn with_history(self, history: Vec<Message>) -> Self

Sets history and returns a new instance

Source

pub fn add_message(self, msg: Message) -> Self

Adds a message to the conversation history and returns a new instance

Source

pub fn push_message(self, msg: Message) -> Self

Alias for add_message for backward compatibility

Source

pub fn tokens_used(&self) -> usize

Gets the current token count

Source

pub fn with_tool(self, tool: impl ToolDefinition) -> Result<Self>

Add a tool and returns a new instance with the tool added

Source

pub fn with_tools(self, tools: Vec<LlmToolInfo>) -> Self

Add multiple tools at once and return a new instance with the tools added

Source

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 tools
  • ToolChoice::None - Model must not use any tools
  • ToolChoice::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);
Source

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.

Source

pub fn most_recent_message(&self) -> Option<&Message>

Return the most recent message in the chat.

Trait Implementations§

Source§

impl Clone for Chat

Source§

fn clone(&self) -> Chat

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Chat

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Chat

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T