-
Notifications
You must be signed in to change notification settings - Fork 2.6k
feat: implement local vector store and embedding capabilities #5685
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 1 commit
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
243 changes: 243 additions & 0 deletions
243
src/services/code-index/embedders/__tests__/fastembed.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,243 @@ | ||
| // npx vitest services/code-index/embedders/__tests__/fastembed.spec.ts | ||
|
|
||
| import { describe, it, expect, beforeEach, vi } from "vitest" | ||
| import { FastEmbedEmbedder } from "../fastembed" | ||
|
|
||
| // Mock TelemetryService | ||
| vi.mock("@roo-code/telemetry", () => ({ | ||
| TelemetryService: { | ||
| instance: { | ||
| captureEvent: vi.fn(), | ||
| }, | ||
| }, | ||
| })) | ||
|
|
||
| // Mock i18n | ||
| vi.mock("../../../i18n", () => ({ | ||
| t: vi.fn((key: string, params?: any) => { | ||
| if (key === "embeddings:fastembed.modelNotSupported") { | ||
| return `Model "${params?.model}" not supported. Available models: ${params?.availableModels}` | ||
| } | ||
| if (key === "embeddings:fastembed.embeddingFailed") { | ||
| return `Failed to create embeddings with FastEmbed: ${params?.message}` | ||
| } | ||
| if (key === "embeddings:fastembed.noValidTexts") { | ||
| return "No valid texts to embed" | ||
| } | ||
| if (key === "embeddings:fastembed.invalidResponseFormat") { | ||
| return "Invalid response format from FastEmbed" | ||
| } | ||
| if (key === "embeddings:fastembed.invalidEmbeddingFormat") { | ||
| return "Invalid embedding format from FastEmbed" | ||
| } | ||
| return key | ||
| }), | ||
| })) | ||
|
|
||
| // Mock getModelQueryPrefix | ||
| vi.mock("../../../shared/embeddingModels", () => ({ | ||
| getModelQueryPrefix: vi.fn(() => null), | ||
| })) | ||
|
|
||
| // Mock @mastra/fastembed | ||
| vi.mock("@mastra/fastembed", () => ({ | ||
| fastembed: { | ||
| small: { | ||
| doEmbed: vi.fn(), | ||
| maxEmbeddingsPerCall: 256, | ||
| }, | ||
| base: { | ||
| doEmbed: vi.fn(), | ||
| maxEmbeddingsPerCall: 256, | ||
| }, | ||
| }, | ||
| })) | ||
|
|
||
| describe("FastEmbedEmbedder", () => { | ||
| let embedder: FastEmbedEmbedder | ||
| let mockSmallDoEmbed: any | ||
| let mockBaseDoEmbed: any | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
|
|
||
| // Get references to the mocked functions | ||
| const { fastembed } = require("@mastra/fastembed") | ||
| mockSmallDoEmbed = fastembed.small.doEmbed | ||
| mockBaseDoEmbed = fastembed.base.doEmbed | ||
| }) | ||
|
|
||
| describe("constructor", () => { | ||
| it("should initialize with default model (bge-small-en-v1.5)", () => { | ||
| embedder = new FastEmbedEmbedder({}) | ||
| expect(embedder.embedderInfo.name).toBe("fastembed") | ||
| }) | ||
|
|
||
| it("should initialize with specified model", () => { | ||
| embedder = new FastEmbedEmbedder({ fastEmbedModel: "bge-base-en-v1.5" }) | ||
| expect(embedder.embedderInfo.name).toBe("fastembed") | ||
| }) | ||
|
|
||
| it("should use fallback model for unsupported model", () => { | ||
| const consoleSpy = vi.spyOn(console, "warn").mockImplementation(() => {}) | ||
|
|
||
| embedder = new FastEmbedEmbedder({ fastEmbedModel: "unsupported-model" }) | ||
|
|
||
| expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('Model "unsupported-model" not available')) | ||
|
|
||
| consoleSpy.mockRestore() | ||
| }) | ||
| }) | ||
|
|
||
| describe("createEmbeddings", () => { | ||
| beforeEach(() => { | ||
| embedder = new FastEmbedEmbedder({}) | ||
| }) | ||
|
|
||
| it("should create embeddings for single text using small model", async () => { | ||
| const mockEmbeddings = [[0.1, 0.2, 0.3, 0.4]] | ||
| mockSmallDoEmbed.mockResolvedValue(mockEmbeddings) | ||
|
|
||
| const result = await embedder.createEmbeddings(["test text"]) | ||
|
|
||
| expect(mockSmallDoEmbed).toHaveBeenCalledWith({ values: ["test text"] }) | ||
| expect(result).toEqual({ | ||
| embeddings: mockEmbeddings, | ||
| }) | ||
| }) | ||
|
|
||
| it("should create embeddings for multiple texts using small model", async () => { | ||
| const mockEmbeddings = [ | ||
| [0.1, 0.2, 0.3, 0.4], | ||
| [0.5, 0.6, 0.7, 0.8], | ||
| ] | ||
| mockSmallDoEmbed.mockResolvedValue(mockEmbeddings) | ||
|
|
||
| const result = await embedder.createEmbeddings(["text 1", "text 2"]) | ||
|
|
||
| expect(mockSmallDoEmbed).toHaveBeenCalledWith({ values: ["text 1", "text 2"] }) | ||
| expect(result).toEqual({ | ||
| embeddings: mockEmbeddings, | ||
| }) | ||
| }) | ||
|
|
||
| it("should create embeddings using base model when specified", async () => { | ||
| embedder = new FastEmbedEmbedder({ fastEmbedModel: "bge-base-en-v1.5" }) | ||
| const mockEmbeddings = [[0.1, 0.2, 0.3, 0.4]] | ||
| mockBaseDoEmbed.mockResolvedValue(mockEmbeddings) | ||
|
|
||
| const result = await embedder.createEmbeddings(["test text"]) | ||
|
|
||
| expect(mockBaseDoEmbed).toHaveBeenCalledWith({ values: ["test text"] }) | ||
| expect(result).toEqual({ | ||
| embeddings: mockEmbeddings, | ||
| }) | ||
| }) | ||
|
|
||
| it("should handle empty input", async () => { | ||
| const result = await embedder.createEmbeddings([]) | ||
|
|
||
| expect(mockSmallDoEmbed).not.toHaveBeenCalled() | ||
| expect(result).toEqual({ | ||
| embeddings: [], | ||
| }) | ||
| }) | ||
|
|
||
| it("should handle FastEmbed API errors", async () => { | ||
| const error = new Error("FastEmbed API error") | ||
| mockSmallDoEmbed.mockRejectedValue(error) | ||
|
|
||
| await expect(embedder.createEmbeddings(["test text"])).rejects.toThrow( | ||
| "Failed to create embeddings with FastEmbed: FastEmbed API error", | ||
| ) | ||
| }) | ||
|
|
||
| it("should process large batches correctly", async () => { | ||
| const texts = Array.from({ length: 150 }, (_, i) => `text ${i}`) | ||
| const mockEmbeddings = texts.map((_, i) => [i * 0.1, i * 0.2, i * 0.3, i * 0.4]) | ||
| mockSmallDoEmbed.mockResolvedValue(mockEmbeddings) | ||
|
|
||
| const result = await embedder.createEmbeddings(texts) | ||
|
|
||
| expect(mockSmallDoEmbed).toHaveBeenCalledWith({ values: texts }) | ||
| expect(result.embeddings).toHaveLength(150) | ||
| }) | ||
| }) | ||
|
|
||
| describe("validateConfiguration", () => { | ||
| beforeEach(() => { | ||
| embedder = new FastEmbedEmbedder({}) | ||
| }) | ||
|
|
||
| it("should validate successfully with small model", async () => { | ||
| const mockEmbeddings = [[0.1, 0.2, 0.3, 0.4]] | ||
| mockSmallDoEmbed.mockResolvedValue(mockEmbeddings) | ||
|
|
||
| const result = await embedder.validateConfiguration() | ||
|
|
||
| expect(mockSmallDoEmbed).toHaveBeenCalledWith({ values: ["test"] }) | ||
| expect(result).toEqual({ valid: true }) | ||
| }) | ||
|
|
||
| it("should validate successfully with base model", async () => { | ||
| embedder = new FastEmbedEmbedder({ fastEmbedModel: "bge-base-en-v1.5" }) | ||
| const mockEmbeddings = [[0.1, 0.2, 0.3, 0.4]] | ||
| mockBaseDoEmbed.mockResolvedValue(mockEmbeddings) | ||
|
|
||
| const result = await embedder.validateConfiguration() | ||
|
|
||
| expect(mockBaseDoEmbed).toHaveBeenCalledWith({ values: ["test"] }) | ||
| expect(result).toEqual({ valid: true }) | ||
| }) | ||
|
|
||
| it("should return invalid when FastEmbed fails", async () => { | ||
| const error = new Error("FastEmbed validation error") | ||
| mockSmallDoEmbed.mockRejectedValue(error) | ||
|
|
||
| const result = await embedder.validateConfiguration() | ||
|
|
||
| expect(result).toEqual({ | ||
| valid: false, | ||
| error: "FastEmbed validation failed: FastEmbed validation error", | ||
| }) | ||
| }) | ||
|
|
||
| it("should handle unexpected validation errors", async () => { | ||
| mockSmallDoEmbed.mockRejectedValue("Unexpected error") | ||
|
|
||
| const result = await embedder.validateConfiguration() | ||
|
|
||
| expect(result).toEqual({ | ||
| valid: false, | ||
| error: "FastEmbed validation failed: Unexpected error", | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe("embedderInfo", () => { | ||
| it("should return correct embedder info", () => { | ||
| embedder = new FastEmbedEmbedder({}) | ||
| expect(embedder.embedderInfo).toEqual({ | ||
| name: "fastembed", | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe("model selection", () => { | ||
| it("should use small model by default", () => { | ||
| embedder = new FastEmbedEmbedder({}) | ||
| // We can't directly test the private property, but we can test the behavior | ||
| expect(() => embedder).not.toThrow() | ||
| }) | ||
|
|
||
| it("should use base model when specified", () => { | ||
| embedder = new FastEmbedEmbedder({ fastEmbedModel: "bge-base-en-v1.5" }) | ||
| expect(() => embedder).not.toThrow() | ||
| }) | ||
|
|
||
| it("should use small model when explicitly specified", () => { | ||
| embedder = new FastEmbedEmbedder({ fastEmbedModel: "bge-small-en-v1.5" }) | ||
| expect(() => embedder).not.toThrow() | ||
| }) | ||
| }) | ||
| }) |
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 config manager now reads new fields for vectorStoreType and localVectorStorePath. Consider validating that when vectorStoreType is 'local', a valid localVectorStorePath is provided or a sensible default is applied.