mirror of
https://github.com/hwchase17/langchain
synced 2024-11-02 09:40:22 +00:00
docs: Update Pinecone example notebook with embedded widget (#21719)
--------- Co-authored-by: Erick Friis <erick@langchain.dev>
This commit is contained in:
parent
0aea7f4b1d
commit
c5a981e3b4
@ -12,16 +12,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook shows how to use functionality related to the `Pinecone` vector database.\n",
|
"This notebook shows how to use functionality related to the `Pinecone` vector database.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"To use Pinecone, you must have an API key. \n",
|
"Set the following environment variables to follow along in this doc:\n",
|
||||||
"Here are the [installation instructions](https://docs.pinecone.io/docs/quickstart).\n",
|
|
||||||
"\n",
|
|
||||||
"Set the following environment variables to make using the `Pinecone` integration easier:\n",
|
|
||||||
"\n",
|
|
||||||
"- `PINECONE_API_KEY`: Your Pinecone API key.\n",
|
|
||||||
"- `PINECONE_INDEX_NAME`: The name of the index you want to use.\n",
|
|
||||||
"\n",
|
|
||||||
"And to follow along in this doc, you should also set\n",
|
|
||||||
"\n",
|
|
||||||
"- `OPENAI_API_KEY`: Your OpenAI API key, for using `OpenAIEmbeddings`"
|
"- `OPENAI_API_KEY`: Your OpenAI API key, for using `OpenAIEmbeddings`"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@ -34,7 +25,11 @@
|
|||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%pip install --upgrade --quiet langchain-pinecone langchain-openai langchain"
|
"%pip install --upgrade --quiet \\\n",
|
||||||
|
" langchain-pinecone \\\n",
|
||||||
|
" langchain-openai \\\n",
|
||||||
|
" langchain \\\n",
|
||||||
|
" pinecone-notebooks"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -72,14 +67,93 @@
|
|||||||
"embeddings = OpenAIEmbeddings()"
|
"embeddings = OpenAIEmbeddings()"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "ef6dc4de",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Now let's create a new Pinecone account, or sign into your existing one, and create an API key to use in this notebook."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "1fdc3c36",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from pinecone_notebooks.colab import Authenticate\n",
|
||||||
|
"\n",
|
||||||
|
"Authenticate()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "54da1a39",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"The newly created API key has been stored in the `PINECONE_API_KEY` environment variable. We will use it to setup the Pinecone client."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "eb554814",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import os\n",
|
||||||
|
"\n",
|
||||||
|
"pinecone_api_key = os.environ.get(\"PINECONE_API_KEY\")\n",
|
||||||
|
"pinecone_api_key\n",
|
||||||
|
"\n",
|
||||||
|
"import time\n",
|
||||||
|
"\n",
|
||||||
|
"from pinecone import Pinecone, ServerlessSpec\n",
|
||||||
|
"\n",
|
||||||
|
"pc = Pinecone(api_key=pinecone_api_key)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "658706a3",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Next, let's connect to your Pinecone index. If one named `index_name` doesn't exist, it will be created."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "276a06dd",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import time\n",
|
||||||
|
"\n",
|
||||||
|
"index_name = \"langchain-index\" # change if desired\n",
|
||||||
|
"\n",
|
||||||
|
"existing_indexes = [index_info[\"name\"] for index_info in pc.list_indexes()]\n",
|
||||||
|
"\n",
|
||||||
|
"if index_name not in existing_indexes:\n",
|
||||||
|
" pc.create_index(\n",
|
||||||
|
" name=index_name,\n",
|
||||||
|
" dimension=1536,\n",
|
||||||
|
" metric=\"cosine\",\n",
|
||||||
|
" spec=ServerlessSpec(cloud=\"aws\", region=\"us-east-1\"),\n",
|
||||||
|
" )\n",
|
||||||
|
" while not pc.describe_index(index_name).status[\"ready\"]:\n",
|
||||||
|
" time.sleep(1)\n",
|
||||||
|
"\n",
|
||||||
|
"index = pc.Index(index_name)"
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"id": "3a4d377f",
|
"id": "3a4d377f",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"Now let's assume you have your Pinecone index set up with `dimension=1536`.\n",
|
"Now that our Pinecone index is setup, we can upsert those chunked docs as contents with `PineconeVectorStore.from_documents`."
|
||||||
"\n",
|
|
||||||
"We can connect to our Pinecone index and insert those chunked docs as contents with `PineconeVectorStore.from_documents`."
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -91,8 +165,6 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"from langchain_pinecone import PineconeVectorStore\n",
|
"from langchain_pinecone import PineconeVectorStore\n",
|
||||||
"\n",
|
"\n",
|
||||||
"index_name = \"langchain-test-index\"\n",
|
|
||||||
"\n",
|
|
||||||
"docsearch = PineconeVectorStore.from_documents(docs, embeddings, index_name=index_name)"
|
"docsearch = PineconeVectorStore.from_documents(docs, embeddings, index_name=index_name)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@ -315,14 +387,6 @@
|
|||||||
"for i, doc in enumerate(found_docs):\n",
|
"for i, doc in enumerate(found_docs):\n",
|
||||||
" print(f\"{i + 1}.\", doc.page_content, \"\\n\")"
|
" print(f\"{i + 1}.\", doc.page_content, \"\\n\")"
|
||||||
]
|
]
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"id": "b0fd750b",
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": []
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
@ -341,7 +405,7 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.11.4"
|
"version": "3.11.6"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
Loading…
Reference in New Issue
Block a user