Harrison/pinecone hybrid update (#2742)

Co-authored-by: acatav <39461369+acatav@users.noreply.github.com>
Co-authored-by: Amnon Catav <catav.amnon1@gmail.com>
This commit is contained in:
Harrison Chase
2023-04-11 21:32:17 -07:00
committed by GitHub
parent 744c25cd0a
commit 507cee5ee5
4 changed files with 312 additions and 456 deletions

View File

@@ -1,6 +1,7 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"id": "ab66dd43",
"metadata": {},
@@ -9,12 +10,12 @@
"\n",
"This notebook goes over how to use a retriever that under the hood uses Pinecone and Hybrid Search.\n",
"\n",
"The logic of this retriever is largely taken from [this blog post](https://www.pinecone.io/learn/hybrid-search-intro/)"
"The logic of this retriever is taken from [this documentaion](https://docs.pinecone.io/docs/hybrid-search)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 75,
"id": "393ac030",
"metadata": {},
"outputs": [],
@@ -31,43 +32,61 @@
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "15390796",
"metadata": {},
"outputs": [],
"source": [
"import pinecone # !pip install pinecone-client\n",
"\n",
"pinecone.init(\n",
" api_key=\"...\", # API key here\n",
" environment=\"...\" # find next to api key in console\n",
")\n",
"# choose a name for your index\n",
"index_name = \"...\""
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "95d5d7f9",
"metadata": {},
"source": [
"You should only have to do this part once."
"You should only have to do this part once.\n",
"\n",
"Note: it's important to make sure that the \"context\" field that holds the document text in the metadata is not indexed. Currently you need to specify explicitly the fields you do want to index. For more information checkout Pinecone's [docs](https://docs.pinecone.io/docs/manage-indexes#selective-metadata-indexing)."
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 76,
"id": "3b8f7697",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"WhoAmIResponse(username='load', user_label='label', projectname='load-test')"
]
},
"execution_count": 76,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import os\n",
"import pinecone\n",
"\n",
"api_key = os.getenv(\"PINECONE_API_KEY\") or \"PINECONE_API_KEY\"\n",
"# find environment next to your API key in the Pinecone console\n",
"env = os.getenv(\"PINECONE_ENVIRONMENT\") or \"PINECONE_ENVIRONMENT\"\n",
"\n",
"index_name = \"langchain-pinecone-hybrid-search\"\n",
"\n",
"pinecone.init(api_key=api_key, enviroment=env)\n",
"pinecone.whoami()"
]
},
{
"cell_type": "code",
"execution_count": 77,
"id": "cfa3a8d8",
"metadata": {},
"outputs": [],
"source": [
"# create the index\n",
" # create the index\n",
"pinecone.create_index(\n",
" name = index_name,\n",
" dimension = 1536, # dimensionality of dense model\n",
" metric = \"dotproduct\",\n",
" pod_type = \"s1\"\n",
" metric = \"dotproduct\", # sparse values supported only for dotproduct\n",
" pod_type = \"s1\",\n",
" metadata_config={\"indexed\": []} # see explaination above\n",
")"
]
},
@@ -81,7 +100,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 78,
"id": "bcb3c8c2",
"metadata": {},
"outputs": [],
@@ -90,18 +109,19 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "dbc025d6",
"metadata": {},
"source": [
"## Get embeddings and tokenizers\n",
"## Get embeddings and sparse encoders\n",
"\n",
"Embeddings are used for the dense vectors, tokenizer is used for the sparse vector"
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 79,
"id": "2f63c911",
"metadata": {},
"outputs": [],
@@ -110,19 +130,51 @@
"embeddings = OpenAIEmbeddings()"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "96bf8879",
"metadata": {},
"source": [
"To encode the text to sparse values you can either choose SPLADE or BM25. For out of domain tasks we recommend using BM25.\n",
"\n",
"For more information about the sparse encoders you can checkout pinecone-text library [docs](https://pinecone-io.github.io/pinecone-text/pinecone_text.html)."
]
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 80,
"id": "c3f030e5",
"metadata": {},
"outputs": [],
"source": [
"from transformers import BertTokenizerFast # !pip install transformers\n",
"from pinecone_text.sparse import BM25Encoder\n",
"# or from pinecone_text.sparse import SpladeEncoder if you wish to work with SPLADE\n",
"\n",
"# load bert tokenizer from huggingface\n",
"tokenizer = BertTokenizerFast.from_pretrained(\n",
" 'bert-base-uncased'\n",
")"
"# use default tf-idf values\n",
"bm25_encoder = BM25Encoder().default()"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "23601ddb",
"metadata": {},
"source": [
"The above code is using default tfids values. It's highly recommended to fit the tf-idf values to your own corpus. You can do it as follow:\n",
"\n",
"```python\n",
"corpus = [\"foo\", \"bar\", \"world\", \"hello\"]\n",
"\n",
"# fit tf-idf values on your corpus\n",
"bm25_encoder.fit(corpus)\n",
"\n",
"# store the values to a json file\n",
"bm25_encoder.dump(\"bm25_values.json\")\n",
"\n",
"# load to your BM25Encoder object\n",
"bm25_encoder = BM25Encoder().load(\"bm25_values.json\")\n",
"```"
]
},
{
@@ -137,12 +189,12 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 81,
"id": "ac77d835",
"metadata": {},
"outputs": [],
"source": [
"retriever = PineconeHybridSearchRetriever(embeddings=embeddings, index=index, tokenizer=tokenizer)"
"retriever = PineconeHybridSearchRetriever(embeddings=embeddings, sparse_encoder=bm25_encoder, index=index)"
]
},
{
@@ -157,23 +209,16 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 82,
"id": "98b1c017",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "4d6f3ee7ca754d07a1a18d100d99e0cd",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/1 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 1/1 [00:02<00:00, 2.27s/it]\n"
]
}
],
"source": [
@@ -192,7 +237,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 83,
"id": "c0455218",
"metadata": {},
"outputs": [],
@@ -202,7 +247,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 84,
"id": "7dfa5c29",
"metadata": {},
"outputs": [
@@ -212,7 +257,7 @@
"Document(page_content='foo', metadata={})"
]
},
"execution_count": 10,
"execution_count": 84,
"metadata": {},
"output_type": "execute_result"
}
@@ -220,19 +265,11 @@
"source": [
"result[0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "74bd9256",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": ".venv",
"language": "python",
"name": "python3"
},
@@ -246,7 +283,12 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.1"
"version": "3.9.13"
},
"vscode": {
"interpreter": {
"hash": "7ec0d8babd8cabf695a1d94b1e586d626e046c9df609f6bad065d15d49f67f54"
}
}
},
"nbformat": 4,