主题
本文档演示 llama_index 与 Jaguar 向量存储的配合使用。
- 它是一个分布式向量数据库,可以存储大量向量。
- ZeroMove 功能可实现即时水平扩展。
- 它支持嵌入、文本、图像、视频、PDF、音频、时间序列和空间数据。
- 全主架构允许并行读写。
- 其异常检测能力可以区分数据集中的异常值。
- RAG 支持可以结合 LLM 与专有数据和实时数据。
- 跨多个向量索引共享元数据可提高数据一致性。
- 距离度量包括 Euclidean、Cosine、InnerProduct、Manhatten、Chebyshev、Hamming、Jeccard 和 Minkowski。
- 相似度搜索可以结合时间截止和时间衰减效应进行。
前置条件
运行本文件中的示例有两项要求。
你必须安装并设置 JaguarDB 服务器及其 HTTP 网关服务器。请参考 Jaguar Setup 中的说明。
你必须安装 llama-index 和 jaguardb-http-client 包。
方法一:Docker
docker pull jaguardb/jaguardb
docker run -d -p 8888:8888 -p 8080:8080 --name jaguardb jaguardb/jaguardb
pip install -U llama-index
pip install -U jaguardb-http-client方法二:快速设置(Linux)
curl -fsSL http://jaguardb.com/install.sh | sh\n
pip install -U llama-index
pip install -U jaguardb-http-client%pip install llama-index-vector-stores-jaguar!pip install -U jaguardb-http-clientCollecting 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 为例。你可以在应用中选择其他嵌入模型。
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 可以有多个 store。vector_index 是 store 中向量索引的名称。一个 store 可以有多个向量索引。不过,store 客户端对象仅绑定到一个向量索引。vector_type 指定向量索引的属性。在字符串 “cosine_fraction_short” 中,cosine 表示两个向量之间的距离使用余弦距离计算。Fraction 表示向量分量是小数。Short 表示向量分量的存储格式是 16 位有符号短整数。存储格式也可以是 32 位浮点数。它也可以是 8 位有符号整数的字节。vector_dimension 是所提供的嵌入模型生成的向量维度。
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()