Skip to content

Commit

Permalink
feat: Part 1 - Add RAG Embedding Interface (#628)
Browse files Browse the repository at this point in the history
**Reason for Change**:
This series of PR will integrate llamaindex RAG service for Kaito. 

This first PR introduces the Embedding Model Interface and two classes
that implement it `huggingface_local` and `huggingface_remote`. These
allows users to specify both local and remote HuggingFace models for
text embeddings.
  • Loading branch information
ishaansehgal99 authored Oct 14, 2024
1 parent 152e683 commit 1d99028
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 0 deletions.
Empty file added ragengine/__init__.py
Empty file.
Empty file added ragengine/embedding/__init__.py
Empty file.
13 changes: 13 additions & 0 deletions ragengine/embedding/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from abc import ABC, abstractmethod


class BaseEmbeddingModel(ABC):
@abstractmethod
def get_text_embedding(self, text: str):
"""Returns the text embedding for a given input string."""
pass

@abstractmethod
def get_embedding_dimension(self) -> int:
"""Returns the embedding dimension for the model."""
pass
19 changes: 19 additions & 0 deletions ragengine/embedding/huggingface_local.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from llama_index.embeddings.huggingface import HuggingFaceEmbedding

from .base import BaseEmbeddingModel


class LocalHuggingFaceEmbedding(BaseEmbeddingModel):
def __init__(self, model_name: str):
self.model = HuggingFaceEmbedding(model_name=model_name) # TODO: Ensure/test loads on GPU (when available)

def get_text_embedding(self, text: str):
"""Returns the text embedding for a given input string."""
return self.model.get_text_embedding(text)

def get_embedding_dimension(self) -> int:
"""Infers the embedding dimension by making a local call to get the embedding of a dummy text."""
dummy_input = "This is a dummy sentence."
embedding = self.get_text_embedding(dummy_input)

return len(embedding)
20 changes: 20 additions & 0 deletions ragengine/embedding/huggingface_remote.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from llama_index.embeddings.huggingface_api import \
HuggingFaceInferenceAPIEmbedding

from .base import BaseEmbeddingModel


class RemoteHuggingFaceEmbedding(BaseEmbeddingModel):
def __init__(self, model_name: str, api_key: str):
self.model = HuggingFaceInferenceAPIEmbedding(model_name=model_name, token=api_key)

def get_text_embedding(self, text: str):
"""Returns the text embedding for a given input string."""
return self.model.get_text_embedding(text)

def get_embedding_dimension(self) -> int:
"""Infers the embedding dimension by making a remote call to get the embedding of a dummy text."""
dummy_input = "This is a dummy sentence."
embedding = self.get_text_embedding(dummy_input)

return len(embedding)
7 changes: 7 additions & 0 deletions ragengine/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# RAG Library Requirements
llama-index
llama-index-embeddings-huggingface
fastapi
faiss-cpu
llama-index-vector-stores-faiss
uvicorn

0 comments on commit 1d99028

Please sign in to comment.