Repository navigation
Python Scripts
5 files in python-scripts folder are related to Russian documents' pre-processing. The procedure includes:
- Converting pdf to docx: cleanpdf.py, process_directory function
- Creating large files to be uploaded on DeepL website: cleanpdf.py, merge_docx_files
- Recreating the original docx files: docxtopdf.py
- Converting docx files to pdfs: docxtopdf.sh, pandoc_latex_template.yaml
6 files in the python-scripts folder are related to the RAG pipeline.
- Loading parameters: parameters.py, config.json
- Loading the embedding model: get_embedding_function.py
- Loading the LLM model: get_llm_function.py
- Creating the RAG chain: get_rag_chain.py
- Class to create the final Conversational RAG chain: ConversationalRagChain.py
This script is responsible for loading and managing configuration parameters, including API keys, paths, and model settings. It reads configuration from a config.json file and sets up global variables like DATA_PATH, CHROMA_ROOT_PATH, EMBEDDING_MODEL, and LLM_MODEL. It also includes functions to load API keys from a .env file and to print the current configuration.
-
load_api_keys(): Loads API keys from a.envfile and sets them as environment variables. -
load_config(config_name, show_config): Loads configuration fromconfig.jsonbased on the given configuration name. -
print_config(): Prints the current configuration settings.
This file contains configuration settings for different environments or use cases. Each configuration specifies paths to data, the embedding model, and the language model to be used.
-
data_path: Path to the data folder. -
chroma_root_path: Path to the folder where the Chroma database will be stored. -
embedding_model: Name of the model used for embeddings. -
llm_model: Name of the language model used.
This script provides a function to load different embedding models based on the specified model name. It supports various models like openai, and voyage models.
-
get_embedding_function(model_name): Returns the embedding function corresponding to the given model name.
This script is responsible for loading language models (LLMs) based on the specified model name. It supports models like Mistral, Llama, and GPT-3.5-turbo.
-
get_llm_function(model_name): Returns the language model function corresponding to the given model name.
This script creates a Retrieval-Augmented Generation (RAG) chain for question-answering tasks. The RAG chain combines document retrieval with language model generation to provide relevant answers based on retrieved context.
-
get_rag_chain(params): Creates and returns a RAG chain configured with the specified parameters. It supports different search types like similarity, similarity with score threshold, and MMR (Maximal Marginal Relevance).
This script defines a class ConversationalRagChain that encapsulates a RAG application to enable natural conversations. It manages chat history, formats outputs, and integrates with the RAG chain created by get_rag_chain.py.
-
ConversationalRagChain: A class that extends theChainclass to handle conversational interactions using RAG. It maintains chat history and formats responses appropriately.
4 files in python-scripts folder are related to the Database.
- Loading parameters : parameters.py, config.json
- Loading the embedding model : get_embedding_function.py
- Function to create, reset, clear the database : populate_database.py
The main() function in the script serves as the entry point for managing the application's database. It provides command-line interface options to clear, reset, and populate the database using a specified configuration.
-
--config: (Optional)
Type:str
Description: Specifies the name of the configuration used to populate the database. Configurations need to be filled inconfig.json. -
--reset: (Optional)
Action:store_true
Description: If this flag is set, the function will reset the database before populating it with the specified configuration. Resetting involves clearing the current data in the database (using theclear_databasefunction) and then loading the new data. -
--clear: (Optional)
Action:store_true
Description: If this flag is set, the function will clear the database entirely. If the--configargument is also provided, the database will be cleared based on the specific subfolder related to the configuration.
-
Populating the Database:
- When only the
--configoption is provided without the--resetor--clearflags, the function populates the database directly based on the configuration specified. - The
load_documentsfunction is used to retrieve the data, which is then processed bysplit_documentsinto smaller chunks. - Finally, these chunks are added to the database using the
add_to_chromafunction.
- When only the
-
Resetting the Database (
--resetflag) with a Configuration:- If the
--resetflag is combined with the--configoption, the function will first clear the database subfolder specified by the configuration. - After clearing, the function proceeds to load the documents with
load_documents, split them into chunks withsplit_documents, and then add them to the database using theadd_to_chromafunction.
- If the
-
Clearing the Database (
--clearflag):- When the
--clearflag is used, the function clears the database. - If a configuration file is specified using the
--configoption, the database is cleared for the subfolder that matches theEMBEDDING_MODELdefined in the config. - The
clear_databasefunction is invoked to handle the clearing process.
- When the
-
Populate the Database:
python populate_database.py --config CONFIG_NAME
-
Reset the Database:
python populate_database.py --config CONFIG_NAME --reset
-
Clear the Database:
python populate_database.py --config CONFIG_NAME --clear