Skip to content

rg.Vector

A vector is a numerical representation of a Record field or attribute, usually the record's text. Vectors can be used to search for similar records via the UI or SDK. Vectors can be added to a record directly or as a dictionary with a key that the matches rg.VectorField name.

Usage Examples

To use vectors within a dataset, you must define a vector field in the dataset settings. The vector field is a list of vector fields that can be attached to a record. The following example demonstrates how to add vectors to a dataset and how to access vectors from a record object:

import argilla as rg

dataset = Dataset(
    name="dataset_with_metadata",
    settings=Settings(
        fields=[TextField(name="text")],
        questions=[LabelQuestion(name="label", labels=["positive", "negative"])],
        vectors=[
            VectorField(name="vector_name"),
        ],
    ),
)
dataset.create()

Then, you can add records to the dataset with vectors that correspond to the vector field defined in the dataset settings:

dataset.records.log(
    [
        {
            "text": "Hello World, how are you?",
            "vector_name": [0.1, 0.2, 0.3]
        }
    ]
)

Vectors can be passed using a mapping, where the key is the key in the data source and the value is the name in the dataset's setting's rg.VectorField object. For example, the following code adds a record with a vector using a mapping:

dataset.records.log(
    [
        {
            "text": "Hello World, how are you?",
            "x": [0.1, 0.2, 0.3]
        }
    ],
    mapping={"x": "vector_name"}
)

Or, vectors can be instantiated and added to a record directly, like this:

dataset.records.log(
    [
        rg.Record(
            fields={"text": "Hello World, how are you?"},
            vectors=[rg.Vector("embedding", [0.1, 0.2, 0.3])],
        )
    ]
)

Vector

Bases: Resource

Class for interacting with Argilla Vectors. Vectors are typically used to represent embeddings or features of records. The Vector class is used to deliver vectors to the Argilla server.

Attributes:

Name Type Description
name str

The name of the vector.

values list[float]

The values of the vector.

Source code in src/argilla/vectors.py
class Vector(Resource):
    """ Class for interacting with Argilla Vectors. Vectors are typically used to represent \
        embeddings or features of records. The `Vector` class is used to deliver vectors to the Argilla server.

    Attributes:
        name (str): The name of the vector.
        values (list[float]): The values of the vector.
    """

    _model: VectorModel

    def __init__(
        self,
        name: str,
        values: list[float],
    ) -> None:
        """Initializes a Vector with a name and values that can be used to search in the Argilla ui.

        Parameters:
            name (str): Name of the vector
            values (list[float]): List of float values

        """
        self._model = VectorModel(
            name=name,
            vector_values=values,
        )

    def __repr__(self) -> str:
        return repr(f"{self.__class__.__name__}({self._model})")

    ##############################
    # Properties
    ##############################

    @property
    def name(self) -> str:
        """Name of the vector that corresponds to the name of the vector in the dataset's `Settings`"""
        return self._model.name

    @property
    def values(self) -> list[float]:
        """List of float values that represent the vector."""
        return self._model.vector_values

    ##############################
    # Methods
    ##############################

    @classmethod
    def from_model(cls, model: VectorModel) -> "Vector":
        return cls(
            name=model.name,
            values=model.vector_values,
        )

    def serialize(self) -> dict[str, Any]:
        dumped_model = self._model.model_dump()
        name = dumped_model.pop("name")
        values = dumped_model.pop("vector_values")
        return {name: values}

name: str property

Name of the vector that corresponds to the name of the vector in the dataset's Settings

values: list[float] property

List of float values that represent the vector.

__init__(name, values)

Initializes a Vector with a name and values that can be used to search in the Argilla ui.

Parameters:

Name Type Description Default
name str

Name of the vector

required
values list[float]

List of float values

required
Source code in src/argilla/vectors.py
def __init__(
    self,
    name: str,
    values: list[float],
) -> None:
    """Initializes a Vector with a name and values that can be used to search in the Argilla ui.

    Parameters:
        name (str): Name of the vector
        values (list[float]): List of float values

    """
    self._model = VectorModel(
        name=name,
        vector_values=values,
    )