From fcfc93afcdc498ac718ae3266761aa7c12cc767b Mon Sep 17 00:00:00 2001 From: prrao87 Date: Tue, 2 Dec 2025 11:20:02 -0500 Subject: [PATCH 1/3] Add support for embedding models in OpenRouter --- rust/cocoindex/src/llm/mod.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/rust/cocoindex/src/llm/mod.rs b/rust/cocoindex/src/llm/mod.rs index 172550f5e..284646d7d 100644 --- a/rust/cocoindex/src/llm/mod.rs +++ b/rust/cocoindex/src/llm/mod.rs @@ -166,6 +166,10 @@ pub async fn new_llm_embedding_client( LlmApiType::Ollama => { Box::new(ollama::Client::new(address).await?) as Box } + LlmApiType::OpenRouter => { + Box::new(openrouter::Client::new_openrouter(address, api_key).await?) + as Box + } LlmApiType::Gemini => { Box::new(gemini::AiStudioClient::new(address, api_key)?) as Box } @@ -178,8 +182,7 @@ pub async fn new_llm_embedding_client( Box::new(gemini::VertexAiClient::new(address, api_key, api_config).await?) as Box } - LlmApiType::OpenRouter - | LlmApiType::LiteLlm + LlmApiType::LiteLlm | LlmApiType::Vllm | LlmApiType::Anthropic | LlmApiType::Bedrock => { From 94c88b9a8b32270cd7291f1aae6b9ff11ff0f990 Mon Sep 17 00:00:00 2001 From: prrao87 Date: Tue, 2 Dec 2025 11:20:15 -0500 Subject: [PATCH 2/3] Update doc for OpenRouter embedding --- docs/docs/ai/llm.mdx | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/docs/docs/ai/llm.mdx b/docs/docs/ai/llm.mdx index 693370010..b356c998d 100644 --- a/docs/docs/ai/llm.mdx +++ b/docs/docs/ai/llm.mdx @@ -26,7 +26,7 @@ We support the following types of LLM APIs: | [Anthropic](#anthropic) | `LlmApiType.ANTHROPIC` | ✅ | ❌ | | [Voyage](#voyage) | `LlmApiType.VOYAGE` | ❌ | ✅ | | [LiteLLM](#litellm) | `LlmApiType.LITE_LLM` | ✅ | ❌ | -| [OpenRouter](#openrouter) | `LlmApiType.OPEN_ROUTER` | ✅ | ❌ | +| [OpenRouter](#openrouter) | `LlmApiType.OPEN_ROUTER` | ✅ | ✅ | | [vLLM](#vllm) | `LlmApiType.VLLM` | ✅ | ❌ | | [Bedrock](#bedrock) | `LlmApiType.BEDROCK` | ✅ | ❌ | @@ -400,7 +400,7 @@ You can find the full list of models supported by LiteLLM [here](https://docs.li 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: +A text generation spec for OpenRouter looks like this: @@ -415,6 +415,27 @@ cocoindex.LlmSpec( +OpenRouter also supports some text embedding models. Note that for OpenRouter embedding +models, you need to explicitly provide the `output_dimension` parameter in the spec. +Here's how you can define the spec to use an OpenRouter embedding model: + + + + +```python +cocoindex.functions.EmbedText( + api_type=cocoindex.LlmApiType.OPEN_ROUTER, + model="openai/text-embedding-3-small", + # Task type for embedding model + task_type="SEMANTICS_SIMILARITY", + # Required: the number of output dimensions for the embedding model + output_dimension=1536, +) +``` + + + + You can find the full list of models supported by OpenRouter [here](https://openrouter.ai/models). ### vLLM From 8084644d19daf0bbd002a94d02a9397cfdca15ff Mon Sep 17 00:00:00 2001 From: prrao87 Date: Tue, 2 Dec 2025 11:22:59 -0500 Subject: [PATCH 3/3] Run cargo fmt --- rust/cocoindex/src/llm/mod.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/rust/cocoindex/src/llm/mod.rs b/rust/cocoindex/src/llm/mod.rs index 284646d7d..fb5e29c2b 100644 --- a/rust/cocoindex/src/llm/mod.rs +++ b/rust/cocoindex/src/llm/mod.rs @@ -182,10 +182,7 @@ pub async fn new_llm_embedding_client( Box::new(gemini::VertexAiClient::new(address, api_key, api_config).await?) as Box } - LlmApiType::LiteLlm - | LlmApiType::Vllm - | LlmApiType::Anthropic - | LlmApiType::Bedrock => { + LlmApiType::LiteLlm | LlmApiType::Vllm | LlmApiType::Anthropic | LlmApiType::Bedrock => { api_bail!("Embedding is not supported for API type {:?}", api_type) } };