@@ -71,7 +71,11 @@ export class OpenAICompatibleEmbedder implements IEmbedder {
7171 * @param model Optional model identifier
7272 * @returns Promise resolving to embedding response
7373 */
74- async createEmbeddings ( texts : string [ ] , model ?: string ) : Promise < EmbeddingResponse > {
74+ async createEmbeddings (
75+ texts : string [ ] ,
76+ model ?: string ,
77+ options ?: { dimension ?: number } ,
78+ ) : Promise < EmbeddingResponse > {
7579 const modelToUse = model || this . defaultModelId
7680
7781 // Apply model-specific query prefix if required
@@ -139,7 +143,7 @@ export class OpenAICompatibleEmbedder implements IEmbedder {
139143 }
140144
141145 if ( currentBatch . length > 0 ) {
142- const batchResult = await this . _embedBatchWithRetries ( currentBatch , modelToUse )
146+ const batchResult = await this . _embedBatchWithRetries ( currentBatch , modelToUse , options )
143147 allEmbeddings . push ( ...batchResult . embeddings )
144148 usage . promptTokens += batchResult . usage . promptTokens
145149 usage . totalTokens += batchResult . usage . totalTokens
@@ -181,7 +185,18 @@ export class OpenAICompatibleEmbedder implements IEmbedder {
181185 url : string ,
182186 batchTexts : string [ ] ,
183187 model : string ,
188+ options ?: { dimension ?: number } ,
184189 ) : Promise < OpenAIEmbeddingResponse > {
190+ const body : Record < string , any > = {
191+ input : batchTexts ,
192+ model : model ,
193+ encoding_format : "base64" ,
194+ }
195+
196+ if ( options ?. dimension ) {
197+ body . dimensions = options . dimension
198+ }
199+
185200 const response = await fetch ( url , {
186201 method : "POST" ,
187202 headers : {
@@ -191,11 +206,7 @@ export class OpenAICompatibleEmbedder implements IEmbedder {
191206 "api-key" : this . apiKey ,
192207 Authorization : `Bearer ${ this . apiKey } ` ,
193208 } ,
194- body : JSON . stringify ( {
195- input : batchTexts ,
196- model : model ,
197- encoding_format : "base64" ,
198- } ) ,
209+ body : JSON . stringify ( body ) ,
199210 } )
200211
201212 if ( ! response || ! response . ok ) {
@@ -234,6 +245,7 @@ export class OpenAICompatibleEmbedder implements IEmbedder {
234245 private async _embedBatchWithRetries (
235246 batchTexts : string [ ] ,
236247 model : string ,
248+ options ?: { dimension ?: number } ,
237249 ) : Promise < { embeddings : number [ ] [ ] ; usage : { promptTokens : number ; totalTokens : number } } > {
238250 // Use cached value for performance
239251 const isFullUrl = this . isFullUrl
@@ -244,7 +256,7 @@ export class OpenAICompatibleEmbedder implements IEmbedder {
244256
245257 if ( isFullUrl ) {
246258 // Use direct HTTP request for full endpoint URLs
247- response = await this . makeDirectEmbeddingRequest ( this . baseUrl , batchTexts , model )
259+ response = await this . makeDirectEmbeddingRequest ( this . baseUrl , batchTexts , model , options )
248260 } else {
249261 // Use OpenAI SDK for base URLs
250262 response = ( await this . embeddingsClient . embeddings . create ( {
@@ -254,6 +266,7 @@ export class OpenAICompatibleEmbedder implements IEmbedder {
254266 // when processing numeric arrays, which breaks compatibility with models using larger dimensions.
255267 // By requesting base64 encoding, we bypass the package's parser and handle decoding ourselves.
256268 encoding_format : "base64" ,
269+ ...( options ?. dimension && { dimensions : options . dimension } ) ,
257270 } ) ) as OpenAIEmbeddingResponse
258271 }
259272
0 commit comments