-
Notifications
You must be signed in to change notification settings - Fork 5
Add e2e tests for embeddings.generate() method #60
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
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,158 @@ | ||
| import { beforeAll, describe, expect, it } from "vitest"; | ||
| import { OpenRouter } from "../../src/sdk/sdk.js"; | ||
|
|
||
| describe("Embeddings E2E Tests", () => { | ||
| let client: OpenRouter; | ||
|
|
||
| beforeAll(() => { | ||
| const apiKey = process.env.OPENROUTER_API_KEY; | ||
| if (!apiKey) { | ||
| throw new Error( | ||
| "OPENROUTER_API_KEY environment variable is required for e2e tests" | ||
| ); | ||
| } | ||
|
|
||
| client = new OpenRouter({ | ||
| apiKey, | ||
| }); | ||
| }); | ||
|
|
||
| describe("embeddings.generate()", () => { | ||
| it("should successfully generate embeddings for a single text input", async () => { | ||
| const response = await client.embeddings.generate({ | ||
| input: "The quick brown fox jumps over the lazy dog", | ||
| model: "openai/text-embedding-3-small", | ||
| }); | ||
|
|
||
| expect(response).toBeDefined(); | ||
|
|
||
| // Check if response is an object (not a string) | ||
| if (typeof response === "object") { | ||
| expect(response.data).toBeDefined(); | ||
| expect(Array.isArray(response.data)).toBe(true); | ||
| expect(response.data.length).toBeGreaterThan(0); | ||
|
|
||
| const firstEmbedding = response.data[0]; | ||
| expect(firstEmbedding).toBeDefined(); | ||
| expect(firstEmbedding?.embedding).toBeDefined(); | ||
|
|
||
| // Handle both array and base64 string embeddings | ||
| if (Array.isArray(firstEmbedding?.embedding)) { | ||
| expect(firstEmbedding.embedding.length).toBeGreaterThan(0); | ||
| // Verify embedding values are numbers | ||
| const firstValue = firstEmbedding.embedding[0]; | ||
| expect(typeof firstValue).toBe("number"); | ||
| } else { | ||
| expect(typeof firstEmbedding?.embedding).toBe("string"); | ||
| } | ||
|
|
||
| // Verify usage information if available | ||
| if (response.usage) { | ||
| expect(response.usage.totalTokens).toBeGreaterThan(0); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| it("should generate embeddings for multiple text inputs", async () => { | ||
| const inputs = [ | ||
| "Hello, world!", | ||
| "OpenRouter is amazing", | ||
| "Embeddings are vector representations of text", | ||
| ]; | ||
|
|
||
| const response = await client.embeddings.generate({ | ||
| input: inputs, | ||
| model: "openai/text-embedding-3-small", | ||
| }); | ||
|
|
||
| expect(response).toBeDefined(); | ||
|
|
||
| if (typeof response === "object") { | ||
| expect(response.data).toBeDefined(); | ||
| expect(Array.isArray(response.data)).toBe(true); | ||
| expect(response.data.length).toBe(inputs.length); | ||
|
|
||
| // Verify each embedding | ||
| response.data.forEach((embedding, index) => { | ||
| expect(embedding).toBeDefined(); | ||
| expect(embedding?.embedding).toBeDefined(); | ||
|
|
||
| if (Array.isArray(embedding?.embedding)) { | ||
| expect(embedding.embedding.length).toBeGreaterThan(0); | ||
| } else { | ||
| expect(typeof embedding?.embedding).toBe("string"); | ||
| } | ||
|
|
||
| expect(embedding?.index).toBe(index); | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| it("should generate consistent embedding dimensions", async () => { | ||
| const response = await client.embeddings.generate({ | ||
| input: ["First text", "Second text"], | ||
| model: "openai/text-embedding-3-small", | ||
| }); | ||
|
|
||
| expect(response).toBeDefined(); | ||
|
|
||
| if (typeof response === "object") { | ||
| expect(response.data.length).toBe(2); | ||
|
|
||
| const firstEmbedding = response.data[0]?.embedding; | ||
| const secondEmbedding = response.data[1]?.embedding; | ||
|
|
||
| // Only check dimensions if both are arrays | ||
| if (Array.isArray(firstEmbedding) && Array.isArray(secondEmbedding)) { | ||
| const firstDimension = firstEmbedding.length; | ||
| const secondDimension = secondEmbedding.length; | ||
|
|
||
| expect(firstDimension).toBe(secondDimension); | ||
| expect(firstDimension).toBeGreaterThan(0); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| it("should handle empty string input gracefully", async () => { | ||
| const response = await client.embeddings.generate({ | ||
| input: "", | ||
| model: "openai/text-embedding-3-small", | ||
| }); | ||
|
|
||
| expect(response).toBeDefined(); | ||
|
|
||
| if (typeof response === "object") { | ||
| expect(response.data).toBeDefined(); | ||
| expect(Array.isArray(response.data)).toBe(true); | ||
|
|
||
| if (response.data.length > 0) { | ||
| const embedding = response.data[0]; | ||
| expect(embedding?.embedding).toBeDefined(); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| it("should include model information in response", async () => { | ||
| const modelName = "openai/text-embedding-3-small"; | ||
| const response = await client.embeddings.generate({ | ||
| input: "Test input for model verification", | ||
| model: modelName, | ||
| }); | ||
|
|
||
| expect(response).toBeDefined(); | ||
|
|
||
| if (typeof response === "object") { | ||
| expect(response.model).toBeDefined(); | ||
| expect(typeof response.model).toBe("string"); | ||
|
|
||
| if (response.usage) { | ||
| expect(response.usage.promptTokens).toBeDefined(); | ||
| expect(response.usage.totalTokens).toBeDefined(); | ||
| expect(typeof response.usage.promptTokens).toBe("number"); | ||
| expect(typeof response.usage.totalTokens).toBe("number"); | ||
| expect(response.usage.totalTokens).toBeGreaterThan(0); | ||
| } | ||
| } | ||
| }); | ||
| }); | ||
| }); | ||
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.