Skip to main content

BaseChatModel

Trait BaseChatModel 

Source
pub trait BaseChatModel: BaseLanguageModel<Vec<Message>, LLMResult> {
    // Required methods
    fn chat<'life0, 'async_trait>(
        &'life0 self,
        messages: Vec<Message>,
        config: Option<RunnableConfig>,
    ) -> Pin<Box<dyn Future<Output = Result<LLMResult, Self::Error>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn stream_chat<'life0, 'async_trait>(
        &'life0 self,
        messages: Vec<Message>,
        config: Option<RunnableConfig>,
    ) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<StreamChunk, Self::Error>> + Send>>, Self::Error>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;

    // Provided methods
    fn chat_with_system<'life0, 'async_trait>(
        &'life0 self,
        system: String,
        messages: Vec<Message>,
    ) -> Pin<Box<dyn Future<Output = Result<LLMResult, Self::Error>> + Send + 'async_trait>>
       where Self: Sync + 'async_trait,
             'life0: 'async_trait { ... }
    fn bind_tools(
        &self,
        _tools: Vec<ToolDefinition>,
    ) -> Option<Box<dyn BaseChatModel<Error = Self::Error> + Send + Sync>> { ... }
}
Expand description

Base trait for chat models.

Extends BaseLanguageModel for chat scenarios. Accepts message list as input, returns AI message.

Required Methods§

Source

fn chat<'life0, 'async_trait>( &'life0 self, messages: Vec<Message>, config: Option<RunnableConfig>, ) -> Pin<Box<dyn Future<Output = Result<LLMResult, Self::Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Chat with the model.

§Arguments
  • messages - Message list.
  • config - Optional configuration.
§Returns

LLM result.

Source

fn stream_chat<'life0, 'async_trait>( &'life0 self, messages: Vec<Message>, config: Option<RunnableConfig>, ) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<StreamChunk, Self::Error>> + Send>>, Self::Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Stream chat with the model.

§Arguments
  • messages - Message list.
  • config - Optional configuration.
§Returns

Stream of StreamChunk items. Chunks carry the text delta; the final chunk may additionally carry StreamChunk::token_usage when the provider reports usage.

Provided Methods§

Source

fn chat_with_system<'life0, 'async_trait>( &'life0 self, system: String, messages: Vec<Message>, ) -> Pin<Box<dyn Future<Output = Result<LLMResult, Self::Error>> + Send + 'async_trait>>
where Self: Sync + 'async_trait, 'life0: 'async_trait,

Chat with system prompt.

§Arguments
  • system - System prompt.
  • messages - Message list.
§Returns

LLM result.

Source

fn bind_tools( &self, _tools: Vec<ToolDefinition>, ) -> Option<Box<dyn BaseChatModel<Error = Self::Error> + Send + Sync>>

Bind tool definitions for function calling.

Returns Some(model) with the tools attached when the provider supports tool calling; returns None when it does not. The default returns None, signalling a hard capability limit — callers MUST treat None as “this model cannot call tools” and branch accordingly (e.g. fall back to text-only prompting). Providers that support function calling (OpenAI, Ollama) override this.

This is an explicit result, not a silent degrade: None is the honest answer that tool-calling is unavailable on this model.

Trait Implementations§

Source§

impl<E> BaseChatModel for Box<dyn BaseChatModel<Error = E> + Send + Sync>
where E: Error + Send + Sync + 'static,

Source§

fn chat<'life0, 'async_trait>( &'life0 self, messages: Vec<Message>, config: Option<RunnableConfig>, ) -> Pin<Box<dyn Future<Output = Result<LLMResult, Self::Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Chat with the model. Read more
Source§

fn stream_chat<'life0, 'async_trait>( &'life0 self, messages: Vec<Message>, config: Option<RunnableConfig>, ) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<StreamChunk, Self::Error>> + Send>>, Self::Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Stream chat with the model. Read more
Source§

fn bind_tools( &self, tools: Vec<ToolDefinition>, ) -> Option<Box<dyn BaseChatModel<Error = Self::Error> + Send + Sync>>

Bind tool definitions for function calling. Read more
Source§

fn chat_with_system<'life0, 'async_trait>( &'life0 self, system: String, messages: Vec<Message>, ) -> Pin<Box<dyn Future<Output = Result<LLMResult, Self::Error>> + Send + 'async_trait>>
where Self: Sync + 'async_trait, 'life0: 'async_trait,

Chat with system prompt. Read more
Source§

impl<E> BaseLanguageModel<Vec<Message>, LLMResult> for Box<dyn BaseChatModel<Error = E> + Send + Sync>
where E: Error + Send + Sync + 'static,

Source§

fn model_name(&self) -> &str

Returns the model name.
Source§

fn get_num_tokens(&self, text: &str) -> usize

Calculates token count for text. Read more
Source§

fn temperature(&self) -> Option<f32>

Returns the temperature parameter.
Source§

fn max_tokens(&self) -> Option<usize>

Returns the max tokens limit.
Source§

fn with_temperature(self, _temp: f32) -> Self
where Self: Sized,

Sets the temperature parameter.
Source§

fn with_max_tokens(self, _max: usize) -> Self
where Self: Sized,

Sets the max tokens limit.
Source§

impl<E> Runnable<Vec<Message>, LLMResult> for Box<dyn BaseChatModel<Error = E> + Send + Sync>
where E: Error + Send + Sync + 'static,

Source§

type Error = E

Error type.
Source§

fn invoke<'life0, 'async_trait>( &'life0 self, input: Vec<Message>, config: Option<RunnableConfig>, ) -> Pin<Box<dyn Future<Output = Result<LLMResult, Self::Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Transforms single input to output. Read more
Source§

fn stream<'life0, 'async_trait>( &'life0 self, input: Vec<Message>, config: Option<RunnableConfig>, ) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<LLMResult, Self::Error>> + Send>>, Self::Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Streaming output - for real-time responses (LLM, etc). Read more
Source§

fn batch<'life0, 'async_trait>( &'life0 self, inputs: Vec<Input>, config: Option<RunnableConfig>, ) -> Pin<Box<dyn Future<Output = Result<Vec<Output>, Self::Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Batch processing - transforms multiple inputs to outputs. Read more
Source§

fn batch_as_completed<'life0, 'async_trait>( &'life0 self, inputs: Vec<Input>, config: Option<RunnableConfig>, ) -> Pin<Box<dyn Future<Output = Result<Vec<(usize, Output)>, Self::Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Batch processing that returns results in completion order. Read more
Source§

fn transform<'life0, 'async_trait>( &'life0 self, input: Pin<Box<dyn Stream<Item = Result<Input, Self::Error>> + Send>>, config: Option<RunnableConfig>, ) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<Output, Self::Error>> + Send + '_>>, Self::Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Stream-to-stream transformation - the core of LCEL streaming. Read more

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<E> BaseChatModel for Box<dyn BaseChatModel<Error = E> + Send + Sync>
where E: Error + Send + Sync + 'static,

Source§

fn chat<'life0, 'async_trait>( &'life0 self, messages: Vec<Message>, config: Option<RunnableConfig>, ) -> Pin<Box<dyn Future<Output = Result<LLMResult, Self::Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn stream_chat<'life0, 'async_trait>( &'life0 self, messages: Vec<Message>, config: Option<RunnableConfig>, ) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<StreamChunk, Self::Error>> + Send>>, Self::Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn bind_tools( &self, tools: Vec<ToolDefinition>, ) -> Option<Box<dyn BaseChatModel<Error = Self::Error> + Send + Sync>>

Implementors§