Skip to content

Add Hugging Face Embedder Support #437

@evanmschultz

Description

@evanmschultz

Overview

The project currently supports OpenAI, Gemini, and Voyage embedders, all of which require API keys and incur usage costs. This issue proposes adding support for local Hugging Face embedding models, which are open source and can run locally without API costs.

Requirements

  • Add support for local Hugging Face embedding models
  • Make dependencies optional to avoid bloating the main package
  • Maintain compatibility with the existing EmbedderClient interface
  • Support async operations matching the current client pattern

Implementation Plan

1. Add Optional Dependencies

Update pyproject.toml to include Hugging Face dependencies as an optional requirement.

2. Create HuggingFaceEmbedder Class

Create a new file graphiti_core/embedder/huggingface.py implementing:

  • HuggingFaceEmbedderConfig - Configuration class for model selection and parameters
  • HuggingFaceEmbedder - Implementation of EmbedderClient for Hugging Face models

3. Update Embedder Module Exports

Update graphiti_core/embedder/__init__.py to export the new classes.

4. Implementation Details

HuggingFaceEmbedderConfig

  • model_name: String for model selection (default to a small, general-purpose model)
  • embedding_dim: Match the required dimension from the base config
  • device: Option to specify CPU or GPU (with fallback to CPU)
  • model_kwargs: Additional parameters to pass to the model

HuggingFaceEmbedder

  • Implement the required create method for generating embeddings
  • Add skeleton for create_batch with NotImplementedError for now
  • Handle both synchronous and asynchronous operations
  • Add lazy loading of models to avoid unnecessary imports when not used

5. Testing

  • Add unit tests for the new embedder in the test suite
  • Test with various model sizes and on different input types
  • Verify compatibility with the rest of the Graphiti system
  • Ensure graceful fallback when GPU is not available

6. Documentation

  • Update documentation to include information about the new embedder

Technical Considerations

  • The implementation will use the sentence-transformers package for model loading and embedding generation
  • Models will be loaded on demand and cached for reuse
  • May require thread/process management for async operations with CPU-only setup
  • Default to small models to minimize memory footprint

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions