Pinecone 向量数据库 - 混合搜索¶
如果您在 Colab 上打开此 Notebook,可能需要安装 LlamaIndex 🦙。
%pip install llama-index-vector-stores-pinecone "transformers[torch]"
创建 Pinecone 索引¶
from pinecone import Pinecone, ServerlessSpec
import os
os.environ["PINECONE_API_KEY"] = "..."
os.environ["OPENAI_API_KEY"] = "sk-..."
api_key = os.environ["PINECONE_API_KEY"]
pc = Pinecone(api_key=api_key)
# delete if needed
pc.delete_index("quickstart")
# dimensions are for text-embedding-ada-002
# NOTE: needs dotproduct for hybrid search
pc.create_index(
name="quickstart",
dimension=1536,
metric="dotproduct",
spec=ServerlessSpec(cloud="aws", region="us-east-1"),
)
# If you need to create a PodBased Pinecone index, you could alternatively do this:
#
# from pinecone import Pinecone, PodSpec
#
# pc = Pinecone(api_key='xxx')
#
# pc.create_index(
# name='my-index',
# dimension=1536,
# metric='cosine',
# spec=PodSpec(
# environment='us-east1-gcp',
# pod_type='p1.x1',
# pods=1
# )
# )
#
pinecone_index = pc.Index("quickstart")
下载数据
!mkdir -p 'data/paul_graham/'
!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'
加载文档并构建 PineconeVectorStore¶
当设置 add_sparse_vector=True 时,PineconeVectorStore 会为每个文档计算稀疏向量。
默认情况下,系统使用简单的词频统计来生成稀疏向量。但您也可以指定自定义的稀疏嵌入模型。
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.vector_stores.pinecone import PineconeVectorStore
from IPython.display import Markdown, display
# load documents
documents = SimpleDirectoryReader("./data/paul_graham/").load_data()
# set add_sparse_vector=True to compute sparse vectors during upsert
from llama_index.core import StorageContext
if "OPENAI_API_KEY" not in os.environ:
raise EnvironmentError(f"Environment variable OPENAI_API_KEY is not set")
vector_store = PineconeVectorStore(
pinecone_index=pinecone_index,
add_sparse_vector=True,
)
storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_documents(
documents, storage_context=storage_context
)
huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks... To disable this warning, you can either: - Avoid using `tokenizers` before the fork if possible - Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)
Upserted vectors: 0%| | 0/22 [00:00<?, ?it/s]
查询索引¶
可能需要等待一两分钟让索引准备就绪
# set Logging to DEBUG for more detailed outputs
query_engine = index.as_query_engine(vector_store_query_mode="hybrid")
response = query_engine.query("What happened at Viaweb?")
display(Markdown(f"<b>{response}</b>"))
Paul Graham started Viaweb because he needed money. As the company grew, he realized he didn't want to run a big company and decided to build a subset of the vision as an open source project. Eventually, Viaweb was bought by Yahoo in the summer of 1998, which was a huge relief for Paul Graham.
更改稀疏嵌入模型¶
%pip install llama-index-sparse-embeddings-fastembed
# Clear the vector store
vector_store.clear()
from llama_index.sparse_embeddings.fastembed import FastEmbedSparseEmbedding
sparse_embedding_model = FastEmbedSparseEmbedding(
model_name="prithivida/Splade_PP_en_v1"
)
vector_store = PineconeVectorStore(
pinecone_index=pinecone_index,
add_sparse_vector=True,
sparse_embedding_model=sparse_embedding_model,
)
Fetching 5 files: 0%| | 0/5 [00:00<?, ?it/s]
index = VectorStoreIndex.from_documents(
documents, storage_context=storage_context
)
Upserted vectors: 0%| | 0/22 [00:00<?, ?it/s]
等待上传完成...
response = query_engine.query("What happened at Viaweb?")
display(Markdown(f"<b>{response}</b>"))
Paul Graham started Viaweb because he needed money. He recruited a team to work on building software and services, with a focus on creating an application builder and network infrastructure. However, halfway through the summer, Paul realized he didn't want to run a big company and decided to shift his focus to building a subset of the project as an open source project. This led to the development of a new dialect of Lisp called Arc. Ultimately, Viaweb was sold to Yahoo in the summer of 1998, providing relief to Paul Graham and allowing him to transition to a new phase in his life.