Document embeddings
This guide shows how you can create document embeddings using the Marengo 3.5 video understanding model. For complete specifications and input requirements, see the Marengo 3.5 page.
The Marengo video understanding model generates embeddings for all modalities in the same latent space. This shared space enables any-to-any searches across different types of content.
For details on how your usage is measured and billed, see the Pricing page.
Key concepts
This section explains the key concepts and terminology used in this guide:
- Asset: Your uploaded content. Once created, you can reference the same asset across multiple operations without uploading the file again.
- Embedding: Vector representation of your content.
- Embedding task: An asynchronous operation for processing your content and creating embeddings. Contains a status and the resulting embeddings when complete.
Workflow
This guide shows how to create embeddings for documents (PDF, plain text, and Markdown files), so your queries can match their content. The example uploads the PDF file as an asset. You can also pass a URL or base64-encoded data inline instead of creating an asset; both are shown as commented-out lines in the code examples.
The platform processes your files asynchronously, one file per request. This example embeds one document; repeat the request for each file in your collection. To embed a query instead, see the Embed a query guide.
You can configure what the platform embeds. For a PDF file, you can create one embedding per rendered page, one for the entire file, one for its extracted text, or split each rendered page into quadrants and receive five embeddings per page. For a plain text or Markdown file, you can create one embedding for the entire file or one for each chunk of whole sentences.
Use these embeddings for similarity search, content classification, clustering, recommendations, or Retrieval-Augmented Generation (RAG).
Embeddings created with the asynchronous method are stored for seven days. After this, you must recreate them to obtain the results again.
Prerequisites
-
To use the platform, you need an API key:
-
Depending on the programming language you are using, install the TwelveLabs SDK by entering one of the following commands:
-
Your documents must meet the following requirements:
-
Upload limits: Local documents up to 200 MB, or public document URLs up to 512 MB.
-
Formats: PDF (
.pdf), plain text (.txt), and Markdown (.md). -
Pages: A PDF file has a page allowance of 64 pages for each MB of file size. A 0.5 MB file is allowed 64 pages, and a 4 MB file is allowed 256 pages. The
quadrantsstrategy counts each page five times against this allowance. Plain text and Markdown files have no page allowance. -
Model capabilities: See the complete input requirements for Marengo 3.5.
-
Complete example
Copy and paste the code below, replacing the placeholders surrounded by <> with your values.
Code explanation
Python
Node.js
Import the SDK and initialize the client
Create a client instance to interact with the TwelveLabs Video Understanding Platform.
Function call: You call the constructor of the TwelveLabs class.
Parameters:
api_key: The API key to authenticate your requests to the platform.
Return value: An object of type TwelveLabs configured for making API calls.
Upload a document
Upload a document to create an asset.
Function call: You call the assets.create function.
Parameters:
method: The upload method for your asset. Useurlfor a publicly accessible file ordirectto upload a local file. This example usesurl.urlorfile: The publicly accessible URL of your document file or an opened file object in binary read mode. This example usesurl.
Return value: An object of type Asset. This object contains, among other information, a field named id representing the unique identifier of your asset.
Check the status of the asset
Asset processing is asynchronous. Poll the status of the asset until it is ready before you use it.
Function call: You call the assets.retrieve function.
Parameters:
asset_id: The unique identifier of your asset.
Return value: An object of type Asset containing, among other information, a field named status representing the current status of the asset. Check this field until its value is ready.
Create an embedding task
Create an embedding task to start processing your document.
Function call: You call the embed.v_2.tasks.create function.
Parameters:
input_type: The type of content. Set this parameter todocument.model_name: The embedding model to use. This example usesmarengo3.5.- (Optional)
embedding_uncertainty: Set this parameter totrueto receive adata[].embedding_uncertaintyfield in the response. This field is a per-dimension uncertainty vector with the same length as theembeddingarray. A higher value shows lower confidence in that dimension. - (Optional)
embedding_dimension: The number of dimensions of the embedding:128,256, or512. The default is512. Applies to the whole task. To embed the same content at a different length, create a second task. document: An object containing the following properties:media_source: An object specifying the source of the document file. Specify one of the following:-
asset_id: The unique identifier of an asset from a previous upload. -
url: The publicly accessible URL of the document file. -
base_64_string: The base64-encoded document data.This example uses the identifier of the asset created in the previous step.
-
- (Optional)
segmentation: Controls how the platform divides your document before embedding it. The object has two fields: setspatialfor a PDF file, orsequentialfor a plain text or Markdown file. If you provide neither field, the platform returns a400error.spatial: For a PDF file, sets how the platform divides each rendered page. Set thestrategyfield to thestandardvalue (the default) for one embedding per page. Set it to thequadrantsvalue to split each page on a 2×2 grid and receive five embeddings per page: one for the whole page and one per quarter. Thequadrantsstrategy uses five times as many tokens as thestandardstrategy and counts each page five times against the page allowance of the file. Requiresembedding_option: ["visual"]andembedding_scope: ["local"].sequential: For a plain text or Markdown file, divides the text into chunks of whole sentences. Setstrategytosentenceandmax_sentencesto the maximum number of sentences per chunk. Both fields are required, andmax_sentenceshas no default. A chunk that exceeds the context window of the model fails the task, so choose a value small enough that every chunk fits. Optionally, setoverlap_sentencesto repeat the last sentences of each chunk at the start of the next; it must be less thanmax_sentences. Requiresembedding_option: ["text"]andembedding_scope: ["local"].
- (Optional)
embedding_option: The types of embeddings to generate for the document. Valid values are the following:visual: Embeds the rendered pages. Valid for PDF files.text: Generates embeddings from the text content. The default for plain text and Markdown files. For a PDF file, valid only with theassetscope. You can request multiple types in one task: for example,embedding_option: ["visual", "text"]withembedding_scope: ["asset"]on a PDF file returns one whole-file visual embedding and one whole-file text embedding together.
- (Optional)
embedding_scope: The scope for which to generate embeddings. Valid values are the following:local: Returns one embedding for each part of the file. For a PDF file, each part is a rendered page, and this is the default. For a plain text or Markdown file, each part is a chunk of whole sentences, and thesegmentation.sequentialfield is required.asset: Returns one embedding for the entire file. The default for plain text and Markdown files. You can request multiple scopes in one task: for example,embedding_scope: ["local", "asset"]on a PDF file returns the per-page embeddings and the whole-file embedding together.
- (Optional)
embedding_type: Specifies how to structure the embedding. The only valid value isseparate_embedding. Documents have a single modality, so the platform returns a400error if you setfused_embedding.
To embed the extracted text of a PDF file, set embedding_option to ["text"] and embedding_scope to ["asset"]. If you omit embedding_scope, the platform uses the PDF default (local). The local scope supports only the visual option, so the request fails with a 400 error.
Return value: An object of type TasksCreateResponse containing, among other information, a field named id, which represents the unique identifier of your embedding task. You can use this identifier to track the status of your embedding task. The object also includes a metadata.embedding_dimension field, which contains the number of dimensions of the embeddings this task returns.
Monitor the status
The platform requires some time to process documents. Poll the status of the embedding task until it is ready. This example uses a loop to check the status every 5 seconds.
Function call: You repeatedly call the embed.v_2.tasks.retrieve function until the task completes.
Parameters:
task_id: The unique identifier of your embedding task.
Return value: An object of type EmbeddingTaskResponse containing, among other information, the following fields:
status: The current status of the task. The possible values are:processing: The platform is creating the embeddings.ready: Processing is complete. Embeddings are available in thedatafield.failed: The task failed.
data: When the status isready, this field contains a list of embedding objects. Each embedding object includes:embedding: The embedding vector (a list of floats).embedding_uncertainty: A per-dimension uncertainty vector with the same length as theembeddingarray. A higher value shows lower confidence in that dimension. Present when the request setsembedding_uncertaintytotrue.embedding_scope: The scope of the embedding. For a PDF file, a request that sets thelocalscope returnspage. For a plain text or Markdown file, a request that sets thelocalscope returnschunk. A request that sets theassetscope returnsasset.quadrant: The quarter of the page this embedding covers, such astop_left. Present only when thesegmentation.spatial.strategyisquadrants;nullon the whole-page embedding. All five embeddings of a page share the same page numbers andpagescope, so use this field to tell them apart.chunk_index: The position of the chunk in the file, counting from 0. Present only when thesegmentation.sequentialfield is set. Thedataarray has no ordering guarantee, so read this field to reconstruct chunk order.start_page_number: The first page this embedding covers, counting from 1. The platform returns this field only for page-level embeddings of a PDF file.end_page_number: The last page this embedding covers, counting from 1 and including that page.
metadata.embedding_dimension: The number of dimensions of the embeddings this task returns. Only Marengo 3.5.