forked from cline/cline
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
feat: add OpenRouter as embedding provider for code indexing #8974
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 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c162cf6
feat: add OpenRouter as an embedding provider for code indexing
roomote 66387bc
test: add unit tests for OpenRouterEmbedder
roomote 7cf14e0
fix: add openrouter to codebase index type definitions
roomote 92ad576
fix: add missing translation for openRouterConfigMissing
roomote 8d3c354
fix: add missing translations for openRouterConfigMissing in all locales
roomote 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
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
96 changes: 96 additions & 0 deletions
96
src/services/code-index/embedders/__tests__/openrouter.spec.ts
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 |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| // npx vitest run src/services/code-index/embedders/__tests__/openrouter.spec.ts | ||
|
|
||
| import { describe, it, expect, vi, beforeEach } from "vitest" | ||
| import { OpenRouterEmbedder } from "../openrouter" | ||
| import { OpenAICompatibleEmbedder } from "../openai-compatible" | ||
| import { t } from "../../../../i18n" | ||
|
|
||
| vi.mock("../../../../i18n", () => ({ | ||
| t: (key: string, params?: any) => { | ||
| const translations: Record<string, string> = { | ||
| "embeddings:validation.apiKeyRequired": "API key is required", | ||
| } | ||
| return translations[key] || key | ||
| }, | ||
| })) | ||
|
|
||
| // Mock the parent class | ||
| vi.mock("../openai-compatible") | ||
|
|
||
| describe("OpenRouterEmbedder", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| // Mock the OpenAICompatibleEmbedder constructor | ||
| vi.mocked(OpenAICompatibleEmbedder).mockImplementation(function ( | ||
| this: any, | ||
| baseUrl: string, | ||
| apiKey: string, | ||
| modelId: string, | ||
| maxTokens: number, | ||
| ) { | ||
| // Store constructor arguments for verification | ||
| this.baseUrl = baseUrl | ||
| this.apiKey = apiKey | ||
| this.modelId = modelId | ||
| this.maxTokens = maxTokens | ||
|
|
||
| // Mock methods | ||
| this.createEmbeddings = vi.fn() | ||
| this.validateConfiguration = vi.fn() | ||
|
|
||
| // Return this for chaining | ||
| return this | ||
| } as any) | ||
| }) | ||
|
|
||
| describe("constructor", () => { | ||
| it("should create an instance with valid API key", () => { | ||
| const embedder = new OpenRouterEmbedder("test-api-key") | ||
| expect(embedder).toBeDefined() | ||
| expect(OpenAICompatibleEmbedder).toHaveBeenCalledWith( | ||
| "https://openrouter.ai/api/v1", | ||
| "test-api-key", | ||
| "openai/text-embedding-3-small", | ||
| 8191, | ||
| ) | ||
| }) | ||
|
|
||
| it("should use custom model ID when provided", () => { | ||
| const embedder = new OpenRouterEmbedder("test-api-key", "openai/text-embedding-3-large") | ||
| expect(embedder).toBeDefined() | ||
| expect(OpenAICompatibleEmbedder).toHaveBeenCalledWith( | ||
| "https://openrouter.ai/api/v1", | ||
| "test-api-key", | ||
| "openai/text-embedding-3-large", | ||
| 8191, | ||
| ) | ||
| }) | ||
|
|
||
| it("should use custom base URL when provided", () => { | ||
| const embedder = new OpenRouterEmbedder("test-api-key", undefined, "https://custom.openrouter.ai/api/v1") | ||
| expect(embedder).toBeDefined() | ||
| expect(OpenAICompatibleEmbedder).toHaveBeenCalledWith( | ||
| "https://custom.openrouter.ai/api/v1", | ||
| "test-api-key", | ||
| "openai/text-embedding-3-small", | ||
| 8191, | ||
| ) | ||
| }) | ||
|
|
||
| it("should throw error when API key is not provided", () => { | ||
| expect(() => new OpenRouterEmbedder(undefined as any)).toThrow("API key is required") | ||
| }) | ||
|
|
||
| it("should throw error when API key is empty string", () => { | ||
| expect(() => new OpenRouterEmbedder("")).toThrow("API key is required") | ||
| }) | ||
| }) | ||
|
|
||
| describe("embedderInfo", () => { | ||
| it("should return openrouter as the embedder name", () => { | ||
| const embedder = new OpenRouterEmbedder("test-api-key") | ||
| // The embedderInfo getter in OpenRouterEmbedder overrides the parent class | ||
| expect(embedder.embedderInfo).toEqual({ name: "openrouter" }) | ||
| }) | ||
| }) | ||
| }) |
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 |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import { OpenAICompatibleEmbedder } from "./openai-compatible" | ||
| import { IEmbedder, EmbedderInfo } from "../interfaces/embedder" | ||
| import { getDefaultModelId } from "../../../shared/embeddingModels" | ||
| import { MAX_ITEM_TOKENS } from "../constants" | ||
| import { t } from "../../../i18n" | ||
|
|
||
| /** | ||
| * OpenRouter embedder implementation that wraps the OpenAI Compatible embedder | ||
| * with configuration for OpenRouter's embedding API. | ||
| * | ||
| * Supported models: | ||
| * - openai/text-embedding-3-small (dimension: 1536) | ||
| * - openai/text-embedding-3-large (dimension: 3072) | ||
| * - openai/text-embedding-ada-002 (dimension: 1536) | ||
| * - cohere/embed-english-v3.0 (dimension: 1024) | ||
| * - cohere/embed-multilingual-v3.0 (dimension: 1024) | ||
| * - voyage/voyage-3 (dimension: 1024) | ||
| * - voyage/voyage-3-lite (dimension: 512) | ||
| */ | ||
| export class OpenRouterEmbedder extends OpenAICompatibleEmbedder implements IEmbedder { | ||
| private static readonly OPENROUTER_BASE_URL = "https://openrouter.ai/api/v1" | ||
| private static readonly DEFAULT_MODEL = "openai/text-embedding-3-small" | ||
| private readonly modelId: string | ||
|
|
||
| /** | ||
| * Creates a new OpenRouter embedder | ||
| * @param apiKey The OpenRouter API key for authentication | ||
| * @param modelId The model ID to use (defaults to openai/text-embedding-3-small) | ||
| * @param baseUrl Optional custom base URL for OpenRouter API (defaults to https://openrouter.ai/api/v1) | ||
| */ | ||
| constructor(apiKey?: string, modelId?: string, baseUrl?: string) { | ||
| if (!apiKey) { | ||
| throw new Error(t("embeddings:validation.apiKeyRequired")) | ||
| } | ||
|
|
||
| // Use the provided base URL or default to OpenRouter's API URL | ||
| const openRouterBaseUrl = baseUrl || OpenRouterEmbedder.OPENROUTER_BASE_URL | ||
|
|
||
| // Initialize the parent OpenAI Compatible embedder with OpenRouter configuration | ||
| super(openRouterBaseUrl, apiKey, modelId || OpenRouterEmbedder.DEFAULT_MODEL, MAX_ITEM_TOKENS) | ||
|
|
||
| this.modelId = modelId || getDefaultModelId("openrouter") | ||
|
Comment on lines
+23
to
+42
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. The |
||
| } | ||
|
|
||
| /** | ||
| * Returns information about this embedder | ||
| */ | ||
| override get embedderInfo(): EmbedderInfo { | ||
| return { | ||
| name: "openrouter", | ||
| } | ||
| } | ||
| } | ||
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The JSDoc comment lists 7 supported models but the implementation in
embeddingModels.tsincludes 8 models. The comment is missingvoyage/voyage-embeddings-v3which is defined on line 89 ofembeddingModels.ts. Update the documentation to include all supported models or remove the unlisted model from the implementation.