forked from Cloud-Pipelines/pipeline-editor
-
Notifications
You must be signed in to change notification settings - Fork 6
Add natural-language component rerank service #2321
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
263 changes: 263 additions & 0 deletions
263
src/services/naturalLanguageComponentSearchService.test.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,263 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| import type { ComponentReference } from "@/utils/componentSpec"; | ||
| import { isRecord } from "@/utils/typeGuards"; | ||
|
|
||
| import { | ||
| componentReferenceToCandidate, | ||
| NaturalLanguageSearchConfigError, | ||
| rerankComponentsByNaturalLanguage, | ||
| } from "./naturalLanguageComponentSearchService"; | ||
|
|
||
| const VALID_OPTIONS = { | ||
| apiBase: "https://api.example.com/v1", | ||
| apiKey: "sk-test", | ||
| model: "gpt-4o-mini", | ||
| }; | ||
|
|
||
| function mockChatResponse(content: unknown, status = 200) { | ||
| return new Response( | ||
| JSON.stringify({ | ||
| choices: [{ message: { content: JSON.stringify(content) } }], | ||
| }), | ||
| { | ||
| status, | ||
| statusText: status === 200 ? "OK" : "Internal Server Error", | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| function parseFetchBody(call: unknown[] | undefined): Record<string, unknown> { | ||
| const init = call?.[1]; | ||
| if ( | ||
| typeof init !== "object" || | ||
| init === null || | ||
| !("body" in init) || | ||
| typeof init.body !== "string" | ||
| ) { | ||
| throw new Error("Expected fetch body to be a string"); | ||
| } | ||
| const { body } = init; | ||
| const parsed: unknown = JSON.parse(body); | ||
| if (!isRecord(parsed)) { | ||
| throw new Error("Expected fetch body to be an object"); | ||
| } | ||
| return parsed; | ||
| } | ||
|
|
||
| describe("componentReferenceToCandidate", () => { | ||
| it("returns null for references without a digest", () => { | ||
| const ref: ComponentReference = { | ||
| spec: { | ||
| name: "no_digest", | ||
| inputs: [], | ||
| outputs: [], | ||
| implementation: { container: { image: "x" } }, | ||
| }, | ||
| }; | ||
| expect(componentReferenceToCandidate(ref)).toBeNull(); | ||
| }); | ||
|
|
||
| it("returns null when the reference has no useful metadata", () => { | ||
| const ref: ComponentReference = { | ||
| digest: "abc", | ||
| spec: { | ||
| inputs: [], | ||
| outputs: [], | ||
| implementation: { container: { image: "x" } }, | ||
| }, | ||
| }; | ||
| expect(componentReferenceToCandidate(ref)).toBeNull(); | ||
| }); | ||
|
|
||
| it("omits empty inputs/outputs from the candidate", () => { | ||
| const ref: ComponentReference = { | ||
| digest: "abc", | ||
| spec: { | ||
| name: "train", | ||
| description: "trainer", | ||
| inputs: [], | ||
| outputs: [], | ||
| implementation: { container: { image: "x" } }, | ||
| }, | ||
| }; | ||
| const candidate = componentReferenceToCandidate(ref); | ||
| expect(candidate).toEqual({ | ||
| id: "abc", | ||
| name: "train", | ||
| description: "trainer", | ||
| }); | ||
| }); | ||
|
|
||
| it("includes input/output names when present", () => { | ||
| const ref: ComponentReference = { | ||
| digest: "abc", | ||
| spec: { | ||
| name: "train", | ||
| description: "", | ||
| inputs: [{ name: "dataset" }], | ||
| outputs: [{ name: "model" }], | ||
| implementation: { container: { image: "x" } }, | ||
| }, | ||
| }; | ||
| expect(componentReferenceToCandidate(ref)).toEqual({ | ||
| id: "abc", | ||
| name: "train", | ||
| description: "", | ||
| inputs: ["dataset"], | ||
| outputs: ["model"], | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("rerankComponentsByNaturalLanguage", () => { | ||
| beforeEach(() => { | ||
| global.fetch = vi.fn(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it("returns an empty result for an empty query", async () => { | ||
| const result = await rerankComponentsByNaturalLanguage( | ||
| "", | ||
| [{ id: "a", name: "n", description: "d" }], | ||
| VALID_OPTIONS, | ||
| ); | ||
| expect(result.matches).toEqual([]); | ||
| expect(global.fetch).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("returns an empty result when no candidates are provided", async () => { | ||
| const result = await rerankComponentsByNaturalLanguage( | ||
| "train", | ||
| [], | ||
| VALID_OPTIONS, | ||
| ); | ||
| expect(result.matches).toEqual([]); | ||
| expect(global.fetch).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("throws NaturalLanguageSearchConfigError when API base or key is missing", async () => { | ||
| await expect( | ||
| rerankComponentsByNaturalLanguage( | ||
| "train", | ||
| [{ id: "a", name: "n", description: "d" }], | ||
| { ...VALID_OPTIONS, apiKey: "" }, | ||
| ), | ||
| ).rejects.toBeInstanceOf(NaturalLanguageSearchConfigError); | ||
|
|
||
| await expect( | ||
| rerankComponentsByNaturalLanguage( | ||
| "train", | ||
| [{ id: "a", name: "n", description: "d" }], | ||
| { ...VALID_OPTIONS, apiBase: "" }, | ||
| ), | ||
| ).rejects.toBeInstanceOf(NaturalLanguageSearchConfigError); | ||
| }); | ||
|
|
||
| it("throws NaturalLanguageSearchConfigError when model is missing", async () => { | ||
| await expect( | ||
| rerankComponentsByNaturalLanguage( | ||
| "train", | ||
| [{ id: "a", name: "n", description: "d" }], | ||
| { ...VALID_OPTIONS, model: "" }, | ||
| ), | ||
| ).rejects.toBeInstanceOf(NaturalLanguageSearchConfigError); | ||
| }); | ||
|
|
||
| it("filters out hallucinated ids the model returned", async () => { | ||
| vi.mocked(global.fetch).mockResolvedValue( | ||
| mockChatResponse({ | ||
| matches: [ | ||
| { id: "a", score: 0.9, reason: "best fit" }, | ||
| { id: "ghost", score: 0.8, reason: "made up" }, | ||
| ], | ||
| }), | ||
| ); | ||
|
|
||
| const result = await rerankComponentsByNaturalLanguage( | ||
| "train", | ||
| [{ id: "a", name: "trainer", description: "" }], | ||
| VALID_OPTIONS, | ||
| ); | ||
| expect(result.matches.map((m) => m.id)).toEqual(["a"]); | ||
| }); | ||
|
|
||
| it("clamps out-of-range score values into [0, 1]", async () => { | ||
| // NaN scores are intentionally not tested here: JSON.stringify({score: NaN}) | ||
| // serializes to `null`, which never reaches `normalizeScore` because | ||
| // `isValidMatch` rejects it upstream. | ||
| vi.mocked(global.fetch).mockResolvedValue( | ||
| mockChatResponse({ | ||
| matches: [ | ||
| { id: "a", score: 1.5, reason: "over" }, | ||
| { id: "b", score: -0.4, reason: "under" }, | ||
| ], | ||
| }), | ||
| ); | ||
|
|
||
| const result = await rerankComponentsByNaturalLanguage( | ||
| "train", | ||
| [ | ||
| { id: "a", name: "a", description: "" }, | ||
| { id: "b", name: "b", description: "" }, | ||
| ], | ||
| VALID_OPTIONS, | ||
| ); | ||
| const byId = Object.fromEntries(result.matches.map((m) => [m.id, m.score])); | ||
| expect(byId.a).toBe(1); | ||
| expect(byId.b).toBe(0); | ||
| }); | ||
|
|
||
| it("returns empty matches when the response shape is wrong, but keeps raw content", async () => { | ||
| vi.mocked(global.fetch).mockResolvedValue( | ||
| mockChatResponse({ matches: "not an array" }), | ||
| ); | ||
|
|
||
| const result = await rerankComponentsByNaturalLanguage( | ||
| "train", | ||
| [{ id: "a", name: "trainer", description: "" }], | ||
| VALID_OPTIONS, | ||
| ); | ||
| expect(result.matches).toEqual([]); | ||
| expect(result.rawContent).toContain("not an array"); | ||
| }); | ||
|
|
||
| it("uses max_completion_tokens for gpt-5 / o-series models", async () => { | ||
| vi.mocked(global.fetch).mockResolvedValue( | ||
| mockChatResponse({ matches: [] }), | ||
| ); | ||
|
|
||
| await rerankComponentsByNaturalLanguage( | ||
| "train", | ||
| [{ id: "a", name: "a", description: "" }], | ||
| { ...VALID_OPTIONS, model: "gpt-5-mini" }, | ||
| ); | ||
|
|
||
| const call = vi.mocked(global.fetch).mock.calls[0]; | ||
| const body = parseFetchBody(call); | ||
| expect(body.max_completion_tokens).toBeDefined(); | ||
| expect(body.max_tokens).toBeUndefined(); | ||
| expect(body.temperature).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("uses max_tokens and temperature for non-reasoning models", async () => { | ||
| vi.mocked(global.fetch).mockResolvedValue( | ||
| mockChatResponse({ matches: [] }), | ||
| ); | ||
|
|
||
| await rerankComponentsByNaturalLanguage( | ||
| "train", | ||
| [{ id: "a", name: "a", description: "" }], | ||
| VALID_OPTIONS, | ||
| ); | ||
|
|
||
| const call = vi.mocked(global.fetch).mock.calls[0]; | ||
| const body = parseFetchBody(call); | ||
| expect(body.max_tokens).toBeDefined(); | ||
| expect(body.max_completion_tokens).toBeUndefined(); | ||
| expect(body.temperature).toBe(0); | ||
| }); | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.