Skip to content

embeddings

The embeddings module provides classes for creating embeddings from text inputs using OpenAI's models and supports both synchronous and asynchronous operations as well as the handling of raw responses and streaming response capabilities.

The module is appropriate for use in applications that require semantic analysis of text, like similarity searches, text clustering, and other natural language processing tasks that can benefit from high-dimensional vector representations of text.

Classes:

Name Description
AsyncEmbeddings
AsyncEmbeddingsWithRawResponse
AsyncEmbeddingsWithStreamingResponse
Embeddings
EmbeddingsWithRawResponse
EmbeddingsWithStreamingResponse

AsyncEmbeddings

AsyncEmbeddings(client: AsyncOpenAI)

Methods:

Name Description
create

Creates an embedding vector representing the input text.

with_raw_response
with_streaming_response

create async

create(
    *,
    input: Union[
        str,
        List[str],
        Iterable[int],
        Iterable[Iterable[int]],
    ],
    model: Union[
        str,
        Literal[
            "text-embedding-ada-002",
            "text-embedding-3-small",
            "text-embedding-3-large",
        ],
    ],
    dimensions: int | NotGiven = NOT_GIVEN,
    encoding_format: (
        Literal["float", "base64"] | NotGiven
    ) = NOT_GIVEN,
    user: str | NotGiven = NOT_GIVEN,
    extra_headers: Headers | None = None,
    extra_query: Query | None = None,
    extra_body: Body | None = None,
    timeout: float | Timeout | None | NotGiven = NOT_GIVEN
) -> CreateEmbeddingResponse

Creates an embedding vector representing the input text.

Parameters:

Name Type Description Default
input Union[str, List[str], Iterable[int], Iterable[Iterable[int]]]

Input text to embed, encoded as a string or array of tokens. To embed multiple inputs in a single request, pass an array of strings or array of token arrays. The input must not exceed the max input tokens for the model (8192 tokens for text-embedding-ada-002), cannot be an empty string, and any array must be 2048 dimensions or less. Example Python code for counting tokens.

required
model Union[str, Literal['text-embedding-ada-002', 'text-embedding-3-small', 'text-embedding-3-large']]

ID of the model to use. You can use the List models API to see all of your available models, or see our Model overview for descriptions of them.

required
dimensions int | NotGiven

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

NOT_GIVEN
encoding_format Literal['float', 'base64'] | NotGiven

The format to return the embeddings in. Can be either float or base64.

NOT_GIVEN
user str | NotGiven

A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. Learn more.

NOT_GIVEN
extra_headers Headers | None

Send extra headers

None
extra_query Query | None

Add additional query parameters to the request

None
extra_body Body | None

Add additional JSON properties to the request

None
timeout float | Timeout | None | NotGiven

Override the client-level default timeout for this request, in seconds

NOT_GIVEN

with_raw_response

with_raw_response() -> AsyncEmbeddingsWithRawResponse

with_streaming_response

with_streaming_response() -> (
    AsyncEmbeddingsWithStreamingResponse
)

AsyncEmbeddingsWithRawResponse

AsyncEmbeddingsWithRawResponse(embeddings: AsyncEmbeddings)

Attributes:

Name Type Description
create

create instance-attribute

create = async_to_raw_response_wrapper(create)

AsyncEmbeddingsWithStreamingResponse

AsyncEmbeddingsWithStreamingResponse(
    embeddings: AsyncEmbeddings,
)

Attributes:

Name Type Description
create

create instance-attribute

create = async_to_streamed_response_wrapper(create)

Embeddings

Embeddings(client: OpenAI)

Methods:

Name Description
create

Creates an embedding vector representing the input text.

with_raw_response
with_streaming_response

create

create(
    *,
    input: Union[
        str,
        List[str],
        Iterable[int],
        Iterable[Iterable[int]],
    ],
    model: Union[
        str,
        Literal[
            "text-embedding-ada-002",
            "text-embedding-3-small",
            "text-embedding-3-large",
        ],
    ],
    dimensions: int | NotGiven = NOT_GIVEN,
    encoding_format: (
        Literal["float", "base64"] | NotGiven
    ) = NOT_GIVEN,
    user: str | NotGiven = NOT_GIVEN,
    extra_headers: Headers | None = None,
    extra_query: Query | None = None,
    extra_body: Body | None = None,
    timeout: float | Timeout | None | NotGiven = NOT_GIVEN
) -> CreateEmbeddingResponse

Creates an embedding vector representing the input text.

Parameters:

Name Type Description Default
input Union[str, List[str], Iterable[int], Iterable[Iterable[int]]]

Input text to embed, encoded as a string or array of tokens. To embed multiple inputs in a single request, pass an array of strings or array of token arrays. The input must not exceed the max input tokens for the model (8192 tokens for text-embedding-ada-002), cannot be an empty string, and any array must be 2048 dimensions or less. Example Python code for counting tokens.

required
model Union[str, Literal['text-embedding-ada-002', 'text-embedding-3-small', 'text-embedding-3-large']]

ID of the model to use. You can use the List models API to see all of your available models, or see our Model overview for descriptions of them.

required
dimensions int | NotGiven

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

NOT_GIVEN
encoding_format Literal['float', 'base64'] | NotGiven

The format to return the embeddings in. Can be either float or base64.

NOT_GIVEN
user str | NotGiven

A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. Learn more.

NOT_GIVEN
extra_headers Headers | None

Send extra headers

None
extra_query Query | None

Add additional query parameters to the request

None
extra_body Body | None

Add additional JSON properties to the request

None
timeout float | Timeout | None | NotGiven

Override the client-level default timeout for this request, in seconds

NOT_GIVEN

with_raw_response

with_raw_response() -> EmbeddingsWithRawResponse

with_streaming_response

with_streaming_response() -> (
    EmbeddingsWithStreamingResponse
)

EmbeddingsWithRawResponse

EmbeddingsWithRawResponse(embeddings: Embeddings)

Attributes:

Name Type Description
create

create instance-attribute

create = to_raw_response_wrapper(create)

EmbeddingsWithStreamingResponse

EmbeddingsWithStreamingResponse(embeddings: Embeddings)

Attributes:

Name Type Description
create

create instance-attribute

create = to_streamed_response_wrapper(create)