Skip to content

使用向量存储#

LlamaIndex 提供了与向量存储/向量数据库的多种集成方式:

  1. LlamaIndex 可以直接将向量存储作为索引使用。与其他索引类型一样,这种索引可以存储文档并用于回答查询。
  2. LlamaIndex 能够从向量存储加载数据,这与使用其他数据连接器类似。这些数据随后可用于 LlamaIndex 的数据结构中。

使用向量存储作为索引#

LlamaIndex 支持将不同向量存储作为 VectorStoreIndex 的后端存储方案。

详细API参考文档请查看此处

与LlamaIndex中的其他索引(树状索引、关键词表、列表)类似,VectorStoreIndex可以在任何文档集合上构建。我们在索引中使用向量存储来存储输入文本块的嵌入向量。

构建完成后,该索引可用于查询。

默认向量存储索引构建/查询

默认情况下,VectorStoreIndex使用内存中的SimpleVectorStore,它作为默认存储上下文的一部分被初始化。

from llama_index.core import VectorStoreIndex, SimpleDirectoryReader

# 加载文档并构建索引
documents = SimpleDirectoryReader("../paul_graham_essay/data").load_data()
index = VectorStoreIndex.from_documents(documents)

# 查询索引
query_engine = index.as_query_engine()
response = query_engine.query("作者成长过程中做了什么?")

自定义向量存储索引构建/查询

我们可以按以下方式查询自定义向量存储:

from llama_index.core import (
    VectorStoreIndex,
    SimpleDirectoryReader,
    StorageContext,
)
from llama_index.vector_stores.deeplake import DeepLakeVectorStore

# 构建向量存储并自定义存储上下文
storage_context = StorageContext.from_defaults(
    vector_store=DeepLakeVectorStore(dataset_path="<dataset_path>")
)

# 加载文档并构建索引
documents = SimpleDirectoryReader("../paul_graham_essay/data").load_data()
index = VectorStoreIndex.from_documents(
    documents, storage_context=storage_context
)

# 查询索引
query_engine = index.as_query_engine()
response = query_engine.query("作者成长过程中做了什么?")

下面我们展示更多如何构建我们支持的各种向量存储的示例。

阿里云开放搜索

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)

Google AlloyDB for PostgreSQL

pip install llama-index
pip install llama-index-alloydb-pg
pip install llama-index-llms-vertex
gcloud services enable aiplatform.googleapis.com
from llama_index_alloydb_pg import AlloyDBEngine, AlloyDBVectorStore
from llama_index.core import Settings
from llama_index.embeddings.vertex import VertexTextEmbedding
from llama_index.llms.vertex import Vertex
import google.auth

# 替换为您自己的AlloyDB信息
engine = AlloyDBEngine.from_instance(
    project_id=PROJECT_ID,
    region=REGION,
    cluster=CLUSTER,
    instance=INSTANCE,
    database=DATABASE,
    user=USER,
    password=PASSWORD,
)

engine.init_vector_store_table(
    table_name=TABLE_NAME,
    vector_size=768,  # VertexAI模型(textembedding-gecko@latest)的向量维度
)

vector_store = AlloyDBVectorStore.create_sync(
    engine=engine,
    table_name=TABLE_NAME,
)

Amazon Neptune - Neptune Analytics

from llama_index.vector_stores.neptune import NeptuneAnalyticsVectorStore

graph_identifier = ""
embed_dim = 1536

neptune_vector_store = NeptuneAnalyticsVectorStore(
    graph_identifier=graph_identifier, embedding_dimension=1536
)

Apache Cassandra®

from llama_index.vector_stores.cassandra import CassandraVectorStore
import cassio

# 通过CQL使用Astra DB云实例:
cassio.init(database_id="1234abcd-...", token="AstraCS:...")

# 对于Cassandra集群:
from cassandra.cluster import Cluster

cluster = Cluster(["127.0.0.1"])
cassio.init(session=cluster.connect(), keyspace="my_keyspace")

# 在上述`cassio.init(...)`之后,创建向量存储:
vector_store = CassandraVectorStore(
    table="cass_v_table", embedding_dimension=1536
)

Astra DB

from llama_index.vector_stores.astra_db import AstraDBVectorStore

astra_db_store = AstraDBVectorStore(
    token="AstraCS:xY3b...",  # 您的Astra DB令牌
    api_endpoint="https://012...abc-us-east1.apps.astra.datastax.com",  # 您的Astra DB API端点
    collection_name="astra_v_table",  # 您选择的表名
    embedding_dimension=1536,  # 使用的嵌入模型的嵌入维度
)

Azure认知搜索

from azure.core.credentials import AzureKeyCredential
from llama_index.vector_stores.azureaisearch import AzureAISearchVectorStore

search_service_api_key = "YOUR-AZURE-SEARCH-SERVICE-ADMIN-KEY"
search_service_endpoint = "YOUR-AZURE-SEARCH-SERVICE-ENDPOINT"
search_service_api_version = "2023-11-01"
credential = AzureKeyCredential(search_service_api_key)

# 要使用的索引名称
index_name = "llamaindex-vector-demo"

client = SearchIndexClient(
    endpoint=search_service_endpoint,
    credential=credential,
)

vector_store = AzureAISearchVectorStore(
    search_or_index_client=client,
    index_name=index_name,
    embedding_dimensionality=1536,
)

Azure CosmosDB Mongo vCore

import pymongo
import os
from llama_index.vector_stores.azurecosmosmongo import (
    AzureCosmosDBMongoDBVectorSearch,
)

# 使用您的Azure CosmosDB MongoDB URI设置连接字符串
connection_string = os.getenv("YOUR_AZURE_COSMOSDB_MONGODB_URI")
mongodb_client = pymongo.MongoClient(connection_string)

# 创建AzureCosmosDBMongoDBVectorSearch实例
vector_store = AzureCosmosDBMongoDBVectorSearch(
    mongodb_client=mongodb_client,
    db_name="demo_vectordb",
    collection_name="paul_graham_essay",
)

Azure CosmosDB NoSql

from azure.cosmos import CosmosClient, PartitionKey
import os
from llama_index.vector_stores.azurecosmosnosql import (
    AzureCosmosDBNoSqlVectorSearch,
)

URL = os.getenv("AZURE_COSMOSDB_URI")
KEY = os.getenv("AZURE_COSMOSDB_KEY")
database_name = "test_database"
container_name = "test_container"
test_client = CosmosClient(URL, credential=KEY)

indexing_policy = {
    "indexingMode": "consistent",
    "includedPaths": [{"path": "/*"}],
    "excludedPaths": [{"path": '/"_etag"/?'}],
    "vectorIndexes": [{"path": "/embedding", "type": "quantizedFlat"}],
}

vector_embedding_policy = {
    "vectorEmbeddings": [
        {
            "path": "/embedding",
            "dataType": "float32",
            "distanceFunction": "cosine",
            "dimensions": 1536,
        }
    ]
}

partition_key = PartitionKey(path="/id")
cosmos_container_properties_test = {"partition_key": partition_key}
cosmos_database_properties_test = {}

vector_store = AzureCosmosDBNoSqlVectorSearch(
    cosmos_client=test_client,
    vector_embedding_policy=vector_embedding_policy,
    indexing_policy=indexing_policy,
    database_name=database_name,
    container_name=container_name,
    cosmos_database_properties=cosmos_database_properties_test,
    cosmos_container_properties=cosmos_container_properties_test,
)

Chroma

import chromadb
from llama_index.vector_stores.chroma import ChromaVectorStore

# 创建Chroma客户端
# EphemeralClient完全在内存中运行,PersistentClient也会保存到磁盘
chroma_client = chromadb.EphemeralClient()
chroma_collection = chroma_client.create_collection("quickstart")

# 构建向量存储
vector_store = ChromaVectorStore(
    chroma_collection=chroma_collection,
)

ClickHouse

import clickhouse_connect
from llama_index.vector_stores import ClickHouseVectorStore

# 创建ClickHouse客户端
client = clickhouse_connect.get_client(
    host="YOUR_CLUSTER_HOST",
    port=8123,
    username="YOUR_USERNAME",
    password="YOUR_CLUSTER_PASSWORD",
)

# 构建向量存储
vector_store = ClickHouseVectorStore(clickhouse_client=client)

Couchbase

from datetime import timedelta

from couchbase.auth import PasswordAuthenticator
from couchbase.cluster import Cluster
from couchbase.options import ClusterOptions

# 创建Couchbase集群对象
auth = PasswordAuthenticator("DATABASE_USERNAME", "DATABASE_PASSWORD")
options = ClusterOptions(auth)
cluster = Cluster("CLUSTER_CONNECTION_STRING", options)

# 等待集群准备就绪
cluster.wait_until_ready(timedelta(seconds=5))

# 创建向量存储
vector_store = CouchbaseSearchVectorStore(
    cluster=cluster,
    bucket_name="BUCKET_NAME",
    scope_name="SCOPE_NAME",
    collection_name="COLLECTION_NAME",
    index_name="SEARCH_INDEX_NAME",
)

DashVector

import dashvector
from llama_index.vector_stores.dashvector import DashVectorStore

# 初始化dashvector客户端
client = dashvector.Client(
    api_key="your-dashvector-api-key",
    endpoint="your-dashvector-cluster-endpoint",
)

# 创建DashVector集合
client.create("quickstart", dimension=1536)
collection = client.get("quickstart")

# 构建向量存储
vector_store = DashVectorStore(collection)

DeepLake

import os
import getpath
from llama_index.vector_stores.deeplake import DeepLakeVectorStore

os.environ["OPENAI_API_KEY"] = getpath.getpath("OPENAI_API_KEY: ")
os.environ["ACTIVELOOP_TOKEN"] = getpath.getpath("ACTIVELOOP_TOKEN: ")
dataset_path = "hub://adilkhan/paul_graham_essay"

# 构建向量存储
vector_store = DeepLakeVectorStore(dataset_path=dataset_path, overwrite=True)

DocArray

from llama_index.vector_stores.docarray import (
    DocArrayHnsw

## 使用数据连接器从向量存储加载数据

LlamaIndex 支持从海量数据源加载数据更多详情及API文档请参阅[数据连接器](../../module_guides/loading/connector/modules.md)

AlloyDB 同时存储文档和向量
本教程展示同步接口的使用方式所有同步方法均有对应的异步版本
以下是使用 AlloyDB 的示例

```bash
pip install llama-index
pip install llama-index-alloydb-pg
from llama_index.core import SummaryIndex
from llama_index_alloydb_pg import AlloyDBEngine, AlloyDBReader

engine = AlloyDBEngine.from_instance(
    project_id=PROJECT_ID,
    region=REGION,
    cluster=CLUSTER,
    instance=INSTANCE,
    database=DATABASE,
    user=USER,
    password=PASSWORD,
)
reader = AlloyDBReader.create_sync(
    engine,
    table_name=TABLE_NAME,
)
documents = reader.load_data()

index = SummaryIndex.from_documents(documents)

query_engine = index.as_query_engine()
response = query_engine.query("<query_text>")
display(Markdown(f"<b>{response}</b>"))

Google Cloud SQL for PostgreSQL 同时存储文档和向量。 本教程展示同步接口的使用方式,所有同步方法均有对应的异步版本。 以下是使用 Cloud SQL for PostgreSQL 的示例:

pip install llama-index
pip install llama-index-cloud-sql-pg
from llama_index.core import SummaryIndex
from llama_index_cloud_sql_pg import PostgresEngine, PostgresReader

engine = PostgresEngine.from_instance(
    project_id=PROJECT_ID,
    region=REGION,
    instance=INSTANCE,
    database=DATABASE,
    user=USER,
    password=PASSWORD,
)
reader = PostgresReader.create_sync(
    engine,
    table_name=TABLE_NAME,
)
documents = reader.load_data()

index = SummaryIndex.from_documents(documents)

query_engine = index.as_query_engine()
response = query_engine.query("<query_text>")
display(Markdown(f"<b>{response}</b>"))

Chroma 同时存储文档和向量。以下是使用 Chroma 的示例:

from llama_index.readers.chroma import ChromaReader
from llama_index.core import SummaryIndex

# Chroma读取器从持久化的Chroma集合加载数据
# 需要指定集合名称和持久化目录
reader = ChromaReader(
    collection_name="chroma_collection",
    persist_directory="examples/data_connectors/chroma_collection",
)

query_vector = [n1, n2, n3, ...]

documents = reader.load_data(
    collection_name="demo", query_vector=query_vector, limit=5
)
index = SummaryIndex.from_documents(documents)

query_engine = index.as_query_engine()
response = query_engine.query("<query_text>")
display(Markdown(f"<b>{response}</b>"))

Qdrant 同样存储文档和向量。以下是使用 Qdrant 的示例:

from llama_index.readers.qdrant import QdrantReader

reader = QdrantReader(host="localhost")

# query_vector是查询向量的嵌入表示
# 示例query_vector
# query_vector = [0.3, 0.3, 0.3, 0.3, ...]

query_vector = [n1, n2, n3, ...]

# 注意:必填参数为collection_name和query_vector
# 更多细节请参阅Python客户端:https;//github.com/qdrant/qdrant_client

documents = reader.load_data(
    collection_name="demo", query_vector=query_vector, limit=5
)

注意:由于 Weaviate 可以存储文档和向量的混合对象,用户可以选择显式指定 class_nameproperties 来查询文档,也可以选择指定原始 GraphQL 查询。用法示例如下:

# 选项1:指定class_name和properties

# 1) 使用class_name和properties加载数据
documents = reader.load_data(
    class_name="<class_name>",
    properties=["property1", "property2", "..."],
    separate_documents=True,
)

# 2) GraphQL查询示例
query = """
{
    Get {
        <class_name> {
            <property1>
            <property2>
        }
    }
}
"""

documents = reader.load_data(graphql_query=query, separate_documents=True)

注意:Pinecone 和 Faiss 数据加载器假定相应数据源仅存储向量,文本内容存储在其他地方。因此这两个数据加载器都要求用户在 load_data 调用中指定 id_to_text_map

例如,以下是 Pinecone 数据加载器 PineconeReader 的使用示例:

from llama_index.readers.pinecone import PineconeReader

reader = PineconeReader(api_key=api_key, environment="us-west1-gcp")

id_to_text_map = {
    "id1": "text blob 1",
    "id2": "text blob 2",
}

query_vector = [n1, n2, n3, ...]

documents = reader.load_data(
    index_name="quickstart",
    id_to_text_map=id_to_text_map,
    top_k=3,
    vector=query_vector,
    separate_documents=True,
)

示例笔记本可在此处查看

向量存储示例#