rag-chatbot / ingestion /embedder.py
Abeshith's picture
RAG Chatbot with LangChain, FastAPI, and service layer architecture
64d7fdf
raw
history blame contribute delete
991 Bytes
from langchain_community.embeddings import FastEmbedEmbeddings
from app.config import config
from app.utils.logger import logger
class Embedder:
def __init__(self):
self.model = None
def get_embeddings(self):
if self.model is None:
model_name = config["models"]["embedding"]["model_name"]
max_length = config["models"]["embedding"]["max_length"]
self.model = FastEmbedEmbeddings(
model_name=model_name,
max_length=max_length
)
logger.info(f"FastEmbed model loaded: {model_name}")
return self.model
def embed_documents(self, texts: list[str]) -> list[list[float]]:
embeddings = self.get_embeddings()
return embeddings.embed_documents(texts)
def embed_query(self, text: str) -> list[float]:
embeddings = self.get_embeddings()
return embeddings.embed_query(text)
embedder = Embedder()