ChatModel

Trait ChatModel 

Source
pub trait ChatModel: Send + Sync {
    // Required methods
    fn llm_type(&self) -> &str;
    fn model_name(&self) -> &str;
    fn generate<'life0, 'async_trait>(
        &'life0 self,
        messages: Vec<BaseMessage>,
        stop: Option<Vec<String>>,
    ) -> Pin<Box<dyn Future<Output = Result<ChatResult>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;

    // Provided methods
    fn generate_with_tools<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        messages: Vec<BaseMessage>,
        tools: &'life1 [ToolDefinition],
        tool_choice: Option<&'life2 ToolChoice>,
        stop: Option<Vec<String>>,
    ) -> Pin<Box<dyn Future<Output = Result<ChatResult>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait { ... }
    fn stream<'life0, 'async_trait>(
        &'life0 self,
        messages: Vec<BaseMessage>,
        stop: Option<Vec<String>>,
    ) -> Pin<Box<dyn Future<Output = Result<ChatStream>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
    fn get_ls_params(&self, stop: Option<&[String]>) -> LangSmithParams { ... }
    fn identifying_params(&self) -> Value { ... }
}
Expand description

Base trait for all chat models.

This trait follows the LangChain pattern where each provider implements the core generation methods. The trait provides both sync-style (via async) and streaming interfaces.

§Example Implementation

use agent_chain_core::chat_model::{ChatModel, ChatResult};
use agent_chain_core::messages::BaseMessage;

struct MyChatModel {
    model: String,
}

#[async_trait::async_trait]
impl ChatModel for MyChatModel {
    fn llm_type(&self) -> &str {
        "my-chat-model"
    }

    async fn generate(
        &self,
        messages: Vec<BaseMessage>,
        stop: Option<Vec<String>>,
    ) -> Result<ChatResult> {
        // Implementation here
        todo!()
    }
}

Required Methods§

Source

fn llm_type(&self) -> &str

Return the type identifier for this chat model.

This is used for logging and tracing purposes.

Source

fn model_name(&self) -> &str

Get the model name/identifier.

Source

fn generate<'life0, 'async_trait>( &'life0 self, messages: Vec<BaseMessage>, stop: Option<Vec<String>>, ) -> Pin<Box<dyn Future<Output = Result<ChatResult>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Generate a response from the model.

§Arguments
  • messages - The conversation history.
  • stop - Optional stop sequences.
§Returns

A ChatResult containing the generated message and metadata.

Provided Methods§

Source

fn generate_with_tools<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, messages: Vec<BaseMessage>, tools: &'life1 [ToolDefinition], tool_choice: Option<&'life2 ToolChoice>, stop: Option<Vec<String>>, ) -> Pin<Box<dyn Future<Output = Result<ChatResult>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Generate a response from the model with tools.

This is the preferred method when tool calling is needed. Default implementation ignores tools and calls generate. Providers should override this to enable tool calling.

§Arguments
  • messages - The conversation history.
  • tools - Tool definitions for the model to use.
  • tool_choice - Optional configuration for tool selection.
  • stop - Optional stop sequences.
§Returns

A ChatResult containing the generated message and metadata.

Source

fn stream<'life0, 'async_trait>( &'life0 self, messages: Vec<BaseMessage>, stop: Option<Vec<String>>, ) -> Pin<Box<dyn Future<Output = Result<ChatStream>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Generate a streaming response from the model.

Default implementation calls generate and wraps the result in a stream. Providers should override this for native streaming support.

§Arguments
  • messages - The conversation history.
  • stop - Optional stop sequences.
§Returns

A stream of ChatChunks.

Source

fn get_ls_params(&self, stop: Option<&[String]>) -> LangSmithParams

Get parameters for tracing/monitoring.

Source

fn identifying_params(&self) -> Value

Get identifying parameters for serialization.

Implementors§