Skip to content

Commit b2d6b74

Browse files
feat(community): Update Voyage embeddings parameters (#7689)
Co-authored-by: jacoblee93 <[email protected]>
1 parent 54b68a4 commit b2d6b74

File tree

2 files changed

+72
-2
lines changed
  • docs/core_docs/docs/integrations/text_embedding
  • libs/langchain-community/src/embeddings

2 files changed

+72
-2
lines changed

docs/core_docs/docs/integrations/text_embedding/voyageai.mdx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,23 @@ The `inputType` parameter allows you to specify the type of input text for bette
88
- `document`: Use this for documents or content that you want to be retrievable. Voyage AI will prepend a prompt to optimize the embeddings for document use cases.
99
- `None` (default): The input text will be directly encoded without any additional prompt.
1010

11+
Additionally, the class supports new parameters for further customization of the embedding process:
12+
13+
- **truncation**: Whether to truncate the input texts to the maximum length allowed by the model.
14+
- **outputDimension**: The desired dimension of the output embeddings.
15+
- **outputDtype**: The data type of the output embeddings. Can be `"float"` or `"int8"`.
16+
- **encodingFormat**: The format of the output embeddings. Can be `"float"`, `"base64"`, or `"ubinary"`.
17+
1118
```typescript
1219
import { VoyageEmbeddings } from "@langchain/community/embeddings/voyage";
1320

1421
const embeddings = new VoyageEmbeddings({
1522
apiKey: "YOUR-API-KEY", // In Node.js defaults to process.env.VOYAGEAI_API_KEY
1623
inputType: "document", // Optional: specify input type as 'query', 'document', or omit for None / Undefined / Null
24+
truncation: true, // Optional: enable truncation of input texts
25+
outputDimension: 768, // Optional: set desired output embedding dimension
26+
outputDtype: "float", // Optional: set output data type ("float" or "int8")
27+
encodingFormat: "float", // Optional: set output encoding format ("float", "base64", or "ubinary")
1728
});
1829
```
1930

libs/langchain-community/src/embeddings/voyage.ts

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,26 @@ export interface VoyageEmbeddingsParams extends EmbeddingsParams {
1919
* Input type for the embeddings request.
2020
*/
2121
inputType?: string;
22+
23+
/**
24+
* Whether to truncate the input texts to the maximum length allowed by the model.
25+
*/
26+
truncation?: boolean;
27+
28+
/**
29+
* The desired dimension of the output embeddings.
30+
*/
31+
outputDimension?: number;
32+
33+
/**
34+
* The data type of the output embeddings. Can be "float" or "int8".
35+
*/
36+
outputDtype?: string;
37+
38+
/**
39+
* The format of the output embeddings. Can be "float", "base64", or "ubinary".
40+
*/
41+
encodingFormat?: string;
2242
}
2343

2444
/**
@@ -42,6 +62,26 @@ export interface CreateVoyageEmbeddingRequest {
4262
* Input type for the embeddings request.
4363
*/
4464
input_type?: string;
65+
66+
/**
67+
* Whether to truncate the input texts.
68+
*/
69+
truncation?: boolean;
70+
71+
/**
72+
* The desired dimension of the output embeddings.
73+
*/
74+
output_dimension?: number;
75+
76+
/**
77+
* The data type of the output embeddings.
78+
*/
79+
output_dtype?: string;
80+
81+
/**
82+
* The format of the output embeddings.
83+
*/
84+
encoding_format?: string;
4585
}
4686

4787
/**
@@ -65,6 +105,14 @@ export class VoyageEmbeddings
65105

66106
inputType?: string;
67107

108+
truncation?: boolean;
109+
110+
outputDimension?: number;
111+
112+
outputDtype?: string;
113+
114+
encodingFormat?: string;
115+
68116
/**
69117
* Constructor for the VoyageEmbeddings class.
70118
* @param fields - An optional object with properties to configure the instance.
@@ -73,7 +121,7 @@ export class VoyageEmbeddings
73121
fields?: Partial<VoyageEmbeddingsParams> & {
74122
verbose?: boolean;
75123
apiKey?: string;
76-
inputType?: string; // Make inputType optional
124+
inputType?: string;
77125
}
78126
) {
79127
const fieldsWithDefaults = { ...fields };
@@ -92,6 +140,10 @@ export class VoyageEmbeddings
92140
this.apiKey = apiKey;
93141
this.apiUrl = `${this.basePath}/embeddings`;
94142
this.inputType = fieldsWithDefaults?.inputType;
143+
this.truncation = fieldsWithDefaults?.truncation;
144+
this.outputDimension = fieldsWithDefaults?.outputDimension;
145+
this.outputDtype = fieldsWithDefaults?.outputDtype;
146+
this.encodingFormat = fieldsWithDefaults?.encodingFormat;
95147
}
96148

97149
/**
@@ -107,6 +159,10 @@ export class VoyageEmbeddings
107159
model: this.modelName,
108160
input: batch,
109161
input_type: this.inputType,
162+
truncation: this.truncation,
163+
output_dimension: this.outputDimension,
164+
output_dtype: this.outputDtype,
165+
encoding_format: this.encodingFormat,
110166
})
111167
);
112168

@@ -135,6 +191,10 @@ export class VoyageEmbeddings
135191
model: this.modelName,
136192
input: text,
137193
input_type: this.inputType,
194+
truncation: this.truncation,
195+
output_dimension: this.outputDimension,
196+
output_dtype: this.outputDtype,
197+
encoding_format: this.encodingFormat,
138198
});
139199

140200
return data[0].embedding;
@@ -145,7 +205,6 @@ export class VoyageEmbeddings
145205
* @param request - An object with properties to configure the request.
146206
* @returns A Promise that resolves to the response from the Voyage AI API.
147207
*/
148-
149208
private async embeddingWithRetry(request: CreateVoyageEmbeddingRequest) {
150209
const makeCompletionRequest = async () => {
151210
const url = `${this.apiUrl}`;

0 commit comments

Comments
 (0)