Skip to content

Commit 79b4959

Browse files
authored
Merge pull request #359 from junefish/master
update Cohere example notebook
2 parents bb081ba + d204289 commit 79b4959

File tree

1 file changed

+50
-15
lines changed

1 file changed

+50
-15
lines changed

integrations/cohere/semantic_search_trec.ipynb

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@
149149
}
150150
],
151151
"source": [
152-
"!pip install cohere pinecone-client datasets"
152+
"!pip install cohere pinecone-client datasets pinecone-notebooks"
153153
]
154154
},
155155
{
@@ -158,7 +158,7 @@
158158
"id": "-aPF7S50NA1P"
159159
},
160160
"source": [
161-
"And sign up for an API key over at [Cohere](https://os.cohere.ai/) and [Pinecone](https://app.pinecone.io), you can enter the keys directly in the cell below."
161+
"Sign up for an API key at [Cohere](https://dashboard.cohere.com/api-keys), you can enter the key directly in the cell below."
162162
]
163163
},
164164
{
@@ -169,8 +169,7 @@
169169
},
170170
"outputs": [],
171171
"source": [
172-
"COHERE_KEY = '<<YOUR-KEY-HERE>>'\n",
173-
"PINECONE_KEY = '<<YOUR-KEY-HERE>>'"
172+
"COHERE_KEY = '<<YOUR-KEY-HERE>>'"
174173
]
175174
},
176175
{
@@ -587,6 +586,23 @@
587586
"Now that we have our embeddings we can move on to indexing them in the Pinecone vector database. Again, this is very simple, we just initialize our connection to Pinecone and then create a new index for storing the embeddings, making sure to specify that we would like to use the cosine similarity metric to align with Cohere's embeddings."
588587
]
589588
},
589+
{
590+
"cell_type": "code",
591+
"execution_count": null,
592+
"metadata": {},
593+
"outputs": [],
594+
"source": [
595+
"from pinecone_notebooks.colab import Authenticate\n",
596+
"Authenticate()"
597+
]
598+
},
599+
{
600+
"cell_type": "markdown",
601+
"metadata": {},
602+
"source": [
603+
"The generated API key is stored in the `PINECONE_API_KEY` environment variable for us to use."
604+
]
605+
},
590606
{
591607
"cell_type": "code",
592608
"execution_count": null,
@@ -595,25 +611,44 @@
595611
},
596612
"outputs": [],
597613
"source": [
598-
"from pinecone import Pinecone\n",
614+
"from pinecone import Pinecone, ServerlessSpec\n",
615+
"import os\n",
599616
"\n",
600-
"pinecone.init(\n",
601-
" PINECONE_KEY,\n",
602-
" environment=\"YOUR_ENV\" # find next to API key in console\n",
603-
")\n",
617+
"api_key = os.environ.get('PINECONE_API_KEY')\n",
604618
"\n",
619+
"# Use the API key to initialize the Pinecone client\n",
620+
"pc = Pinecone(api_key=api_key)"
621+
]
622+
},
623+
{
624+
"cell_type": "markdown",
625+
"metadata": {},
626+
"source": [
627+
"Now that the Pinecone client is initialized, we can use it to connect to an index."
628+
]
629+
},
630+
{
631+
"cell_type": "code",
632+
"execution_count": null,
633+
"metadata": {},
634+
"outputs": [],
635+
"source": [
605636
"index_name = 'cohere-pinecone-trec'\n",
606637
"\n",
607638
"# if the index does not exist, we create it\n",
608-
"if index_name not in pinecone.list_indexes().names():\n",
609-
" pinecone.create_index(\n",
610-
" index_name,\n",
639+
"if index_name not in pc.list_indexes().names():\n",
640+
" pc.create_index(\n",
641+
" name=index_name,\n",
611642
" dimension=shape[1],\n",
612-
" metric='cosine'\n",
643+
" metric=\"cosine\",\n",
644+
" spec=ServerlessSpec(\n",
645+
" cloud='aws', \n",
646+
" region='us-east-1'\n",
647+
" ) \n",
613648
" )\n",
614649
"\n",
615650
"# connect to index\n",
616-
"index = pinecone.Index(index_name)"
651+
"index = pc.Index(index_name)"
617652
]
618653
},
619654
{
@@ -961,7 +996,7 @@
961996
"metadata": {},
962997
"outputs": [],
963998
"source": [
964-
"piencone.delete_index(index_name)"
999+
"pc.delete_index(index_name)"
9651000
]
9661001
},
9671002
{

0 commit comments

Comments
 (0)