mirror of
				https://github.com/hwchase17/langchain.git
				synced 2025-10-31 07:41:40 +00:00 
			
		
		
		
	# Docs: improvements in the `retrievers/examples/` notebooks Its primary purpose is to make the Jupyter notebook examples **consistent** and more suitable for first-time viewers. - add links to the integration source (if applicable) with a short description of this source; - removed `_retriever` suffix from the file names (where it existed) for consistency; - removed ` retriever` from the notebook title (where it existed) for consistency; - added code to install necessary Python package(s); - added code to set up the necessary API Key. - very small fixes in notebooks from other folders (for consistency): - docs/modules/indexes/vectorstores/examples/elasticsearch.ipynb - docs/modules/indexes/vectorstores/examples/pinecone.ipynb - docs/modules/models/llms/integrations/cohere.ipynb - fixed misspelling in langchain/retrievers/time_weighted_retriever.py comment (sorry, about this change in a .py file ) ## Who can review @dev2049
		
			
				
	
	
		
			169 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
			
		
		
	
	
			169 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
| {
 | |
|  "cells": [
 | |
|   {
 | |
|    "cell_type": "markdown",
 | |
|    "id": "683953b3",
 | |
|    "metadata": {},
 | |
|    "source": [
 | |
|     "# Pinecone\n",
 | |
|     "\n",
 | |
|     ">[Pinecone](https://docs.pinecone.io/docs/overview) is a vector database with broad functionality.\n",
 | |
|     "\n",
 | |
|     "This notebook shows how to use functionality related to the `Pinecone` vector database.\n",
 | |
|     "\n",
 | |
|     "To use Pinecone, you must have an API key. \n",
 | |
|     "Here are the [installation instructions](https://docs.pinecone.io/docs/quickstart)."
 | |
|    ]
 | |
|   },
 | |
|   {
 | |
|    "cell_type": "code",
 | |
|    "execution_count": null,
 | |
|    "id": "b4c41cad-08ef-4f72-a545-2151e4598efe",
 | |
|    "metadata": {
 | |
|     "tags": []
 | |
|    },
 | |
|    "outputs": [],
 | |
|    "source": [
 | |
|     "!pip install pinecone-client"
 | |
|    ]
 | |
|   },
 | |
|   {
 | |
|    "cell_type": "code",
 | |
|    "execution_count": null,
 | |
|    "id": "c1e38361-c1fe-4ac6-86e9-c90ebaf7ae87",
 | |
|    "metadata": {},
 | |
|    "outputs": [],
 | |
|    "source": [
 | |
|     "import os\n",
 | |
|     "import getpass\n",
 | |
|     "\n",
 | |
|     "PINECONE_API_KEY = getpass.getpass('Pinecone API Key:')"
 | |
|    ]
 | |
|   },
 | |
|   {
 | |
|    "cell_type": "code",
 | |
|    "execution_count": null,
 | |
|    "id": "02a536e0-d603-4d79-b18b-1ed562977b40",
 | |
|    "metadata": {},
 | |
|    "outputs": [],
 | |
|    "source": [
 | |
|     "PINECONE_ENV = getpass.getpass('Pinecone Environment:')"
 | |
|    ]
 | |
|   },
 | |
|   {
 | |
|    "cell_type": "markdown",
 | |
|    "id": "320af802-9271-46ee-948f-d2453933d44b",
 | |
|    "metadata": {},
 | |
|    "source": [
 | |
|     "We want to use `OpenAIEmbeddings` so we have to get the OpenAI API Key."
 | |
|    ]
 | |
|   },
 | |
|   {
 | |
|    "cell_type": "code",
 | |
|    "execution_count": null,
 | |
|    "id": "ffea66e4-bc23-46a9-9580-b348dfe7b7a7",
 | |
|    "metadata": {},
 | |
|    "outputs": [],
 | |
|    "source": [
 | |
|     "os.environ['OPENAI_API_KEY'] = getpass.getpass('OpenAI API Key:')"
 | |
|    ]
 | |
|   },
 | |
|   {
 | |
|    "cell_type": "code",
 | |
|    "execution_count": 2,
 | |
|    "id": "aac9563e",
 | |
|    "metadata": {
 | |
|     "tags": []
 | |
|    },
 | |
|    "outputs": [],
 | |
|    "source": [
 | |
|     "from langchain.embeddings.openai import OpenAIEmbeddings\n",
 | |
|     "from langchain.text_splitter import CharacterTextSplitter\n",
 | |
|     "from langchain.vectorstores import Pinecone\n",
 | |
|     "from langchain.document_loaders import TextLoader"
 | |
|    ]
 | |
|   },
 | |
|   {
 | |
|    "cell_type": "code",
 | |
|    "execution_count": 2,
 | |
|    "id": "a3c3999a",
 | |
|    "metadata": {},
 | |
|    "outputs": [],
 | |
|    "source": [
 | |
|     "from langchain.document_loaders import TextLoader\n",
 | |
|     "loader = TextLoader('../../../state_of_the_union.txt')\n",
 | |
|     "documents = loader.load()\n",
 | |
|     "text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n",
 | |
|     "docs = text_splitter.split_documents(documents)\n",
 | |
|     "\n",
 | |
|     "embeddings = OpenAIEmbeddings()"
 | |
|    ]
 | |
|   },
 | |
|   {
 | |
|    "cell_type": "code",
 | |
|    "execution_count": null,
 | |
|    "id": "6e104aee",
 | |
|    "metadata": {},
 | |
|    "outputs": [],
 | |
|    "source": [
 | |
|     "import pinecone \n",
 | |
|     "\n",
 | |
|     "# initialize pinecone\n",
 | |
|     "pinecone.init(\n",
 | |
|     "    api_key=PINECONE_API_KEY,  # find at app.pinecone.io\n",
 | |
|     "    environment=PINECONE_ENV  # next to api key in console\n",
 | |
|     ")\n",
 | |
|     "\n",
 | |
|     "index_name = \"langchain-demo\"\n",
 | |
|     "\n",
 | |
|     "docsearch = Pinecone.from_documents(docs, embeddings, index_name=index_name)\n",
 | |
|     "\n",
 | |
|     "# if you already have an index, you can load it like this\n",
 | |
|     "# docsearch = Pinecone.from_existing_index(index_name, embeddings)\n",
 | |
|     "\n",
 | |
|     "query = \"What did the president say about Ketanji Brown Jackson\"\n",
 | |
|     "docs = docsearch.similarity_search(query)"
 | |
|    ]
 | |
|   },
 | |
|   {
 | |
|    "cell_type": "code",
 | |
|    "execution_count": null,
 | |
|    "id": "9c608226",
 | |
|    "metadata": {},
 | |
|    "outputs": [],
 | |
|    "source": [
 | |
|     "print(docs[0].page_content)"
 | |
|    ]
 | |
|   },
 | |
|   {
 | |
|    "cell_type": "code",
 | |
|    "execution_count": null,
 | |
|    "id": "a359ed74",
 | |
|    "metadata": {},
 | |
|    "outputs": [],
 | |
|    "source": []
 | |
|   }
 | |
|  ],
 | |
|  "metadata": {
 | |
|   "kernelspec": {
 | |
|    "display_name": "Python 3 (ipykernel)",
 | |
|    "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.10.6"
 | |
|   }
 | |
|  },
 | |
|  "nbformat": 4,
 | |
|  "nbformat_minor": 5
 | |
| }
 |