This repository contains a project that implements a Retrieval-Augmented Generation (RAG) system using any model supported by Ollama. The project focuses on creating embeddings for instructions of a professional bioinformatic software to help users conduct biology research.
Takollama AI Project is supported by DNALinux, Toyoko Bio, and a dedicated bioinformatics community.
ai/
├── data/
│ ├── raw/
│ │ ├── pdfs/
│ │ ├── htmls/
│ │ └── ...
│ ├── processed/
│ │ ├── texts/
│ │ ├── embeddings/
│ │ └── ...
│ └── example_data/
├── notebooks/
│ ├── 01_data_extraction.ipynb
│ ├── 02_data_preprocessing.ipynb
│ ├── 03_embedding_generation.ipynb
│ └── 04_rag_pipeline.ipynb
├── scripts/
│ ├── extract_text.py
│ ├── preprocess_text.py
│ ├── generate_embeddings.py
│ ├── create_rag_pipeline.py
│ └── ...
├── rag_llama3/
│ ├── init.py
│ ├── main.py
│ ├── VectorDB.py
| ├── config_template.yaml
│ ├── TextExtractor.py
│ └── RAG.py
├── tests/
│ ├── test_extract.py
│ ├── test_preprocess.py
│ ├── test_generate_embeddings.py
│ ├── test_rag_pipeline.py
│ └── ...
├── requirements.txt
├── setup.py
├── README.md
├── LICENSE
├── .gitignore
└── config.yaml
To install this program using uv you need:
If you don' t uv, install it:
curl -fsSL https://astral.sh/uv/install.sh | sh
Download takollama package:
wget https://github.com/DNALinux/ai/releases/download/demo/takollama.tar.bz2
Uncompress:
tar -xjf takollama.tar.bz2
Install:
cd ai
uv sync --locked
Edit the config_template.yaml file under src directory to set your project-specific parameters and paths.
There is a Instruction.ipynb you can use to test the code.
Configuration:
input_dir: Directory for PDF, HTML files, and URLs.urls_path: Path to a file named urls.txt where you put all the urls.output_dir: Directory where TextExtractor will store all .txt files (for debugging purposes).chroma_db_dir: Directory where your Chroma database will be stored.chroma_db_name: Collection name for your Chroma database.'mxbai-embed-large'. Feel free to choose your preferred Ollama embedding model.from rag_llama3 import RAG as rag
from rag_llama3 import TextExtractor as te
from rag_llama3 import VectorDB as vdb
# Load configuration, please insert the path to your configuration file.
config = rag.load_config('/home/tagore/repos/ai/rag_llama3/config_template.yaml')
# Extract configuration values
vector_db_config = config.get('vector_db')
input_dir = vector_db_config.get('input_dir')
output_dir = vector_db_config.get('output_dir')
urls_path = vector_db_config.get('urls_path')
chroma_db_dir = vector_db_config.get('chroma_db_dir')
chroma_db_name = vector_db_config.get('chroma_db_name')
model = vector_db_config.get('model')
Directory Setup:
test_vector_db = vdb(input_dir, output_dir, urls_path, chroma_db_dir, chroma_db_name)
Add Files:
urls.txt file, each on a new line.Load Data:
test_vector_db object to load files into the vector database:
test_vector_db.load_data()
test_vector_db.load_url()
test_vector_db.load_pdf()
test_vector_db.load_html()
test_vector_db.peek()
test_vector_db.show_sources()
test_vector_db.query_sources(source_name)
test_vector_db.delete_source(source_name)
test_vector_db.clear_database()
Generate Answers:
testRAG = rag(input, output, chroma_db, collection_name)
print(testRAG.generate_answer("Your question goes here"))
rag-llama3 "your question goes here"
The TextExtractor class is designed to handle text extraction from various sources such as PDF files, HTML files, and URLs. This class facilitates the transformation of raw text data into a format suitable for creating vector embeddings and RAG (Retrieval-Augmented Generation) systems. It also includes functionality for saving extracted text to files for debugging purposes.
get_pdf: Returns a list of PDF file paths from the input directory.
get_html: Returns a list of HTML file paths from the input directory.
get_urls: Retrieves URLs from a text file within the input directory.
save_text_to_file: Saves a given text to a specified file for debugging purposes.
save_a_pdf_text: Extracts text from a PDF file and saves each page's text to separate files. It ensures limited overlap between pages and avoids saving pages with only one line of text.
save_pdfs_texts: Extracts text from multiple PDF files and saves each page's text to separate files.
extract_pdf_texts: Extracts text from a PDF file and returns it as a list, ensuring limited overlap between pages and avoiding pages with only one line of text.
save_a_html_text: Extracts text from an HTML file and saves it to multiple files with specified character limits and overlap.
save_html_texts: Extracts text from multiple HTML files and saves them to multiple files.
extract_html_text: Extracts text from an HTML file and returns it as a list of text chunks with specified character limits and overlap.
is_relevant_link: Determines if a link is relevant based on specific heuristics.
extract_url_text: Extracts headings and paragraphs from a URL and returns the text along with relevant links.
save_chunks_to_files: Saves text chunks to files in the specified directory.
split_text_into_chunks: Splits text into chunks with specified overlap.
get_main_domain: Extracts the main domain from a URL.
crawl_and_save: Crawls a website recursively, extracts text, and saves it to files.
crawl_and_extract: Crawls a website recursively, extracts text, and returns it as a list of text chunks.
from rag_llama3 import TextExtractor as te
# Initialize TextExtractor with input and output directories
text_extractor = te(input_dir='path/to/input', output_dir='path/to/output', urls_file = 'path/to/urls.txt')
# Get PDF File Paths
pdf_files = text_extractor.get_pdf()
print(pdf_files) # List of PDF file paths
#Extract and Save PDF Texts to Files
text_extractor.save_pdfs_texts(pdf_files)
#Extract PDF Texts to a List
pdf_texts = text_extractor.extract_pdf_texts('path/to/sample.pdf')
print(pdf_texts) # List of extracted text chunks from the PDF
#The same process applies for HTML and URL
The VectorDB class manages a Chroma vector database, allowing for the extraction, processing, and storage of text data from various sources, including PDFs, HTML files, and URLs. This class integrates with the TextExtractor class for text extraction and uses the Ollama model for generating vector embeddings.
VectorDB instance, sets up directories, and loads or creates the Chroma vector database.RAG Class IntroductionThe RAG (Retrieval-Augmented Generation) class provides a comprehensive solution for integrating a vector database with the LLaMA3 model to generate and stream answers based on user queries. It includes functionalities for setting up the vector database, generating prompts, and creating answers with both regular and streaming outputs.
Initialization:
__init__: Initializes the RAG class, setting up the vector database with specified directories and a default embedding model._setup_vector_db: Checks if the vector database exists and populates it with data if needed._is_database_populated: Verifies if the vector database contains data.Prompt Generation:
generate_prompt: Creates a prompt for querying the model, incorporating the user's question and the retrieved context.Answer Generation:
generate_answer: Generates an answer using the vector database and the LLaMA3 model.stream_answer: Generates an answer with streaming output, providing real-time response chunks.preprocess_textThe preprocess_text function performs a series of text preprocessing steps to clean and standardize raw text. It includes:
This function outputs a preprocessed text string that is ready for further analysis or processing.
docker run -it --network host takollama /bin/bash
This project is licensed under the GNU GENERAL PUBLIC LICENSE. See the LICENSE file for details.
This repository contains a project that implements a Retrieval-Augmented Generation (RAG) system using any model supported by Ollama. The project focuses on creating embeddings for instructions of a professional bioinformatic software to help users conduct biology research.
Takollama AI Project is supported by DNALinux, Toyoko Bio, and a dedicated bioinformatics community.
ai/
├── data/
│ ├── raw/
│ │ ├── pdfs/
│ │ ├── htmls/
│ │ └── ...
│ ├── processed/
│ │ ├── texts/
│ │ ├── embeddings/
│ │ └── ...
│ └── example_data/
├── notebooks/
│ ├── 01_data_extraction.ipynb
│ ├── 02_data_preprocessing.ipynb
│ ├── 03_embedding_generation.ipynb
│ └── 04_rag_pipeline.ipynb
├── scripts/
│ ├── extract_text.py
│ ├── preprocess_text.py
│ ├── generate_embeddings.py
│ ├── create_rag_pipeline.py
│ └── ...
├── rag_llama3/
│ ├── init.py
│ ├── main.py
│ ├── VectorDB.py
| ├── config_template.yaml
│ ├── TextExtractor.py
│ └── RAG.py
├── tests/
│ ├── test_extract.py
│ ├── test_preprocess.py
│ ├── test_generate_embeddings.py
│ ├── test_rag_pipeline.py
│ └── ...
├── requirements.txt
├── setup.py
├── README.md
├── LICENSE
├── .gitignore
└── config.yaml
To install this program using uv you need:
If you don' t uv, install it:
curl -fsSL https://astral.sh/uv/install.sh | sh
Download takollama package:
wget https://github.com/DNALinux/ai/releases/download/demo/takollama.tar.bz2
Uncompress:
tar -xjf takollama.tar.bz2
Install:
cd ai
uv sync --locked
Edit the config_template.yaml file under src directory to set your project-specific parameters and paths.
There is a Instruction.ipynb you can use to test the code.
Configuration:
input_dir: Directory for PDF, HTML files, and URLs.urls_path: Path to a file named urls.txt where you put all the urls.output_dir: Directory where TextExtractor will store all .txt files (for debugging purposes).chroma_db_dir: Directory where your Chroma database will be stored.chroma_db_name: Collection name for your Chroma database.'mxbai-embed-large'. Feel free to choose your preferred Ollama embedding model.from rag_llama3 import RAG as rag
from rag_llama3 import TextExtractor as te
from rag_llama3 import VectorDB as vdb
# Load configuration, please insert the path to your configuration file.
config = rag.load_config('/home/tagore/repos/ai/rag_llama3/config_template.yaml')
# Extract configuration values
vector_db_config = config.get('vector_db')
input_dir = vector_db_config.get('input_dir')
output_dir = vector_db_config.get('output_dir')
urls_path = vector_db_config.get('urls_path')
chroma_db_dir = vector_db_config.get('chroma_db_dir')
chroma_db_name = vector_db_config.get('chroma_db_name')
model = vector_db_config.get('model')
Directory Setup:
test_vector_db = vdb(input_dir, output_dir, urls_path, chroma_db_dir, chroma_db_name)
Add Files:
urls.txt file, each on a new line.Load Data:
test_vector_db object to load files into the vector database:
test_vector_db.load_data()
test_vector_db.load_url()
test_vector_db.load_pdf()
test_vector_db.load_html()
test_vector_db.peek()
test_vector_db.show_sources()
test_vector_db.query_sources(source_name)
test_vector_db.delete_source(source_name)
test_vector_db.clear_database()
Generate Answers:
testRAG = rag(input, output, chroma_db, collection_name)
print(testRAG.generate_answer("Your question goes here"))
rag-llama3 "your question goes here"
The TextExtractor class is designed to handle text extraction from various sources such as PDF files, HTML files, and URLs. This class facilitates the transformation of raw text data into a format suitable for creating vector embeddings and RAG (Retrieval-Augmented Generation) systems. It also includes functionality for saving extracted text to files for debugging purposes.
get_pdf: Returns a list of PDF file paths from the input directory.
get_html: Returns a list of HTML file paths from the input directory.
get_urls: Retrieves URLs from a text file within the input directory.
save_text_to_file: Saves a given text to a specified file for debugging purposes.
save_a_pdf_text: Extracts text from a PDF file and saves each page's text to separate files. It ensures limited overlap between pages and avoids saving pages with only one line of text.
save_pdfs_texts: Extracts text from multiple PDF files and saves each page's text to separate files.
extract_pdf_texts: Extracts text from a PDF file and returns it as a list, ensuring limited overlap between pages and avoiding pages with only one line of text.
save_a_html_text: Extracts text from an HTML file and saves it to multiple files with specified character limits and overlap.
save_html_texts: Extracts text from multiple HTML files and saves them to multiple files.
extract_html_text: Extracts text from an HTML file and returns it as a list of text chunks with specified character limits and overlap.
is_relevant_link: Determines if a link is relevant based on specific heuristics.
extract_url_text: Extracts headings and paragraphs from a URL and returns the text along with relevant links.
save_chunks_to_files: Saves text chunks to files in the specified directory.
split_text_into_chunks: Splits text into chunks with specified overlap.
get_main_domain: Extracts the main domain from a URL.
crawl_and_save: Crawls a website recursively, extracts text, and saves it to files.
crawl_and_extract: Crawls a website recursively, extracts text, and returns it as a list of text chunks.
from rag_llama3 import TextExtractor as te
# Initialize TextExtractor with input and output directories
text_extractor = te(input_dir='path/to/input', output_dir='path/to/output', urls_file = 'path/to/urls.txt')
# Get PDF File Paths
pdf_files = text_extractor.get_pdf()
print(pdf_files) # List of PDF file paths
#Extract and Save PDF Texts to Files
text_extractor.save_pdfs_texts(pdf_files)
#Extract PDF Texts to a List
pdf_texts = text_extractor.extract_pdf_texts('path/to/sample.pdf')
print(pdf_texts) # List of extracted text chunks from the PDF
#The same process applies for HTML and URL
The VectorDB class manages a Chroma vector database, allowing for the extraction, processing, and storage of text data from various sources, including PDFs, HTML files, and URLs. This class integrates with the TextExtractor class for text extraction and uses the Ollama model for generating vector embeddings.
VectorDB instance, sets up directories, and loads or creates the Chroma vector database.RAG Class IntroductionThe RAG (Retrieval-Augmented Generation) class provides a comprehensive solution for integrating a vector database with the LLaMA3 model to generate and stream answers based on user queries. It includes functionalities for setting up the vector database, generating prompts, and creating answers with both regular and streaming outputs.
Initialization:
__init__: Initializes the RAG class, setting up the vector database with specified directories and a default embedding model._setup_vector_db: Checks if the vector database exists and populates it with data if needed._is_database_populated: Verifies if the vector database contains data.Prompt Generation:
generate_prompt: Creates a prompt for querying the model, incorporating the user's question and the retrieved context.Answer Generation:
generate_answer: Generates an answer using the vector database and the LLaMA3 model.stream_answer: Generates an answer with streaming output, providing real-time response chunks.preprocess_textThe preprocess_text function performs a series of text preprocessing steps to clean and standardize raw text. It includes:
This function outputs a preprocessed text string that is ready for further analysis or processing.
docker run -it --network host takollama /bin/bash
This project is licensed under the GNU GENERAL PUBLIC LICENSE. See the LICENSE file for details.