-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: integration on pinecone hosted rerankers (#17192)
- Loading branch information
Showing
11 changed files
with
466 additions
and
0 deletions.
There are no files selected for viewing
153 changes: 153 additions & 0 deletions
153
...ex-integrations/postprocessor/llama-index-postprocessor-pinecone-native-rerank/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
3 changes: 3 additions & 0 deletions
3
...a-index-integrations/postprocessor/llama-index-postprocessor-pinecone-native-rerank/BUILD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
poetry_requirements( | ||
name = "poetry", | ||
) |
17 changes: 17 additions & 0 deletions
17
...ndex-integrations/postprocessor/llama-index-postprocessor-pinecone-native-rerank/Makefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
73 changes: 73 additions & 0 deletions
73
...ations/postprocessor/llama-index-postprocessor-pinecone-native-rerank/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# LLamaIndex node postprocessing reranker using pinecone hosted models | ||
|
||
- use rerank models with the pinecone managed vector service to rerank the search results | ||
- available rerank models from [pinecone](https://app.pinecone.io/organizations/-Nn577_974iRsvC6nVxg/projects/a4fe57a4-b1cc-4a99-bf1d-c35a595cae4a/models) | ||
|
||
```python | ||
import os | ||
from llama_index.core import VectorStoreIndex | ||
from llama_index.core.schema import TextNode | ||
from llama_index.postprocessor.pinecone_native_rerank import ( | ||
PineconeNativeRerank, | ||
) | ||
from llama_index.core.response.pprint_utils import pprint_response | ||
|
||
os.environ["PINECONE_API_KEY"] = "your_pinecone_api_key" | ||
os.environ["OPENAI_API_KEY"] = "your_openai_api_key" | ||
|
||
txts = [ | ||
"Apple is a popular fruit known for its sweetness and crisp texture.", | ||
"Apple is known for its innovative products like the iPhone.", | ||
"Many people enjoy eating apples as a healthy snack.", | ||
"Apple Inc. has revolutionized the tech industry with its sleek designs and user-friendly interfaces.", | ||
"An apple a day keeps the doctor away, as the saying goes.", | ||
"apple has a lot of vitamins", | ||
] | ||
|
||
nodes = [TextNode(id_=f"vec{i}", text=txt) for i, txt in enumerate(txts)] | ||
|
||
pinecone_reranker = PineconeNativeRerank(top_n=4, model="pinecone-rerank-v0") | ||
|
||
index = VectorStoreIndex(nodes) | ||
|
||
query_engine = index.as_query_engine( | ||
similarity_top_k=10, | ||
node_postprocessors=[pinecone_reranker], | ||
) | ||
|
||
response = query_engine.query( | ||
"The tech company Apple is known for its innovative products like the iPhone." | ||
) | ||
|
||
pprint_response(response, show_source=True) | ||
``` | ||
|
||
output | ||
|
||
```txt | ||
Final Response: Apple is recognized for its innovative products like | ||
the iPhone. | ||
______________________________________________________________________ | ||
Source Node 1/4 | ||
Node ID: vec1 | ||
Similarity: 0.9655668 | ||
Text: Apple is known for its innovative products like the iPhone. | ||
______________________________________________________________________ | ||
Source Node 2/4 | ||
Node ID: vec3 | ||
Similarity: 0.55420566 | ||
Text: Apple Inc. has revolutionized the tech industry with its sleek | ||
designs and user-friendly interfaces. | ||
______________________________________________________________________ | ||
Source Node 3/4 | ||
Node ID: vec4 | ||
Similarity: 0.3172258 | ||
Text: An apple a day keeps the doctor away, as the saying goes. | ||
______________________________________________________________________ | ||
Source Node 4/4 | ||
Node ID: vec0 | ||
Similarity: 0.25139993 | ||
Text: Apple is a popular fruit known for its sweetness and crisp | ||
texture. | ||
``` |
1 change: 1 addition & 0 deletions
1
...stprocessor-pinecone-native-rerank/llama_index/postprocessor/pinecone_native_rerank/BUILD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
python_sources() |
4 changes: 4 additions & 0 deletions
4
...essor-pinecone-native-rerank/llama_index/postprocessor/pinecone_native_rerank/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from llama_index.postprocessor.pinecone_native_rerank.base import PineconeNativeRerank | ||
|
||
|
||
__all__ = ["PineconeNativeRerank"] |
105 changes: 105 additions & 0 deletions
105
...processor-pinecone-native-rerank/llama_index/postprocessor/pinecone_native_rerank/base.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import os | ||
from typing import List, Optional, Literal | ||
from llama_index.core.bridge.pydantic import Field, PrivateAttr | ||
from llama_index.core.callbacks import CBEventType, EventPayload | ||
from llama_index.core.instrumentation import get_dispatcher | ||
from llama_index.core.instrumentation.events.rerank import ( | ||
ReRankEndEvent, | ||
ReRankStartEvent, | ||
) | ||
from llama_index.core.postprocessor.types import BaseNodePostprocessor | ||
from llama_index.core.schema import NodeWithScore, QueryBundle, MetadataMode | ||
|
||
dispatcher = get_dispatcher(__name__) | ||
|
||
|
||
# as of today 2024/12/7 | ||
# pinecone itself supports 3 rerank models out of its sdk | ||
# https://app.pinecone.io/organizations/-Nn577_974iRsvC6nVxg/projects/a4fe57a4-b1cc-4a99-bf1d-c35a595cae4a/models | ||
class PineconeNativeRerank(BaseNodePostprocessor): | ||
model: Literal[ | ||
"bge-reranker-v2-m3", "cohere-rerank-3.5", "pinecone-rerank-v0" | ||
] = Field( | ||
description="supported Pinecone inference rerank model name", | ||
default="bge-reranker-v2-m3", | ||
) | ||
top_n: int = Field(description="Top N nodes to return") | ||
|
||
_pc: any = PrivateAttr() | ||
|
||
def __init__( | ||
self, | ||
top_n: int = 2, | ||
model: str = "bge-reranker-v2-m3", | ||
api_key: Optional[str] = None, | ||
): | ||
super().__init__(top_n=top_n, model=model) | ||
try: | ||
api_key = api_key or os.environ["PINECONE_API_KEY"] | ||
except IndexError: | ||
raise ValueError( | ||
"Must pass in pinecone api key or " | ||
"specify via PINECONE_API_KEY environment variable " | ||
) | ||
|
||
try: | ||
from pinecone import Pinecone | ||
except ImportError: | ||
raise ImportError( | ||
"Cannot import pinecone package, please `pip install pinecone-client`." | ||
) | ||
|
||
self._pc = Pinecone(api_key=api_key) | ||
|
||
@classmethod | ||
def class_name(cls) -> str: | ||
return "PineconeNativeRerank" | ||
|
||
def _postprocess_nodes( | ||
self, | ||
nodes: List[NodeWithScore], | ||
query_bundle: Optional[QueryBundle] = None, | ||
) -> List[NodeWithScore]: | ||
dispatcher.event( | ||
ReRankStartEvent( | ||
query=query_bundle, nodes=nodes, top_n=self.top_n, model_name=self.model | ||
) | ||
) | ||
|
||
if query_bundle is None: | ||
raise ValueError("Missing query bundle in extra info.") | ||
if len(nodes) == 0: | ||
return [] | ||
|
||
with self.callback_manager.event( | ||
CBEventType.RERANKING, | ||
payload={ | ||
EventPayload.NODES: nodes, | ||
EventPayload.MODEL_NAME: self.model, | ||
EventPayload.QUERY_STR: query_bundle.query_str, | ||
EventPayload.TOP_K: self.top_n, | ||
}, | ||
) as event: | ||
texts = [ | ||
node.node.get_content(metadata_mode=MetadataMode.EMBED) | ||
for node in nodes | ||
] | ||
|
||
reranked_result = self._pc.inference.rerank( | ||
model=self.model, | ||
top_n=self.top_n, | ||
query=query_bundle.query_str, | ||
documents=texts, | ||
return_documents=True, | ||
) | ||
new_nodes = [] | ||
for result in reranked_result.data: | ||
new_nodes.append( | ||
NodeWithScore( | ||
node=nodes[result.index].node, | ||
score=result.score, | ||
) | ||
) | ||
event.on_end(payload={EventPayload.NODES: new_nodes}) | ||
dispatcher.event(ReRankEndEvent(nodes=new_nodes)) | ||
return new_nodes |
Oops, something went wrong.