-
Notifications
You must be signed in to change notification settings - Fork 2k
fix: resolve OpenRouter embedding dimensions from model table + env override #1004
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
Open
kagura-agent
wants to merge
3
commits into
rohitg00:main
Choose a base branch
from
kagura-agent:fix/openrouter-embedding-dimensions-1002
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+138
−2
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
2cc015c
fix: resolve embedding dimensions from model table + env override
kagura-agent 29522ed
test: add OpenRouterEmbeddingProvider dimension resolution tests
kagura-agent b50cafe
fix: stricter dimension override parsing + warn on unknown model fall…
kagura-agent 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 |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; | ||
| import { OpenRouterEmbeddingProvider } from "../src/providers/embedding/openrouter.js"; | ||
|
|
||
| describe("OpenRouterEmbeddingProvider", () => { | ||
| const originalEnv = { ...process.env }; | ||
|
|
||
| beforeEach(() => { | ||
| process.env = { ...originalEnv }; | ||
| delete process.env["OPENROUTER_API_KEY"]; | ||
| delete process.env["OPENROUTER_EMBEDDING_MODEL"]; | ||
| delete process.env["OPENROUTER_EMBEDDING_DIMENSIONS"]; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| process.env = originalEnv; | ||
| }); | ||
|
|
||
| it("defaults to 1536 dimensions for the default model", () => { | ||
| const provider = new OpenRouterEmbeddingProvider("test-key"); | ||
| expect(provider.name).toBe("openrouter"); | ||
| expect(provider.dimensions).toBe(1536); | ||
| }); | ||
|
|
||
| it("throws when no API key is provided", () => { | ||
| expect(() => new OpenRouterEmbeddingProvider()).toThrow( | ||
| /OPENROUTER_API_KEY is required/, | ||
| ); | ||
| }); | ||
|
|
||
| it("derives dimensions from the known-models table", () => { | ||
| process.env["OPENROUTER_EMBEDDING_MODEL"] = "qwen/qwen3-embedding-8b"; | ||
| const qwen = new OpenRouterEmbeddingProvider("test-key"); | ||
| expect(qwen.dimensions).toBe(4096); | ||
|
|
||
| process.env["OPENROUTER_EMBEDDING_MODEL"] = "openai/text-embedding-3-large"; | ||
| const large = new OpenRouterEmbeddingProvider("test-key"); | ||
| expect(large.dimensions).toBe(3072); | ||
|
|
||
| process.env["OPENROUTER_EMBEDDING_MODEL"] = "cohere/embed-multilingual-v3.0"; | ||
| const cohere = new OpenRouterEmbeddingProvider("test-key"); | ||
| expect(cohere.dimensions).toBe(1024); | ||
| }); | ||
|
|
||
| it("OPENROUTER_EMBEDDING_DIMENSIONS overrides model-derived dimensions", () => { | ||
| process.env["OPENROUTER_EMBEDDING_MODEL"] = "openai/text-embedding-3-large"; | ||
| process.env["OPENROUTER_EMBEDDING_DIMENSIONS"] = "768"; | ||
| const provider = new OpenRouterEmbeddingProvider("test-key"); | ||
| expect(provider.dimensions).toBe(768); | ||
| }); | ||
|
|
||
| it("falls back to 1536 for unknown models", () => { | ||
| process.env["OPENROUTER_EMBEDDING_MODEL"] = "unknown/custom-model"; | ||
| const provider = new OpenRouterEmbeddingProvider("test-key"); | ||
| expect(provider.dimensions).toBe(1536); | ||
| }); | ||
|
|
||
| it("rejects invalid OPENROUTER_EMBEDDING_DIMENSIONS values", () => { | ||
| process.env["OPENROUTER_EMBEDDING_DIMENSIONS"] = "not-a-number"; | ||
| expect(() => new OpenRouterEmbeddingProvider("test-key")).toThrow( | ||
| /OPENROUTER_EMBEDDING_DIMENSIONS must be a positive integer/, | ||
| ); | ||
|
|
||
| process.env["OPENROUTER_EMBEDDING_DIMENSIONS"] = "-5"; | ||
| expect(() => new OpenRouterEmbeddingProvider("test-key")).toThrow( | ||
| /OPENROUTER_EMBEDDING_DIMENSIONS must be a positive integer/, | ||
| ); | ||
|
|
||
| process.env["OPENROUTER_EMBEDDING_DIMENSIONS"] = "0"; | ||
| expect(() => new OpenRouterEmbeddingProvider("test-key")).toThrow( | ||
| /OPENROUTER_EMBEDDING_DIMENSIONS must be a positive integer/, | ||
| ); | ||
| }); | ||
|
|
||
| it("sends correct model in the request body", async () => { | ||
| process.env["OPENROUTER_EMBEDDING_MODEL"] = "qwen/qwen3-embedding-8b"; | ||
| const provider = new OpenRouterEmbeddingProvider("test-key"); | ||
|
|
||
| const fetchSpy = vi.spyOn(globalThis, "fetch").mockResolvedValue( | ||
| new Response( | ||
| JSON.stringify({ data: [{ embedding: Array(4096).fill(0.1) }] }), | ||
| { status: 200 }, | ||
| ), | ||
| ); | ||
|
|
||
| await provider.embed("hello"); | ||
| const body = JSON.parse( | ||
| (fetchSpy.mock.calls[0][1] as RequestInit).body as string, | ||
| ); | ||
| expect(body.model).toBe("qwen/qwen3-embedding-8b"); | ||
|
|
||
| fetchSpy.mockRestore(); | ||
| }); | ||
| }); |
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.