File size: 7,135 Bytes
cfd3735
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "ab66dd43",
   "metadata": {},
   "source": [
    "# Pinecone Hybrid Search\n",
    "\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 taken from [this documentaion](https://docs.pinecone.io/docs/hybrid-search)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "393ac030",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.retrievers import PineconeHybridSearchRetriever"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aaf80e7f",
   "metadata": {},
   "source": [
    "## Setup Pinecone"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "95d5d7f9",
   "metadata": {},
   "source": [
    "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": 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",
    "pinecone.create_index(\n",
    "   name = index_name,\n",
    "   dimension = 1536,  # dimensionality of dense model\n",
    "   metric = \"dotproduct\",  # sparse values supported only for dotproduct\n",
    "   pod_type = \"s1\",\n",
    "   metadata_config={\"indexed\": []}  # see explaination above\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e01549af",
   "metadata": {},
   "source": [
    "Now that its created, we can use it"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "bcb3c8c2",
   "metadata": {},
   "outputs": [],
   "source": [
    "index = pinecone.Index(index_name)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "dbc025d6",
   "metadata": {},
   "source": [
    "## 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": 79,
   "id": "2f63c911",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.embeddings import OpenAIEmbeddings\n",
    "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": 80,
   "id": "c3f030e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pinecone_text.sparse import BM25Encoder\n",
    "# or from pinecone_text.sparse import SpladeEncoder if you wish to work with SPLADE\n",
    "\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",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5462801e",
   "metadata": {},
   "source": [
    "## Load Retriever\n",
    "\n",
    "We can now construct the retriever!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "ac77d835",
   "metadata": {},
   "outputs": [],
   "source": [
    "retriever = PineconeHybridSearchRetriever(embeddings=embeddings, sparse_encoder=bm25_encoder, index=index)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c518c42",
   "metadata": {},
   "source": [
    "## Add texts (if necessary)\n",
    "\n",
    "We can optionally add texts to the retriever (if they aren't already in there)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "98b1c017",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 1/1 [00:02<00:00,  2.27s/it]\n"
     ]
    }
   ],
   "source": [
    "retriever.add_texts([\"foo\", \"bar\", \"world\", \"hello\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "08437fa2",
   "metadata": {},
   "source": [
    "## Use Retriever\n",
    "\n",
    "We can now use the retriever!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "c0455218",
   "metadata": {},
   "outputs": [],
   "source": [
    "result = retriever.get_relevant_documents(\"foo\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "7dfa5c29",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Document(page_content='foo', metadata={})"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result[0]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  },
  "vscode": {
   "interpreter": {
    "hash": "7ec0d8babd8cabf695a1d94b1e586d626e046c9df609f6bad065d15d49f67f54"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}