feat(retriever): implement doc processing and FastEmbed vector store indexing
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
# pyrefly: ignore [missing-import]
|
||||
from pydantic_settings import BaseSettings,SettingsConfigDict
|
||||
|
||||
class Settings(BaseSettings):
|
||||
# App Config
|
||||
app_name: str = "Technical Documentation Assistant"
|
||||
app_version: str = "1.0.0"
|
||||
app_environment: str = "development" # development,production
|
||||
|
||||
# LLM Config
|
||||
api_key: str
|
||||
model: str
|
||||
|
||||
# RAG Config
|
||||
chunk_size: int = 1000
|
||||
chunk_overlap: int = 200
|
||||
docs_dir: str = "./data/documents"
|
||||
chroma_persist_dir: str = "./data/vector_db"
|
||||
|
||||
model_config = SettingsConfigDict(
|
||||
env_file=".env",
|
||||
env_file_encoding="utf-8",
|
||||
extra="ignore"
|
||||
)
|
||||
|
||||
settings = Settings()
|
||||
@@ -0,0 +1,36 @@
|
||||
|
||||
from app.core.config import settings
|
||||
# pyrefly: ignore [missing-import]
|
||||
from langchain_community.document_loaders import DirectoryLoader, TextLoader
|
||||
# pyrefly: ignore [missing-import]
|
||||
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
||||
|
||||
class DocProcessor:
|
||||
def __init__(self):
|
||||
self.docs_dir = settings.docs_dir
|
||||
self.chunk_size = settings.chunk_size
|
||||
self.chunk_overlap = settings.chunk_overlap
|
||||
|
||||
def loader_docs(self):
|
||||
loader = DirectoryLoader(self.docs_dir, glob="**/*.md", loader_cls=TextLoader,loader_kwargs={"encoding":"utf-8"})
|
||||
return loader.load()
|
||||
|
||||
def process_docs(self):
|
||||
docs = self.loader_docs()
|
||||
text_splitter = RecursiveCharacterTextSplitter(
|
||||
chunk_size=self.chunk_size,
|
||||
chunk_overlap=self.chunk_overlap,
|
||||
length_function=len,
|
||||
)
|
||||
return text_splitter.split_documents(docs)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
doc_processor = DocProcessor()
|
||||
chunks = doc_processor.process_docs()
|
||||
print(f"Total Chunks Created: {len(chunks)}")
|
||||
if chunks:
|
||||
print("\n--- First Chunk Metadata ---")
|
||||
print(chunks[0].metadata)
|
||||
print("\n--- First Chunk Preview ---")
|
||||
print(chunks[0].page_content[:200])
|
||||
@@ -0,0 +1,47 @@
|
||||
from app.core.config import settings
|
||||
from app.services.doc_processor import DocProcessor
|
||||
# pyrefly: ignore [missing-import]
|
||||
from langchain_chroma import Chroma
|
||||
# pyrefly: ignore [missing-import]
|
||||
from langchain_community.embeddings import FastEmbedEmbeddings
|
||||
import os
|
||||
|
||||
class ContextRetriever:
|
||||
def __init__(self):
|
||||
self.doc_processor = DocProcessor()
|
||||
self.embedding = FastEmbedEmbeddings(
|
||||
model_name="BAAI/bge-small-en-v1.5"
|
||||
)
|
||||
self.vec_store = self.get_or_create_vector_store()
|
||||
|
||||
def get_or_create_vector_store(self):
|
||||
if os.path.exists(settings.chroma_persist_dir):
|
||||
return Chroma(
|
||||
persist_directory=settings.chroma_persist_dir,
|
||||
embedding_function=self.embedding
|
||||
)
|
||||
else:
|
||||
return self.index_documents()
|
||||
|
||||
|
||||
def index_documents(self):
|
||||
|
||||
vec_store = Chroma.from_documents(
|
||||
documents=self.doc_processor.process_docs(),
|
||||
embedding=self.embedding,
|
||||
persist_directory=settings.chroma_persist_dir
|
||||
)
|
||||
print(f"[vector store] Indexed documents at {settings.chroma_persist_dir}")
|
||||
return vec_store
|
||||
def retrieve(self,query:str,k=3):
|
||||
return self.vec_store.similarity_search(query, k=k)
|
||||
|
||||
retriever = ContextRetriever()
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
results = retriever.retrieve("How to handle exceptions?", k=2)
|
||||
print(f"Results found: {len(results)}")
|
||||
if results:
|
||||
print("First result snippet:", results[0].page_content[:150])
|
||||
Reference in New Issue
Block a user