-
Notifications
You must be signed in to change notification settings - Fork 574
fix: add NVIDIA NIM provider profile for input_type embedding field #268
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
vicjayjay
wants to merge
6
commits into
CortexReach:master
Choose a base branch
from
vicjayjay:fix/nvidia-nim-provider-profile
base: master
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.
+251
−8
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
39ba703
fix: add NVIDIA NIM provider profile for input_type embedding field
vicjayjay 423f8c2
fix: forward dimensions for NVIDIA dynamic embedding models
vicjayjay a9b4f66
Add NVIDIA detection test and update imports
vicjayjay 3f26c60
Refactor NVIDIA compatibility check regex
vicjayjay e6e705d
fix: tighten NVIDIA detection precedence and add negative test coverage
vicjayjay 255475d
fix: parse hostname for provider detection instead of raw URL matching
vicjayjay 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,211 @@ | ||
| import assert from "node:assert/strict"; | ||
| import http from "node:http"; | ||
| import { describe, it } from "node:test"; | ||
|
|
||
| import jitiFactory from "jiti"; | ||
|
|
||
| const jiti = jitiFactory(import.meta.url, { interopDefault: true }); | ||
| const { Embedder, formatEmbeddingProviderError } = jiti("../src/embedder.ts"); | ||
|
|
||
| /** | ||
| * Create a capture server that records POST bodies and returns embeddings | ||
| * with configurable dimension count. | ||
| */ | ||
| async function withCaptureServer(dims, fn) { | ||
| let capturedBody = null; | ||
| const fakeVec = Array.from({ length: dims }, (_, i) => i * 0.01); | ||
| const server = http.createServer((req, res) => { | ||
| if (req.url === "/v1/embeddings" && req.method === "POST") { | ||
| const chunks = []; | ||
| req.on("data", (c) => chunks.push(c)); | ||
| req.on("end", () => { | ||
| capturedBody = JSON.parse(Buffer.concat(chunks).toString()); | ||
| res.writeHead(200, { "content-type": "application/json" }); | ||
| res.end( | ||
| JSON.stringify({ | ||
| object: "list", | ||
| data: [{ object: "embedding", index: 0, embedding: fakeVec }], | ||
| usage: { prompt_tokens: 5, total_tokens: 5 }, | ||
| }), | ||
| ); | ||
| }); | ||
| return; | ||
| } | ||
| res.writeHead(404); | ||
| res.end("not found"); | ||
| }); | ||
|
|
||
| await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); | ||
| const address = server.address(); | ||
| const port = typeof address === "object" && address ? address.port : 0; | ||
| const baseURL = `http://127.0.0.1:${port}/v1`; | ||
|
|
||
| try { | ||
| await fn({ baseURL, port, getCaptured: () => capturedBody }); | ||
| } finally { | ||
| await new Promise((resolve) => server.close(resolve)); | ||
| } | ||
| } | ||
|
|
||
| describe("NVIDIA NIM provider profile", () => { | ||
| it("sends input_type=query for NVIDIA NIM (nv-embed model prefix)", async () => { | ||
| const dims = 128; | ||
| await withCaptureServer(dims, async ({ baseURL, getCaptured }) => { | ||
| const embedder = new Embedder({ | ||
| baseURL, | ||
| model: "nv-embedqa-e5-v5", | ||
| apiKey: "test-key", | ||
| dimensions: dims, | ||
| taskQuery: "retrieval.query", | ||
| taskPassage: "retrieval.passage", | ||
| }); | ||
|
|
||
| await embedder.embedQuery("test query"); | ||
| const body = getCaptured(); | ||
|
|
||
| assert.ok(body, "Request body should be captured"); | ||
| assert.equal(body.input_type, "query", "Should send input_type=query for NVIDIA"); | ||
| assert.equal(body.task, undefined, "Should NOT send task field for NVIDIA"); | ||
| }); | ||
| }); | ||
|
|
||
| it("maps retrieval.passage → passage for NVIDIA NIM", async () => { | ||
| const dims = 128; | ||
| await withCaptureServer(dims, async ({ baseURL, getCaptured }) => { | ||
| const embedder = new Embedder({ | ||
| baseURL, | ||
| model: "nv-embedqa-e5-v5", | ||
| apiKey: "test-key", | ||
| dimensions: dims, | ||
| taskQuery: "retrieval.query", | ||
| taskPassage: "retrieval.passage", | ||
| }); | ||
|
|
||
| await embedder.embedPassage("test document"); | ||
| const body = getCaptured(); | ||
|
|
||
| assert.ok(body, "Request body should be captured"); | ||
| assert.equal(body.input_type, "passage", "Should map retrieval.passage → passage"); | ||
| assert.equal(body.task, undefined, "Should NOT send task field for NVIDIA"); | ||
| }); | ||
| }); | ||
|
|
||
| it("detects NVIDIA from nvidia/ model prefix", async () => { | ||
| const dims = 128; | ||
| await withCaptureServer(dims, async ({ baseURL, getCaptured }) => { | ||
| const embedder = new Embedder({ | ||
| baseURL, | ||
| model: "nvidia/llama-3.2-nv-embedqa-1b-v2", | ||
| apiKey: "test-key", | ||
| dimensions: dims, | ||
| taskQuery: "query", | ||
| taskPassage: "passage", | ||
| }); | ||
|
|
||
| await embedder.embedQuery("test"); | ||
| const body = getCaptured(); | ||
|
|
||
| assert.ok(body, "Request body should be captured"); | ||
| assert.equal(body.input_type, "query", "nvidia/ model prefix should trigger input_type"); | ||
| assert.equal(body.task, undefined, "nvidia/ model prefix should NOT send task"); | ||
| }); | ||
| }); | ||
|
|
||
| it("detects NVIDIA from a .nvidia.com baseURL", () => { | ||
| const message = formatEmbeddingProviderError(new Error("boom"), { | ||
| baseURL: "https://build.nvidia.com/v1", | ||
| model: "custom-embed-model", | ||
| mode: "single", | ||
| }); | ||
|
|
||
| assert.equal(message, "Failed to generate embedding from NVIDIA NIM: boom"); | ||
| }); | ||
|
|
||
| it(".nvidia.com baseURL with conflicting jina- model prefix → NVIDIA wins", async () => { | ||
| const dims = 128; | ||
| await withCaptureServer(dims, async ({ baseURL, getCaptured }) => { | ||
| // Replace localhost URL with a .nvidia.com URL for detection, but route | ||
| // the actual HTTP request to the capture server. | ||
| const nvidiaBaseURL = baseURL.replace("127.0.0.1", "integrate.api.nvidia.com"); | ||
| const embedder = new Embedder({ | ||
| baseURL, // actual network target | ||
| model: "jina-embeddings-v3", | ||
| apiKey: "test-key", | ||
| dimensions: dims, | ||
| taskQuery: "retrieval.query", | ||
| taskPassage: "retrieval.passage", | ||
| }); | ||
| // Override the detected profile by using a real .nvidia.com baseURL in detection | ||
| // We test detection separately via the error label path: | ||
| const message = formatEmbeddingProviderError(new Error("test"), { | ||
| baseURL: "https://integrate.api.nvidia.com/v1", | ||
| model: "jina-embeddings-v3", | ||
| mode: "single", | ||
| }); | ||
| assert.equal(message, "Failed to generate embedding from NVIDIA NIM: test", | ||
| ".nvidia.com host should win over jina- model prefix"); | ||
| }); | ||
| }); | ||
|
|
||
| it(".nvidia.com baseURL without taskQuery/taskPassage → no input_type injected", async () => { | ||
| const dims = 128; | ||
| await withCaptureServer(dims, async ({ baseURL, getCaptured }) => { | ||
| const embedder = new Embedder({ | ||
| baseURL, | ||
| model: "nvidia/nv-clip-v1", | ||
| apiKey: "test-key", | ||
| dimensions: dims, | ||
| // Deliberately omit taskQuery and taskPassage | ||
| }); | ||
|
|
||
| await embedder.embedQuery("test query"); | ||
| const body = getCaptured(); | ||
|
|
||
| assert.ok(body, "Request body should be captured"); | ||
| assert.equal(body.input_type, undefined, | ||
| "NVIDIA profile without taskQuery/taskPassage should NOT inject input_type"); | ||
| assert.equal(body.task, undefined, | ||
| "NVIDIA profile without taskQuery/taskPassage should NOT inject task"); | ||
| }); | ||
| }); | ||
|
|
||
| it("non-NVIDIA: Jina sends task field", async () => { | ||
| const dims = 128; | ||
| await withCaptureServer(dims, async ({ baseURL, getCaptured }) => { | ||
| const embedder = new Embedder({ | ||
| baseURL, | ||
| model: "jina-embeddings-v5-text-small", | ||
| apiKey: "test-key", | ||
| dimensions: dims, | ||
| taskQuery: "retrieval.query", | ||
| taskPassage: "retrieval.passage", | ||
| }); | ||
|
|
||
| await embedder.embedQuery("test query"); | ||
| const body = getCaptured(); | ||
|
|
||
| assert.ok(body, "Request body should be captured"); | ||
| assert.equal(body.task, "retrieval.query", "Jina should send task field"); | ||
| assert.equal(body.input_type, undefined, "Jina should NOT send input_type"); | ||
| }); | ||
| }); | ||
|
|
||
| it("non-NVIDIA: generic OpenAI-compatible sends neither task nor input_type", async () => { | ||
| const dims = 128; | ||
| await withCaptureServer(dims, async ({ baseURL, getCaptured }) => { | ||
| const embedder = new Embedder({ | ||
| baseURL, | ||
| model: "custom-embed-model", | ||
| apiKey: "test-key", | ||
| dimensions: dims, | ||
| }); | ||
|
|
||
| await embedder.embedQuery("test query"); | ||
| const body = getCaptured(); | ||
|
|
||
| assert.ok(body, "Request body should be captured"); | ||
| assert.equal(body.task, undefined, "Generic provider should NOT send task"); | ||
| assert.equal(body.input_type, undefined, "Generic provider should NOT send input_type"); | ||
| }); | ||
| }); | ||
| }); |
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 new NVIDIA detection checks
/\.nvidia\.com/iagainst the entirebaseURLstring, so any proxy URL that merely contains.nvidia.comin its path or query can be misclassified asnvidiaeven when the actual host is different. In that casebuildPayload()will apply NVIDIA-specific request semantics (input_typemapping and NVIDIA profile behavior) to non-NVIDIA backends, which can cause request failures when task hints are configured. Matching should be done on the parsed hostname (or with a strict host-bound regex) rather than the raw URL string.Useful? React with 👍 / 👎.