Strictly adhere to PEP-8 standards, ensuring code is clean, well-structured, and follows Python's style guide for readability.
Use appropriate modules like typing for type hints, ensuring code clarity, maintainability, and leveraging Python's standard libraries effectively.
Focus on time, space, robustness, scalability; write optimized, efficient code that can handle large datasets and scale seamlessly.
Implement comprehensive error handling, catching exceptions gracefully, providing meaningful error messages, and ensuring code robustness under unexpected conditions.
Ensure the developer agent writes advanced, maintainable Python code, balancing performance and readability while adhering to best practices.
TASK : main problem statement user query , we should relevent content , meta_data including relevent file name also
using user folder (reccusively in nature) we should convert into folder(with .txt files) write properly so that chucking will every easy next step
import os
import logging
from typing import List, Optional
from pathlib import Path
from concurrent.futures import ThreadPoolExecutor
import shutil
import pandas as pd
import json
import yaml
import docx
import pptx
import fitz
import nltk
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
folder(with .txt files) , user query , k=selectinon , int
import datasets
from typing import List, Dict, Optional
from langchain.docstore.document import Document as LangchainDocument
from langchain.text_splitter import RecursiveCharacterTextSplitter
from transformers import AutoTokenizer
from tqdm import tqdm
import logging
from concurrent.futures import ProcessPoolExecutor
from langchain_community.vectorstores import FAISS
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_community.vectorstores.utils import DistanceStrategy
from raglogger import setup_logger
logger = setup_logger()
Constants for embedding
EMBEDDING_MODEL_NAME = "thenlper/gte-large" # Replace with your preferred model
Define markdown separators
MARKDOWN_SEPARATORS = [
"\n#{1,6} ",
"```\n",
"\n\\\*+\n",
"\n---+\n",
"\n___+\n",
"\n\n",
"\n",
" ", # Include space as separator
"", # Empty string to finalize small chunks
]
saving and loading vector data base
Serialization
save
Saves an object to a disk file.
load
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Loads an object saved with torch.save() from a file.
Parallelism
get_num_threads
Returns the number of threads used for parallelizing CPU operations
set_num_threads
Sets the number of threads used for intraop parallelism on CPU.
get_num_interop_threads
Returns the number of threads used for inter-op parallelism on CPU (e.g.
set_num_interop_threads
Sets the number of threads used for interop parallelism (e.g.
Here is the Python code that accomplishes the tasks you've described, adhering to PEP-8 standards, utilizing appropriate modules for type hints, and focusing on performance, scalability, and robust error handling.
Step 1: Convert Files in a User Folder to .txt Files Recursively
This script recursively traverses a user-specified folder, converts various file types to plain text, and saves them as .txt files in a specified output directory. It supports txt, pdf, docx, pptx, json, yaml, and csv files.
python
import os
import logging
from typing import Optional
from pathlib import Path
from concurrent.futures import ThreadPoolExecutor
import shutil
import pandas as pd
import json
import yaml
import docx # python-docx
import pptx # python-pptx
import fitz # PyMuPDF
import nltk
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
# Setup NLTK resources quietly
nltk.download('punkt', quiet=True)
nltk.download('stopwords', quiet=True)
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def extract_text_from_file(file_path: Path) -> Optional[str]:
"""
Extracts text content from various file types.
"""
try:
if file_path.suffix == '.txt':
with file_path.open('r', encoding='utf-8') as file:
return file.read()
elif file_path.suffix == '.pdf':
text = ''
with fitz.open(file_path) as doc:
for page in doc:
text += page.get_text()
return text
elif file_path.suffix == '.docx':
doc = docx.Document(file_path)
return '\n'.join([para.text for para in doc.paragraphs])
elif file_path.suffix == '.pptx':
prs = pptx.Presentation(file_path)
text_runs = []
for slide in prs.slides:
for shape in slide.shapes:
if hasattr(shape, "text"):
text_runs.append(shape.text)
return '\n'.join(text_runs)
elif file_path.suffix == '.json':
with file_path.open('r', encoding='utf-8') as file:
data = json.load(file)
return json.dumps(data)
elif file_path.suffix in ['.yaml', '.yml']:
with file_path.open('r', encoding='utf-8') as file:
data = yaml.safe_load(file)
return yaml.dump(data)
elif file_path.suffix == '.csv':
df = pd.read_csv(file_path)
return df.to_csv(index=False)
else:
logger.warning(f"Unsupported file type: {file_path.suffix}")
return None
except Exception as e:
logger.error(f"Error processing {file_path}: {e}")
return None
def process_files_in_directory(
source_dir: Path,
output_dir: Path,
num_workers: int = 4
) -> None:
"""
Recursively processes files in the source directory and saves them as .txt files in the output directory.
"""
output_dir.mkdir(parents=True, exist_ok=True)
file_paths = [p for p in source_dir.rglob('*') if p.is_file()]
def process_file(file_path: Path) -> None:
relative_path = file_path.relative_to(source_dir)
output_file_path = output_dir / relative_path.with_suffix('.txt')
output_file_path.parent.mkdir(parents=True, exist_ok=True)
text = extract_text_from_file(file_path)
if text:
try:
with output_file_path.open('w', encoding='utf-8') as file:
file.write(text)
logger.info(f"Processed and saved: {output_file_path}")
except Exception as e:
logger.error(f"Failed to write {output_file_path}: {e}")
with ThreadPoolExecutor(max_workers=num_workers) as executor:
executor.map(process_file, file_paths)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Convert files to .txt recursively.")
parser.add_argument("source_dir", type=str, help="Source directory path.")
parser.add_argument("output_dir", type=str, help="Output directory path.")
parser.add_argument(
"--workers",
type=int,
default=4,
help="Number of worker threads for processing files."
)
args = parser.parse_args()
source_directory = Path(args.source_dir)
output_directory = Path(args.output_dir)
process_files_in_directory(source_directory, output_directory, args.workers)
Step 2: Process Text Files with User Query and Retrieve Top K Matches
This script reads the .txt files from the specified directory, splits the texts into manageable chunks, embeds them using a transformer model, and retrieves the top k relevant documents based on a user query.
Step 3: Save and Load the Vector Store for Reusability
This code demonstrates how to save the FAISS vector store to disk and load it later for future queries, allowing for efficient reuse without rebuilding the index.
python
import os
import logging
from pathlib import Path
from langchain.vectorstores import FAISS
from langchain.embeddings.huggingface import HuggingFaceEmbeddings
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def save_vector_store(vector_store: FAISS, folder_path: str) -> None:
"""
Saves the FAISS vector store to the specified folder.
"""
try:
vector_store.save_local(folder_path)
logger.info(f"Vector store saved to {folder_path}")
except Exception as e:
logger.error(f"Failed to save vector store: {e}")
def load_vector_store(folder_path: str) -> FAISS:
"""
Loads the FAISS vector store from the specified folder.
"""
try:
embeddings = HuggingFaceEmbeddings(model_name=EMBEDDING_MODEL_NAME)
vector_store = FAISS.load_local(folder_path, embeddings)
logger.info(f"Vector store loaded from {folder_path}")
return vector_store
except Exception as e:
logger.error(f"Failed to load vector store: {e}")
raise e
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Save and load FAISS vector store.")
parser.add_argument("action", choices=["save", "load"], help="Action to perform.")
parser.add_argument("folder_path", type=str, help="Path to save to or load from.")
args = parser.parse_args()
if args.action == "save":
# Assume vector_store is already built (e.g., from previous code)
save_vector_store(vector_store, args.folder_path)
elif args.action == "load":
vector_store = load_vector_store(args.folder_path)
Instructions to Use:
To save the vector store after building it:
bash
python script_name.py save /path/to/save_vector_store
Replace vector_store in the save action with your actual vector store instance.
Notes:
Ensure that all paths provided are valid and accessible.
Replace "thenlper/gte-base" with your preferred embedding model as needed.
The scripts assume a Unix-like environment for path conventions; adjust accordingly for Windows.
By following these steps, you'll have a robust system that converts various file types into text, processes them for efficient searching, and allows for saving and loading of the vector store for scalability and reusability.
import logging
from typing import List
from pathlib import Path
from concurrent.futures import ProcessPoolExecutor
from langchain.docstore.document import Document
from langchain.text_splitter import RecursiveCharacterTextSplitter
from transformers import AutoTokenizer
from tqdm import tqdm
from langchain.vectorstores import FAISS
from langchain.embeddings.huggingface import HuggingFaceEmbeddings
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Constants for embedding
EMBEDDING_MODEL_NAME = "thenlper/gte-base" # Replace with your preferred model
# Initialize tokenizer
tokenizer = AutoTokenizer.from_pretrained(EMBEDDING_MODEL_NAME)
# Define text splitter
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=512,
chunk_overlap=64,
length_function=lambda text: len(tokenizer.encode(text, truncation=True)),
)
def load_and_split_document(file_path: Path) -> List[Document]:
"""
Loads text from a file and splits it into chunks.
"""
try:
with file_path.open('r', encoding='utf-8') as file:
text = file.read()
metadata = {"source": str(file_path)}
texts = text_splitter.split_text(text)
return [Document(page_content=t, metadata=metadata) for t in texts]
except Exception as e:
logger.error(f"Failed to process {file_path}: {e}")
return []
def build_vector_store(documents: List[Document]) -> FAISS:
"""
Builds a FAISS vector store from the documents.
"""
embeddings = HuggingFaceEmbeddings(model_name=EMBEDDING_MODEL_NAME)
vector_store = FAISS.from_documents(documents, embeddings)
return vector_store
def search_documents(
vector_store: FAISS,
query: str,
k: int = 5
) -> List[Document]:
"""
Searches the vector store for the top k documents matching the query.
"""
results = vector_store.similarity_search(query, k=k)
return results
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Process text files and retrieve top K matches.")
parser.add_argument("text_files_dir", type=str, help="Directory containing .txt files.")
parser.add_argument("query", type=str, help="User query for searching documents.")
parser.add_argument(
"--k",
type=int,
default=5,
help="Number of top documents to retrieve."
)
args = parser.parse_args()
text_files_directory = Path(args.text_files_dir)
all_txt_files = list(text_files_directory.rglob('*.txt'))
all_documents = []
with ProcessPoolExecutor() as executor:
results = executor.map(load_and_split_document, all_txt_files)
for docs in tqdm(results, total=len(all_txt_files), desc="Processing documents"):
all_documents.extend(docs)
logger.info(f"Total chunks created: {len(all_documents)}")
vector_store = build_vector_store(all_documents)
search_results = search_documents(vector_store, args.query, k=args.k)
for idx, doc in enumerate(search_results, 1):
print(f"\nResult {idx}:")
print(f"Source: {doc.metadata['source']}")
print(f"Content:\n{doc.page_content}")