vtcode_core/llm/
mod.rs

1//! # LLM Integration Layer
2//!
3//! This module provides a unified, modular interface for integrating multiple LLM providers
4//! with VTCode, supporting Gemini, OpenAI, Anthropic, xAI, and DeepSeek.
5//!
6//! ## Architecture Overview
7//!
8//! The LLM layer is designed with several key principles:
9//!
10//! - **Unified Interface**: Single `AnyClient` trait for all providers
11//! - **Provider Agnostic**: Easy switching between providers
12//! - **Configuration Driven**: TOML-based provider configuration
13//! - **Error Handling**: Comprehensive error types and recovery
14//! - **Async Support**: Full async/await support for all operations
15//!
16//! ## Supported Providers
17//!
18//! | Provider | Status | Models |
19//! |----------|--------|---------|
20//! | Gemini | ✅ | gemini-2.5-pro, gemini-2.5-flash-preview-05-20 |
21//! | OpenAI | ✅ | gpt-5, gpt-4.1, gpt-5-mini |
22//! | Anthropic | ✅ | claude-4.1-opus, claude-4-sonnet |
23//! | xAI | ✅ | grok-2-latest, grok-2-mini |
24//! | DeepSeek | ✅ | deepseek-chat |
25//!
26//! ## Basic Usage
27//!
28//! ```rust,no_run
29//! use vtcode_core::llm::{AnyClient, make_client};
30//! use vtcode_core::utils::dot_config::ProviderConfigs;
31//!
32//! #[tokio::main]
33//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
34//!     // Configure providers
35//!     let providers = ProviderConfigs {
36//!         gemini: Some(vtcode_core::utils::dot_config::ProviderConfig {
37//!             api_key: std::env::var("GEMINI_API_KEY")?,
38//!             model: "gemini-2.5-flash".to_string(),
39//!             ..Default::default()
40//!         }),
41//!         ..Default::default()
42//!     };
43//!
44//!     // Create client
45//!     let client = make_client(&providers, "gemini")?;
46//!
47//!     // Make a request
48//!     let messages = vec![
49//!         vtcode_core::llm::types::Message {
50//!             role: "user".to_string(),
51//!             content: "Hello, how can you help me with coding?".to_string(),
52//!         }
53//!     ];
54//!
55//!     let response = client.chat(&messages, None).await?;
56//!     println!("Response: {}", response.content);
57//!
58//!     Ok(())
59//! }
60//! ```
61//!
62//! ## Provider Configuration
63//!
64//! ```rust,no_run
65//! use vtcode_core::utils::dot_config::{ProviderConfigs, ProviderConfig};
66//!
67//! let config = ProviderConfigs {
68//!     gemini: Some(ProviderConfig {
69//!         api_key: "your-api-key".to_string(),
70//!         model: "gemini-2.5-flash".to_string(),
71//!         temperature: Some(0.7),
72//!         max_tokens: Some(4096),
73//!         ..Default::default()
74//!     }),
75//!     openai: Some(ProviderConfig {
76//!         api_key: "your-openai-key".to_string(),
77//!         model: "gpt-5".to_string(),
78//!         temperature: Some(0.3),
79//!         max_tokens: Some(8192),
80//!         ..Default::default()
81//!     }),
82//!     ..Default::default()
83//! };
84//! ```
85//!
86//! ## Advanced Features
87//!
88//! ### Streaming Responses
89//! ```rust,no_run
90//! use vtcode_core::llm::AnyClient;
91//! use futures::StreamExt;
92//!
93//! let client = make_client(&providers, "gemini")?;
94//!
95//! let mut stream = client.chat_stream(&messages, None).await?;
96//! while let Some(chunk) = stream.next().await {
97//!     match chunk {
98//!         Ok(response) => print!("{}", response.content),
99//!         Err(e) => eprintln!("Error: {}", e),
100//!     }
101//! }
102//! ```
103//!
104//! ### Function Calling
105//! ```rust,no_run
106//! use vtcode_core::llm::types::{FunctionDeclaration, FunctionCall};
107//!
108//! let functions = vec![
109//!     FunctionDeclaration {
110//!         name: "read_file".to_string(),
111//!         description: "Read a file from the filesystem".to_string(),
112//!         parameters: serde_json::json!({
113//!             "type": "object",
114//!             "properties": {
115//!                 "path": {"type": "string", "description": "File path to read"}
116//!             },
117//!             "required": ["path"]
118//!         }),
119//!     }
120//! ];
121//!
122//! let response = client.chat_with_functions(&messages, &functions, None).await?;
123//!
124//! if let Some(function_call) = response.function_call {
125//!     match function_call.name.as_str() {
126//!         "read_file" => {
127//!             // Handle function call
128//!         }
129//!         _ => {}
130//!     }
131//! }
132//! ```
133//!
134//! ## Error Handling
135//!
136//! The LLM layer provides comprehensive error handling:
137//!
138//! ```rust,no_run
139//! use vtcode_core::llm::LLMError;
140//!
141//! match client.chat(&messages, None).await {
142//!     Ok(response) => println!("Success: {}", response.content),
143//!     Err(LLMError::Authentication) => eprintln!("Authentication failed"),
144//!     Err(LLMError::RateLimit) => eprintln!("Rate limit exceeded"),
145//!     Err(LLMError::Network(e)) => eprintln!("Network error: {}", e),
146//!     Err(LLMError::Provider(e)) => eprintln!("Provider error: {}", e),
147//!     Err(e) => eprintln!("Other error: {}", e),
148//! }
149//! ```
150//!
151//! ## Performance Considerations
152//!
153//! - **Connection Pooling**: Efficient connection reuse
154//! - **Request Batching**: Where supported by providers
155//! - **Caching**: Built-in prompt caching for repeated requests
156//! - **Timeout Handling**: Configurable timeouts and retries
157//! - **Rate Limiting**: Automatic rate limit handling
158//!
159//! # LLM abstraction layer with modular architecture
160//!
161//! This module provides a unified interface for different LLM providers
162//! with provider-specific implementations.
163
164pub mod client;
165pub mod error_display;
166pub mod factory;
167pub mod provider;
168pub mod providers;
169pub mod types;
170
171#[cfg(test)]
172mod error_display_test;
173
174// Re-export main types for backward compatibility
175pub use client::{AnyClient, make_client};
176pub use factory::{create_provider_with_config, get_factory};
177pub use provider::{LLMStream, LLMStreamEvent};
178pub use providers::{AnthropicProvider, GeminiProvider, OpenAIProvider, XAIProvider};
179pub use types::{BackendKind, LLMError, LLMResponse};