Skip to main content

Module llm

Module llm 

Source
Expand description

§Language Models and Conversation Management

This module provides everything you need to work with language models in a provider-agnostic way. Build chat applications, generate structured output, and integrate tools without being tied to any specific AI service.

§Core Components

  • LanguageModel - The main trait for text generation and conversation
  • LLMRequest - Encapsulates messages, tools, and parameters for model calls
  • Event - Stream events from the model (text, reasoning, tool calls)
  • Message - Represents individual messages in a conversation
  • Tool - Function calling interface for extending model capabilities

§Design Philosophy

The core crate provides a low-level API that emits events without executing tools. Tool execution is the responsibility of higher-level abstractions like aither-agent.

This design allows:

  • Full control over tool execution flow
  • Hooks for intercepting and modifying tool calls
  • Proper context management between turns
  • Clean separation between LLM communication and agent logic

§Quick Start

§Basic Conversation

use aither::llm::{LanguageModel, Event, oneshot};
use futures_lite::StreamExt;

async fn chat_with_model(model: impl LanguageModel) -> Result<String, Box<dyn std::error::Error>> {
    let request = oneshot("You are a helpful assistant", "What's the capital of Japan?");
    let mut stream = model.respond(request);
    let mut full_text = String::new();

    while let Some(event) = stream.next().await {
        match event? {
            Event::Text(chunk) => full_text.push_str(&chunk),
            Event::Reasoning(thought) => eprintln!("[thinking] {}", thought),
            Event::ToolCall(call) => {
                // Handle tool call (typically done by agent crate)
                println!("Tool requested: {}", call.name);
            }
            _ => {}
        }
    }

    Ok(full_text)
}

§With Tools (Agent-Controlled)

use aither::llm::{LanguageModel, Event, LLMRequest, Message};

// The core crate does NOT execute tools - it emits ToolCall events.
// Tool execution should be handled by the agent crate.
let request = LLMRequest::new([Message::user("What's the weather?")])
    .with_tool_definitions(vec![weather_tool_definition()]);

let mut stream = model.respond(request);
while let Some(event) = stream.next().await {
    match event? {
        Event::ToolCall(call) => {
            // Execute tool and continue conversation
            let result = my_tool_executor.execute(&call).await;
            // Add result to messages and send another request...
        }
        _ => {}
    }
}

Re-exports§

pub use event::Event;
pub use event::ToolCall;
pub use event::Usage;
pub use message::Attachment;
pub use message::Message;
pub use message::Role;
pub use provider::LanguageModelProvider;
pub use reasoning::ReasoningState;
pub use researcher::ResearchCitation;
pub use researcher::ResearchEvent;
pub use researcher::ResearchFinding;
pub use researcher::ResearchOptions;
pub use researcher::ResearchReport;
pub use researcher::ResearchRequest;
pub use researcher::ResearchSource;
pub use researcher::ResearchStage;
pub use researcher::Researcher;
pub use researcher::ResearcherProfile;
pub use tool::IntoToolResult;
pub use tool::Tool;
pub use tool::ToolResult;

Modules§

assistant
Assistant module for managing assistant-related functionality.
event
Event types for streaming responses. LLM response events.
message
Message types and conversation handling. Message types for AI language model conversations.
model
Model profiles and capabilities. AI language model configuration and profiling types.
provider
Provider module for managing language model providers and their configurations.
reasoning
Provider-opaque reasoning state carried across turns. Provider-opaque reasoning state.
researcher
Deep research workflows and agent capabilities. Deep research workflows and agent-based investigation capabilities.
tool
Tool system for function calling.

Structs§

LLMRequest
Builder-style request passed into LanguageModel::respond.
LLMRequestWithTools
Legacy request builder that supports mutable tool registry.

Enums§

GenerateError
Why a structured-output call failed.

Traits§

LanguageModel
Language models for text generation and conversation.

Functions§

collect_text
Collects text from an event stream.
oneshot
Convenience helper that creates a single system + user LLMRequest.