Create text embeddings

To create text embeddings, you must specify at least the following parameters:

  • engine_name: The name of the engine you want to use. For details about the available engines, see the Engines page.
  • text: The text for which you want to create an embedding.

The response is an object containing the following fields:

  • engine_name: The name of the engine the platform has used to create this embedding.
  • text_embedding: An object that contains a field named float which is an array of floating point numbers representing the embedding.

Note that text embeddings cannot exceed 77 tokens. If your text exceeds this limit, the platform defaults to truncating the embedding at the end. You can alter this default behavior by using the text_truncate parameter to specify one of the following actions:

  • start: Truncate the beginning of the text.
  • end: Truncate the end of the text.
  • none: Return an error if the text exceeds the token limit.

For a description of each field in the request and response, see the API Reference > Create text embeddings page.

You can interact with the platform using one of the available SDKs or an HTTP client like requests or axios. This guide demonstrates how to use the SDKs, the recommended approach for most scenarios. If you need to make direct HTTP requests, refer to the API Reference > Create text embeddings page for details.

Prerequisites

  • You’re familiar with the concepts that are described on the Platform overview page.
  • You have an API key. To retrieve your API key, navigate to the API Key page and log in with your credentials. Then, select the Copy icon to the right of your API key to copy it to your clipboard.

Examples

The example code below creates a text embedding using the default behavior for handling text that is too long. Ensure you replace the placeholders surrounded by <> with your values.

from twelvelabs import TwelveLabs

client = TwelveLabs(api_key="<YOUR_API_KEY>")

embedding = client.embed.create(
  engine_name="Marengo-retrieval-2.6",
  text="<YOUR_TEXT>"
)

print("Created a text embedding")
print(f" Engine: {embedding.engine_name}")
print(f" Embedding: {embedding.text_embedding.float}")
import { TwelveLabs } from 'twelvelabs';

const client = new TwelveLabs({ apiKey:'<YOUR_API_KEY>' });
const embedding = await client.embed.create({
  engineName: 'Marengo-retrieval-2.6',
  text: '<YOUR_TEXT>
});

console.log("Created a text embedding")
console.log(`  Engine: ${embedding.engineName}`);
console.log(`  Embedding: ${embedding.textEmbedding.float}`)

The example output below was truncated for brevity:

Created a text embedding
 Engine: Marengo-retrieval-2.6
 Embedding: [0.012884886, 0.01771853, ..., -0.0022822001]

To override the default behavior of truncating the end of the text if a text embedding exceeds 77 tokens, specify the text_truncate parameter. The following example code sets the value of the text_truncate parameter to start to truncate the beginning of your text. Ensure you replace the placeholder surrounded by <> with your text.

embedding = client.embed.create(
    engine_name="Marengo-retrieval-2.6"
    text="<YOUR_TEXT>",
    text_truncate="start",
)
const embedding = await client.embed.create({
  engineName: 'Marengo-retrieval-2.6',
  text: '<YOUR_TEXT>',
  textTruncate: 'start',
});