Faiss 读取器¶
如果您在colab上打开这个笔记本,您可能需要安装LlamaIndex 🦙。
In [ ]:
Copied!
%pip install llama-index-readers-faiss
%pip install llama-index-readers-faiss
In [ ]:
Copied!
!pip install llama-index
!pip install llama-index
In [ ]:
Copied!
import logging
import sys
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
import logging
import sys
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
In [ ]:
Copied!
from llama_index.readers.faiss import FaissReader
from llama_index.readers.faiss import FaissReader
In [ ]:
Copied!
# 构建Faiss索引。# 有关如何开始使用Faiss的指南,请参阅:https://github.com/facebookresearch/faiss/wiki/Getting-started# 我们在下面提供了一些示例代码。import faiss# # 示例代码# d = 8# docs = np.array([# [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1],# [0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2],# [0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3],# [0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4],# [0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]# ])# # id_to_text_map 用于查询检索# id_to_text_map = {# 0: "aaaaaaaaa bbbbbbb cccccc",# 1: "foooooo barrrrrr",# 2: "tmp tmptmp tmp",# 3: "hello world hello world",# 4: "cat dog cat dog"# }# # 构建索引# index = faiss.IndexFlatL2(d)# index.add(docs)id_to_text_map = { "id1": "文本块1", "id2": "文本块2",}index = ...
# 构建Faiss索引。# 有关如何开始使用Faiss的指南,请参阅:https://github.com/facebookresearch/faiss/wiki/Getting-started# 我们在下面提供了一些示例代码。import faiss# # 示例代码# d = 8# docs = np.array([# [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1],# [0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2],# [0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3],# [0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4],# [0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]# ])# # id_to_text_map 用于查询检索# id_to_text_map = {# 0: "aaaaaaaaa bbbbbbb cccccc",# 1: "foooooo barrrrrr",# 2: "tmp tmptmp tmp",# 3: "hello world hello world",# 4: "cat dog cat dog"# }# # 构建索引# index = faiss.IndexFlatL2(d)# index.add(docs)id_to_text_map = { "id1": "文本块1", "id2": "文本块2",}index = ...
In [ ]:
Copied!
reader = FaissReader(index)
reader = FaissReader(index)
In [ ]:
Copied!
# 从Faiss索引加载数据时,必须指定:# k:最近邻居的数量# query:查询的二维嵌入表示(行是查询)k = 4query1 = np.array([...])query2 = np.array([...])query = np.array([query1, query2])documents = reader.load_data(query=query, id_to_text_map=id_to_text_map, k=k)
# 从Faiss索引加载数据时,必须指定:# k:最近邻居的数量# query:查询的二维嵌入表示(行是查询)k = 4query1 = np.array([...])query2 = np.array([...])query = np.array([query1, query2])documents = reader.load_data(query=query, id_to_text_map=id_to_text_map, k=k)
创建索引¶
In [ ]:
Copied!
index = SummaryIndex.from_documents(documents)
index = SummaryIndex.from_documents(documents)
In [ ]:
Copied!
# 将日志级别设置为DEBUG,以获得更详细的输出query_engine = index.as_query_engine()response = query_engine.query("<query_text>")
# 将日志级别设置为DEBUG,以获得更详细的输出query_engine = index.as_query_engine()response = query_engine.query("")
In [ ]:
Copied!
display(Markdown(f"<b>{response}</b>"))
display(Markdown(f"{response}"))