获取从node_sources引用的文档。

huangapple go评论62阅读模式
英文:

llama_index get the document referenced from node_sources

问题

我在使用 llama_index 对已索引的PDF进行检索时获得了良好的结果,但我在找到这些结果所在的PDF文档以便获取答案方面遇到了困难。result.node_sources 使用了一个似乎是内部生成的文档ID。如何获取文档的引用呢?

英文:

I'm getting good results with llama_index having indexed PDFs, however I am having trouble finding which PDF it found the results in to base its answers upon. result.node_sources uses a Doc id which it seems to internally generate. How can I get a reference back to the document?

答案1

得分: 4

以下是翻译好的内容:

直接从Llama团队获取到这个答案 -

感谢您的提问和对LlamaIndex的支持。您可以采取以下几种常见方法:

  • 将元数据注入到每个文档的extra_info中,例如文件名、链接等。许多LlamaHub加载程序应该已经自动将元数据添加到extra_info中,但如果您愿意,也可以自己添加/删除extra_info。这个extra_info会被注入到每个节点中。当您从查询引擎获取响应时,您可以使用response.source_nodes来获取相关的来源。

这些来源将包含原始文本以及元数据。请查看此文档:
https://gpt-index.readthedocs.io/en/stable/core_modules/data_modules/documents_and_nodes/usage_documents.html

  • 假设您已经将适当的元数据添加到extra_info字段中,您可以选择修改查询字符串,或者在查询提示中说一些类似于“请在回答中引用来源”的内容。对于查询字符串,您只需追加它,要自定义提示,请查看https://gpt-index.readthedocs.io/en/latest/how_to/customization/custom_prompts.html
英文:

Got this answer directly from the Llama team -

Thanks for the questions and for your support of LlamaIndex. There are a few general approaches you can do:

  • Inject metadata into the extra_info of each Document, such as file name, link, etc. A lot of LlamaHub loaders should already automatically add metadata into the extra_info, but you can add/remove extra_info yourself if you'd like. This extra_info gets injected into each Node. When you get a response from a query engine, you can do response.source_nodes to fetch the relevant sources.

These sources will contain both the original text as well as the metadata. Take a look at this doc:
https://gpt-index.readthedocs.io/en/stable/core_modules/data_modules/documents_and_nodes/usage_documents.html

  • Assuming you add the appropriate metadata to the extra_info field, you can choose to either modify the query string, or the QA/refine prompts and say something like "Please cite sources along with your answer" in either of those.

The query string you can just append to, for customizing prompts, take a look at https://gpt-index.readthedocs.io/en/latest/how_to/customization/custom_prompts.html

答案2

得分: 1

他们似乎将 'extra_info' 更改为 'metadata'。

我使用了以下代码,它运行得非常完美:

if hasattr(response, 'metadata'):
    document_info = str(response.metadata)
    find = re.findall(r"'page_label': '[^']*', 'file_name': '[^']*'", document_info)

    print('\n' + '=' * 60 + '\n')
    print('上下文信息')
    print(str(find))
    print('\n' + '=' * 60 + '\n')
英文:

It seems that they changed 'extra_info' to 'metadata'.

I used this code and it works perfectly:

    if hasattr(response, 'metadata'):
        document_info = str(response.metadata)
        find = re.findall(r"'page_label': '[^']*', 'file_name': '[^']*'", document_info)

        print('\n'+'=' * 60+'\n')
        print('Context Information')
        print(str(find))
        print('\n'+'=' * 60+'\n')

huangapple
  • 本文由 发表于 2023年5月23日 00:13:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76308107.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定