- 
                Notifications
    You must be signed in to change notification settings 
- Fork 151
chore: When querying with vectorSearch use the generated embeddings MCP-245 #662
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 7 commits
      Commits
    
    
            Show all changes
          
          
            8 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      b973af8
              
                chore: Add interface for embeddings providers
              
              
                kmruiz 8839148
              
                chore: improve prompts for better accuracy
              
              
                kmruiz 89a556d
              
                chore: merge with main
              
              
                kmruiz dcf7d87
              
                chore: add more tests to ensure embedding generation works
              
              
                kmruiz bdb3d5a
              
                Merge branch 'main' into chore/mcp-245
              
              
                kmruiz 4588224
              
                chore: check-style yeehaw
              
              
                kmruiz b253aab
              
                chore: improvements based on PR comments
              
              
                kmruiz 4c0d6a3
              
                chore: add clarifying comments
              
              
                kmruiz 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
      
      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
    
  
  
    
              
  
    
      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,87 @@ | ||
| import { createVoyage } from "voyage-ai-provider"; | ||
| import type { VoyageProvider } from "voyage-ai-provider"; | ||
| import { embedMany } from "ai"; | ||
| import type { UserConfig } from "../config.js"; | ||
| import assert from "assert"; | ||
| import { createFetch } from "@mongodb-js/devtools-proxy-support"; | ||
| import { z } from "zod"; | ||
|  | ||
| type EmbeddingsInput = string; | ||
| type Embeddings = number[]; | ||
| export type EmbeddingParameters = { | ||
| inputType: "query" | "document"; | ||
| }; | ||
|  | ||
| export interface EmbeddingsProvider< | ||
| SupportedModels extends string, | ||
| SupportedEmbeddingParameters extends EmbeddingParameters, | ||
| > { | ||
| embed( | ||
| modelId: SupportedModels, | ||
| content: EmbeddingsInput[], | ||
| parameters: SupportedEmbeddingParameters | ||
| ): Promise<Embeddings[]>; | ||
| } | ||
|  | ||
| export const zVoyageModels = z | ||
| .enum(["voyage-3-large", "voyage-3.5", "voyage-3.5-lite", "voyage-code-3"]) | ||
| .default("voyage-3-large"); | ||
|  | ||
| export const zVoyageEmbeddingParameters = z.object({ | ||
| outputDimension: z | ||
| .union([z.literal(256), z.literal(512), z.literal(1024), z.literal(2048), z.literal(4096)]) | ||
| .optional() | ||
| .default(1024), | ||
| outputDType: z.enum(["float", "int8", "uint8", "binary", "ubinary"]).optional().default("float"), | ||
| }); | ||
|  | ||
| type VoyageModels = z.infer<typeof zVoyageModels>; | ||
| type VoyageEmbeddingParameters = z.infer<typeof zVoyageEmbeddingParameters> & EmbeddingParameters; | ||
|  | ||
| class VoyageEmbeddingsProvider implements EmbeddingsProvider<VoyageModels, VoyageEmbeddingParameters> { | ||
| private readonly voyage: VoyageProvider; | ||
|  | ||
| constructor({ voyageApiKey }: UserConfig, providedFetch?: typeof fetch) { | ||
| assert(voyageApiKey, "The VoyageAI API Key does not exist. This is likely a bug."); | ||
|  | ||
| // We should always use, by default, any enterprise proxy that the user has configured. | ||
| // Direct requests to VoyageAI might get blocked by the network if they don't go through | ||
| // the provided proxy. | ||
| const customFetch: typeof fetch = (providedFetch ?? | ||
| createFetch({ useEnvironmentVariableProxies: true })) as unknown as typeof fetch; | ||
|  | ||
| this.voyage = createVoyage({ apiKey: voyageApiKey, fetch: customFetch }); | ||
| } | ||
|  | ||
| static isConfiguredIn({ voyageApiKey }: UserConfig): boolean { | ||
| return !!voyageApiKey; | ||
| } | ||
|  | ||
| async embed<Model extends VoyageModels>( | ||
| modelId: Model, | ||
| content: EmbeddingsInput[], | ||
| parameters: VoyageEmbeddingParameters | ||
| ): Promise<Embeddings[]> { | ||
| const model = this.voyage.textEmbeddingModel(modelId); | ||
| const { embeddings } = await embedMany({ | ||
| model, | ||
| values: content, | ||
| providerOptions: { voyage: parameters }, | ||
| }); | ||
|  | ||
| return embeddings; | ||
| } | ||
| } | ||
|  | ||
| export function getEmbeddingsProvider( | ||
| userConfig: UserConfig | ||
| ): EmbeddingsProvider<VoyageModels, VoyageEmbeddingParameters> | undefined { | ||
| if (VoyageEmbeddingsProvider.isConfiguredIn(userConfig)) { | ||
|         
                  gagik marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| return new VoyageEmbeddingsProvider(userConfig); | ||
| } | ||
|  | ||
| return undefined; | ||
| } | ||
|  | ||
| export const zSupportedEmbeddingParameters = zVoyageEmbeddingParameters.extend({ model: zVoyageModels }); | ||
| export type SupportedEmbeddingParameters = z.infer<typeof zSupportedEmbeddingParameters>; | ||
      
      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.