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:
execute_llm(): Execute a standard LLM requestexecute_structured_llm(): Execute with JSON schema outputprovider_name(): Get the provider identifier
§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§
- Request
Config - Configuration for a single LLM request.
- Response
- Response from an LLM operation.
- Response
Format - Schema specification for structured JSON output.
- Token
Usage - Token usage statistics for an LLM request.
- Tool
- Definition of a tool/function that the LLM can call.
- Tool
Call - A tool call generated by the LLM.
- Tool
Calling Round - State for a tool calling round in multi-turn conversations.
- Tool
Result - Result from executing a tool, sent back to the LLM.
Enums§
- Tool
Choice - Strategy for how the LLM should handle tool selection.
Traits§
- LlmProvider
- Trait implemented by all LLM providers.
Type Aliases§
- LLMRequest
Config - Type aliases for backward compatibility
- LLMResponse
Format - LLMToken
Usage - Result
- Result type alias for provider operations.