赞
踩
向量数据库是专门用来存储和查询向量的数据库,其存储的向量来自于对文本、语音、图像、视频等的向量化。与传统数据库相比,向量数据库可以处理更多非结构化数据(比如图像和音频)。在机器学习和深度学习中,数据通常以向量形式表示。
- import chromadb
- from chromadb.config import Settings
-
-
- class MyVectorDBConnector:
- def __init__(self, collection_name, embedding_fn):
- chroma_client = chromadb.Client(Settings(allow_reset=True))
-
- # 为了演示,实际不需要每次 reset()
- chroma_client.reset()
-
- # 创建一个 collection
- self.collection = chroma_client.get_or_create_collection(
- name=collection_name)
- self.embedding_fn = embedding_fn
-
- def add_documents(self, documents):
- '''向 collection 中添加文档与向量'''
- self.collection.add(
- embeddings=self.embedding_fn(documents), # 每个文档的向量
- documents=documents, # 文档的原文
- ids=[f"id{i}" for i in range(len(documents))] # 每个文档的 id
- )
-
- def search(self, query, top_n):
- '''检索向量数据库'''
- results = self.collection.query(
- query_embeddings=self.embedding_fn([query]),
- n_results=top_n
- )
- return results
需要使用到《RAG系统(一)》中的extract_text_from_pdf方法和《RAG系统(二)》中的get_embeddings方法。
- # 只取两页
- paragraphs = extract_text_from_pdf(
- "llama2.pdf",
- page_numbers=[2, 3],
- min_line_length=10
- )
- # 创建一个向量数据库对象
- vector_db = MyVectorDBConnector("demo", get_embeddings)
- # 向向量数据库中添加文档
- vector_db.add_documents(paragraphs)
- user_query = "Llama 2有多少参数"
- results = vector_db.search(user_query, 2)
- for para in results['documents'][0]:
- 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.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。