Rockset 向量存储库¶
作为实时搜索与分析数据库,Rockset 通过索引技术为个性化推荐、产品搜索、语义搜索、聊天机器人等应用提供可扩展的高性能支持。由于 Rockset 专为实时场景设计,您可以在持续更新的流式数据上构建这些即时响应型应用。通过将 Rockset 与 LlamaIndex 集成,您可以轻松地对自有实时数据应用大语言模型,打造生产就绪的向量搜索解决方案。
我们将逐步演示如何在 LlamaIndex 中使用 Rockset 作为向量存储库。
教程¶
本示例将使用 OpenAI 的 text-embedding-ada-002 模型生成嵌入向量,并通过 Rockset 向量存储库保存这些嵌入。我们将从文件中提取文本内容,并针对文本内容提出问题。
环境配置¶
- 通过 Rockset 控制台创建集合,选择写入API作为数据源。将集合命名为
llamaindex_demo。配置以下数据摄取转换,使用VECTOR_ENFORCE定义嵌入字段并启用性能与存储优化:
SELECT
_input.* EXCEPT(_meta),
VECTOR_ENFORCE(
_input.embedding,
1536,
'float'
) as embedding
FROM _input
从 Rockset 控制台创建API密钥并设置
ROCKSET_API_KEY环境变量。在此处查找您的API服务器地址并设置ROCKSET_API_SERVER环境变量。同时设置OPENAI_API_KEY环境变量。安装依赖项:
pip3 install llama_index rockset
- LlamaIndex 支持从多种数据源摄取数据。本示例将读取名为
constitution.txt的文本文件(美国宪法文本),文件内容可在此获取。
数据摄取¶
使用 LlamaIndex 的 SimpleDirectoryReader 类将文本文件转换为 Document 对象列表。
In [ ]:
Copied!
%pip install llama-index-llms-openai
%pip install llama-index-vector-stores-rocksetdb
%pip install llama-index-llms-openai
%pip install llama-index-vector-stores-rocksetdb
In [ ]:
Copied!
from llama_index.core import SimpleDirectoryReader
docs = SimpleDirectoryReader(
input_files=["{path to}/consitution.txt"]
).load_data()
from llama_index.core import SimpleDirectoryReader
docs = SimpleDirectoryReader(
input_files=["{path to}/consitution.txt"]
).load_data()
实例化 LLM 和服务上下文。
In [ ]:
Copied!
from llama_index.core import Settings
from llama_index.llms.openai import OpenAI
Settings.llm = OpenAI(temperature=0.8, model="gpt-3.5-turbo")
from llama_index.core import Settings
from llama_index.llms.openai import OpenAI
Settings.llm = OpenAI(temperature=0.8, model="gpt-3.5-turbo")
实例化向量存储和存储上下文。
In [ ]:
Copied!
from llama_index.core import StorageContext
from llama_index.vector_stores.rocksetdb import RocksetVectorStore
vector_store = RocksetVectorStore(collection="llamaindex_demo")
storage_context = StorageContext.from_defaults(vector_store=vector_store)
from llama_index.core import StorageContext
from llama_index.vector_stores.rocksetdb import RocksetVectorStore
vector_store = RocksetVectorStore(collection="llamaindex_demo")
storage_context = StorageContext.from_defaults(vector_store=vector_store)
将文档添加至 llamaindex_demo 集合并创建索引。
In [ ]:
Copied!
from llama_index.core import VectorStoreIndex
index = VectorStoreIndex.from_documents(
docs,
storage_context=storage_context,
)
from llama_index.core import VectorStoreIndex
index = VectorStoreIndex.from_documents(
docs,
storage_context=storage_context,
)
查询功能¶
针对您的文档提出问题并生成相应回答。
In [ ]:
Copied!
response = index.as_query_engine().query("What is the duty of the president?")
print(str(response))
response = index.as_query_engine().query("What is the duty of the president?")
print(str(response))
运行程序。
$ python3 main.py
The duty of the president is to faithfully execute the Office of President of the United States, preserve, protect and defend the Constitution of the United States, serve as the Commander in Chief of the Army and Navy, grant reprieves and pardons for offenses against the United States (except in cases of impeachment), make treaties and appoint ambassadors and other public ministers, take care that the laws be faithfully executed, and commission all the officers of the United States.
元数据过滤¶
元数据过滤功能允许您检索符合特定筛选条件的相关文档。
- 将节点添加至向量存储并创建索引。
In [ ]:
Copied!
from llama_index.vector_stores.rocksetdb import RocksetVectorStore
from llama_index.core import VectorStoreIndex, StorageContext
from llama_index.core.vector_stores.types import NodeWithEmbedding
from llama_index.core.schema import TextNode
nodes = [
NodeWithEmbedding(
node=TextNode(
text="Apples are blue",
metadata={"type": "fruit"},
),
embedding=[],
)
]
index = VectorStoreIndex(
nodes,
storage_context=StorageContext.from_defaults(
vector_store=RocksetVectorStore(collection="llamaindex_demo")
),
)
from llama_index.vector_stores.rocksetdb import RocksetVectorStore
from llama_index.core import VectorStoreIndex, StorageContext
from llama_index.core.vector_stores.types import NodeWithEmbedding
from llama_index.core.schema import TextNode
nodes = [
NodeWithEmbedding(
node=TextNode(
text="Apples are blue",
metadata={"type": "fruit"},
),
embedding=[],
)
]
index = VectorStoreIndex(
nodes,
storage_context=StorageContext.from_defaults(
vector_store=RocksetVectorStore(collection="llamaindex_demo")
),
)
- 定义元数据过滤器。
In [ ]:
Copied!
from llama_index.core.vector_stores import ExactMatchFilter, MetadataFilters
filters = MetadataFilters(
filters=[ExactMatchFilter(key="type", value="fruit")]
)
from llama_index.core.vector_stores import ExactMatchFilter, MetadataFilters
filters = MetadataFilters(
filters=[ExactMatchFilter(key="type", value="fruit")]
)
- 检索符合筛选条件的相关文档。
In [ ]:
Copied!
retriever = index.as_retriever(filters=filters)
retriever.retrieve("What colors are apples?")
retriever = index.as_retriever(filters=filters)
retriever.retrieve("What colors are apples?")
从现有集合创建索引¶
您可以使用现有集合中的数据来创建索引。
In [ ]:
Copied!
from llama_index.core import VectorStoreIndex
from llama_index.vector_stores.rocksetdb import RocksetVectorStore
vector_store = RocksetVectorStore(collection="llamaindex_demo")
index = VectorStoreIndex.from_vector_store(vector_store)
from llama_index.core import VectorStoreIndex
from llama_index.vector_stores.rocksetdb import RocksetVectorStore
vector_store = RocksetVectorStore(collection="llamaindex_demo")
index = VectorStoreIndex.from_vector_store(vector_store)
从新建集合创建索引¶
您也可以新建一个 Rockset 集合作为向量存储库使用。
In [ ]:
Copied!
from llama_index.vector_stores.rocksetdb import RocksetVectorStore
vector_store = RocksetVectorStore.with_new_collection(
collection="llamaindex_demo", # name of new collection
dimensions=1536, # specifies length of vectors in ingest tranformation (optional)
# other RocksetVectorStore args
)
index = VectorStoreIndex(
nodes,
storage_context=StorageContext.from_defaults(vector_store=vector_store),
)
from llama_index.vector_stores.rocksetdb import RocksetVectorStore
vector_store = RocksetVectorStore.with_new_collection(
collection="llamaindex_demo", # name of new collection
dimensions=1536, # specifies length of vectors in ingest tranformation (optional)
# other RocksetVectorStore args
)
index = VectorStoreIndex(
nodes,
storage_context=StorageContext.from_defaults(vector_store=vector_store),
)
配置¶
- collection: 要查询的集合名称(必填项)。
RocksetVectorStore(collection="my_collection")
- workspace: 包含该集合的工作区名称。默认为
"commons"。
RocksetVectorStore(worksapce="my_workspace")
- api_key: 用于 Rockset 请求认证的 API 密钥。若传入
client参数则忽略此选项。默认使用ROCKSET_API_KEY环境变量。
RocksetVectorStore(api_key="<my key>")
- api_server: Rockset 请求使用的 API 服务器地址。若传入
client参数则忽略此选项。默认使用ROCKSET_API_SERVER环境变量,若未设置则默认为"https://api.use1a1.rockset.com"。
from rockset import Regions
RocksetVectorStore(api_server=Regions.euc1a1)
- client: 用于执行 Rockset 请求的客户端对象。若未指定,将根据
api_key参数(或ROCKSET_API_KEY环境变量)和api_server参数(或ROCKSET_API_SERVER环境变量)内部构建客户端对象。
from rockset import RocksetClient
RocksetVectorStore(client=RocksetClient(api_key="<my key>"))
- embedding_col: 存储嵌入向量的数据库字段名称。默认为
"embedding"。
RocksetVectorStore(embedding_col="my_embedding")
- metadata_col: 存储节点元数据的数据库字段名称。默认为
"metadata"。
RocksetVectorStore(metadata_col="node")
- distance_func: 向量关系度量指标。默认为余弦相似度。
RocksetVectorStore(distance_func=RocksetVectorStore.DistanceFunc.DOT_PRODUCT)