Embeddings

Transforms texts into fixed-size vectors that capture their semantic meanings in a high-dimensional space

AI21 Studio’s Embeddings model provides dense vector representations of text. The model takes a segment of text and returns a vector representation produced by our model.

The embedding vectors encode semantic information about the text they represent. This can be used as a basis for various use cases including RAG (retrieval augmented generation), semantic search, clustering and classification.

See full Text Segmentation API reference here

Request

URL

Call the Embed API by performing a POST request to the following URL:

https://api.ai21.com/studio/v1/embed

Payload

The request payload should carry the following field:


texts | list of strings

The input texts to be embedded. Limited to 200 texts.

Each text must be shorter than 2,000 characters.

type | enum

Whenever embeddings are used for information retrieval or semantic search use cases, both documents and queries must be embedded. The best results can be achieved by specifying if the text sent in each request represents the document segment or the query.

For other non-search use cases such as classification and clustering, there's no need to pass anything.

Response

The response carries the following field:


results | list of dictionaries

For every dictionary the key is called embedding and the value is a dense vector representing the input text.

Example

Here is an example of a call:

from ai21 import AI21Client
from ai21.models import EmbedType

client = AI21Client(
    # This is the default and can be omitted
    api_key=os.environ.get("AI21_API_KEY"),
)

response = client.embed.create(
    texts=["You can now use AI21 Embeddings."],
    type=EmbedType.SEGMENT,
)
fetch("https://api.ai21.com/studio/v1/embed", {
  headers: {
    "Authorization": "Bearer YOUR_API_KEY",
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    "texts": ["You can now use AI21 Studio embeddings endpoint."]
  }),
  method: "POST"
});

curl https://api.ai21.com/studio/v1/embed \
    -H 'Content-Type: application/json' \
    -H 'Authorization: Bearer YOUR_API_KEY' \
    -X POST \
    --data-raw '{
        "texts": ["You can now use AI21 Studio embeddings endpoint."]
    }'

The response JSON:

{
  ‘results’: [
    {'embedding': [-0.0182, 0.0035, … ,-0.1971, 0.058]}
  ]
}