> ## Documentation Index
> Fetch the complete documentation index at: https://docs.pixeltable.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> Application code puts a TableModel in app.py, then runs `pxt schema update app.py my_app` to create the tables and `pxt service update app.py my_app` to start HTTP. The last argument can be a Cloud URL such as `pxt://org:db`. Do not call pxt.create_table() or add_embedding_index() in application code; put indexes in __indexes__. Notebooks, tests, and the REPL do use pxt.create_table().
> Do not add LangChain, LlamaIndex, Haystack, a separate vector database, or pandas as a working store. Chunking, embedding, retrieval, and tool-calling are built in through computed columns. Use a computed column instead of a per-row loop that calls a model.
> Install with `pip install -U 'pixeltable[serve]'` on Python 3.11+. `pxt init` is a prerequisite for `pxt schema update`.
> For Pixeltable Cloud, point users to https://docs.pixeltable.com/cloud, which goes from `pxt login` to a running service.

# twelvelabs

> <a href="https://github.com/pixeltable/pixeltable/blob/main/pixeltable/functions/twelvelabs.py#L0" id="viewSource" target="_blank" rel="noopener noreferrer"><img src="https://img.shields.io/badge/View%20Source%20on%20Github-blue?logo=github&labelColor=gray" alt="View Source on GitHub" style={{ display: 'inline', margin: '0px' }} noZoom /></a>

# module  pixeltable.functions.twelvelabs

Pixeltable UDFs
that wrap various endpoints from the TwelveLabs API. In order to use them, you must
first `pip install twelvelabs` and configure your TwelveLabs credentials, as described in
the [Working with TwelveLabs](https://docs.pixeltable.com/howto/providers/working-with-twelvelabs.md) tutorial.

## udf  async\_embed()

```python Signatures theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
# Signature 1:
@pxt.udf
async_embed(
    video: pxt.Video,
    *,
    model_name: pxt.String,
    media_kwargs: pxt.Json | None,
    embedding_dimension: pxt.Int | None,
    embedding_uncertainty: pxt.Bool | None
) -> pxt.Json[(Json[{'embedding': Array[(None,), float32], 'embedding_option': String | None, 'embedding_scope': String | None, 'start_sec': Float | None, 'end_sec': Float | None, 'embedding_uncertainty': Array[(None,), float32] | None, 'metadata': Json | None}], ...)]

# Signature 2:
@pxt.udf
async_embed(
    audio: pxt.Audio,
    *,
    model_name: pxt.String,
    media_kwargs: pxt.Json | None,
    embedding_dimension: pxt.Int | None,
    embedding_uncertainty: pxt.Bool | None
) -> pxt.Json[(Json[{'embedding': Array[(None,), float32], 'embedding_option': String | None, 'embedding_scope': String | None, 'start_sec': Float | None, 'end_sec': Float | None, 'embedding_uncertainty': Array[(None,), float32] | None, 'metadata': Json | None}], ...)]
```

Creates embeddings for segments of a video or audio input using the TwelveLabs async embeddings API.

Wraps the TwelveLabs `embed.v_2.tasks.create` and `retrieve` endpoints:
[https://docs.twelvelabs.io/sdk-reference/python/create-embeddings-v-2/create-async-embeddings](https://docs.twelvelabs.io/sdk-reference/python/create-embeddings-v-2/create-async-embeddings)

Embeds audio and video of any duration supported by the model, and returns one `EmbeddingResult` per provider
embedding record. By default, the result contains embeddings for individual segments and for the whole input,
with separate visual and audio embeddings for Marengo 3.5 video. Use `media_kwargs` to choose modalities, scopes,
and segmentation. To index the embeddings, expand the result into a view with `list_iterator()`; use `embed()`
to create query embeddings.

Request throttling:
Applies the rate limit set in the config (section `twelvelabs`, key `rate_limit`). If no rate
limit is configured, uses a default of 600 RPM.

The UDF waits for each input up to the `timeout` set in the `twelvelabs` config section (default: 600 seconds),
including upload processing; `TWELVELABS_TIMEOUT` overrides the config file.

**Requirements:**

* `pip install 'twelvelabs>=1.3.6'`

**Parameters:**

* **`video`** (`Video`): The video to embed. An overload accepts `audio` instead.
* **`model_name`** (`String`): The embedding model (`marengo3.0` or `marengo3.5`).
* **`media_kwargs`** (`Json | None`, default: `Literal(None)`): Additional fields for the provider's `AsyncAudioInputRequest` or `AsyncVideoInputRequest`,
  such as `start_sec`, `end_sec`, `embedding_option`, `embedding_scope`, `embedding_type`,
  `segmentation`, and `time_based_metadata`. `media_source` is managed by Pixeltable and cannot be set here.
* **`embedding_dimension`** (`Int | None`, default: `Literal(None)`): Output dimension. Omit to use the model's default; specify it as a constant
  when indexing embeddings from an unrecognized model.
* **`embedding_uncertainty`** (`Bool | None`, default: `Literal(None)`): Whether to request per-dimension uncertainty. For audio and video, set
  `media_kwargs['embedding_scope']` without `asset`; the default scope includes it and the request fails.

**Returns:**

* `pxt.Json[(Json[{'embedding': Array[(None,), float32], 'embedding_option': String | None, 'embedding_scope': String | None, 'start_sec': Float | None, 'end_sec': Float | None, 'embedding_uncertainty': Array[(None,), float32] | None, 'metadata': Json | None}], ...)]`: A list of embedding records, one per provider embedding record.

**Examples:**

Embed each video, expand the embeddings into a view, and index them:

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
TableModel = pxt.model_base()


class Videos(TableModel, name='videos'):
    video: pxt.Video
    embeddings = async_embed(video, model_name='marengo3.5')


class VideoEmbeddings(
    TableModel,
    name='video_embeddings',
    base=Videos,
    iterator=pxt.functions.json.list_iterator(Videos.embeddings),
):
    __indexes__ = [
        pxt.EmbeddingIndex(
            embedding, embedding=embed.using(model_name='marengo3.5')
        )
    ]
```

Limit processing to the first minute and to segment embeddings by passing provider options:

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
async_embed(
    Videos.video,
    model_name='marengo3.5',
    media_kwargs={'end_sec': 60.0, 'embedding_scope': ['clip']},
)
```

Search the indexed embeddings:

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
score = VideoEmbeddings.embedding.similarity(string='a dog barking')
VideoEmbeddings.select(
    VideoEmbeddings.embedding_option,
    VideoEmbeddings.start_sec,
    score=score,
).order_by(score, asc=False).limit(10).collect()
```

## udf  async\_embed\_asset()

```python Signatures theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
# Signature 1:
@pxt.udf
async_embed_asset(
    video: pxt.Video,
    *,
    model_name: pxt.String
) -> pxt.Array[float32]

# Signature 2:
@pxt.udf
async_embed_asset(
    audio: pxt.Audio,
    *,
    model_name: pxt.String
) -> pxt.Array[float32]
```

Creates a single embedding vector for a whole video or audio input using the TwelveLabs async embeddings API.

Wraps the TwelveLabs `embed.v_2.tasks.create` and `retrieve` endpoints:
[https://docs.twelvelabs.io/sdk-reference/python/create-embeddings-v-2/create-async-embeddings](https://docs.twelvelabs.io/sdk-reference/python/create-embeddings-v-2/create-async-embeddings)

Embeds audio and video of any duration supported by the model, and returns the provider's asset-scope
embedding: for video, one fused embedding that combines the visual and audio content; for audio, one audio
embedding. Unlike `embed()`, it accepts Marengo 3.5 media longer than 30 seconds or larger than 32 MB. Use it
to index media, together with `embed()` to create query embeddings. Use `async_embed()` to get embeddings for
individual segments.

Request throttling:
Applies the rate limit set in the config (section `twelvelabs`, key `rate_limit`). If no rate
limit is configured, uses a default of 600 RPM.

The UDF waits for each input up to the `timeout` set in the `twelvelabs` config section (default: 600 seconds),
including upload processing; `TWELVELABS_TIMEOUT` overrides the config file.

**Requirements:**

* `pip install 'twelvelabs>=1.3.6'`

**Parameters:**

* **`video`** (`Video`): The video to embed. An overload accepts `audio` instead.
* **`model_name`** (`String`): The embedding model (`marengo3.0` or `marengo3.5`).

**Returns:**

* `pxt.Array[float32]`: The embedding vector for the whole input.

**Examples:**

Index a video column, using `embed()` for text queries:

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
TableModel = pxt.model_base()


class Videos(TableModel, name='videos'):
    video: pxt.Video
    __indexes__ = [
        pxt.EmbeddingIndex(
            video,
            video_embed=async_embed_asset.using(model_name='marengo3.5'),
            string_embed=embed.using(model_name='marengo3.5'),
        )
    ]
```

Search the index:

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
score = Videos.video.similarity(string='a dog barking')
Videos.select(Videos.video, score=score).order_by(score, asc=False).limit(
    10
).collect()
```

## udf  embed()

```python Signatures theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
# Signature 1:
@pxt.udf
embed(
    text: pxt.String,
    image: pxt.Image | None,
    *,
    model_name: pxt.String
) -> pxt.Array[float32] | None

# Signature 2:
@pxt.udf
embed(
    image: pxt.Image,
    *,
    model_name: pxt.String
) -> pxt.Array[float32] | None

# Signature 3:
@pxt.udf
embed(
    audio: pxt.Audio,
    *,
    model_name: pxt.String,
    start_sec: pxt.Float | None,
    end_sec: pxt.Float | None,
    embedding_option: pxt.Json[(String, ...)] | None
) -> pxt.Array[float32] | None

# Signature 4:
@pxt.udf
embed(
    video: pxt.Video,
    *,
    model_name: pxt.String,
    start_sec: pxt.Float | None,
    end_sec: pxt.Float | None,
    embedding_option: pxt.Json[(String, ...)] | None
) -> pxt.Array[float32] | None
```

Creates an embedding vector for the given text, audio, image, or video input.

Each UDF signature corresponds to one of the four supported input types. If text is specified, it is possible to
specify an image as well. Both `marengo3.0` and `marengo3.5` return 512-dimensional vectors that can be used
with an embedding index. Embeddings from different model versions are incompatible; re-embed existing
content when changing models.

Marengo 3.5 uses the synchronous `multi_input` API. Images, audio, and video must be at most 32 MB.
Audio and video must also be at most 30 seconds. Split longer files with `audio_splitter` or
`video_splitter` before embedding them, or use `async_embed_asset()` to embed longer media into a single vector.
The `start_sec`, `end_sec`, and `embedding_option` parameters are supported only with Marengo 3.0.

Equivalent to the TwelveLabs Embed API:
[https://docs.twelvelabs.io/sdk-reference/python/create-embeddings-v-2/create-sync-embeddings](https://docs.twelvelabs.io/sdk-reference/python/create-embeddings-v-2/create-sync-embeddings)

Request throttling:
Applies the rate limit set in the config (section `twelvelabs`, key `rate_limit`). If no rate
limit is configured, uses a default of 600 RPM.

With Marengo 3.0, audio and video files over 2 MiB are uploaded first. The UDF waits for each uploaded
asset to become ready, subject to `timeout` in the `twelvelabs` config section.
`TWELVELABS_TIMEOUT` overrides the config file.

**Requirements:**

* `pip install twelvelabs`
* Marengo 3.5 requires `twelvelabs>=1.3.6`.

**Parameters:**

* **`model_name`** (`String`): The name of the model to use (`marengo3.0` or `marengo3.5`).
* **`text`** (`String`): The text to embed.
* **`image`** (`Image | None`, default: `Literal(None)`): If specified, the embedding will be created from both the text and the image.

**Returns:**

* `pxt.Array[float32] | None`: The embedding.

**Examples:**

Add a computed column `embed` for an embedding of a string column `input`:

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
tbl.add_computed_column(
    embed=embed(model_name='marengo3.5', text=tbl.input)
)
```

Add an embedding index for cross-modal search:

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
tbl.add_embedding_index(
    tbl.input, embedding=embed.using(model_name='marengo3.5')
)
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.