Module provider

Module provider 

Source
Expand description

Provider trait and types for LLM abstraction.

This module defines the LlmProvider trait that all LLM providers implement, along with request/response types, tool definitions, and configuration.

§Overview

The provider abstraction allows you to:

  • Switch between LLM providers without changing your application code
  • Use a consistent interface for all LLM operations
  • Access provider-specific features (like caching) through unified APIs

§Provider Trait

All providers implement LlmProvider, which provides:

§Tool Calling

Define tools with Tool and handle the calling flow:

use multi_llm::{Tool, ToolChoice, ToolCall, ToolResult};

// Define a tool
let weather_tool = Tool {
    name: "get_weather".to_string(),
    description: "Get current weather for a city".to_string(),
    parameters: serde_json::json!({
        "type": "object",
        "properties": {
            "city": {"type": "string", "description": "City name"}
        },
        "required": ["city"]
    }),
};

// Handle a tool call from the LLM
let tool_call = ToolCall {
    id: "call_123".to_string(),
    name: "get_weather".to_string(),
    arguments: serde_json::json!({"city": "London"}),
};

// Return the result
let result = ToolResult {
    tool_call_id: "call_123".to_string(),
    content: "Sunny, 22°C".to_string(),
    is_error: false,
    error_category: None,
};

§Response Structure

All providers return a Response containing:

  • Text content (for standard requests)
  • Structured JSON (when using execute_structured_llm)
  • Tool calls (when the model wants to call functions)
  • Token usage statistics

Structs§

RequestConfig
Configuration for a single LLM request.
Response
Response from an LLM operation.
ResponseFormat
Schema specification for structured JSON output.
TokenUsage
Token usage statistics for an LLM request.
Tool
Definition of a tool/function that the LLM can call.
ToolCall
A tool call generated by the LLM.
ToolCallingRound
State for a tool calling round in multi-turn conversations.
ToolResult
Result from executing a tool, sent back to the LLM.

Enums§

ToolChoice
Strategy for how the LLM should handle tool selection.

Traits§

LlmProvider
Trait implemented by all LLM providers.

Type Aliases§

LLMRequestConfig
Type aliases for backward compatibility
LLMResponseFormat
LLMTokenUsage
Result
Result type alias for provider operations.