RAPTOR allows you to use models like Llama, Mistral, or Gemma by extending the base classes for summarization, QA, and embeddings.
To use custom models, follow these steps:
- Subclass
BaseSummarizationModel and implement summarize(self, context, max_tokens=150). - Subclass
BaseQAModel and implement answer_question(self, context, question). - Subclass
BaseEmbeddingModel and implement create_embedding(self, text). - Wrap these models in a
RetrievalAugmentationConfig object. - Pass the config to
RetrievalAugmentation(config=...).
from raptor import BaseSummarizationModel, BaseQAModel, BaseEmbeddingModel, RetrievalAugmentationConfig
# Example: Custom Summarization Model
class GEMMASummarizationModel(BaseSummarizationModel):
def __init__(self, model_name="google/gemma-2b-it"):
# ... initialization logic ...
pass
def summarize(self, context, max_tokens=150):
# ... implementation ...
return summary
# Example: Custom QA Model
class GEMMAQAModel(BaseQAModel):
def __init__(self, model_name="google/gemma-2b-it"):
# ... initialization logic ...
pass
def answer_question(self, context, question):
# ... implementation ...
return answer
# Example: Custom Embedding Model
from sentence_transformers import SentenceTransformer
class SBertEmbeddingModel(BaseEmbeddingModel):
def __init__(self, model_name="sentence-transformers/multi-qa-mpnet-base-cos-v1"):
self.model = SentenceTransformer(model_name)
def create_embedding(self, text):
return self.model.encode(text)
# Integration
RAC = RetrievalAugmentationConfig(
summarization_model=GEMMASummarizationModel(),
qa_model=GEMMAQAModel(),
embedding_model=SBertEmbeddingModel()
)
RA = RetrievalAugmentation(config=RAC)