diff --git a/core/index.d.ts b/core/index.d.ts index 4f53b3c42..f3c8b03af 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -720,6 +720,9 @@ export interface EmbedOptions { apiBase?: string; apiKey?: string; model?: string; + engine?: string; + apiType?: string; + apiVersion?: string; requestOptions?: RequestOptions; } diff --git a/core/indexing/embeddings/OpenAIEmbeddingsProvider.ts b/core/indexing/embeddings/OpenAIEmbeddingsProvider.ts index 29e24ceeb..83d79a42a 100644 --- a/core/indexing/embeddings/OpenAIEmbeddingsProvider.ts +++ b/core/indexing/embeddings/OpenAIEmbeddingsProvider.ts @@ -13,11 +13,25 @@ class OpenAIEmbeddingsProvider extends BaseEmbeddingsProvider { model: "text-embedding-3-small", }; - async embed(chunks: string[]) { - if (!this.options.apiBase?.endsWith("/")) { - this.options.apiBase += "/"; + private _getEndpoint() { + if (!this.options.apiBase) { + throw new Error("No API base URL provided. Please set the 'apiBase' option in config.json"); } + this.options.apiBase = this.options.apiBase.endsWith("/") + ? this.options.apiBase + : `${this.options.apiBase}/`; + + if (this.options.apiType === "azure") { + return new URL( + `openai/deployments/${this.options.engine}/embeddings?api-version=${this.options.apiVersion}`, + this.options.apiBase, + ); + } + return new URL("embedding", this.options.apiBase); + } + + async embed(chunks: string[]) { const batchedChunks = []; for ( let i = 0; @@ -33,7 +47,7 @@ class OpenAIEmbeddingsProvider extends BaseEmbeddingsProvider { batchedChunks.map(async (batch) => { const fetchWithBackoff = () => withExponentialBackoff(() => - this.fetch(new URL("embeddings", this.options.apiBase), { + this.fetch(this._getEndpoint(), { method: "POST", body: JSON.stringify({ input: batch, @@ -42,6 +56,7 @@ class OpenAIEmbeddingsProvider extends BaseEmbeddingsProvider { headers: { Authorization: `Bearer ${this.options.apiKey}`, "Content-Type": "application/json", + "api-key": this.options.apiKey ?? "", // For Azure }, }), );