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

阿里云 OpenSearch 向量检索版是阿里巴巴集团开发的大规模分布式搜索引擎。阿里云 OpenSearch 向量检索版为整个阿里巴巴集团提供搜索服务,包括淘宝、天猫、菜鸟、优酷等,以及面向中国大陆以外地区客户提供的其他电商平台。阿里云 OpenSearch 向量检索版也是阿里云 OpenSearch 的基础引擎。经过多年发展,阿里云 OpenSearch 向量检索版已满足高可用性、高时效性和成本效益的业务需求。阿里云 OpenSearch 向量检索版还提供自动化运维系统,你可以在其上根据业务特点构建自定义搜索服务。

要运行,你应该有一个实例。

准备

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

%pip install llama-index-vector-stores-alibabacloud-opensearch
%pip install llama-index
import logging
import sys

logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))

请提供 OpenAI 访问密钥

要使用 OpenAI 的嵌入,你需要提供 OpenAI API 密钥:

import openai

OPENAI_API_KEY = getpass.getpass("OpenAI API Key:")
openai.api_key = OPENAI_API_KEY

下载数据

!mkdir -p 'data/paul_graham/'
!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'

加载文档

from llama_index.core import SimpleDirectoryReader
from IPython.display import Markdown, display
# load documents
documents = SimpleDirectoryReader("./data/paul_graham").load_data()
print(f"Total documents: {len(documents)}")
Total documents: 1

创建阿里云 OpenSearch 向量存储对象:

要运行下一步,你应该有一个阿里云 OpenSearch 向量服务实例,并配置一个表。

# if run fllowing cells raise async io exception, run this
import nest_asyncio

nest_asyncio.apply()
# initialize without metadata filter
from llama_index.core import StorageContext, VectorStoreIndex
from llama_index.vector_stores.alibabacloud_opensearch import (
    AlibabaCloudOpenSearchStore,
    AlibabaCloudOpenSearchConfig,
)

config = AlibabaCloudOpenSearchConfig(
    endpoint="*****",
    instance_id="*****",
    username="your_username",
    password="your_password",
    table_name="llama",
)

vector_store = AlibabaCloudOpenSearchStore(config)
storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_documents(
    documents, storage_context=storage_context
)

查询索引

# set Logging to DEBUG for more detailed outputs
query_engine = index.as_query_engine()
response = query_engine.query("What did the author do growing up?")
display(Markdown(f"<b>{response}</b>"))

上大学之前,作者从事写作和编程。他写短篇小说,并在九年级时尝试使用早期版本的 Fortran 在 IBM 1401 上编写程序。

连接到现有存储

由于此存储由阿里云 OpenSearch 支持,因此从定义上讲它是持久化的。所以,如果你想连接到之前创建并填充的存储,可以这样做:

from llama_index.core import VectorStoreIndex
from llama_index.vector_stores.alibabacloud_opensearch import (
    AlibabaCloudOpenSearchStore,
    AlibabaCloudOpenSearchConfig,
)

config = AlibabaCloudOpenSearchConfig(
    endpoint="***",
    instance_id="***",
    username="your_username",
    password="your_password",
    table_name="llama",
)

vector_store = AlibabaCloudOpenSearchStore(config)

# Create index from existing stored vectors
index = VectorStoreIndex.from_vector_store(vector_store)
query_engine = index.as_query_engine()
response = query_engine.query(
    "What did the author study prior to working on AI?"
)

display(Markdown(f"<b>{response}</b>"))

元数据过滤

阿里云 OpenSearch 向量存储支持在查询时进行元数据过滤。以下单元格在一个全新的表上运行,演示了此功能。

在此演示中,为简洁起见,只加载一个源文档(../data/paul_graham/paul_graham_essay.txt 文本文件)。不过,你将为该文档附加一些自定义元数据,以说明如何通过文档所附元数据的条件来限制查询。

from llama_index.core import StorageContext, VectorStoreIndex
from llama_index.vector_stores.alibabacloud_opensearch import (
    AlibabaCloudOpenSearchStore,
    AlibabaCloudOpenSearchConfig,
)

config = AlibabaCloudOpenSearchConfig(
    endpoint="****",
    instance_id="****",
    username="your_username",
    password="your_password",
    table_name="llama",
)

md_storage_context = StorageContext.from_defaults(
    vector_store=AlibabaCloudOpenSearchStore(config)
)

def my_file_metadata(file_name: str):
    """Depending on the input file name, associate a different metadata."""
    if "essay" in file_name:
        source_type = "essay"
    elif "dinosaur" in file_name:
        # this (unfortunately) will not happen in this demo
        source_type = "dinos"
    else:
        source_type = "other"
    return {"source_type": source_type}

# Load documents and build index
md_documents = SimpleDirectoryReader(
    "../data/paul_graham", file_metadata=my_file_metadata
).load_data()
md_index = VectorStoreIndex.from_documents(
    md_documents, storage_context=md_storage_context
)

向查询引擎添加过滤器:

from llama_index.core.vector_stores import MetadataFilter, MetadataFilters

md_query_engine = md_index.as_query_engine(
    filters=MetadataFilters(
        filters=[MetadataFilter(key="source_type", value="essay")]
    )
)
md_response = md_query_engine.query(
    "How long it took the author to write his thesis?"
)

display(Markdown(f"<b>{md_response}</b>"))

要测试过滤是否生效,请尝试将其改为仅使用 "dinos" 文档……这次将没有答案 😃

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