当前位置:   article > 正文

RAG系统(三)向量数据库_hroma的向量存储器

hroma的向量存储器

RAG系统(一)系统介绍与向量检索

RAG系统(二)文档的加载与分段

向量数据库

向量数据库是专门用来存储和查询向量的数据库,其存储的向量来自于对文本、语音、图像、视频等的向量化。与传统数据库相比,向量数据库可以处理更多非结构化数据(比如图像和音频)。在机器学习和深度学习中,数据通常以向量形式表示。

chromadb向量数据库使用示例

  1. import chromadb
  2. from chromadb.config import Settings
  3. class MyVectorDBConnector:
  4. def __init__(self, collection_name, embedding_fn):
  5. chroma_client = chromadb.Client(Settings(allow_reset=True))
  6. # 为了演示,实际不需要每次 reset()
  7. chroma_client.reset()
  8. # 创建一个 collection
  9. self.collection = chroma_client.get_or_create_collection(
  10. name=collection_name)
  11. self.embedding_fn = embedding_fn
  12. def add_documents(self, documents):
  13. '''向 collection 中添加文档与向量'''
  14. self.collection.add(
  15. embeddings=self.embedding_fn(documents), # 每个文档的向量
  16. documents=documents, # 文档的原文
  17. ids=[f"id{i}" for i in range(len(documents))] # 每个文档的 id
  18. )
  19. def search(self, query, top_n):
  20. '''检索向量数据库'''
  21. results = self.collection.query(
  22. query_embeddings=self.embedding_fn([query]),
  23. n_results=top_n
  24. )
  25. return results

调用示例

需要使用到《RAG系统(一)》中的extract_text_from_pdf方法和《RAG系统(二)》中的get_embeddings方法。

  1. # 只取两页
  2. paragraphs = extract_text_from_pdf(
  3. "llama2.pdf",
  4. page_numbers=[2, 3],
  5. min_line_length=10
  6. )
  7. # 创建一个向量数据库对象
  8. vector_db = MyVectorDBConnector("demo", get_embeddings)
  9. # 向向量数据库中添加文档
  10. vector_db.add_documents(paragraphs)
  11. user_query = "Llama 2有多少参数"
  12. results = vector_db.search(user_query, 2)
  13. for para in results['documents'][0]:
  14. print(para+"\n")

输出结果:

1. Llama 2, an updated version of Llama 1, trained on a new mix of publicly available data. We also increased the size of the pretraining corpus by 40%, doubled the context length of the model, and adopted grouped-query attention (Ainslie et al., 2023). We are releasing variants of Llama 2 with 7B, 13B, and 70B parameters. We have also trained 34B variants, which we report on in this paper but are not releasing.§

 In this work, we develop and release Llama 2, a family of pretrained and fine-tuned LLMs, Llama 2 and Llama 2-Chat, at scales up to 70B parameters. On the series of helpfulness and safety benchmarks we tested, Llama 2-Chat models generally perform better than existing open-source models. They also appear to be on par with some of the closed-source models, at least on the human evaluations we performed (see Figures 1 and 3). We have taken measures to increase the safety of these models, using safety-specific data annotation and tuning, as well as conducting red-teaming and employing iterative evaluations. Additionally, this paper contributes a thorough description of our fine-tuning methodology and approach to improving LLM safety. We hope that this openness will enable the community to reproduce fine-tuned LLMs and continue to improve the safety of those models, paving the way for more responsible development of LLMs. We also share novel observations we made during the development of Llama 2 and Llama 2-Chat, such as the emergence of tool usage and temporal organization of knowledge.

关键概念:

  • 向量数据库的意义是快速的检索;
  • 向量数据库本身不生成向量,向量是由 Embedding 模型产生的;
  • 向量数据库与传统的关系型数据库是互补的,不是替代关系,在实际应用中根据实际需求经常同时使用。
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/791114
推荐阅读
相关标签
  

闽ICP备14008679号