Skip to content

Commit e5eecb4

Browse files
committed
feat: Implement SDK error handling and middleware structure
- Added SDKError enum for comprehensive error management in the SDK. - Introduced Middleware trait and MiddlewareStack for future middleware implementations. - Created a unified SDK module with re-exports for ease of use. - Developed a simplified LLM client with basic chat functionality and provider management. - Established a provider registry for managing multiple LLM providers. - Implemented monitoring and routing structures as placeholders for future enhancements. - Added extensive data types for messages, tools, and chat interactions. - Created integration tests for SDK functionality, including configuration and error handling. - Updated server routes to expose chat and completion endpoints.
1 parent 434e8cc commit e5eecb4

File tree

26 files changed

+2065
-176
lines changed

26 files changed

+2065
-176
lines changed

CLAUDE.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# CLAUDE.md
2+
3+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4+
5+
## Essential Commands
6+
7+
### Development Commands
8+
- **Start development**: `make dev` or `cargo run` (auto-loads config/gateway.yaml)
9+
- **Build**: `cargo build --all-features`
10+
- **Test**: `cargo test --all-features`
11+
- **Lint**: `cargo clippy --all-targets --all-features -- -D warnings`
12+
- **Format**: `cargo fmt --all`
13+
- **Quick start**: `make start` (fastest way to start the gateway)
14+
15+
### Testing Commands
16+
- **All tests**: `make test`
17+
- **Unit tests only**: `make test-unit`
18+
- **Integration tests**: `make test-integration`
19+
- **Test coverage**: `make test-coverage`
20+
- **Single test**: `cargo test <test_name> --all-features`
21+
22+
### Development Services
23+
- **Start dev services**: `make dev-services` (starts PostgreSQL, Redis)
24+
- **Stop dev services**: `make dev-stop`
25+
- **Database migration**: `make db-migrate`
26+
- **Reset database**: `make db-reset`
27+
28+
## Architecture Overview
29+
30+
This is a **high-performance AI Gateway** written in Rust that provides OpenAI-compatible APIs with intelligent routing across 20+ AI providers.
31+
32+
### Core Components
33+
34+
**Gateway Architecture**: Modular, trait-based design with dependency injection
35+
- `src/core/` - Central orchestrator and business logic
36+
- `src/server/` - Actix-web HTTP server with middleware pipeline
37+
- `src/auth/` - Multi-layered authentication (JWT, API keys, RBAC)
38+
- `src/core/providers/` - Pluggable provider system (OpenAI, Anthropic, Azure, Google, etc.)
39+
- `src/core/router/` - Intelligent routing with multiple strategies
40+
- `src/storage/` - Multi-backend storage (PostgreSQL, Redis, S3, Vector DB)
41+
- `src/monitoring/` - Observability (Prometheus, tracing, health checks)
42+
43+
### Key Design Patterns
44+
- **Async-first**: All I/O is non-blocking using Tokio
45+
- **Trait-based abstractions**: Pluggable components via traits
46+
- **Error handling**: Comprehensive error types with context preservation
47+
- **Configuration**: Type-safe config models with Default implementations
48+
49+
### Provider Integration
50+
- **Unified Provider trait**: Common interface for all AI providers
51+
- **Format conversion**: Automatic translation between OpenAI and provider-specific APIs
52+
- **Health monitoring**: Per-provider health checks and failover
53+
- **Cost calculation**: Built-in token counting and cost estimation
54+
55+
### Request Flow
56+
1. HTTP Request → Authentication → Authorization → Router → Provider → Response
57+
2. Middleware pipeline handles auth, logging, metrics, and transformations
58+
3. Intelligent routing selects optimal provider based on health, latency, cost
59+
60+
## Configuration
61+
62+
- **Main config**: `config/gateway.yaml` (auto-loaded by default)
63+
- **Example config**: `config/gateway.yaml.example`
64+
- **Environment variables**: Override config values with `${ENV_VAR}` syntax
65+
- **Config validation**: `make config-validate`
66+
67+
## Important Files
68+
69+
- `src/main.rs` - Application entry point
70+
- `src/lib.rs` - Library entry point with core Gateway struct
71+
- `Cargo.toml` - Dependencies and features (use `--all-features` for development)
72+
- `Makefile` - All development commands and workflows
73+
- `config/gateway.yaml` - Main configuration file
74+
75+
## Binaries
76+
77+
- `gateway` (default) - Main gateway server
78+
- `google-gateway` - Specialized Google API gateway
79+
80+
## Features
81+
82+
The codebase uses Cargo features extensively:
83+
- **Storage**: `postgres`, `sqlite`, `redis`, `s3`
84+
- **Monitoring**: `metrics`, `tracing`
85+
- **Advanced**: `vector-db`, `websockets`, `analytics`, `enterprise`
86+
- **Development**: Use `--all-features` flag for full functionality
87+
88+
## Database & Storage
89+
90+
- **Primary DB**: PostgreSQL with SQLx migrations
91+
- **Cache**: Redis for high-speed operations
92+
- **File storage**: S3-compatible object storage
93+
- **Vector DB**: Optional Qdrant integration for semantic caching
94+
95+
## Testing Architecture
96+
97+
- Unit tests in each module (`#[cfg(test)]`)
98+
- Integration tests in `tests/integration_tests.rs`
99+
- Postman collections for API testing
100+
- Mock implementations for external services
101+
102+
## Common Development Patterns
103+
104+
1. **Adding new providers**: Implement the `Provider` trait in `src/core/providers/`
105+
2. **New API endpoints**: Add routes in `src/server/routes/`
106+
3. **Authentication**: Extend auth modules in `src/auth/`
107+
4. **Configuration**: Update models in `src/config/models/`
108+
5. **Monitoring**: Add metrics in respective modules
109+
110+
## Docker & Deployment
111+
112+
- **Docker build**: `make docker`
113+
- **Development stack**: `make docker-compose-dev`
114+
- **Production**: `make docker-compose`
115+
- **Kubernetes**: `make k8s-apply`
116+
117+
## Performance Characteristics
118+
119+
- **Throughput**: 10,000+ requests/second
120+
- **Latency**: <10ms routing overhead
121+
- **Memory**: ~50MB base footprint
122+
- **Architecture**: Fully async, connection pooling, zero-copy where possible

SDK_README.md

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
# LiteLLM Unified SDK
2+
3+
这是基于LiteLLM-RS项目构建的统一LLM Provider SDK,提供了简化的接口来与多个LLM提供商进行交互。
4+
5+
## 🎯 特性
6+
7+
- **统一接口**: 使用相同的API与OpenAI、Anthropic、Azure等多个provider交互
8+
- **类型安全**: 基于Rust的强类型系统,编译时错误检查
9+
- **简化配置**: 灵活的配置选项,支持环境变量和配置文件
10+
- **高性能**: 基于现有的LiteLLM-RS高性能架构
11+
- **易于扩展**: 模块化设计,轻松添加新的provider
12+
13+
## 📁 文件结构
14+
15+
```
16+
src/sdk/
17+
├── mod.rs # SDK主模块
18+
├── simple_client.rs # 简化的LLM客户端
19+
├── config.rs # 配置系统
20+
├── types.rs # 数据类型定义
21+
├── errors.rs # 错误处理
22+
├── providers.rs # Provider注册器
23+
├── middleware.rs # 中间件系统(占位符)
24+
├── cache.rs # 缓存系统(占位符)
25+
├── router.rs # 路由系统(占位符)
26+
└── monitoring.rs # 监控系统(占位符)
27+
```
28+
29+
## 🚀 快速开始
30+
31+
### 1. 基本使用
32+
33+
```rust
34+
use litellm_rs::sdk::*;
35+
36+
#[tokio::main]
37+
async fn main() -> Result<()> {
38+
// 创建配置
39+
let config = ConfigBuilder::new()
40+
.add_openai("openai", "your-api-key")
41+
.add_anthropic("anthropic", "your-anthropic-key")
42+
.default_provider("openai")
43+
.build();
44+
45+
// 创建客户端
46+
let client = LLMClient::new(config)?;
47+
48+
// 发送消息
49+
let messages = vec![
50+
Message {
51+
role: Role::User,
52+
content: Some(Content::Text("Hello!".to_string())),
53+
name: None,
54+
tool_calls: None,
55+
}
56+
];
57+
58+
let response = client.chat(messages).await?;
59+
println!("Response: {:?}", response);
60+
61+
Ok(())
62+
}
63+
```
64+
65+
### 2. 从环境变量配置
66+
67+
```rust
68+
use litellm_rs::sdk::*;
69+
70+
#[tokio::main]
71+
async fn main() -> Result<()> {
72+
// 从环境变量加载配置
73+
// 需要设置 OPENAI_API_KEY 或 ANTHROPIC_API_KEY
74+
let config = ClientConfig::from_env()?;
75+
let client = LLMClient::new(config)?;
76+
77+
// 使用客户端...
78+
Ok(())
79+
}
80+
```
81+
82+
### 3. 从配置文件加载
83+
84+
```yaml
85+
# config.yaml
86+
default_provider: "openai"
87+
88+
providers:
89+
- id: "openai"
90+
provider_type: "openai"
91+
name: "OpenAI"
92+
api_key: "${OPENAI_API_KEY}"
93+
models: ["gpt-4", "gpt-3.5-turbo"]
94+
enabled: true
95+
weight: 1.0
96+
rate_limit_rpm: 3000
97+
rate_limit_tpm: 250000
98+
99+
- id: "anthropic"
100+
provider_type: "anthropic"
101+
name: "Anthropic"
102+
api_key: "${ANTHROPIC_API_KEY}"
103+
models: ["claude-3-opus-20240229"]
104+
enabled: true
105+
weight: 1.0
106+
107+
settings:
108+
timeout: 30
109+
max_retries: 3
110+
enable_logging: true
111+
```
112+
113+
```rust
114+
let config = ClientConfig::from_file("config.yaml")?;
115+
let client = LLMClient::new(config)?;
116+
```
117+
118+
## 📊 配置选项
119+
120+
### ClientConfig
121+
122+
- `default_provider`: 默认使用的provider ID
123+
- `providers`: provider配置列表
124+
- `settings`: 全局设置
125+
126+
### ProviderConfig
127+
128+
- `id`: provider唯一标识
129+
- `provider_type`: provider类型(OpenAI、Anthropic等)
130+
- `name`: 显示名称
131+
- `api_key`: API密钥
132+
- `base_url`: 自定义API端点(可选)
133+
- `models`: 支持的模型列表
134+
- `enabled`: 是否启用
135+
- `weight`: 负载均衡权重
136+
- `rate_limit_rpm`: 每分钟请求限制
137+
- `rate_limit_tpm`: 每分钟token限制
138+
139+
## 🔧 支持的Provider类型
140+
141+
- `OpenAI`: OpenAI API
142+
- `Anthropic`: Anthropic Claude API
143+
- `Azure`: Azure OpenAI Service
144+
- `Google`: Google AI API
145+
- `Cohere`: Cohere API
146+
- `HuggingFace`: HuggingFace API
147+
- `Ollama`: Ollama本地API
148+
- `AwsBedrock`: AWS Bedrock
149+
- `GoogleVertex`: Google Vertex AI
150+
- `Mistral`: Mistral AI
151+
152+
## 🧪 运行示例
153+
154+
```bash
155+
# 设置环境变量
156+
export OPENAI_API_KEY="your-openai-key"
157+
export ANTHROPIC_API_KEY="your-anthropic-key"
158+
159+
# 运行示例
160+
cargo run --example sdk_example
161+
```
162+
163+
## 🔮 未来计划
164+
165+
当前的SDK实现了基础功能,未来计划添加:
166+
167+
1. **完整的Provider集成**: 与现有LiteLLM-RS的provider系统完整集成
168+
2. **流式响应**: 支持真实的流式聊天
169+
3. **中间件系统**: 请求/响应处理管道
170+
4. **缓存系统**: 智能缓存和语义缓存
171+
5. **负载均衡**: 多种路由策略
172+
6. **监控指标**: 详细的性能和使用指标
173+
7. **错误重试**: 智能重试和熔断机制
174+
175+
## 🤝 贡献
176+
177+
欢迎提交Issue和Pull Request来改进这个SDK!
178+
179+
## 📄 许可证
180+
181+
与主项目相同的MIT许可证。

0 commit comments

Comments
 (0)