Skip to content
LlamaIndex 文档版本 跟随上游 源快照 f6157d8b 更新于 2026-10-18

如果你在 Colab 中打开这个 Notebook,可能需要先安装 LlamaIndex 🦙。

%pip install llama-index-vector-stores-weaviate
%pip install llama-index-embeddings-openai
!pip install llama-index
import weaviate
client = weaviate.Client("https://test-cluster-bbn8vqsn.weaviate.network")

准备示例“现有”Weaviate 向量存储

定义 schema

我们为 “Book” 类创建一个 schema,包含 4 个属性:title (str)、author (str)、content (str) 和 year (int)

try:
    client.schema.delete_class("Book")
except:
    pass
schema = {
    "classes": [
        {
            "class": "Book",
            "properties": [
                {"name": "title", "dataType": ["text"]},
                {"name": "author", "dataType": ["text"]},
                {"name": "content", "dataType": ["text"]},
                {"name": "year", "dataType": ["int"]},
            ],
        },
    ]
}

if not client.schema.contains(schema):
    client.schema.create(schema)

定义示例数据

我们创建 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 字段进行嵌入

from llama_index.embeddings.openai import OpenAIEmbedding

embed_model = OpenAIEmbedding()
with client.batch as batch:
    for book in books:
        vector = embed_model.get_text_embedding(book["content"])
        batch.add_data_object(
            data_object=book, class_name="Book", vector=vector
        )

查询“现有”Weaviate 向量存储

from llama_index.vector_stores.weaviate import WeaviateVectorStore
from llama_index.core import VectorStoreIndex
from llama_index.core.response.pprint_utils import pprint_source_node

你必须正确指定与目标 Weaviate 类匹配的 “index_name”,并选择一个类属性作为 “text” 字段。

vector_store = WeaviateVectorStore(
    weaviate_client=client, index_name="Book", 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: cf927ce7-0672-4696-8aae-7e77b33b9659
Similarity: None
Text: author: Harper Lee title: To Kill a Mockingbird year: 1960  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}

BytesRouter 是社区翻译站点,所有译文仅供参考,一切以官方英文原文为准。