Jaguar Vector Store¶
本文档演示了 llama_index 如何与 Jaguar 向量存储协同工作。
- 它是一个分布式向量数据库,可以存储大量的向量。
- ZeroMove 功能支持即时水平扩展。
- 它支持 Embedding、文本、图像、视频、PDF、音频、时间序列和空间数据。
- 全主架构支持并行读写。
- 其异常检测功能可以区分数据集中的离群值。
- RAG 支持可以结合大型语言模型、专有数据和实时数据。
- 在多个向量索引之间共享元数据提高了数据一致性。
- 距离度量包括 Euclidean、Cosine、InnerProduct、Manhatten、Chebyshev、Hamming、Jeccard 和 Minkowski。
- 相似度搜索可以结合时间截止和时间衰减效果进行。
前置条件¶
运行此文件中的示例需要满足两个条件。
您必须安装并设置 JaguarDB 服务器及其 HTTP 网关服务器。请参考 Jaguar Setup 中的说明。
您必须安装 llama-index 和 jaguardb-http-client 包。
docker pull jaguardb/jaguardb_with_http
docker run -d -p 8888:8888 -p 8080:8080 --name jaguardb_with_http jaguardb/jaguardb_with_http
pip install -U llama-index
pip install -U jaguardb-http-client
%pip install llama-index-vector-stores-jaguar
!pip install -U jaguardb-http-client
Collecting jaguardb-http-client Using cached jaguardb_http_client-3.4.1-py2.py3-none-any.whl (15 kB) Installing collected packages: jaguardb-http-client Successfully installed jaguardb-http-client-3.4.1
导入¶
应该导入以下包。我们使用 OpenAIEmbedding 作为示例。您可以在您的应用程序中选择其他 Embedding 模型。
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.core import StorageContext
from llama_index.vector_stores.jaguar import JaguarVectorStore
from jaguardb_http_client.JaguarHttpClient import JaguarHttpClient
客户端对象¶
现在我们实例化一个 jaguar 向量存储客户端对象。url 是网关服务器的 http 端点。url 应替换为您的环境设置。pod 是 Pod(或数据库)的名称。store 是向量存储的名称。一个 Pod 可以有多个存储。vector_index 是存储中向量索引的名称。一个存储可以有多个向量索引。然而,存储客户端对象只绑定到一个向量索引。vector_type 指定向量索引的属性。在字符串 "cosine_fraction_short" 中,cosine 表示使用余弦距离计算两个向量之间的距离。Fraction 表示向量分量是小数。Short 表示向量分量的存储格式是带符号的 16 位短整型。存储格式可以是 32 位浮点型的 float。它也可以是 8 位带符号整型的 byte。vector_dimension 是由提供的 Embedding 模型生成的向量的维度。
url = "http://127.0.0.1:8080/fwww/"
pod = "vdb"
store = "llamaindex_jaguar_store"
vector_index = "v"
vector_type = "cosine_fraction_float"
# vector_type = "cosine_fraction_short" # half of memory usage compared to float
# vector_type = "cosine_fraction_byte" # quarter of memory usage compared to float
vector_dimension = 1536 # per OpenAIEmbedding model
jaguarstore = JaguarVectorStore(
pod,
store,
vector_index,
vector_type,
vector_dimension,
url,
)
认证¶
客户端必须登录或连接到后端 jaguar 服务器以进行系统安全和用户认证。环境变量 JAGUAR_API_KEY 或文件 $HOME/.jagrc 必须包含系统管理员颁发的 jaguar api 密钥。login() 方法返回 True 或 False。如果返回 False,则可能意味着您的 jaguar api 密钥无效,或者 http 网关服务器未运行,或者 jaguar 服务器未正常运行。
true_or_false = jaguarstore.login()
print(f"login result is {true_or_false}")
login result is True
创建向量存储¶
现在我们创建一个向量存储,其中包含一个大小为 1024 字节的 'v:text' 字段用于存放文本,以及两个额外的元数据字段 'author' 和 'category'。
metadata_str = "author char(32), category char(16)"
text_size = 1024
jaguarstore.create(metadata_str, text_size)
加载文档¶
以下代码打开示例 Paul Gram 文档并将其读入内存
documents = SimpleDirectoryReader("../data/paul_graham/").load_data()
print(f"loading {len(documents)} doument(s)")
loading 1 doument(s)
构建索引¶
准备存储上下文、服务上下文并创建一个索引对象。调用 from_documents() 后,向量存储中将保存 22 个向量。
### make a storage context using our vector store
storage_context = StorageContext.from_defaults(vector_store=jaguarstore)
### clear all vectors in the vector store
jaguarstore.clear()
### make an index with the documents,storage context
index = VectorStoreIndex.from_documents(
documents, storage_context=storage_context
)
### You could add more documents to the vector store:
# jaguarstore.add_documents(some_docs)
# jaguarstore.add_documents(more_docs, text_tag="tag to these documents")
### print number of documents in jaguar vector store
num = jaguarstore.count()
print(f"There are {num} vectors in jaguar vector store")
There are 22 vectors in jaguar vector store
提问¶
我们获取一个查询引擎并向其提出一些问题。
query_engine = index.as_query_engine()
q = "What did the author do growing up?"
print(f"Question: {q}")
response = query_engine.query(q)
print(f"Answer: {str(response)}\n")
q = "What did the author do after his time at Viaweb?"
print(f"Question: {q}")
response = query_engine.query(q)
print(f"Answer: {str(response)}")
Question: What did the author do growing up? Answer: The author mentioned that growing up, they worked on two main things outside of school: writing and programming. They wrote short stories and tried writing programs on an IBM 1401 computer. Question: What did the author do after his time at Viaweb? Answer: After his time at Viaweb, the author started a company to put art galleries online. However, this idea did not turn out to be successful as art galleries did not want to be online.
传递查询选项¶
我们可以向查询引擎传递额外的参数,以便从 jaguar 向量存储中只选择一部分数据。这可以通过使用 vector_store_kwargs
参数实现。参数 day_cutoff 是文本将被忽略的天数上限。day_decay_rate 是相似度评分的每日衰减率。
qkwargs = {
"args": "day_cutoff=365,day_decay_rate=0.01",
"where": "category='startup' or category=''",
}
query_engine_filter = index.as_query_engine(vector_store_kwargs=qkwargs)
q = "What was the author's life style?"
print(f"Question: {q}")
response = query_engine_filter.query(q)
print(f"Answer: {str(response)}")
Question: What was the author's life style? Answer: The author's lifestyle involved attending the Accademia as a student and painting still lives in their bedroom at night. They also wrote essays and had a messy life, which they thought would be interesting and encouraging to others.
清理和退出¶
向量存储中的所有向量和相关数据都可以删除,向量存储也可以完全移除以完成测试。logout 调用确保客户端使用的资源被释放。
### remove all the data in the vector store if you want
jaguarstore.clear()
### delete the whole vector in the database if you want
jaguarstore.drop()
### disconnect from jaguar server and cleanup resources
jaguarstore.logout()