@@ -2,7 +2,6 @@ import { type ClientOptions, OpenAI as OpenAIClient } from "openai";
22import { getEnvironmentVariable } from "@langchain/core/utils/env" ;
33import { Embeddings , type EmbeddingsParams } from "@langchain/core/embeddings" ;
44import { chunkArray } from "@langchain/core/utils/chunk_array" ;
5- import { OpenAICoreRequestOptions } from "./types.js" ;
65import { getEndpoint , OpenAIEndpointConfig } from "./utils/azure.js" ;
76import { wrapOpenAIClientError } from "./utils/openai.js" ;
87
@@ -50,6 +49,11 @@ export interface OpenAIEmbeddingsParams extends EmbeddingsParams {
5049 * See: https://github.com/openai/openai-python/issues/418#issuecomment-1525939500
5150 */
5251 stripNewLines ?: boolean ;
52+
53+ /**
54+ * The format to return the embeddings in. Can be either 'float' or 'base64'.
55+ */
56+ encodingFormat ?: "float" | "base64" ;
5357}
5458
5559/**
@@ -68,8 +72,8 @@ export interface OpenAIEmbeddingsParams extends EmbeddingsParams {
6872 *
6973 * ```
7074 */
71- export class OpenAIEmbeddings
72- extends Embeddings
75+ export class OpenAIEmbeddings < TOutput = number [ ] >
76+ extends Embeddings < TOutput >
7377 implements Partial < OpenAIEmbeddingsParams >
7478{
7579 model = "text-embedding-ada-002" ;
@@ -92,6 +96,8 @@ export class OpenAIEmbeddings
9296
9397 organization ?: string ;
9498
99+ encodingFormat ?: "float" | "base64" ;
100+
95101 protected client : OpenAIClient ;
96102
97103 protected clientConfig : ClientOptions ;
@@ -130,6 +136,7 @@ export class OpenAIEmbeddings
130136 fieldsWithDefaults ?. stripNewLines ?? this . stripNewLines ;
131137 this . timeout = fieldsWithDefaults ?. timeout ;
132138 this . dimensions = fieldsWithDefaults ?. dimensions ;
139+ this . encodingFormat = fieldsWithDefaults ?. encodingFormat ;
133140
134141 this . clientConfig = {
135142 apiKey,
@@ -146,7 +153,7 @@ export class OpenAIEmbeddings
146153 * @param texts Array of documents to generate embeddings for.
147154 * @returns Promise that resolves to a 2D array of embeddings for each document.
148155 */
149- async embedDocuments ( texts : string [ ] ) : Promise < number [ ] [ ] > {
156+ async embedDocuments ( texts : string [ ] ) : Promise < TOutput [ ] > {
150157 const batches = chunkArray (
151158 this . stripNewLines ? texts . map ( ( t ) => t . replace ( / \n / g, " " ) ) : texts ,
152159 this . batchSize
@@ -160,16 +167,19 @@ export class OpenAIEmbeddings
160167 if ( this . dimensions ) {
161168 params . dimensions = this . dimensions ;
162169 }
170+ if ( this . encodingFormat ) {
171+ params . encoding_format = this . encodingFormat ;
172+ }
163173 return this . embeddingWithRetry ( params ) ;
164174 } ) ;
165175 const batchResponses = await Promise . all ( batchRequests ) ;
166176
167- const embeddings : number [ ] [ ] = [ ] ;
177+ const embeddings : TOutput [ ] = [ ] ;
168178 for ( let i = 0 ; i < batchResponses . length ; i += 1 ) {
169179 const batch = batches [ i ] ;
170180 const { data : batchResponse } = batchResponses [ i ] ;
171181 for ( let j = 0 ; j < batch . length ; j += 1 ) {
172- embeddings . push ( batchResponse [ j ] . embedding ) ;
182+ embeddings . push ( batchResponse [ j ] . embedding as TOutput ) ;
173183 }
174184 }
175185 return embeddings ;
@@ -181,16 +191,19 @@ export class OpenAIEmbeddings
181191 * @param text Document to generate an embedding for.
182192 * @returns Promise that resolves to an embedding for the document.
183193 */
184- async embedQuery ( text : string ) : Promise < number [ ] > {
194+ async embedQuery ( text : string ) : Promise < TOutput > {
185195 const params : OpenAIClient . EmbeddingCreateParams = {
186196 model : this . model ,
187197 input : this . stripNewLines ? text . replace ( / \n / g, " " ) : text ,
188198 } ;
189199 if ( this . dimensions ) {
190200 params . dimensions = this . dimensions ;
191201 }
202+ if ( this . encodingFormat ) {
203+ params . encoding_format = this . encodingFormat ;
204+ }
192205 const { data } = await this . embeddingWithRetry ( params ) ;
193- return data [ 0 ] . embedding ;
206+ return data [ 0 ] . embedding as TOutput ;
194207 }
195208
196209 /**
@@ -223,7 +236,8 @@ export class OpenAIEmbeddings
223236
224237 this . client = new OpenAIClient ( params ) ;
225238 }
226- const requestOptions : OpenAICoreRequestOptions = { } ;
239+ const requestOptions = { } ;
240+
227241 return this . caller . call ( async ( ) => {
228242 try {
229243 const res = await this . client . embeddings . create (
0 commit comments