Skip to content

Commit

Permalink
Add Corrective RAG LlamaPack (#10715)
Browse files Browse the repository at this point in the history
* Add Corrective RAG LlamaPack

* Update with review suggestions

* Remove requirements.txt file

* Update BUILD

* Update examples

* Testing done
  • Loading branch information
ravi03071991 authored Feb 14, 2024
1 parent 448584c commit ebbf1a7
Show file tree
Hide file tree
Showing 15 changed files with 5,187 additions and 0 deletions.
153 changes: 153 additions & 0 deletions llama-index-packs/llama-index-packs-corrective-rag/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
llama_index/_static
.DS_Store
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
bin/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
etc/
include/
lib/
lib64/
parts/
sdist/
share/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
.ruff_cache

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints
notebooks/

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
pyvenv.cfg

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# Jetbrains
.idea
modules/
*.swp

# VsCode
.vscode

# pipenv
Pipfile
Pipfile.lock

# pyright
pyrightconfig.json
5 changes: 5 additions & 0 deletions llama-index-packs/llama-index-packs-corrective-rag/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
python_sources()

poetry_requirements(
name="poetry",
)
17 changes: 17 additions & 0 deletions llama-index-packs/llama-index-packs-corrective-rag/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
GIT_ROOT ?= $(shell git rev-parse --show-toplevel)

help: ## Show all Makefile targets.
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[33m%-30s\033[0m %s\n", $$1, $$2}'

format: ## Run code autoformatters (black).
pre-commit install
git ls-files | xargs pre-commit run black --files

lint: ## Run linters: pre-commit (black, ruff, codespell) and mypy
pre-commit install && git ls-files | xargs pre-commit run --show-diff-on-failure --files

test: ## Run tests via pytest.
pytest tests

watch-docs: ## Build and watch documentation.
sphinx-autobuild docs/ docs/_build/html --open-browser --watch $(GIT_ROOT)/llama_index/
47 changes: 47 additions & 0 deletions llama-index-packs/llama-index-packs-corrective-rag/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Corrective Retrieval Augmented Generation Llama Pack

This LlamaPack implements the Corrective Retrieval Augmented Generation (CRAG) [paper](https://arxiv.org/pdf/2401.15884.pdf)

Corrective Retrieval Augmented Generation (CRAG) is a method designed to enhance the robustness of language model generation by evaluating and augmenting the relevance of retrieved documents through a an evaluator and large-scale web searches, ensuring more accurate and reliable information is used in generation.

This LlamaPack uses [Tavily AI](https://app.tavily.com/home) API for web-searches. So, we recommend you to get the api-key before proceeding further.

### Installation

```bash
pip install llama-index llama-index-tools-tavily-research
```

## CLI Usage

You can download llamapacks directly using `llamaindex-cli`, which comes installed with the `llama-index` python package:

```bash
llamaindex-cli download-llamapack CorrectiveRAGPack --download-dir ./corrective_rag_pack
```

You can then inspect the files at `./corrective_rag_pack` and use them as a template for your own project.

## Code Usage

You can download the pack to a the `./corrective_rag_pack` directory:

```python
from llama_index.core.llama_pack import download_llama_pack

# download and install dependencies
CorrectiveRAGPack = download_llama_pack(
"CorrectiveRAGPack", "./corrective_rag_pack"
)

# You can use any llama-hub loader to get documents!
corrective_rag = CorrectiveRAGPack(documents, tavily_ai_api_key)
```

From here, you can use the pack, or inspect and modify the pack in `./corrective_rag_pack`.

The `run()` function contains around logic behind Corrective Retrieval Augmented Generation - [CRAG](https://arxiv.org/pdf/2401.15884.pdf) paper.

```python
response = corrective_rag.run("<query>", similarity_top_k=2)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python_sources()
Loading

0 comments on commit ebbf1a7

Please sign in to comment.