Skip to content

Commit 28f26d1

Browse files
committed
feat: Add cloud LLM provider support with Anthropic, OpenAI, and OpenAI-compatible APIs
This major update integrates cloud-based LLM providers alongside existing local options, providing flexibility in deployment and usage. ## New Features ### LLM Provider Infrastructure - Created unified `LLMProvider` trait for consistent provider interface - Implemented `LLMProviderFactory` for dynamic provider instantiation - Added comprehensive provider characteristics and configuration types ### Cloud Provider Implementations - **Anthropic Claude**: Full support for Claude 3.5 (Opus, Sonnet, Haiku) - 200K token context window - Automatic retry with exponential backoff - Feature-gated with `anthropic` flag - **OpenAI**: Complete GPT-4o and GPT-4 Turbo support - 128K token context window - Organization ID support - Feature-gated with `openai-llm` flag - **OpenAI-Compatible**: Generic support for any OpenAI-compatible endpoint - Works with LM Studio, vLLM, Ollama v1 API, etc. - Optional API key authentication - Feature-gated with `openai-compatible` flag ### Updated Existing Providers - Refactored `QwenClient` to implement `LLMProvider` trait - Maintained backward compatibility with existing code - Enhanced with consistent error handling and metrics ### Interactive Setup Wizard - Created `codegraph-setup` binary for guided configuration - Interactive prompts for all provider types - Supports both embedding and LLM provider selection - Automatic configuration file generation at ~/.codegraph/config.toml ### Configuration Enhancements - Extended `LLMConfig` with cloud provider fields: - `anthropic_api_key`, `openai_api_key` - `openai_compatible_url` for custom endpoints - `max_tokens`, `timeout_secs` for fine-tuning - Updated `.codegraph.toml.example` with comprehensive examples - Added environment variable support (ANTHROPIC_API_KEY, OPENAI_API_KEY) ### Documentation - Created comprehensive `docs/CLOUD_PROVIDERS.md` guide - Provider comparison matrix - Setup instructions for each provider - Usage examples and code snippets - Troubleshooting guide - Security best practices - Updated README.md with cloud provider quickstart - Added provider-specific configuration examples ## Technical Details ### Architecture - All cloud providers follow the same `LLMProvider` trait - Async-first design with proper error propagation - Configurable retry logic with exponential backoff - Token usage tracking and metrics - Streaming support (prepared for future implementation) ### Code Quality - Feature flags for optional dependencies - Comprehensive test coverage for factory and providers - Type-safe configuration with serde - Clear error messages for missing API keys ### Dependencies Added - dialoguer: Interactive CLI prompts - dirs: Cross-platform config directory detection - Feature flags for optional cloud providers ## Migration Guide Existing configurations continue to work without changes. To use cloud providers: 1. Enable desired features during build: ```bash cargo build --features anthropic,openai-llm,openai-compatible ``` 2. Run setup wizard or manually configure in .codegraph.toml: ```toml [llm] enabled = true provider = "anthropic" # or "openai", "openai-compatible" model = "claude-3-5-sonnet-20241022" anthropic_api_key = "sk-ant-..." ``` ## Files Changed - Core implementation: 5 new provider files in codegraph-ai - Factory: New llm_factory.rs for provider instantiation - Configuration: Updated config_manager.rs with cloud fields - Setup wizard: New codegraph-setup binary - Documentation: New CLOUD_PROVIDERS.md guide - Examples: Updated .codegraph.toml.example ## Breaking Changes None. All changes are backward compatible with existing configurations. ## Future Enhancements - Streaming response support - Function calling for supported providers - Response caching layer - Cost tracking and optimization - Multi-provider fallback strategies
1 parent 4eb8f58 commit 28f26d1

File tree

14 files changed

+2518
-14
lines changed

14 files changed

+2518
-14
lines changed

.codegraph.toml.example

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,29 +42,56 @@ batch_size = 64
4242
# Set to false for maximum speed if using an external agent
4343
enabled = false
4444

45-
# LLM provider: "ollama" or "lmstudio"
46-
# "lmstudio" recommended for MLX + Flash Attention 2 (macOS)
45+
# LLM provider: "ollama", "lmstudio", "anthropic", "openai", or "openai-compatible"
46+
# - "lmstudio": Local LLMs via LM Studio (recommended for MLX + Flash Attention 2 on macOS)
47+
# - "ollama": Local LLMs via Ollama
48+
# - "anthropic": Anthropic Claude API (requires API key)
49+
# - "openai": OpenAI GPT API (requires API key)
50+
# - "openai-compatible": Any OpenAI-compatible API endpoint
4751
provider = "lmstudio"
4852

4953
# LLM model identifier
50-
# For LM Studio: lmstudio-community/DeepSeek-Coder-V2-Lite-Instruct-GGUF/DeepSeek-Coder-V2-Lite-Instruct-Q4_K_M.gguf
54+
# For LM Studio: lmstudio-community/DeepSeek-Coder-V2-Lite-Instruct-GGUF
5155
# For Ollama: Model name (e.g., "qwen2.5-coder:14b", "codellama:13b")
52-
# Recommended: DeepSeek Coder v2 Lite Instruct Q4_K_M (superior performance)
56+
# For Anthropic: Model name (e.g., "claude-3-5-sonnet-20241022", "claude-3-5-haiku-20241022")
57+
# For OpenAI: Model name (e.g., "gpt-4o", "gpt-4o-mini", "gpt-4-turbo")
58+
# For OpenAI-compatible: Custom model name
59+
# Recommended: DeepSeek Coder v2 Lite Instruct Q4_K_M (local), or Claude 3.5 Sonnet (cloud)
5360
model = "lmstudio-community/DeepSeek-Coder-V2-Lite-Instruct-GGUF"
5461

55-
# LM Studio URL (default port 1234)
62+
# LM Studio URL (only used if provider is "lmstudio")
5663
lmstudio_url = "http://localhost:1234"
5764

58-
# Ollama URL
65+
# Ollama URL (only used if provider is "ollama")
5966
ollama_url = "http://localhost:11434"
6067

68+
# OpenAI-compatible base URL (only used if provider is "openai-compatible")
69+
# Example: "http://localhost:1234/v1" for LM Studio OpenAI endpoint
70+
# openai_compatible_url = "http://localhost:1234/v1"
71+
72+
# Anthropic API key (only used if provider is "anthropic")
73+
# Can also be set via ANTHROPIC_API_KEY environment variable
74+
# anthropic_api_key = "sk-ant-..."
75+
76+
# OpenAI API key (only used if provider is "openai" or some "openai-compatible" endpoints)
77+
# Can also be set via OPENAI_API_KEY environment variable
78+
# openai_api_key = "sk-..."
79+
6180
# Context window size (tokens)
6281
# DeepSeek Coder v2 Lite: 32768 tokens
82+
# Claude 3.5 Sonnet: 200000 tokens
83+
# GPT-4o: 128000 tokens
6384
context_window = 32000
6485

65-
# Temperature for generation (0.0 = deterministic, 1.0 = creative)
86+
# Temperature for generation (0.0 = deterministic, 2.0 = very creative)
6687
temperature = 0.1
6788

89+
# Maximum tokens to generate in responses
90+
max_tokens = 4096
91+
92+
# Request timeout in seconds
93+
timeout_secs = 120
94+
6895
# Insights mode: "context-only", "balanced", or "deep"
6996
# - context-only: Return context only (fastest, for agents)
7097
# - balanced: Process top 10 files with LLM (good speed/quality)

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,51 @@ That is the entire end-to-end workflow. LM Studio now has a project-specific vec
4848

4949
---
5050

51+
## Cloud Provider Support ☁️
52+
53+
CodeGraph now supports both **local** and **cloud-based** LLM and embedding providers, giving you flexibility in deployment:
54+
55+
### Supported Cloud Providers
56+
57+
- **Anthropic Claude** - State-of-the-art code understanding with 200K context
58+
- **OpenAI GPT** - GPT-4o and other OpenAI models
59+
- **OpenAI-Compatible** - Any custom OpenAI-compatible endpoint
60+
61+
### Quick Setup with Wizard
62+
63+
The easiest way to configure cloud providers:
64+
65+
```bash
66+
# Build and run the setup wizard
67+
cargo build --release --bin codegraph-setup --features all-cloud-providers
68+
./target/release/codegraph-setup
69+
```
70+
71+
The interactive wizard will guide you through:
72+
1. Choosing your embedding provider (ONNX, Ollama, LM Studio, or OpenAI)
73+
2. Selecting your LLM provider (Ollama, LM Studio, Anthropic, OpenAI, or custom)
74+
3. Configuring API keys and models
75+
4. Setting advanced options
76+
77+
### Manual Configuration
78+
79+
Or configure manually in `.codegraph.toml`:
80+
81+
```toml
82+
[llm]
83+
enabled = true
84+
provider = "anthropic" # or "openai", "ollama", "lmstudio", "openai-compatible"
85+
model = "claude-3-5-sonnet-20241022"
86+
anthropic_api_key = "sk-ant-..." # Or set ANTHROPIC_API_KEY env var
87+
context_window = 200000
88+
temperature = 0.1
89+
max_tokens = 4096
90+
```
91+
92+
**For detailed provider setup, pricing, and comparisons, see [docs/CLOUD_PROVIDERS.md](docs/CLOUD_PROVIDERS.md).**
93+
94+
---
95+
5196
## Everyday CLI
5297

5398
| Command | Description |

crates/codegraph-ai/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,10 @@ arc-swap = { workspace = true }
2828
num_cpus = { workspace = true }
2929
prometheus = { workspace = true }
3030
reqwest = { workspace = true }
31+
32+
[features]
33+
default = []
34+
anthropic = []
35+
openai-llm = []
36+
openai-compatible = []
37+
all-cloud-providers = ["anthropic", "openai-llm", "openai-compatible"]

0 commit comments

Comments
 (0)