Rockset 向量存储¶
作为一款实时搜索和分析数据库,Rockset 利用索引技术提供可扩展且高性能的个性化推荐、产品搜索、语义搜索、聊天机器人应用等功能。由于 Rockset 专为实时数据构建,您可以在持续更新、流式传输的数据上构建这些响应式应用。通过将 Rockset 与 LlamaIndex 集成,您可以轻松地在自己的实时数据上使用大型语言模型(LLMs),以构建可用于生产环境的向量搜索应用。
我们将演示如何在 LlamaIndex 中将 Rockset 用作向量存储。
教程¶
在本例中,我们将使用 OpenAI 的 text-embedding-ada-002
模型生成 embedding(嵌入),并使用 Rockset 作为向量存储来存储 embedding。我们将从文件中摄取文本,并询问有关内容的问题。
环境设置¶
- 使用 Write API 作为源,从 Rockset 控制台创建一个集合 (collection)。将您的集合命名为
llamaindex_demo
。使用VECTOR_ENFORCE
配置以下摄取转换 (ingest transformation),以定义您的 embedding 字段并利用性能和存储优化。
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 [ ]
已复制!
%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 [ ]
已复制!
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 [ ]
已复制!
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 [ ]
已复制!
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 [ ]
已复制!
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 [ ]
已复制!
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 [ ]
已复制!
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 [ ]
已复制!
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 [ ]
已复制!
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 [ ]
已复制!
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 [ ]
已复制!
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", # 新集合的名称 dimensions=1536, # 指定摄取转换中向量的长度 (可选) # 其他 RocksetVectorStore 参数 ) 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_KEY
环境变量;如果未设置ROCKSET_API_SERVER
,则默认为"https://api.use1a1.rockset.com"
。
from rockset import Regions
RocksetVectorStore(api_server=Regions.euc1a1)
- client: 用于执行 Rockset 请求的 Rockset 客户端对象。如果未指定,则会使用
api_key
参数(或ROCKSET_API_SERVER
环境变量)和api_server
参数(或ROCKSET_API_SERVER
环境变量)在内部构建一个客户端对象。
from rockset import RocksetClient
RocksetVectorStore(client=RocksetClient(api_key="<my key>"))
- embedding_col: 包含 embedding 的数据库字段名称。默认为
"embedding"
。
RocksetVectorStore(embedding_col="my_embedding")
- metadata_col: 包含节点数据的数据库字段名称。默认为
"metadata"
。
RocksetVectorStore(metadata_col="node")
- distance_func: 用于衡量向量关系的度量标准。默认为余弦相似度。
RocksetVectorStore(distance_func=RocksetVectorStore.DistanceFunc.DOT_PRODUCT)