Code Monkey home page Code Monkey logo

renumics-rag's Introduction

🤖 Renumics RAG: Explore and Visualize RAG Data

Retrieval-augmented generation assistant demo using LangChain and Streamlit.

🛠️ Installation

Setup a virtual environment in the project directory:

python3.8 -m venv .venv
source .venv/bin/activate  # Linux/MacOS
# .\.venv\Scripts\activate.bat  # Windows CMD
# .\.venv\Scripts\activate.ps1  # PowerShell
pip install -IU pip setuptools wheel

Install the RAG demo package and some extra dependencies:

# For GPU support
pip install renumics-rag[all]@git+https://github.com/Renumics/renumics-rag.git torch torchvision sentence-transformers accelerate
# For CPU support
# pip install renumics-rag[all]@git+https://github.com/Renumics/renumics-rag.git torch torchvision sentence-transformers accelerate --extra-index-url https://download.pytorch.org/whl/cpu

⚒️ Local Setup

If you intend to edit, not simply use, this project, clone the entire repository:

git clone [email protected]:Renumics/renumics-rag.git

Then install it in editable mode.

Via pip

Setup virtual environment in the project folder:

python3.8 -m venv .venv
source .venv/bin/activate  # Linux/MacOS
# .\.venv\Scripts\activate.bat  # Windows CMD
# .\.venv\Scripts\activate.ps1  # PowerShell
pip install -IU pip setuptools wheel

Install the RAG demo package and some extra dependencies:

pip install -e .[all]
# For GPU support
pip install pandas torch torchvision sentence-transformers accelerate
# For CPU support
# pip install pandas torch torchvision sentence-transformers accelerate --extra-index-url https://download.pytorch.org/whl/cpu

Via poetry

Install the RAG demo and some extra dependencies:

poetry install --all-extras
# Torch with GPU support
pip install pandas torch torchvision sentence-transformers accelerate
# Torch with CPU support
# pip install pandas torch torchvision sentence-transformers accelerate --extra-index-url https://download.pytorch.org/whl/cpu

Activate the environment (otherwise, prexis all subsequent commands with poetry run):

poetry shell

Note: If you have Direnv installed, you can avoid prefixing python commands with poetry run by executing direnv allow in the project directory. It will activate environment each time you enter the project directory.

⚙️ Configuration

If you plan to use OpenAI models, create a .env file with the following content:

OPENAI_API_KEY="Your OpenAI API key"

If you plan to use OpenAI models via Azure, create .env with the following content:

OPENAI_API_TYPE="azure"
OPENAI_API_VERSION="2023-08-01-preview"
AZURE_OPENAI_API_KEY="Your Azure OpenAI API key"
AZURE_OPENAI_ENDPOINT="Your Azure OpenAI endpoint"

If you are using Hugging Face models, a .env file is not necessary. You can configure the embeddings model, retriever and the LLM in the config file (settings.yaml). The default settings (taken from settings file are:

llm_type: 'openai'       # 'openai', 'hf' or 'azure'
llm_name: 'gpt-3.5-turbo'

relevance_score_fn: 'l2'
k: 20
search_type: 'similarity'
score_threshold: 0.5
fetch_k: 20
lambda_mult: 0.5

embeddings_model_type: 'openai'     # 'openai', 'hf' or 'azure'
embeddings_model_name: 'text-embedding-ada-002'

You can adapt it without cloning the repository by setting up an environment variable RAG_SETTINGS pointing to your local config file. You can also configure it from the GUI during the question and answering sessions. But it's important to choose the desired embeddings model because the indexing is done beforehand.

🚀 Usage: Indexing

You can skip this section download the demo database with embeddings of a Formula One Dataset. This dataset is based on articles from Wikipedia and is licensed under the Creative Commons Attribution-ShareAlike License. The original articles and a list of authors can be found on the respective Wikipedia pages. To use your own data create a new data/docs directory within the project and place your documents in there (recursive directories are supported).

Note: at the moment, only HTML files can be indexed but it can be adjusted in the create-db script, this requires the ⚒️ Local Setup

Begin the process by indexing your documents. Execute the following command:

create-db

This will create a db-docs directory within the project consisting of indexed documents. To index additional documents, use the --exist-ok and --on-match flags (refer to create-db --help for more information).

🚀 Usage: Questioning

Now, you can leverage the indexed documents to answer questions.

To only retrieve relevant documents via command line interface:

retrieve "Your question here"
# QUESTION: ...
# SOURCES: ...

To answer a question based on the indexed documents:

answer "Your question here"
# QUESTION: ...
# ANSWER: ...
# SOURCES: ...

To start a web application (See app --help for available application options):

app

This will open a new browser window: RAG-demo-gui

You can enter your questions to get them answered by the RAG System. Each answer contains an expandable 'Sources' section that includes the text and filename for each snippet used to generate the answer.  The Settings section on the left allows you to choose different LLMs from OpenAI or Hugging Face. In the advanced settings, you can adjust the retrieval settings like the choice of relevance score function, the number of retrieved snippets, and search type. You can also change the embeddings model.  Note: Changing the embeddings model requires creating a new database with the new embeddings.

🔎 Interactive exploration

After submitting some questions, you can explore them using Renumics Spotlight by clicking the red explore button on the left: RAG-Demo-Explore

It will open a new browser tab: RAG-Demo-Spotlight

You can see a table of all questions and all document snippets on the top left side. Using the 'visible columns' button, you can control which columns are shown in the table.

On the right side is the similarity map; it shows the questions and documents based on the embeddings. You can customize it by clicking the settings button and apply coloring by 'used_by_num_question' to color the snippets by the number of questions where the retriever loaded this snippet.

You can select snippets in the similarity map or in the table to display them in the details view at the bottom. To customize the detail view, you can remove rows by clicking the 'x' and add views with the 'Add View' button. A good setup here is to display the document, used_by_num_question and type.

Rag_demo_Spotlight-adjusted

Exploring the exemplary F1 Dataset and over 500 questions from our RAG evaluation article can reveal interesting clusters. Document clusters of race statistics, racing techniques and strategies, and team details are frequently referenced by at least one question. Others, like Television Rights or specific race track details, are retrieved less often.

🔎 Tutorials and References

To learn more about visulization for RAG, check out our articles:

renumics-rag's People

Contributors

druzsan avatar markus-stoll avatar renumics avatar

Stargazers

Deva Praveen K avatar  avatar  avatar Victor Gras avatar Thiago P B Bessimo 贝玉卿 avatar Vinod Ponmanadiyil avatar  avatar Konecheck avatar Simon Barratt avatar  avatar FJDEV avatar Mumtaz Moon avatar  avatar Burden Haze  avatar  avatar  avatar Nick avatar Monish Soundar Raj avatar  avatar  avatar Mark Douthwaite avatar  avatar Eike Steffen Kohlmeyer avatar  avatar Aiman Hakimi avatar Dmytro Bobrenko avatar SUJAL LUHAR avatar danielelando avatar Abolfazl Nasr avatar Grzegorz Muszynski avatar Piotr Cieluchowski avatar  avatar  avatar George Birbilis avatar  avatar  avatar  avatar Alexander Tuzhikov avatar Blaž Stojanovič avatar Anton Tsitou avatar Plamen avatar  avatar Yurii Didkovskyi avatar Nahean Fardous avatar Brancen Gregory avatar  avatar Hyunjin Bae avatar  avatar Bobby Jo avatar WW Henderson avatar  avatar Abdullahi Olaoye avatar Jaishree Singh avatar  avatar  avatar Gian Paolo Santopaolo avatar  avatar  avatar Ian Ormesher avatar John Bellamy avatar Ayman Farhat avatar Huaqing Ding avatar Martino Bettucci avatar lightfield botanist avatar Vengatesh Dayalan avatar Dandy Kleybrink avatar Helge Stahlmann avatar  avatar KindLab avatar Damla avatar  avatar siddicky avatar  avatar Daniel Visca avatar Mattia Notari avatar Geevarghese George avatar Joerg avatar  avatar  avatar Vaibhaw Krishna avatar Alan avatar  avatar Dirco avatar martintmv avatar Dimitrios Kapetanios avatar  avatar Benito Martin avatar Viktor Aleksandrov avatar  avatar  avatar Ivan Reznikov avatar  avatar 王嘉楠 avatar Liu Xiao avatar  avatar Pandazki avatar Michael Y. Choi avatar Sayantan Das avatar Prince Rumi avatar William avatar

Watchers

Stefan Suwelack avatar  avatar  avatar  avatar

renumics-rag's Issues

No instruction for use with Huggingface models

Why there are no instructions to use with open source embedding models? The notebook does not work because you can not create docs/data and fill it unless you are paying for OpenAI api.

DuplicateIDError

docs_vectorstore.add_documents(splits, ids=split_ids) throws the below error:

---------------------------------------------------------------------------
{
	"name": "DuplicateIDError",
	"message": "Expected IDs to be unique, found duplicates of: f7bf54e0f0a6f6fdf377d0904d443afab7177378, f8d159fc1246a55f0d0758e689c6099f440bfbc2",
	"stack": "---------------------------------------------------------------------------
DuplicateIDError                          Traceback (most recent call last)
Cell In[5], line 14
     12 split_ids = list(map(stable_hash, splits))
     13 #docs_vectorstore.add_documents(splits)
---> 14 docs_vectorstore.add_documents(splits, ids=split_ids)
     15 docs_vectorstore.persist()

File ~\\AppData\\Roaming\\Python\\Python311\\site-packages\\langchain_core\\vectorstores.py:119, in VectorStore.add_documents(self, documents, **kwargs)
    117 texts = [doc.page_content for doc in documents]
    118 metadatas = [doc.metadata for doc in documents]
--> 119 return self.add_texts(texts, metadatas, **kwargs)

File ~\\AppData\\Roaming\\Python\\Python311\\site-packages\\langchain_community\\vectorstores\\chroma.py:297, in Chroma.add_texts(self, texts, metadatas, ids, **kwargs)
    295 ids_with_metadata = [ids[idx] for idx in non_empty_ids]
    296 try:
--> 297     self._collection.upsert(
    298         metadatas=metadatas,
    299         embeddings=embeddings_with_metadatas,
    300         documents=texts_with_metadatas,
    301         ids=ids_with_metadata,
    302     )
    303 except ValueError as e:
    304     if \"Expected metadata value to be\" in str(e):

File ~\\AppData\\Roaming\\Python\\Python311\\site-packages\\chromadb\\api\\models\\Collection.py:477, in Collection.upsert(self, ids, embeddings, metadatas, documents, images, uris)
    444 def upsert(
    445     self,
    446     ids: OneOrMany[ID],
   (...)
    456     uris: Optional[OneOrMany[URI]] = None,
    457 ) -> None:
    458     \"\"\"Update the embeddings, metadatas or documents for provided ids, or create them if they don't exist.
    459 
    460     Args:
   (...)
    467         None
    468     \"\"\"
    470     (
    471         ids,
    472         embeddings,
    473         metadatas,
    474         documents,
    475         images,
    476         uris,
--> 477     ) = self._validate_embedding_set(
    478         ids, embeddings, metadatas, documents, images, uris
    479     )
    481     if embeddings is None:
    482         if documents is not None:

File ~\\AppData\\Roaming\\Python\\Python311\\site-packages\\chromadb\\api\\models\\Collection.py:545, in Collection._validate_embedding_set(self, ids, embeddings, metadatas, documents, images, uris, require_embeddings_or_data)
    523 def _validate_embedding_set(
    524     self,
    525     ids: OneOrMany[ID],
   (...)
    543     Optional[URIs],
    544 ]:
--> 545     valid_ids = validate_ids(maybe_cast_one_to_many_ids(ids))
    546     valid_embeddings = (
    547         validate_embeddings(
    548             self._normalize_embeddings(maybe_cast_one_to_many_embedding(embeddings))
   (...)
    551         else None
    552     )
    553     valid_metadatas = (
    554         validate_metadatas(maybe_cast_one_to_many_metadata(metadatas))
    555         if metadatas is not None
    556         else None
    557     )

File ~\\AppData\\Roaming\\Python\\Python311\\site-packages\\chromadb\\api\\types.py:240, in validate_ids(ids)
    236         example_string = (
    237             f\"{', '.join(examples[:5])}, ..., {', '.join(examples[-5:])}\"
    238         )
    239         message = f\"Expected IDs to be unique, found {n_dups} duplicated IDs: {example_string}\"
--> 240     raise errors.DuplicateIDError(message)
    241 return ids

DuplicateIDError: Expected IDs to be unique, found duplicates of: f7bf54e0f0a6f6fdf377d0904d443afab7177378, f8d159fc1246a55f0d0758e689c6099f440bfbc2"
}

LangChain Issue

Do one without LangChain, the dependencies for it make it not usable

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.