Embeddings

Overview

Provide a vector representation of the provided text.

Embedding vectors encode semantic information about the given text. This can be used as a basis for various use cases including custom RAG engine implementations, semantic search, clustering, and classification.

Examples

import os
from ai21 import AI21Client
from ai21.models import EmbedType
os.environ["AI21_API_KEY"] = <YOUR_API_KEY>

client = AI21Client()

def embedding():
    response = client.embed.create(
        texts=["You can now use AI21 Embeddings."],
        type=EmbedType.SEGMENT
    )
    print(response.to_json())
import requests
ROOT_URL = "https://api.ai21.com/studio/v1/"

def embedding():
  url = ROOT_URL + "embed"
  data = {
    "texts": ["You can now use AI21 Embeddings."],
    "type": "segment"
  }
  response = requests.post(
    url=url,
    headers={"Authorization": f"Bearer {AI21_API_KEY}"},
    json=data
  )
  print(response.json())

{
  "id": "4f38bb78-61b7-1a86-dc1f-405683c99dd6", 
  "results": [
    {
      "embedding": [
        0.037780508399009705,
       -0.015447948127985, 
       -0.004723816178739071, 
       -0.03125549480319023,
        0.015297219157218933,
        ...
      ]
    }
  ]
}

Response

  • id
  • results Array of embedding vectors, parallel to the array of texts passed in. Each embedding looks like this:
    { "embedding": [float_1, float_2, ..., float_n]}
Language
Credentials
Header