Class for generating embeddings using the OpenAI API. Extends the Embeddings class and implements OpenAIEmbeddingsParams and AzureOpenAIInput.

// Embed a query using OpenAIEmbeddings to generate embeddings for a given text
const model = new OpenAIEmbeddings();
const res = await model.embedQuery(
"What would be a good company name for a company that makes colorful socks?",
);
console.log({ res });

Hierarchy (view full)

Constructors

Properties

azureADTokenProvider?: (() => Promise<string>)

A function that returns an access token for Microsoft Entra (formerly known as Azure Active Directory), which will be invoked on every request.

azureOpenAIApiDeploymentName?: string

Azure OpenAI API deployment name to use for completions when making requests to Azure OpenAI. This is the name of the deployment you created in the Azure portal. e.g. "my-openai-deployment" this will be used in the endpoint URL: https://{InstanceName}.openai.azure.com/openai/deployments/my-openai-deployment/

azureOpenAIApiInstanceName?: string

Azure OpenAI API instance name to use when making requests to Azure OpenAI. this is the name of the instance you created in the Azure portal. e.g. "my-openai-instance" this will be used in the endpoint URL: https://my-openai-instance.openai.azure.com/openai/deployments/{DeploymentName}/

azureOpenAIApiKey?: string

API key to use when making requests to Azure OpenAI.

azureOpenAIApiVersion?: string

API version to use when making requests to Azure OpenAI.

azureOpenAIBasePath?: string

Custom endpoint for Azure OpenAI API. This is useful in case you have a deployment in another region. e.g. setting this value to "https://westeurope.api.cognitive.microsoft.com/openai/deployments" will be result in the endpoint URL: https://westeurope.api.cognitive.microsoft.com/openai/deployments/{DeploymentName}/

batchSize: number = 512

The maximum number of documents to embed in a single request. This is limited by the OpenAI API to a maximum of 2048.

caller: AsyncCaller

The async caller should be used by subclasses to make any async calls, which will thus benefit from the concurrency and retry logic.

client: OpenAIClient
clientConfig: ClientOptions
dimensions?: number

The number of dimensions the resulting output embeddings should have. Only supported in text-embedding-3 and later models.

model: string = "text-embedding-ada-002"

Model name to use

modelName: string = "text-embedding-ada-002"

Model name to use Alias for model

organization?: string
stripNewLines: boolean = true

Whether to strip new lines from the input text. This is recommended by OpenAI for older models, but may not be suitable for all use cases. See: https://github.com/openai/openai-python/issues/418#issuecomment-1525939500

timeout?: number

Timeout to use when making requests to OpenAI.

Methods

  • Method to generate embeddings for an array of documents. Splits the documents into batches and makes requests to the OpenAI API to generate embeddings.

    Parameters

    • texts: string[]

      Array of documents to generate embeddings for.

    Returns Promise<number[][]>

    Promise that resolves to a 2D array of embeddings for each document.

  • Method to generate an embedding for a single document. Calls the embeddingWithRetry method with the document as the input.

    Parameters

    • text: string

      Document to generate an embedding for.

    Returns Promise<number[]>

    Promise that resolves to an embedding for the document.