Integrate Hugging Face Dedicated as an embedding provider

Integrate Hugging Face Dedicated as an external embedding provider for Astra DB vectorize to leverage Hugging Face Dedicated’s embeddings API within Astra DB Serverless.

Prerequisites

To configure the Hugging Face Dedicated embedding provider integration, you need the following:

Create the Hugging Face Dedicated user access token

Sign in to your Hugging Face Dedicated account and create a new user access token with unrestricted access to the API. Make sure to copy the user access token to a secure location.

Don’t modify or delete the user access token in your Hugging Face Dedicated account after you’ve added it to Astra DB. This breaks the integration. For more information, see Embedding provider authentication.

Add the Hugging Face Dedicated integration to your organization

Use the Astra Portal to add the Hugging Face Dedicated embedding provider integration to your Astra DB organization:

  1. In the Astra Portal header, click Settings.

  2. In the Settings navigation menu, click the name of the current organization, and then select the organization where you want to enable the Hugging Face Dedicated integration.

  3. In the Settings navigation menu, click Integrations.

  4. In the All Integrations section, click Hugging Face Dedicated Embedding provider.

  5. Click Add integration.

  6. In the Add Integration dialog, do the following:

    1. Enter a unique User access token name.

      You can’t change user access token names. Make sure the name is meaningful and that it helps you identify your Hugging Face Dedicated user access token in Astra DB.

      User access token names must start and end with a letter or number, and they can’t exceed 50 characters. Valid characters include letters, numbers, underscores (_), and hyphens (-).

    2. Enter your Hugging Face Dedicated user access token.

    3. In the Add databases to scope section, select a Serverless (Vector) database that you want to use the Hugging Face Dedicated user access token.

      When you create a collection or use the Data API to create or alter a vector column in a table in a scoped database, you can select any of the user access tokens that are available to the database. Astra DB uses the user access token to request embeddings from your embedding provider when you load data into the collection or table.

      You can add up to 10 databases at once, and you can add more databases later.

      For greater access control, you can add multiple user access tokens, and each user access token can have different scoped databases. Additionally, you can add the same database to multiple user access token scopes.

      For example, you can have a few broadly-scoped user access tokens or many narrowly-scoped user access tokens.

  7. Click Add Integration.

    The Hugging Face Dedicated integration switches to Active, and your user access token and its scoped databases appear in the API keys section. If you want to add more user access tokens for this integration, click Add API key.

Add the Hugging Face Dedicated integration to collections and tables

You can use the Hugging Face Dedicated integration in collections and tables.

Add the Hugging Face Dedicated integration to a new collection

Before you can use the Hugging Face Dedicated integration to generate embeddings, you must add the integration to a new collection.

You can’t change a collection’s embedding provider or embedding generation method after you create it. To use a different embedding provider, you must create a new collection with a different embedding provider integration.

  • Astra Portal

  • Python

  • TypeScript

  • Java

  • curl

  1. In the Astra Portal, go to Databases, and then select your Serverless (Vector) database.

  2. Click Data Explorer.

  3. In the Keyspace field, select the keyspace where you want to create the collection or use default_keyspace.

  4. Click Create Collection.

  5. In the Create collection dialog, enter a name for the collection. Collection names can contain no more than 50 characters, including letters, numbers, and underscores.

  6. Turn on Vector-enabled collection.

  7. Under Embedding generation method, select the Hugging Face Dedicated embedding provider integration.

    If the integration isn’t listed, follow the steps in Add the Hugging Face Dedicated integration to your organization and Manage scoped databases to make sure the integration is active and that your database is scoped to at least one user access token.

  8. Complete the following fields:

    • User access token: The user access token that you want the collection to use to access your embedding provider and generate embeddings. This field is only active if the database is scoped to multiple Hugging Face Dedicated user access tokens.

    • Endpoint name: The programmatically-generated name of your Hugging Face Dedicated endpoint. This is the first part of the endpoint URL. For example, if your endpoint URL is https://mtp1x7muf6qyn3yh.us-east-2.aws.endpoints.huggingface.cloud, the endpoint name is mtp1x7muf6qyn3yh.

    • Region name: The cloud provider region your Hugging Face Dedicated endpoint is deployed to. For example, us-east-2.

    • Cloud name: The cloud provider your Hugging Face Dedicated endpoint is deployed to. For example, aws.

    • Embedding model: The model that you want to use to generate embeddings. The available models are: endpoint-defined-model.

      For Hugging Face Dedicated, you must deploy the model as a text embeddings inference (TEI) container.

      You must set Embedding model to endpoint-defined-model because this integration uses the model specified in your dedicated endpoint configuration.

    • Dimensions: The number of dimensions that you want the generated vectors to have. Most models automatically populate the Dimensions. You can edit this field if the model supports a range of dimensions or the embedding provider integration uses an endpoint-defined model. Your chosen embedding model must support the specified number of dimensions.

    • Similarity metric: The method you want to use to calculate vector similarity scores. The available metrics are Cosine, Dot Product, and Euclidean.

  9. Click Create collection.

Use the Python client to create a collection that uses the Hugging Face Dedicated integration.

Initialize the client

If you haven’t done so already, initialize the client before creating a collection:

import os
from astrapy import DataAPIClient
from astrapy.constants import VectorMetric
from astrapy.info import CollectionVectorServiceOptions

# Initialize the client and get a "Database" object
client = DataAPIClient(os.environ["ASTRA_DB_APPLICATION_TOKEN"])
database = client.get_database(os.environ["ASTRA_DB_API_ENDPOINT"])
print(f"* Database: {database.info().name}\n")

Create a collection integrated with Hugging Face Dedicated:

collection = database.create_collection(
    "COLLECTION_NAME",
    metric=VectorMetric.SIMILARITY_METRIC,
    dimension=MODEL_DIMENSIONS,
    service=CollectionVectorServiceOptions(
        provider="huggingfaceDedicated",
        model_name="endpoint-defined-model",
        authentication={
            "providerKey": "API_KEY_NAME",
        },
        parameters={
            "endpointName": "ENDPOINT_NAME",
            "regionName": "REGION_NAME",
            "cloudName": "CLOUD_NAME",
        },
    ),
)
print(f"* Collection: {collection.full_name}\n")

Replace the following:

  • COLLECTION_NAME: The name for your collection.

  • SIMILARITY_METRIC: The method you want to use to calculate vector similarity scores. The available metrics are COSINE (default), DOT_PRODUCT, and EUCLIDEAN.

  • API_KEY_NAME: The name of the Hugging Face Dedicated user access token that you want to use. Must be the name of an existing Hugging Face Dedicated user access token in the Astra Portal.

  • MODEL_NAME: The model that you want to use to generate embeddings. The available models are: endpoint-defined-model.

    For Hugging Face Dedicated, you must deploy the model as a text embeddings inference (TEI) container.

    You must set MODEL_NAME to endpoint-defined-model because this integration uses the model specified in your dedicated endpoint configuration.

  • MODEL_DIMENSIONS: The number of dimensions that you want the generated vectors to have. Your chosen embedding model must support the specified number of dimensions.

    If you omit dimension, Astra DB can use a default dimension value. However, some models don’t have default dimensions.

    You can use the Data API to find supported embedding providers and their configuration parameters, including dimensions ranges and default dimensions.

  • ENDPOINT_NAME: The programmatically-generated name of your Hugging Face Dedicated endpoint. This is the first part of the endpoint URL. For example, if your endpoint URL is https://mtp1x7muf6qyn3yh.us-east-2.aws.endpoints.huggingface.cloud, the endpoint name is mtp1x7muf6qyn3yh.

  • REGION_NAME: The cloud provider region your Hugging Face Dedicated endpoint is deployed to. For example, us-east-2.

  • CLOUD_NAME: The cloud provider your Hugging Face Dedicated endpoint is deployed to. For example, aws.

Use the TypeScript client to create a collection that uses the Hugging Face Dedicated integration.

Initialize the client

If you haven’t done so already, initialize the client before creating a collection:

import { DataAPIClient, VectorDoc, UUID } from '@datastax/astra-db-ts';

const { ASTRA_DB_APPLICATION_TOKEN, ASTRA_DB_API_ENDPOINT } = process.env;

// Initialize the client and get a 'Db' object
const client = new DataAPIClient(ASTRA_DB_APPLICATION_TOKEN);
const db = client.db(ASTRA_DB_API_ENDPOINT);

console.log(`* Connected to DB ${db.id}`);

Create a collection integrated with Hugging Face Dedicated:

(async function () {
  const collection = await db.createCollection('COLLECTION_NAME', {
    vector: {
      dimension: MODEL_DIMENSIONS,
      metric: 'SIMILARITY_METRIC', // optional
      service: {
        provider: 'huggingfaceDedicated',
        modelName: 'endpoint-defined-model',
        authentication: {
          providerKey: 'API_KEY_NAME',
        },
        parameters: {
          endpointName: 'ENDPOINT_NAME',
          regionName: 'REGION_NAME',
          cloudName: 'CLOUD_NAME',
        },
      },
    },
  });
  console.log(`* Created collection ${collection.keyspace}.${collection.collectionName}`);

Replace the following:

  • COLLECTION_NAME: The name for your collection.

  • SIMILARITY_METRIC: The method you want to use to calculate vector similarity scores. The available metrics are COSINE (default), DOT_PRODUCT, and EUCLIDEAN.

  • API_KEY_NAME: The name of the Hugging Face Dedicated user access token that you want to use. Must be the name of an existing Hugging Face Dedicated user access token in the Astra Portal.

  • MODEL_NAME: The model that you want to use to generate embeddings. The available models are: endpoint-defined-model.

    For Hugging Face Dedicated, you must deploy the model as a text embeddings inference (TEI) container.

    You must set MODEL_NAME to endpoint-defined-model because this integration uses the model specified in your dedicated endpoint configuration.

  • MODEL_DIMENSIONS: The number of dimensions that you want the generated vectors to have. Your chosen embedding model must support the specified number of dimensions.

    If you omit dimension, Astra DB can use a default dimension value. However, some models don’t have default dimensions.

    You can use the Data API to find supported embedding providers and their configuration parameters, including dimensions ranges and default dimensions.

  • ENDPOINT_NAME: The programmatically-generated name of your Hugging Face Dedicated endpoint. This is the first part of the endpoint URL. For example, if your endpoint URL is https://mtp1x7muf6qyn3yh.us-east-2.aws.endpoints.huggingface.cloud, the endpoint name is mtp1x7muf6qyn3yh.

  • REGION_NAME: The cloud provider region your Hugging Face Dedicated endpoint is deployed to. For example, us-east-2.

  • CLOUD_NAME: The cloud provider your Hugging Face Dedicated endpoint is deployed to. For example, aws.

Use the Java client to create a collection that uses the Hugging Face Dedicated integration.

Initialize the client

If you haven’t done so already, initialize the client before creating a collection:

import com.datastax.astra.client.Collection;
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.Database;
import com.datastax.astra.client.model.CollectionOptions;
import com.datastax.astra.client.model.Document;
import com.datastax.astra.client.model.FindIterable;
import com.datastax.astra.client.model.FindOptions;
import com.datastax.astra.client.model.SimilarityMetric;

// Replace SIMILARITY_METRIC with your desired similarity metric:
// COSINE, DOT_PRODUCT, EUCLIDEAN
import static com.datastax.astra.client.model.SimilarityMetric.SIMILARITY_METRIC;

public class Quickstart {

  public static void main(String[] args) {
    // Loading Arguments
    String astraToken = System.getenv("ASTRA_DB_APPLICATION_TOKEN");
    String astraApiEndpoint = System.getenv("ASTRA_DB_API_ENDPOINT");

    // Initialize the client
    DataAPIClient client = new DataAPIClient(astraToken);
    System.out.println("Connected to AstraDB");

    Database db = client.getDatabase(astraApiEndpoint);
    System.out.println("Connected to Database.");

Create a collection integrated with Hugging Face Dedicated:

Map<String, Object > params = new HashMap<>();
params.put("endpointName", "ENDPOINT_NAME");
params.put("regionName", "REGION_NAME");
params.put("cloudName", "CLOUD_NAME");
CollectionOptions.CollectionOptionsBuilder builder = CollectionOptions
       .builder()
       .vectorSimilarity(SimilarityMetric.SIMILARITY_METRIC)
       .vectorDimension(MODEL_DIMENSIONS)
       .vectorize("huggingfaceDedicated", "endpoint-defined-model", "API_KEY_NAME", params);
Collection<Document> collection = db
       .createCollection("COLLECTION_NAME", builder.build());

Replace the following:

  • COLLECTION_NAME: The name for your collection.

  • SIMILARITY_METRIC: The method you want to use to calculate vector similarity scores. The available metrics are COSINE (default), DOT_PRODUCT, and EUCLIDEAN.

  • API_KEY_NAME: The name of the Hugging Face Dedicated user access token that you want to use. Must be the name of an existing Hugging Face Dedicated user access token in the Astra Portal.

  • MODEL_NAME: The model that you want to use to generate embeddings. The available models are: endpoint-defined-model.

    For Hugging Face Dedicated, you must deploy the model as a text embeddings inference (TEI) container.

    You must set MODEL_NAME to endpoint-defined-model because this integration uses the model specified in your dedicated endpoint configuration.

  • MODEL_DIMENSIONS: The number of dimensions that you want the generated vectors to have. Your chosen embedding model must support the specified number of dimensions.

    If you omit dimension, Astra DB can use a default dimension value. However, some models don’t have default dimensions.

    You can use the Data API to find supported embedding providers and their configuration parameters, including dimensions ranges and default dimensions.

  • ENDPOINT_NAME: The programmatically-generated name of your Hugging Face Dedicated endpoint. This is the first part of the endpoint URL. For example, if your endpoint URL is https://mtp1x7muf6qyn3yh.us-east-2.aws.endpoints.huggingface.cloud, the endpoint name is mtp1x7muf6qyn3yh.

  • REGION_NAME: The cloud provider region your Hugging Face Dedicated endpoint is deployed to. For example, us-east-2.

  • CLOUD_NAME: The cloud provider your Hugging Face Dedicated endpoint is deployed to. For example, aws.

Use the Data API to create a collection that uses the Hugging Face Dedicated integration:

curl -sS -L -X POST "$ASTRA_DB_API_ENDPOINT/api/json/v1/default_keyspace" \
--header "Token: $ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
  "createCollection": {
    "name": "COLLECTION_NAME",
    "options": {
      "vector": {
        "metric": "SIMILARITY_METRIC", # optional
        "dimension": MODEL_DIMENSIONS, # optional
        "service": {
          "provider": "huggingfaceDedicated",
          "modelName": "endpoint-defined-model",
          "authentication": {
            "providerKey": "API_KEY_NAME"
          },
          "parameters": {
            "endpointName": "ENDPOINT_NAME",
            "regionName": "REGION_NAME",
            "cloudName": "CLOUD_NAME"
          }
        }
      }
    }
  }
}' | jq

Replace the following:

  • COLLECTION_NAME: The name for your collection.

  • SIMILARITY_METRIC: The method you want to use to calculate vector similarity scores. The available metrics are COSINE (default), DOT_PRODUCT, and EUCLIDEAN.

  • API_KEY_NAME: The name of the Hugging Face Dedicated user access token that you want to use. Must be the name of an existing Hugging Face Dedicated user access token in the Astra Portal.

  • MODEL_NAME: The model that you want to use to generate embeddings. The available models are: endpoint-defined-model.

    For Hugging Face Dedicated, you must deploy the model as a text embeddings inference (TEI) container.

    You must set MODEL_NAME to endpoint-defined-model because this integration uses the model specified in your dedicated endpoint configuration.

  • MODEL_DIMENSIONS: The number of dimensions that you want the generated vectors to have. Your chosen embedding model must support the specified number of dimensions.

    If you omit dimension, Astra DB can use a default dimension value. However, some models don’t have default dimensions.

    You can use the Data API to find supported embedding providers and their configuration parameters, including dimensions ranges and default dimensions.

  • ENDPOINT_NAME: The programmatically-generated name of your Hugging Face Dedicated endpoint. This is the first part of the endpoint URL. For example, if your endpoint URL is https://mtp1x7muf6qyn3yh.us-east-2.aws.endpoints.huggingface.cloud, the endpoint name is mtp1x7muf6qyn3yh.

  • REGION_NAME: The cloud provider region your Hugging Face Dedicated endpoint is deployed to. For example, us-east-2.

  • CLOUD_NAME: The cloud provider your Hugging Face Dedicated endpoint is deployed to. For example, aws.

If you get a Collection Limit Reached or TOO_MANY_INDEXES message, you must delete a collection before you can create a new one.

Serverless (Vector) databases created after June 24, 2024 can have approximately 10 collections. Databases created before this date can have approximately 5 collections. The collection limit is based on the number of indexes.

Add the Hugging Face Dedicated integration to a table

You can use the Data API to add the Hugging Face Dedicated integration to a table in multiple ways:

  • Add the integration to a vector column when you create a table.

  • Add the integration to an existing vector column in a table.

  • Add the integration when you add a vector column to an existing table.

If you are new to the Data API, see Get started with the Data API.

Add the integration to a new table

  • Python

  • TypeScript

  • Java

  • curl

Use the Python client to create a table with a column that is integrated with Hugging Face Dedicated:

import os
from astrapy import DataAPIClient
from astrapy.constants import VectorMetric
from astrapy.info import (
    CreateTableDefinition,
    ColumnType,
    VectorServiceOptions,
    TableVectorColumnTypeDescriptor,
    TableScalarColumnTypeDescriptor,
    TablePrimaryKeyDescriptor,
    TableVectorIndexOptions
)

# Initialize the client and get a "Database" object
client = DataAPIClient(os.environ["ASTRA_DB_APPLICATION_TOKEN"])
database = client.get_database(os.environ["ASTRA_DB_API_ENDPOINT"])
print(f"* Database: {database.info().name}\n")

# Define the columns and primary key for the table
table_definition = CreateTableDefinition(
    columns={
        # This column will store vector embeddings.
        # The Hugging Face Dedicated integration
        # will automatically generate vector embeddings
        # for any text inserted to this column.
        "example_vector": TableVectorColumnTypeDescriptor(
            dimension=MODEL_DIMENSIONS,
            service=VectorServiceOptions(
                provider="huggingfaceDedicated",
                model_name="endpoint-defined-model",
                authentication={
                    "providerKey": "API_KEY_NAME",
                },
                parameters={
                    "endpointName": "ENDPOINT_NAME",
                    "regionName": "REGION_NAME",
                    "cloudName": "CLOUD_NAME",
                },
            ),
        ),
        # If you want to store the original text
        # in addition to the generated embeddings
        # you must create a separate column.
        "original_text": TableScalarColumnTypeDescriptor(
            column_type=ColumnType.TEXT
        ),
    },
    # Define the primary key for the table.
    # You should change the primary key definition to meet the needs of your data.
    primary_key=TablePrimaryKeyDescriptor(
        partition_by=["original_text"],
        partition_sort={}
    ),
)

table = database.create_table(
    "TABLE_NAME",
    definition=table_definition,
)

print("Created table")

# Index the vector column so that you can perform a vector search on it.
table.create_vector_index(
    "example_vector_index",
    column="example_vector",
    options=TableVectorIndexOptions(
        metric=VectorMetric.SIMILARITY_METRIC,
    ),
)

print("Indexed columns")

Replace the following:

  • TABLE_NAME: The name for your table.

  • SIMILARITY_METRIC: The method you want to use to calculate vector similarity scores. The available metrics are COSINE (default), DOT_PRODUCT, and EUCLIDEAN.

  • API_KEY_NAME: The name of the Hugging Face Dedicated user access token that you want to use. Must be the name of an existing Hugging Face Dedicated user access token in the Astra Portal.

  • MODEL_NAME: The model that you want to use to generate embeddings. The available models are: endpoint-defined-model.

    For Hugging Face Dedicated, you must deploy the model as a text embeddings inference (TEI) container.

    You must set MODEL_NAME to endpoint-defined-model because this integration uses the model specified in your dedicated endpoint configuration.

  • MODEL_DIMENSIONS: The number of dimensions that you want the generated vectors to have. Your chosen embedding model must support the specified number of dimensions.

    If you omit dimension, Astra DB can use a default dimension value. However, some models don’t have default dimensions.

    You can use the Data API to find supported embedding providers and their configuration parameters, including dimensions ranges and default dimensions.

  • ENDPOINT_NAME: The programmatically-generated name of your Hugging Face Dedicated endpoint. This is the first part of the endpoint URL. For example, if your endpoint URL is https://mtp1x7muf6qyn3yh.us-east-2.aws.endpoints.huggingface.cloud, the endpoint name is mtp1x7muf6qyn3yh.

  • REGION_NAME: The cloud provider region your Hugging Face Dedicated endpoint is deployed to. For example, us-east-2.

  • CLOUD_NAME: The cloud provider your Hugging Face Dedicated endpoint is deployed to. For example, aws.

Use the TypeScript client to create a table with a column that is integrated with Hugging Face Dedicated:

import { DataAPIClient, Table, InferTableSchema } from '@datastax/astra-db-ts';

const { ASTRA_DB_APPLICATION_TOKEN, ASTRA_DB_API_ENDPOINT } = process.env;

// Initialize the client and get a 'Db' object
const client = new DataAPIClient(ASTRA_DB_APPLICATION_TOKEN);
const db = client.db(ASTRA_DB_API_ENDPOINT);

console.log(`* Connected to DB ${db.id}`);

const tableDefinition = Table.schema({
  // Define all of the columns in the table
  columns: {
    // This column will store vector embeddings.
    // The Hugging Face Dedicated integration
    // will automatically generate vector embeddings
    // for any text inserted to this column.
    example_vector: {
      type: "vector",
      dimension: MODEL_DIMENSIONS,
      service: {
        provider: 'huggingfaceDedicated',
        modelName: 'endpoint-defined-model',
        authentication: {
          providerKey: 'API_KEY_NAME',
        },
        parameters: {
          endpointName: 'ENDPOINT_NAME',
          regionName: 'REGION_NAME',
          cloudName: 'CLOUD_NAME',
        },
      },
    },
    // If you want to store the original text
    // in addition to the generated embeddings
    // you must create a separate column.
    original_text: "text",
  },
  // Define the primary key for the table.
  // You should change the primary key definition to meet the needs of your data.
  primaryKey: {
    partitionBy: ["original_text"],
  },
});

type TableSchema = InferTableSchema<typeof tableDefinition>;

(async function () {
  const table = await db.createTable<TableSchema>(
    'TABLE_NAME',
    { definition: tableDefinition },
  );

  console.log("Created table");

  // Index the vector column so that you can perform a vector search on it.
  await table.createVectorIndex(
    "example_vector_index",
    "example_vector",
    {
      options: {
        metric: 'SIMILARITY_METRIC', // optional
      },
    },
  );

  console.log("Indexed columns");
})();

Replace the following:

  • TABLE_NAME: The name for your table.

  • SIMILARITY_METRIC: The method you want to use to calculate vector similarity scores. The available metrics are COSINE (default), DOT_PRODUCT, and EUCLIDEAN.

  • API_KEY_NAME: The name of the Hugging Face Dedicated user access token that you want to use. Must be the name of an existing Hugging Face Dedicated user access token in the Astra Portal.

  • MODEL_NAME: The model that you want to use to generate embeddings. The available models are: endpoint-defined-model.

    For Hugging Face Dedicated, you must deploy the model as a text embeddings inference (TEI) container.

    You must set MODEL_NAME to endpoint-defined-model because this integration uses the model specified in your dedicated endpoint configuration.

  • MODEL_DIMENSIONS: The number of dimensions that you want the generated vectors to have. Your chosen embedding model must support the specified number of dimensions.

    If you omit dimension, Astra DB can use a default dimension value. However, some models don’t have default dimensions.

    You can use the Data API to find supported embedding providers and their configuration parameters, including dimensions ranges and default dimensions.

  • ENDPOINT_NAME: The programmatically-generated name of your Hugging Face Dedicated endpoint. This is the first part of the endpoint URL. For example, if your endpoint URL is https://mtp1x7muf6qyn3yh.us-east-2.aws.endpoints.huggingface.cloud, the endpoint name is mtp1x7muf6qyn3yh.

  • REGION_NAME: The cloud provider region your Hugging Face Dedicated endpoint is deployed to. For example, us-east-2.

  • CLOUD_NAME: The cloud provider your Hugging Face Dedicated endpoint is deployed to. For example, aws.

Use the Java client to create a table with a column that is integrated with Hugging Face Dedicated:

import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.vector.SimilarityMetric;
import com.datastax.astra.client.core.vectorize.VectorServiceOptions;
import com.datastax.astra.client.databases.Database;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.definition.TableDefinition;
import com.datastax.astra.client.tables.definition.columns.ColumnDefinitionVector;
import com.datastax.astra.client.tables.definition.indexes.TableVectorIndexDefinition;
import com.datastax.astra.client.tables.definition.rows.Row;

import java.util.HashMap;
import java.util.Map;

public class EmbeddingDemo {

  public static void main(String[] args) {
    // Loading Arguments
    String astraToken = System.getenv("ASTRA_DB_APPLICATION_TOKEN");
    String astraApiEndpoint = System.getenv("ASTRA_DB_API_ENDPOINT");

    // Initialize the client
    DataAPIClient client = new DataAPIClient(astraToken);
    System.out.println("Connected to AstraDB");

    Database database = client.getDatabase(astraApiEndpoint);
    System.out.println("Connected to Database.");

    // Define parameters for the service provider
    Map<String, Object > params = new HashMap<>();
    params.put("endpointName", "ENDPOINT_NAME");
    params.put("regionName", "REGION_NAME");
    params.put("cloudName", "CLOUD_NAME");

    // Define the columns and primary key for the table
    TableDefinition tableDefinition =
        new TableDefinition()
            // This column will store vector embeddings.
            // The Hugging Face Dedicated integration
            // will automatically generate vector embeddings
            // for any text inserted to this column.
            .addColumnVector(
                "example_vector",
                new ColumnDefinitionVector()
                    .dimension(MODEL_DIMENSIONS)
                    .metric(SimilarityMetric.SIMILARITY_METRIC)
                    .service(
                        new VectorServiceOptions()
                            .provider("huggingfaceDedicated")
                            .modelName("endpoint-defined-model")
                            .authentication(Map.of("providerKey", "API_KEY_NAME"))
                    )
            )
            // If you want to store the original text
            // in addition to the generated embeddings
            // you must create a separate column.
            .addColumnText("original_text")
            // Define the primary key for the table.
            // You should change the primary key definition to meet the needs of your data.
            .addPartitionBy("original_text");

    Table<Row> table = database.createTable("TABLE_NAME", tableDefinition);

    System.out.println("Created table");

    // Index the vector column so that you can perform a vector search on it.
    TableVectorIndexDefinition definition =
        new TableVectorIndexDefinition().column("example_vector").metric(SimilarityMetric.SIMILARITY_METRIC);

    table.createVectorIndex("example_vector_index", definition);

    System.out.println("Indexed columns");
  }
}

Replace the following:

  • TABLE_NAME: The name for your table.

  • SIMILARITY_METRIC: The method you want to use to calculate vector similarity scores. The available metrics are COSINE (default), DOT_PRODUCT, and EUCLIDEAN.

  • API_KEY_NAME: The name of the Hugging Face Dedicated user access token that you want to use. Must be the name of an existing Hugging Face Dedicated user access token in the Astra Portal.

  • MODEL_NAME: The model that you want to use to generate embeddings. The available models are: endpoint-defined-model.

    For Hugging Face Dedicated, you must deploy the model as a text embeddings inference (TEI) container.

    You must set MODEL_NAME to endpoint-defined-model because this integration uses the model specified in your dedicated endpoint configuration.

  • MODEL_DIMENSIONS: The number of dimensions that you want the generated vectors to have. Your chosen embedding model must support the specified number of dimensions.

    If you omit dimension, Astra DB can use a default dimension value. However, some models don’t have default dimensions.

    You can use the Data API to find supported embedding providers and their configuration parameters, including dimensions ranges and default dimensions.

  • ENDPOINT_NAME: The programmatically-generated name of your Hugging Face Dedicated endpoint. This is the first part of the endpoint URL. For example, if your endpoint URL is https://mtp1x7muf6qyn3yh.us-east-2.aws.endpoints.huggingface.cloud, the endpoint name is mtp1x7muf6qyn3yh.

  • REGION_NAME: The cloud provider region your Hugging Face Dedicated endpoint is deployed to. For example, us-east-2.

  • CLOUD_NAME: The cloud provider your Hugging Face Dedicated endpoint is deployed to. For example, aws.

Use the Data API to add the Hugging Face Dedicated integration to a vector column in a table.

curl -sS -L -X POST "$ASTRA_DB_API_ENDPOINT/api/json/v1/default_keyspace" \
--header "Token: $ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
  "createTable": {
    "name": "TABLE_NAME",
    "definition": {
      "columns": {
        # This column will store vector embeddings.
        # The Hugging Face Dedicated integration
        # will automatically generate vector embeddings
        # for any text inserted to this column.
        "example_vector": {
          "type": "vector",
          "dimension": MODEL_DIMENSIONS, # optional
          "service": {
            "provider": "huggingfaceDedicated",
            "modelName": "endpoint-defined-model",
            "authentication": {
              "providerKey": "API_KEY_NAME"
            },
            "parameters": {
              "endpointName": "ENDPOINT_NAME",
              "regionName": "REGION_NAME",
              "cloudName": "CLOUD_NAME"
            }
          }
        },
        # If you want to store the original text
        # in addition to the generated embeddings
        # you must create a separate column.
        "original_text": "text"
      },
      # Define the primary key for the table.
      # You should change the primary key definition to meet the needs of your data.
      "primaryKey": "original_text"
    }
  }
}'

Replace the following:

  • TABLE_NAME: The name for your table.

  • SIMILARITY_METRIC: The method you want to use to calculate vector similarity scores. The available metrics are COSINE (default), DOT_PRODUCT, and EUCLIDEAN.

  • API_KEY_NAME: The name of the Hugging Face Dedicated user access token that you want to use. Must be the name of an existing Hugging Face Dedicated user access token in the Astra Portal.

  • MODEL_NAME: The model that you want to use to generate embeddings. The available models are: endpoint-defined-model.

    For Hugging Face Dedicated, you must deploy the model as a text embeddings inference (TEI) container.

    You must set MODEL_NAME to endpoint-defined-model because this integration uses the model specified in your dedicated endpoint configuration.

  • MODEL_DIMENSIONS: The number of dimensions that you want the generated vectors to have. Your chosen embedding model must support the specified number of dimensions.

    If you omit dimension, Astra DB can use a default dimension value. However, some models don’t have default dimensions.

    You can use the Data API to find supported embedding providers and their configuration parameters, including dimensions ranges and default dimensions.

  • ENDPOINT_NAME: The programmatically-generated name of your Hugging Face Dedicated endpoint. This is the first part of the endpoint URL. For example, if your endpoint URL is https://mtp1x7muf6qyn3yh.us-east-2.aws.endpoints.huggingface.cloud, the endpoint name is mtp1x7muf6qyn3yh.

  • REGION_NAME: The cloud provider region your Hugging Face Dedicated endpoint is deployed to. For example, us-east-2.

  • CLOUD_NAME: The cloud provider your Hugging Face Dedicated endpoint is deployed to. For example, aws.

Index the vector column so that you can perform a vector search on it.

curl -sS -L -X POST "$ASTRA_DB_API_ENDPOINT/api/json/v1/default_keyspace" \
--header "Token: $ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
  "createVectorIndex": {
    "name": "example_vector_index",
    "definition": {
      "column": "example_vector",
      "options": {
        "metric": "SIMILARITY_METRIC",
        "sourceModel": "endpoint-defined-model"
      }
    }
  }
}'

Add the integration to an existing table

To add the integration to an existing table, use the method to alter a table. Use the same column parameters as demonstrated in the previous example.

Automatically generate vector embeddings

After you add the Hugging Face Dedicated integration to a collection or table, vector embeddings are automatically generated when you insert data.

For collections, vector embeddings are automatically generated when you insert a document with a $vectorize field.

For tables, vector embeddings are automatically generated when you insert a row with a string value for the column that has the Hugging Face Dedicated integration added.

For more information, see Load your data.

After loading data, you can perform a vector search by providing a natural-language text string. Vectorize generates an embedding from your text string, and then runs the vector search.

Manage scoped databases

For each user access token, you select the databases that can use that user access token. These are referred to as scoped databases.

To change the scoped databases for an existing Hugging Face Dedicated user access token, do the following:

  1. In the Astra Portal header, click Settings.

  2. In the Settings navigation menu, click the name of the current organization, and then select the organization where you want to edit the Hugging Face Dedicated integration.

  3. In the Settings navigation menu, click Integrations.

  4. Select Hugging Face Dedicated Embedding provider.

  5. In the API keys section, expand each user access token to show the list of scoped databases.

  6. Add or remove databases from each user access token’s scope, as needed:

    • To remove a database from the user access token’s scope, click Delete, enter the Database name, and then click Remove scope.

    • To add a database to the user access token’s scope, click More, select Add database, select the Serverless (Vector) database that you want to add to the scope, and then click Add database.

Remove Hugging Face Dedicated user access tokens

Removing user access tokens immediately disables $vectorize embedding generation for any collections or tables that used the removed user access tokens. Make sure the user access token is not used by any active collections or tables before you remove it.

Removing user access tokens from Astra DB Serverless does not delete them from your Hugging Face Dedicated account.

To remove user access tokens, do the following:

  1. In the Astra Portal header, click Settings.

  2. In the Settings navigation menu, click the name of the current organization, and then select the organization where you want to edit the Hugging Face Dedicated integration.

  3. In the Settings navigation menu, click Integrations.

  4. Select Hugging Face Dedicated Embedding provider.

  5. In the API keys section, locate the user access token that you want to remove, click More, and then select Remove API key.

  6. In the confirmation dialog, enter the API key name, and then click Remove key.

  7. In your Hugging Face Dedicated account, delete the user access token if you don’t plan to reuse it.

  8. If you no longer want to use this embedding provider or you are not rotating the user access token, then you must remove the integration from any collections and tables that used the removed user access token to generate embeddings:

    • Collections: Recreate any collections that used the removed user access token.

    • Tables: Use the Data API alter command to remove the integration from any vector columns that used the removed user access token.

      For more information, see Change providers or credentials.

Rotate Hugging Face Dedicated user access tokens

To rotate user access tokens, you must remove the user access token, and then recreate it with the same name and scoped databases.

Removing the user access token immediately disables $vectorize embedding generation for any collections or tables that used that user access token. Vectorize remains unavailable until you add the new user access token to the Hugging Face Dedicated integration.

For more information, see Change providers or credentials.

To rotate user access tokens, do the following:

  1. In your Hugging Face Dedicated, create a new user access token.

  2. Remove the user access token that you want to rotate. Make a note of the user access token’s name and scoped databases. When you recreate the user access token, it must have the exact same name and scope.

  3. In the Astra Portal header, click Settings.

  4. In the Settings navigation menu, click the name of the current organization, and then select the organization where you want to edit the Hugging Face Dedicated integration.

  5. In the Settings navigation menu, click Integrations.

  6. Select Hugging Face Dedicated Embedding provider.

  7. In the API keys section, add a new user access token with the same name as the removed user access token.

    If the name doesn’t match, any collections or tables that used the removed user access token can’t detect the replacement user access token.

  8. Add all relevant databases to the new user access token’s scoped databases.

    At minimum, you must add all databases that used the removed user access token so that the collections and tables in those databases can detect the replacement user access token. To ensure that you don’t miss any databases, DataStax recommends adding all of the databases that were in the removed user access token’s scope.

Remove the Hugging Face Dedicated integration from your organization

To remove the Hugging Face Dedicated embedding provider integration from your Astra DB organization remove all existing Hugging Face Dedicated user access tokens. Then, remove the integration from any collections and tables that previously used it to generate embeddings.

  • Collections: Recreate any applicable collections.

    To preserve your data, you can export the original collection’s data, and then load it into the new collection. If you plan to use a different embedding provider, model, or dimensions, make sure you remove the $vector data before reuploading the data.

  • Tables: Use the Data API alter command to remove the integration from any relevant vector columns. Consider dropping the column if you no longer need those embeddings.

Troubleshoot vectorize

When working with vectorize, including the $vectorize reserved field in the Data API, errors can occur from two sources:

Astra DB

There is an issue within Astra DB, including the Astra DB platform, the Data API server, Data API clients, or something else.

Some of the most common Astra DB vectorize errors are related to scoped databases. In your vectorize integration settings, make sure your database is in the scope of the credential that you want to use. Scoped database errors don’t apply to the NVIDIA Astra-hosted embedding provider integration.

When using the Data API with collections, make sure you don’t use $vector and $vectorize in the same query. For more information, see the Data API collections references, such as Vector and vectorize, Insert many documents, and Sort clauses for documents.

When using the Data API with tables, you can only run a vector search on one vector column at a time. To generate an embedding from a string, the target vector column must have a defined embedding provider integration. For more information, see the Data API tables references, such as Vector type and Sort clauses for tables.

The embedding provider

The embedding provider encountered an issue while processing the embedding generation request. Astra DB passes these errors to you through the Astra Portal or Data API with a qualifying statement such as The embedding provider returned a HTTP client error.

Possible embedding provider errors include rate limiting, billing or account funding issues, and chunk or token size limits. For more information about these errors, see the embedding provider’s documentation, including the documentation for your chosen model.

Carefully read all error messages to determine the source and possible cause for the issue.

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2025 DataStax | Privacy policy | Terms of use | Manage Privacy Choices

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: +1 (650) 389-6000, info@datastax.com