Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs/docs/ai/llm.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,24 @@ cocoindex.LlmSpec(

You can find the full list of models supported by LiteLLM [here](https://docs.litellm.ai/docs/providers).

### OpenRouter

To use the OpenRouter API, you need to set the environment variable `OPENROUTER_API_KEY`.
You can generate the API key from [here](https://openrouter.ai/settings/keys).

A spec for OpenRouter looks like this:

<Tabs>
<TabItem value="python" label="Python" default>

```python
cocoindex.LlmSpec(
api_type=cocoindex.LlmApiType.OPENROUTER,
model="deepseek/deepseek-r1:free",
)
```

</TabItem>
</Tabs>

You can find the full list of models supported by OpenRouter [here](https://openrouter.ai/models).
1 change: 1 addition & 0 deletions python/cocoindex/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class LlmApiType(Enum):
GEMINI = "Gemini"
ANTHROPIC = "Anthropic"
LITELLM = "LiteLlm"
OPENROUTER = "OpenRouter"


@dataclass
Expand Down
7 changes: 7 additions & 0 deletions src/llm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub enum LlmApiType {
Gemini,
Anthropic,
LiteLlm,
OpenRouter,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -58,6 +59,7 @@ mod gemini;
mod ollama;
mod openai;
mod litellm;
mod openrouter;

pub async fn new_llm_generation_client(spec: LlmSpec) -> Result<Box<dyn LlmGenerationClient>> {
let client = match spec.api_type {
Expand All @@ -76,6 +78,11 @@ pub async fn new_llm_generation_client(spec: LlmSpec) -> Result<Box<dyn LlmGener
LlmApiType::LiteLlm => {
Box::new(litellm::Client::new_litellm(spec).await?) as Box<dyn LlmGenerationClient>
}
LlmApiType::OpenRouter => {
Box::new(openrouter::Client::new_openrouter(spec).await?) as Box<dyn LlmGenerationClient>
}


};
Ok(client)
}
16 changes: 16 additions & 0 deletions src/llm/openrouter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use async_openai::config::OpenAIConfig;
use async_openai::Client as OpenAIClient;

pub use super::openai::Client;

impl Client {
pub async fn new_openrouter(spec: super::LlmSpec) -> anyhow::Result<Self> {
let address = spec.address.clone().unwrap_or_else(|| "https://openrouter.ai/api/v1".to_string());
let api_key = std::env::var("OPENROUTER_API_KEY").ok();
let mut config = OpenAIConfig::new().with_api_base(address);
if let Some(api_key) = api_key {
config = config.with_api_key(api_key);
}
Ok(Client::from_parts(OpenAIClient::with_config(config), spec.model))
}
}
Loading