-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat: Add OpenRouter as LLM backend option #50
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -175,9 +175,11 @@ impl DatabaseConfig { | |||||
| /// Which LLM backend to use. | ||||||
| /// | ||||||
| /// Defaults to `NearAi` to keep IronClaw close to the NEAR ecosystem. | ||||||
| /// Users can override with `LLM_BACKEND` env var to use their own API keys. | ||||||
| /// Users can override with `LLM_BACKEND` env var to use other providers. | ||||||
| #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] | ||||||
| pub enum LlmBackend { | ||||||
| /// OpenRouter -- OpenAI-compatible API with access to many models | ||||||
| OpenRouter, | ||||||
| /// NEAR AI proxy (default) -- session or API key auth | ||||||
| #[default] | ||||||
| NearAi, | ||||||
|
|
@@ -196,13 +198,14 @@ impl std::str::FromStr for LlmBackend { | |||||
|
|
||||||
| fn from_str(s: &str) -> Result<Self, Self::Err> { | ||||||
| match s.to_lowercase().as_str() { | ||||||
| "openrouter" | "open_router" => Ok(Self::OpenRouter), | ||||||
| "nearai" | "near_ai" | "near" => Ok(Self::NearAi), | ||||||
| "openai" | "open_ai" => Ok(Self::OpenAi), | ||||||
| "anthropic" | "claude" => Ok(Self::Anthropic), | ||||||
| "ollama" => Ok(Self::Ollama), | ||||||
| "openai_compatible" | "openai-compatible" | "compatible" => Ok(Self::OpenAiCompatible), | ||||||
| _ => Err(format!( | ||||||
| "invalid LLM backend '{}', expected one of: nearai, openai, anthropic, ollama, openai_compatible", | ||||||
| "invalid LLM backend '{}', expected one of: openrouter, nearai, openai, anthropic, ollama, openai_compatible", | ||||||
| s | ||||||
| )), | ||||||
| } | ||||||
|
|
@@ -212,6 +215,7 @@ impl std::str::FromStr for LlmBackend { | |||||
| impl std::fmt::Display for LlmBackend { | ||||||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||||||
| match self { | ||||||
| Self::OpenRouter => write!(f, "openrouter"), | ||||||
| Self::NearAi => write!(f, "nearai"), | ||||||
| Self::OpenAi => write!(f, "openai"), | ||||||
| Self::Anthropic => write!(f, "anthropic"), | ||||||
|
|
@@ -242,6 +246,13 @@ pub struct OllamaConfig { | |||||
| pub model: String, | ||||||
| } | ||||||
|
|
||||||
| /// Configuration for OpenRouter API. | ||||||
| #[derive(Debug, Clone)] | ||||||
| pub struct OpenRouterConfig { | ||||||
| pub api_key: SecretString, | ||||||
| pub model: String, | ||||||
| } | ||||||
|
|
||||||
| /// Configuration for any OpenAI-compatible endpoint. | ||||||
| #[derive(Debug, Clone)] | ||||||
| pub struct OpenAiCompatibleConfig { | ||||||
|
|
@@ -252,14 +263,16 @@ pub struct OpenAiCompatibleConfig { | |||||
|
|
||||||
| /// LLM provider configuration. | ||||||
| /// | ||||||
| /// NEAR AI remains the default backend. Users can switch to other providers | ||||||
| /// by setting `LLM_BACKEND` (e.g. `openai`, `anthropic`, `ollama`). | ||||||
| /// NEAR AI is the default backend. Users can switch to other providers | ||||||
| /// by setting `LLM_BACKEND` (e.g. `openrouter`, `openai`, `anthropic`, `ollama`). | ||||||
| #[derive(Debug, Clone)] | ||||||
| pub struct LlmConfig { | ||||||
| /// Which backend to use (default: NearAi) | ||||||
| pub backend: LlmBackend, | ||||||
| /// NEAR AI config (always populated for NEAR AI embeddings, etc.) | ||||||
| pub nearai: NearAiConfig, | ||||||
| /// OpenRouter config (populated when backend=openrouter) | ||||||
| pub openrouter: Option<OpenRouterConfig>, | ||||||
| /// Direct OpenAI config (populated when backend=openai) | ||||||
| pub openai: Option<OpenAiDirectConfig>, | ||||||
| /// Direct Anthropic config (populated when backend=anthropic) | ||||||
|
|
@@ -323,7 +336,7 @@ impl LlmConfig { | |||||
| message: e, | ||||||
| })? | ||||||
| } else { | ||||||
| LlmBackend::NearAi | ||||||
| LlmBackend::default() | ||||||
| }; | ||||||
|
|
||||||
| // Always resolve NEAR AI config (used as fallback and for embeddings) | ||||||
|
|
@@ -359,6 +372,20 @@ impl LlmConfig { | |||||
| }; | ||||||
|
|
||||||
| // Resolve provider-specific configs based on backend | ||||||
| let openrouter = if backend == LlmBackend::OpenRouter { | ||||||
| let api_key = optional_env("OPENROUTER_API_KEY")? | ||||||
| .map(SecretString::from) | ||||||
| .ok_or_else(|| ConfigError::MissingRequired { | ||||||
| key: "OPENROUTER_API_KEY".to_string(), | ||||||
| hint: "Set OPENROUTER_API_KEY when LLM_BACKEND=openrouter".to_string(), | ||||||
|
Contributor
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. Since OpenRouter is now the default backend, this error message might be confusing for new users who haven't set
Suggested change
|
||||||
| })?; | ||||||
| let model = optional_env("OPENROUTER_MODEL")? | ||||||
| .unwrap_or_else(|| "anthropic/claude-sonnet-4".to_string()); | ||||||
|
lwlee2608 marked this conversation as resolved.
|
||||||
| Some(OpenRouterConfig { api_key, model }) | ||||||
| } else { | ||||||
| None | ||||||
| }; | ||||||
|
|
||||||
| let openai = if backend == LlmBackend::OpenAi { | ||||||
| let api_key = optional_env("OPENAI_API_KEY")? | ||||||
| .map(SecretString::from) | ||||||
|
|
@@ -415,6 +442,7 @@ impl LlmConfig { | |||||
| Ok(Self { | ||||||
| backend, | ||||||
| nearai, | ||||||
| openrouter, | ||||||
| openai, | ||||||
| anthropic, | ||||||
| ollama, | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.