-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from crux-bphc/campus-rag
feat: add RAG for general campus queries
- Loading branch information
Showing
10 changed files
with
5,810 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
.env | ||
__pycache__/ | ||
.langgraph_api | ||
chroma | ||
.vscode/ |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import os | ||
import shutil | ||
|
||
from langchain.schema import Document | ||
from langchain.text_splitter import RecursiveCharacterTextSplitter | ||
from langchain_community.document_loaders import ( | ||
DirectoryLoader, | ||
SitemapLoader, | ||
WebBaseLoader, | ||
) | ||
from langchain_community.vectorstores import Chroma | ||
from langchain_huggingface import HuggingFaceEmbeddings | ||
|
||
model_id = "sentence-transformers/all-MiniLM-L6-v2" | ||
model_kwargs = {"device": "cpu"} | ||
embeddings = HuggingFaceEmbeddings(model_name=model_id, model_kwargs=model_kwargs) | ||
|
||
CHROMA_PATH = "chroma" | ||
DATA_PATH = "data" | ||
|
||
|
||
def load_documents(extension="txt") -> list[Document]: | ||
print(f"Loading {extension} documents...") | ||
loader = DirectoryLoader(DATA_PATH, glob=f"**/*.{extension}") | ||
documents = loader.load() | ||
return documents | ||
|
||
|
||
def load_web_documents() -> list[Document]: | ||
# Sitemap Loader is taking forever and failing because of TooManyRedirects | ||
loader = SitemapLoader( | ||
web_path="https://www.bits-pilani.ac.in/campus-sitemap.xml", | ||
filter_urls=["https://www.bits-pilani.ac.in/hyderabad"], | ||
continue_on_failure=True, | ||
) # filter for only hyderabad related pages | ||
|
||
# loader = WebBaseLoader("https://www.bits-pilani.ac.in/hyderabad/") | ||
documents = loader.load() | ||
return documents | ||
|
||
|
||
def split_text(documents: list[Document]): | ||
text_splitter = RecursiveCharacterTextSplitter( | ||
chunk_size=1000, | ||
chunk_overlap=300, | ||
length_function=len, | ||
add_start_index=True, | ||
) | ||
chunks = text_splitter.split_documents(documents) | ||
print(f"Split {len(documents)} documents into {len(chunks)} chunks.") | ||
|
||
return chunks | ||
|
||
|
||
def save_to_chroma(chunks: list[Document]): | ||
if os.path.exists(CHROMA_PATH): | ||
shutil.rmtree(CHROMA_PATH) | ||
|
||
Chroma.from_documents(chunks, embeddings, persist_directory=CHROMA_PATH) | ||
print(f"Saved {len(chunks)} chunks to {CHROMA_PATH}.") | ||
|
||
|
||
def generate_data_store(): | ||
print(f"Loading documents in {DATA_PATH}...") | ||
|
||
documents = load_documents("md") | ||
chunks = split_text(documents) | ||
|
||
documents = load_documents("pdf") | ||
chunks.extend(split_text(documents)) | ||
|
||
documents = load_documents("txt") | ||
chunks.extend(split_text(documents)) | ||
|
||
# print("Loading web documents...") | ||
# web_documents = load_web_documents() | ||
# web_chunks = split_text(web_documents) | ||
|
||
# chunks.extend(web_chunks) | ||
print("Saving to Chroma...") | ||
save_to_chroma(chunks) | ||
|
||
|
||
def main(): | ||
generate_data_store() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Binary file not shown.
Oops, something went wrong.