Use this file to discover all available pages before exploring further.
Pinecone is a vector database with broad functionality.
This notebook goes over how to use a retriever that under the hood uses Pinecone and Hybrid Search.The logic of this retriever is taken from this documentationTo use Pinecone, you must have an API key and an Environment.
Here are the installation instructions.
# Connect to Pinecone and get an API key.from pinecone_notebooks.colab import AuthenticateAuthenticate()import osapi_key = os.environ["PINECONE_API_KEY"]
from langchain_community.retrievers import ( PineconeHybridSearchRetriever,)
我们想要使用 OpenAIEmbeddings,所以需要获取 OpenAI API 密钥。
import getpassif "OPENAI_API_KEY" not in os.environ: os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:")
Embeddings are used for the dense vectors, tokenizer is used for the sparse vector
from langchain_openai import OpenAIEmbeddingsembeddings = OpenAIEmbeddings()
To encode the text to sparse values you can either choose SPLADE or BM25. For out of domain tasks we recommend using BM25.For more information about the sparse encoders you can checkout pinecone-text library docs.
from pinecone_text.sparse import BM25Encoder# or from pinecone_text.sparse import SpladeEncoder if you wish to work with SPLADE# use default tf-idf valuesbm25_encoder = BM25Encoder().default()
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:
corpus = ["foo", "bar", "world", "hello"]# fit tf-idf values on your corpusbm25_encoder.fit(corpus)# store the values to a json filebm25_encoder.dump("bm25_values.json")# load to your BM25Encoder objectbm25_encoder = BM25Encoder().load("bm25_values.json")