-
-
Notifications
You must be signed in to change notification settings - Fork 232
feat: add litellm provider #123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /// LiteLLM provider — OpenAI-compatible chat completions against a LiteLLM proxy. | ||
| /// | ||
| /// A LiteLLM proxy speaks the OpenAI wire format, so this provider reuses the | ||
| /// `OpenAiProvider` transport unchanged. Pointing it at a LiteLLM gateway lets | ||
| /// webclaw reach 100+ upstream providers (OpenAI, Anthropic, Bedrock, Vertex | ||
| /// AI, Azure, and more) through a single endpoint with centralized keys. | ||
| use async_trait::async_trait; | ||
|
|
||
| use crate::error::LlmError; | ||
| use crate::provider::{CompletionRequest, LlmProvider}; | ||
|
|
||
| use super::openai::OpenAiProvider; | ||
|
|
||
| pub struct LiteLlmProvider { | ||
| inner: OpenAiProvider, | ||
| } | ||
|
|
||
| impl LiteLlmProvider { | ||
| /// Returns `None` if no LiteLLM API key is available (param or env). | ||
| pub fn new( | ||
| key_override: Option<String>, | ||
| base_url: Option<String>, | ||
| model: Option<String>, | ||
| ) -> Option<Self> { | ||
| let key = super::load_api_key(key_override, "LITELLM_API_KEY")?; | ||
| let base_url = base_url | ||
| .or_else(|| std::env::var("LITELLM_BASE_URL").ok()) | ||
| .unwrap_or_else(|| "http://localhost:4000/v1".into()); | ||
|
Comment on lines
+26
to
+28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔒 Security & Privacy | 🟠 Major | ⚡ Quick win Require HTTPS for non-loopback LiteLLM endpoints.
📍 Affects 2 files
🤖 Prompt for AI Agents |
||
| let model = model | ||
| .or_else(|| std::env::var("LITELLM_MODEL").ok()) | ||
| .unwrap_or_else(|| "gpt-4o-mini".into()); | ||
| let inner = OpenAiProvider::new(Some(key), Some(base_url), Some(model))?; | ||
| Some(Self { inner }) | ||
| } | ||
|
|
||
| pub fn default_model(&self) -> &str { | ||
| self.inner.default_model() | ||
| } | ||
| } | ||
|
|
||
| #[async_trait] | ||
| impl LlmProvider for LiteLlmProvider { | ||
| async fn complete(&self, request: &CompletionRequest) -> Result<String, LlmError> { | ||
| self.inner.complete(request).await | ||
| } | ||
|
|
||
| async fn is_available(&self) -> bool { | ||
| self.inner.is_available().await | ||
| } | ||
|
|
||
| fn name(&self) -> &str { | ||
| "litellm" | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn empty_key_returns_none() { | ||
| assert!(LiteLlmProvider::new(Some(String::new()), None, None).is_none()); | ||
| } | ||
|
|
||
| #[test] | ||
| #[ignore = "reads LITELLM_MODEL from the process env; run with --test-threads=1"] | ||
| fn explicit_key_constructs_with_litellm_defaults() { | ||
| let provider = | ||
| LiteLlmProvider::new(Some("test-key".into()), None, None).expect("should construct"); | ||
| assert_eq!(provider.name(), "litellm"); | ||
| assert_eq!(provider.default_model(), "gpt-4o-mini"); | ||
|
Comment on lines
+68
to
+71
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win Isolate the default-model test from environment variables.
🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| #[test] | ||
| fn explicit_model_override() { | ||
| let provider = LiteLlmProvider::new( | ||
| Some("test-key".into()), | ||
| Some("http://proxy.example.com:4000/v1".into()), | ||
| Some("claude-sonnet-4-6".into()), | ||
| ) | ||
| .expect("should construct"); | ||
| assert_eq!(provider.default_model(), "claude-sonnet-4-6"); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.