捷豹向量存储
本文档演示了llama_index与Jaguar向量存储库的协同工作。
- 它是一个分布式向量数据库,能够存储大量向量。
- ZeroMove功能支持即时水平扩展。
- 它支持嵌入、文本、图像、视频、PDF、音频、时间序列和空间数据。
- 全主架构允许并行读取和写入。
- 其异常检测能力能够识别数据集中的异常值。
- RAG支持可以将LLM与专有及实时数据相结合。
- 跨多个向量索引共享元数据可提高数据一致性。
- 距离度量包括欧几里得、余弦、内积、曼哈顿、切比雪夫、汉明、杰卡德和闵可夫斯基。
- 相似性搜索可以执行时间截止和时间衰减效果。
运行此文件中的示例有两个要求。
您必须安装并设置JaguarDB服务器及其HTTP网关服务器。 请参考Jaguar设置中的说明进行操作。
您必须安装 llama-index 和 jaguardb-http-client 软件包。
方法一:Docker
docker pull jaguardb/jaguardbdocker run -d -p 8888:8888 -p 8080:8080 --name jaguardb jaguardb/jaguardbpip install -U llama-indexpip install -U jaguardb-http-client方法二:快速安装(Linux)
curl -fsSL http://jaguardb.com/install.sh | sh\npip install -U llama-indexpip 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-clientSuccessfully installed jaguardb-http-client-3.4.1以下包应当被导入。我们以 OpenAIEmbedding 为例。您可以在应用中选择其他嵌入模型。
from llama_index.core import VectorStoreIndex, SimpleDirectoryReaderfrom llama_index.core import StorageContextfrom llama_index.vector_stores.jaguar import JaguarVectorStorefrom 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是由提供的嵌入模型生成的向量维度。
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 floatvector_dimension = 1536 # per OpenAIEmbedding modeljaguarstore = 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 = 1024jaguarstore.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 storestorage_context = StorageContext.from_defaults(vector_store=jaguarstore)
### clear all vectors in the vector storejaguarstore.clear()
### make an index with the documents,storage contextindex = 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 storenum = 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 wantjaguarstore.clear()
### delete the whole vector in the database if you wantjaguarstore.drop()
### disconnect from jaguar server and cleanup resourcesjaguarstore.logout()