@@ -27,14 +27,33 @@ export const zVoyageModels = z
2727 . enum ( [ "voyage-3-large" , "voyage-3.5" , "voyage-3.5-lite" , "voyage-code-3" ] )
2828 . default ( "voyage-3-large" ) ;
2929
30+ // Zod does not undestand JS boxed numbers (like Int32) as integer literals,
31+ // so we preprocess them to unwrap them so Zod understands them.
32+ function unboxNumber ( v : unknown ) : number {
33+ if ( v && typeof v === "object" && typeof v . valueOf === "function" ) {
34+ const n = Number ( v . valueOf ( ) ) ;
35+ if ( ! Number . isNaN ( n ) ) return n ;
36+ }
37+ return v as number ;
38+ }
39+
3040export const zVoyageEmbeddingParameters = z . object ( {
3141 outputDimension : z
32- . union ( [ z . literal ( 256 ) , z . literal ( 512 ) , z . literal ( 1024 ) , z . literal ( 2048 ) , z . literal ( 4096 ) ] )
42+ . preprocess (
43+ unboxNumber ,
44+ z . union ( [ z . literal ( 256 ) , z . literal ( 512 ) , z . literal ( 1024 ) , z . literal ( 2048 ) , z . literal ( 4096 ) ] )
45+ )
3346 . optional ( )
3447 . default ( 1024 ) ,
35- outputDType : z . enum ( [ "float" , "int8" , "uint8" , "binary" , "ubinary" ] ) . optional ( ) . default ( "float" ) ,
48+ outputDtype : z . enum ( [ "float" , "int8" , "uint8" , "binary" , "ubinary" ] ) . optional ( ) . default ( "float" ) ,
3649} ) ;
3750
51+ const zVoyageAPIParameters = zVoyageEmbeddingParameters
52+ . extend ( {
53+ inputType : z . enum ( [ "query" , "document" ] ) ,
54+ } )
55+ . strip ( ) ;
56+
3857type VoyageModels = z . infer < typeof zVoyageModels > ;
3958type VoyageEmbeddingParameters = z . infer < typeof zVoyageEmbeddingParameters > & EmbeddingParameters ;
4059
@@ -62,11 +81,15 @@ class VoyageEmbeddingsProvider implements EmbeddingsProvider<VoyageModels, Voyag
6281 content : EmbeddingsInput [ ] ,
6382 parameters : VoyageEmbeddingParameters
6483 ) : Promise < Embeddings [ ] > {
84+ // This ensures that if we receive any random parameter from the outside (agent or us)
85+ // it's stripped before sending it to Voyage, as Voyage will reject the request on
86+ // a single unknown parameter.
87+ const voyage = zVoyageAPIParameters . parse ( parameters ) ;
6588 const model = this . voyage . textEmbeddingModel ( modelId ) ;
6689 const { embeddings } = await embedMany ( {
6790 model,
6891 values : content ,
69- providerOptions : { voyage : parameters } ,
92+ providerOptions : { voyage } ,
7093 } ) ;
7194
7295 return embeddings ;
0 commit comments