Jaguar 向量存储库¶
本文档演示了 llama_index 与 Jaguar 向量存储库的协同工作。
- 这是一个分布式向量数据库,可存储海量向量数据
- ZeroMove 特性支持即时水平扩展
- 支持嵌入向量、文本、图像、视频、PDF、音频、时间序列和空间数据
- 全主架构支持并行读写操作
- 异常检测功能可识别数据集中的离群值
- RAG 支持可将大语言模型与专有实时数据相结合
- 跨多向量索引的元数据共享确保数据一致性
- 距离度量方式包括:欧几里得、余弦、内积、曼哈顿、切比雪夫、汉明、杰卡德和闵可夫斯基
- 相似性搜索支持时间截断和时间衰减效应
先决条件¶
运行本文件示例需要满足两个要求。
必须安装并配置 JaguarDB 服务器及其 HTTP 网关服务器。 请参考 Jaguar 安装指南 进行操作。
必须安装 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 为例进行说明,实际应用中您可以选择其他嵌入模型。
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 端点地址,需替换为您实际环境的配置。pod 参数指定 Pod(或数据库)名称。store 参数表示向量存储的名称,一个 Pod 可包含多个存储。vector_index 参数是该存储中的向量索引名称,单个存储可包含多个向量索引,但存储客户端对象仅绑定到一个特定向量索引。vector_type 参数定义向量索引的属性:在字符串 "cosine_fraction_short" 中,"cosine" 表示使用余弦距离计算向量间距,"fraction" 表示向量分量为分数形式,"short" 表示向量分量存储格式为 16 位有符号短整型。存储格式也可选用 32 位浮点数的 float 类型,或 8 位有符号整数的 byte 类型。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.
清理与登出¶
可以删除向量存储中的所有向量及相关数据,并完全移除该向量存储以完成测试。登出调用确保释放客户端占用的资源。
### 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()