Skip to main content

Use Hugging Face Models

Use models hosted on Hugging Face in your pipelines.


About This Task

You can use any model from Hugging Face in your pipelines. There are a couple of ways to do it:

Prerequisites

You need a Hugging Face token for the API you want to use.

Use Models from Hugging Face

Connect deepset AI Platform to Hugging Face through the Integrations page. You can set up the connection for a single workspace or for the whole organization:

Add Workspace-Level Integration

  1. Click your profile icon and choose Settings.
  2. Go to Workspace>Integrations.
  3. Find the provider you want to connect and click Connect next to them.
  4. Enter the API key and any other required details.
  5. Click Connect. You can use this integration in pipelines and indexes in the current workspace.

Add Organization-Level Integration

  1. Click your profile icon and choose Settings.
  2. Go to Organization>Integrations.
  3. Find the provider you want to connect and click Connect next to them.
  4. Enter the API key and any other required details.
  5. Click Connect. You can use this integration in pipelines and indexes in all workspaces in the current organization.

Then, add a component that uses the model you have in mind. Here are the available components by the model type:

  • Embedding models:

    • HuggingFaceAPIDocumentEmbedder: Computes document embeddings using Hugging Face's APIs.
    • HuggingFaceAPITextEmbedder: Computes text embeddings using Hugging Face's APIs.
    • OptimumDocumentEmbedder: Calculates document embeddings using models from Hugging Face's Optimum library. You can use it through Hugging Face's APIs.
    • OptimumTextEmbedder: Calculates text embeddings using models from Hugging Face's Optimum library. You can use it through Hugging Face's APIs.
    • SentenceTransformersDocumentEmbedder: Embeds documents using Sentence Transformers models. You can use it through Hugging Face's APIs.
    • SentenceTransformersTextEmbedder: Embeds text using Sentence Transformers models. You can use it through Hugging Face's APIs.

      Embedding Models in Query Pipelines and Indexes

      The embedding model you use to embed documents in your indexing pipeline must be the same as the embedding model you use to embed the query in your query pipeline.

      This means the embedders for your indexing and query pipelines must match. For example, if you use CohereDocumentEmbedder to embed your documents, you should use CohereTextEmbedder with the same model to embed your queries.

  • Named entity extraction:

    • NamedEntityExtractor: Can work with Hugging Face models if you set its backend parameter to hugging_face.
  • LLMs:

    • HuggingFaceAPIChatGenerator: Completes chats using Hugging Face APIs.
    • HuggingFaceAPIGenerator: Uses text-generating models through Hugging Face APIs.
    • HuggingFaceLocalChatGenerator: Uses a Hugging Face chat-completion model that runs locally.
    • HuggingFaceLocalGenerator: Generates text using a Hugging Face model that runs locally.
  • Ranking models:

    • TransformersSimilarityRanker: Uses a cross-encoder model to rank documents. You can use models through Hugging Face APIs.
    • SentenceTransformersDiversityRanker: Uses a sentence transformers model to rank documents. You can use models through Hugging Face APIs.
  • Readers:

    • ExtractiveReader: Extracts answers from the text.
  • Routers:

    • TransformersTextRouter: Routes text to various outputs based on a categorization label provided by the model it uses.
    • TransformersZeroShotTextRouter: Uses a text classification model to route text to various outputs based on user-defined labels.

Usage Examples

Check the documentation of the component you want to use for usage examples.