主题
如果你在 Colab 中打开这个 Notebook,可能需要先安装 LlamaIndex 🦙。
%pip install llama-index-embeddings-openai
%pip install llama-index-vector-stores-pinecone!pip install llama-indeximport os
import pineconeapi_key = os.environ["PINECONE_API_KEY"]
pinecone.init(api_key=api_key, environment="eu-west1-gcp")准备示例“现有”Pinecone 向量存储
创建索引
indexes = pinecone.list_indexes()
print(indexes)['quickstart-index']if "quickstart-index" not in indexes:
# dimensions are for text-embedding-ada-002
pinecone.create_index(
"quickstart-index", dimension=1536, metric="euclidean", pod_type="p1"
)pinecone_index = pinecone.Index("quickstart-index")pinecone_index.delete(deleteAll="true"){}定义示例数据
我们创建 4 本示例书籍
books = [
{
"title": "To Kill a Mockingbird",
"author": "Harper Lee",
"content": (
"To Kill a Mockingbird is a novel by Harper Lee published in"
" 1960..."
),
"year": 1960,
},
{
"title": "1984",
"author": "George Orwell",
"content": (
"1984 is a dystopian novel by George Orwell published in 1949..."
),
"year": 1949,
},
{
"title": "The Great Gatsby",
"author": "F. Scott Fitzgerald",
"content": (
"The Great Gatsby is a novel by F. Scott Fitzgerald published in"
" 1925..."
),
"year": 1925,
},
{
"title": "Pride and Prejudice",
"author": "Jane Austen",
"content": (
"Pride and Prejudice is a novel by Jane Austen published in"
" 1813..."
),
"year": 1813,
},
]添加数据
我们将示例书籍添加到 Weaviate 的 “Book” 类中(并对 content 字段进行嵌入
import uuid
from llama_index.embeddings.openai import OpenAIEmbedding
embed_model = OpenAIEmbedding()entries = []
for book in books:
vector = embed_model.get_text_embedding(book["content"])
entries.append(
{"id": str(uuid.uuid4()), "values": vector, "metadata": book}
)
pinecone_index.upsert(entries){'upserted_count': 4}查询“现有”Pinecone 向量存储
from llama_index.vector_stores.pinecone import PineconeVectorStore
from llama_index.core import VectorStoreIndex
from llama_index.core.response.pprint_utils import pprint_source_node你必须正确选择一个类属性作为 “text” 字段。
vector_store = PineconeVectorStore(
pinecone_index=pinecone_index, text_key="content"
)retriever = VectorStoreIndex.from_vector_store(vector_store).as_retriever(
similarity_top_k=1
)nodes = retriever.retrieve("What is that book about a bird again?")让我们检查检索到的节点。可以看到书籍数据被加载为 LlamaIndex Node 对象,并将 “content” 字段作为主文本。
pprint_source_node(nodes[0])Document ID: 07e47f1d-cb90-431b-89c7-35462afcda28
Similarity: 0.797243237
Text: author: Harper Lee title: To Kill a Mockingbird year: 1960.0 To
Kill a Mockingbird is a novel by Harper Lee published in 1960......其余字段应作为元数据加载(位于 metadata 中)
nodes[0].node.metadata{'author': 'Harper Lee', 'title': 'To Kill a Mockingbird', 'year': 1960.0}