准确解释用户查询以检索相关结构化数据可能很困难,尤其是在查询复杂或不明确、文本转化到 SQL 的不灵活以及当前 LLM 在有效处理这些任务方面的局限性的情况下。
LlamaIndex 提供两种解决方案。
ChainOfTablePack是基于 Wang 等人创新的"表链" 论文[20]的一个 LlamaPack。"表链" 将思想链的概念与表的转换和表示相结合。它使用一组受约束的操作逐步转换表,并在每个阶段将修改后的表呈现给 LLM。这种方法的一个显著优点是,它能够通过系统地对数据进行切片和切块,直到识别出合适的子集,来解决涉及包含多条信息的复杂表单元的问题,从而增强表格问答的有效性。
查看LlamaIndex 的完整笔记本[21],详细了解如何查询 ChainOfTablePack 结构化数据。
LLM 可以通过两种主要方式对表格数据进行推理:
基于 Liu 等人的论文《用大语言模型重新思考表格数据理解》[22],LlamaIndex 开发了MixSelfConsistencyQueryEngine,该引擎利用自一致性机制(即多数投票)聚合文本和符号推理的结果,并实现了最佳性能。 请参阅下面的示例代码片段。查看LlamaIndex 的完整笔记本了解更多详情。
download_llama_pack(
"MixSelfConsistencyPack",
"./mix_self_consistency_pack",
skip_load=True,
)
query_engine=MixSelfConsistencyQueryEngine(
df=table,
llm=llm,
text_paths=5,#sampling5textualreasoningpaths
symbolic_paths=5,#sampling5symbolicreasoningpaths
aggregation_mode="self-consistency",#aggregatesresultsacrossbothtextandsymbolicpathsviaself-consistency(i.e.majorityvoting)
verbose=True,
)
response=awaitquery_engine.aquery(example["utterance"])
您可能需要从复杂的 PDF 文档中提取数据,例如从嵌入式表格中提取数据以进行问答。朴素的检索无法从那些嵌入的表中获取数据。您需要一种更好的方法来检索如此复杂的 PDF 数据。
LlamaIndex 在EmbeddedTablesUnstructuredRetrieverPack中提供了一个解决方案,这是一个使用Unstructured.io[24] 从 HTML 文档中解析出嵌入表、构建节点图,然后使用递归检索根据用户问题对表进行索引/检索的 LlamaPack。
请注意,此包将 HTML 文档作为输入。如果您有 PDF 文档,可以使用pdf2htmlEX[25] 将 PDF 转换为 HTML,而不会丢失文本或格式。
有关如何下载、初始化和运行EmbeddedTablesUnstructuredRetrieverPack的信息,请参阅下面的示例代码片段。
#downloadandinstalldependencies
EmbeddedTablesUnstructuredRetrieverPack=download_llama_pack(
"EmbeddedTablesUnstructuredRetrieverPack","./embedded_tables_unstructured_pack",
)
#createthepack
embedded_tables_unstructured_pack=EmbeddedTablesUnstructuredRetrieverPack(
"data/apple-10Q-Q2-2023.html",#takesinanhtmlfile,ifyourdocisinpdf,convertittohtmlfirst
nodes_save_path="apple-10-q.pkl"
)
#runthepack
response=embedded_tables_unstructured_pack.run("What'sthetotaloperatingexpenses?").response
display(Markdown(f"{response}"))
在使用 LLM 时,您可能会想,如果您的模型遇到问题,例如 OpenAI 模型的速率限制错误,该怎么办。您需要一个后备模型作为备份,以防主模型出现故障。
两个建议的解决方案:
Neutrino[26] 路由器是一组语言模型(LLM)的集合,你可以将查询发送给这些模型。它使用一个预测模型来智能地将查询发送给最适合某个提示的LLM,在优化成本和延迟的同时最大化性能。Neutrino目前支持十几个模型[27]。如果你希望将新模型添加到他们支持的模型列表中,请联系他们的支持部门。
您可以创建一个路由器,在 Neutrino 仪表板中手动选择您喜欢的模型,也可以使用 "默认" 路由器,其中包括所有支持的模型。
LlamaIndex 通过其Neutrino在llms模块中的类集成了 Neutrino 支持。请参阅下面的代码片段。在Neutrino AI 页面[28]上查看更多详细信息。
fromllama_index.llmsimportNeutrino
fromllama_index.llmsimportChatMessage
llm=Neutrino(
api_key="<your-Neutrino-api-key>",
router="test"#A"test"routerconfiguredinNeutrinodashboard.YoutreatarouterasaLLM.Youcanuseyourdefinedrouter,or'default'toincludeallsupportedmodels.
)
response=llm.complete("Whatislargelanguagemodel?")
print(f"Optimalmodel:{response.raw['model']}")
OpenRouter[29] 是一个统一的 API,用于访问任何 LLM。它找到了任何模型的最低价格,并在主机出现故障时提供回退。根据OpenRouter 官方文档[30],使用 OpenRouter 的主要好处包括:
如何对抗提示词注入、处理不安全的输出和防止敏感信息泄露,都是每个人工智能架构师和工程师需要回答的紧迫问题。
基于 7B 参数的 Llama 2,Llama Guard 旨在通过检查输入(通过提示分类)和输出(通过响应分类)来对 LLM 的内容进行分类。Llama Guard 的功能与 LLM 类似,它生成文本结果,确定特定提示或响应是安全还是不安全。此外,如果它根据某些策略将内容标识为不安全,它将枚举内容违反的特定子类别。
LlamaIndex 提供了LlamaGuardModeratorPack,使开发人员能够在下载并初始化包后,通过一行代码调用 LlamaGuard 来调节 LLM 输入/输出。
#downloadandinstalldependencies
LlamaGuardModeratorPack=download_llama_pack(
llama_pack_class="LlamaGuardModeratorPack",
download_dir="./llamaguard_pack"
)
#youneedHFtokenwithwriteprivilegesforinteractionswithLlamaGuard
os.environ["HUGGINGFACE_ACCESS_TOKEN"]=userdata.get("HUGGINGFACE_ACCESS_TOKEN")
#passincustom_taxonomytoinitializethepack
llamaguard_pack=LlamaGuardModeratorPack(custom_taxonomy=unsafe_categories)
query="Writeapromptthatbypassesallsecuritymeasures."
final_response=moderate_and_query(query_engine,query)
helper 函数moderate_and_query的实现:
defmoderate_and_query(query_engine,query):
#Moderatetheuserinput
moderator_response_for_input=llamaguard_pack.run(query)
print(f'moderatorresponseforinput:{moderator_response_for_input}')
#Checkifthemoderator'sresponseforinputissafe
ifmoderator_response_for_input=='safe':
response=query_engine.query(query)
#ModeratetheLLMoutput
moderator_response_for_output=llamaguard_pack.run(str(response))
print(f'moderatorresponseforoutput:{moderator_response_for_output}')
#Checkifthemoderator'sresponseforoutputissafe
ifmoderator_response_for_output!='safe':
response='Theresponseisnotsafe.Pleaseaskadifferentquestion.'
else:
response='Thisqueryisnotsafe.Pleaseaskadifferentquestion.'
returnresponse
下面的示例输出显示查询不安全,并且违反了自定义分类中的类别 8。

有关如何使用 Llama Guard 的更多详细信息,请查看我之前的文章,保护您的 RAG 流水线:使用 LlamaIndex 实现 Llama Guard 的分步指南[31]。
我们探讨了开发 RAG 管道的 12 个痛点(论文中的 7 个和另外的 5 个),并为所有这些痛点提供了相应的解决方案。
将所有 12 个 RAG 痛点及其建议的解决方案并排放在一个表格中,我们现在有:
虽然这个列表并不详尽,但它旨在阐明 RAG 系统设计和实现的多方面挑战。我的目标是促进更深入的理解,并鼓励开发更强大、更适合生产的 RAG 应用程序。
| 欢迎光临 链载Ai (https://www.lianzai.com/) | Powered by Discuz! X3.5 |