LLM之RAG实战(二):使用LlamaIndex + Metaphor实现知识工作自动化

      最先进的大型语言模型(LLM),如ChatGPT、GPT-4、Claude 2,具有令人难以置信的推理能力,可以解锁各种用例——从洞察力提取到问答,再到通用工作流自动化。然而,他们检索上下文相关信息的能力有限。检索增强生成(RAG)系统可以将LLM与静态知识源上的外部存储解决方案相结合。

        RAG通常需要两个核心组件

  1. 通用抽象,允许LLM以“读取”和“写入”的方式智能地对数据执行各种任务;

  2. 一个适合LLM使用的好搜索引擎

      LlamaIndex数据代理抽象有助于满足第一个核心组件。一个完整的数据代理由一个推理循环和一组工具组成。这些工具可以是用于搜索/检索的接口,或者更一般地是任何外部API。给定一个查询,代理将执行其推理循环,并动态地计算出完成手头任务所需的工具集。

       数据代理可以访问LlamaHub上提供的一套丰富的工具,从Gmail API到SQL数据库API,再到Bing搜索形式的基本工具。我们已经证明,他们能够执行e2e任务,从发送电子邮件、安排会议到自动化定制支持洞察力提取。然而,从来没有专门为LLM使用而设计的工具。

      本文将介绍LlamaIndex和Metaphor的集成来实现RAG:将LlamaIndex数据代理的功能与Metaphor作为一种本地LLM搜索工具相结合,使知识工作者能够回答任何数据上的任何问题,无论是最近的还是复杂的。示例可以参考:https://github.com/emptycrown/llama-hub/blob/main/llama_hub/tools/notebooks/metaphor.ipynb

一、Metaphor介绍

      Metaphor API旨在将你的LLM连接到互联网,它允许你在互联网上进行完全神经化、高度语义化的搜索,还可以从结果中获得干净的HTML内容。

       根据人们在互联网上谈论事物的方式,Metaphor被训练来预测互联网上的链接。例如,有人可能会这样发布他们读到的一篇很棒的文章:

Found an amazing article I read about the history of Rome’s architecture: [LINK]

       通过训练一个模型来预测人们谈论这些链接的方式,最终的结果是一种完全不同的互联网搜索方式——就像你要分享你想要的链接一样进行搜索。虽然一开始有点不直观,但以这种方式搜索可以返回极高质量的结果。但就LlamaIndex而言,您不必担心这一点,因为默认情况下,查询将转换为Metaphor Prompt。

为什么你会在Bing/Google上使用Metaphor搜索?主要有三个原因:

  • 您可以完全从语义上进行搜索,例如使用感觉或复杂的描述符;
  • 您只能搜索所需实体的类型。公司、文章、人;
  • 你可能会发现谷歌的内容表现不佳,可能是因为关键词不是正确的工具,也可能只是因为谷歌不在乎为这类内容返回好的结果。

PS:要了解更多信息,您可以阅读完整的Metaphor API博客文章(https://platform.metaphor.systems/blog/building-search-for-the-post-chatgpt-world)

二、LlamaIndex和Metaphor的集成原理

LlamaHub提供了Metaphor API接口,包括如下5个工具可供Agent使用。

  • 搜索:是Metaphor的入口——Agent可以通过自然语言向Metaphor搜索引擎进行查询。查询还可以包含一些附加参数,例如返回结果的数量、要包含/排除的领域以及日期筛选器;
  • 检索文档:根据搜索到的文档内容从中检索出符合条件的部分内容;
  • 搜索和检索文档:结合了“搜索”和“检索文档”的功能;
  • 查找相似:直接调用Metaphor提供的端点,可以返回与给定URL相似的文档列表;
  • 当前日期:这是一个返回当前日期的函数。就其本身而言,它与Metaphor的API无关,但可能会事先调用它,以确定传递到Metaphor的某些端点的正确日期过滤器。

在下一节中,让我们了解数据代理如何通过各种用例使用这些端点。

三、LlamaIndex和Metaphor集成示例

让我们看一下LlamaIndex数据Agent是如何与Metaphor一起使用的。

3.1 Metaphor工具测试

第一步是导入MetaphorToolSpec:

# Set up Metaphor toolfrom llama_hub.tools.metaphor.base import MetaphorToolSpecmetaphor_tool = MetaphorToolSpec(api_key='your-key',)# convert tool spec to a list of toolsmetaphor_tool_list = metaphor_tool.to_tool_list()for tool in metaphor_tool_list:print(tool.metadata.name)

输入

metaphor_tool.search('machine learning transformers', num_results=3)

输出

[{'title': 'On the potential of Transformers in Reinforcement Learning','url': 'https://lorenzopieri.com/rl_transformers/','id': 'ysJlYSgeGW3l4zyOBoSGcg'},{'title': 'Transformers: Attention in Disguise','url': 'https://www.mihaileric.com/posts/transformers-attention-in-disguise/','id': 'iEYMai5rS9k0hN5_BH0VZg'},{'title': 'Transformers in Computer Vision: Farewell Convolutions!','url': 'https://towardsdatascience.com/transformers-in-computer-vision-farewell-convolutions-f083da6ef8ab?gi=a1d0a9a2896c','id': 'kX1Z89DdjSvBrH1S1XLvwg'}]

3.2 使用Metaphor设置OpenAI Agent

我们可以创建一个可以访问上述所有工具的代理,并开始测试它:

from llama_index.agent import OpenAIAgent# We don't give the Agent our unwrapped retrieve document tools, instead passing the wrapped toolsagent = OpenAIAgent.from_tools(  metaphor_tool_list,  verbose=True,)

下面看一个直接查询的例子:

print(agent.chat('What are the best restaurants in toronto?"))

了解一下该例子中Metaphor工具的执行细节:

=== Calling Function ===Calling function: search with args: {  "query": "best restaurants in Toronto"}[Metaphor Tool] Autoprompt string: Here's a link to the best restaurant in Toronto:Got output: [{'title': 'Via Allegro Ristorante - Toronto Fine Dining Restaurant', 'url': 'https://viaallegroristorante.com/', 'id': 'EVlexzJh-lzkVr4tb2y_qw'}, {'title': 'The Senator – Home', 'url': 'https://thesenator.com/', 'id': 'dA3HVr5P8E0Bs7nH2gH7ZQ'}, {'title': 'Home - The Rushton', 'url': 'https://therushton.com/', 'id': '6Je-igG-i-ApqISC5XXmGQ'}, {'title': 'Location', 'url': 'https://osteriagiulia.ca/', 'id': 'HjP5c54vqb3n3UNa3HevSA'}, {'title': 'StockYards | Stockyards Toronto', 'url': 'https://www.thestockyards.ca/', 'id': 'Pffz-DQlOepqVgKQDmW5Ig'}, {'title': 'Select A Restaurant', 'url': 'https://www.torontopho.com/', 'id': 'DiQ1hU1gmrIzpKnOaVvZmw'}, {'title': 'Home | Kit Kat Italian Bar & Grill', 'url': 'http://www.kitkattoronto.com/', 'id': 'kdAcLioBgnwzuHyd0rWS1w'}, {'title': 'La Fenice', 'url': 'https://www.lafenice.ca/', 'id': 'M-LHQZP6V40V81fqLFAQxQ'}, {'title': 'Le Phénix', 'url': 'https://www.lephenixto.com/', 'id': 'spCTcFr0GHlFUTzyngfRVw'}, {'title': 'ITALIAN, INSPIRED.', 'url': 'https://figotoronto.com/', 'id': 'OvBcTqEo1tCSywr4ATptCg'}]========================Here are some of the best restaurants in Toronto:1. [Via Allegro Ristorante](https://viaallegroristorante.com/)2. [The Senator](https://thesenator.com/)3. [The Rushton](https://therushton.com/)4. [Osteria Giulia](https://osteriagiulia.ca/)5. [Stockyards](https://www.thestockyards.ca/)6. [Toronto Pho](https://www.torontopho.com/)7. [Kit Kat Italian Bar & Grill](http://www.kitkattoronto.com/)8. [La Fenice](https://www.lafenice.ca/)9. [Le Phénix](https://www.lephenixto.com/)10. [Figo](https://figotoronto.com/)You can visit their websites for more information. Enjoy your dining experience in Toronto!

可以看到agent执行了”search“操作,结果返回了Toronto最好的饭店列表。

继续追问进行多轮对话:

print(agent.chat('tell me more about Osteria Giulia'))
=== Calling Function ===Calling function: retrieve_documents with args: {"ids": ["HjP5c54vqb3n3UNa3HevSA"]}Got output: […]========================Osteria Giulia is a restaurant located at 134 Avenue Road in Toronto, Ontario. You can contact them at 416.964.8686 or via email at info@osteriagiulia.ca (for general inquiries only, no reservation requests via email).The restaurant's operating hours are from Monday to Saturday, from 5:00pm to 11:00pm. On Sundays, the restaurant is available for private bookings.Parking is available on Avenue Road and Davenport Road.You can follow Osteria Giulia on Instagram [@osteriagiulia](https://www.instagram.com/osteriagiulia). They also have a sister restaurant called Giulietta, which you can visit at [giu.ca](https://giu.ca) or on Instagram [@giulietta972](https://www.instagram.com/giulietta972).Please note that the information provided is based on the available document and may be subject to change. It is recommended to visit their official website or contact them directly for the most up-to-date information.

3.3 避免上下文窗口问题(高级)

       使用retrieve的一个问题是内容可能很长。如果内容被直接地附加到会话历史并转储到LLM上下文窗口中,那么我们可能会遇到上下文窗口限制。

    LlamaIndex提供了工具抽象来帮助处理这一问题。我们的LoadAndSearchToolSpec嵌入了任何可能返回大量数据的工具,并将其分为两个工具:一个是将数据动态存储在索引中的加载工具,另一个是允许在该索引上进行搜索的搜索工具。

       在Metaphor方面,我们定义search_and_recovere_documents端点来结合search和retrieve。这允许代理进行单个查询以检索大量文档,当这些文档与LoadAndSearchToolSpec结合使用时,这些文档将直接存储在索引中。如果代理分别调用search和retrieve,那么将搜索结果写入会话历史记录,然后再次将其传递到提示中,以调用retrieve覆盖所有文档ID,将花费更长的时间,并消耗更多的token。

       创建LoadAndSearchToolSpec:

from llama_index.tools.tool_spec.load_and_search.base import LoadAndSearchToolSpec# The search_and_retrieve_documents tool is the third in the tool list, as seen abovewrapped_retrieve = LoadAndSearchToolSpec.from_defaults(  metaphor_tool_list[2],)

         下面展示一个完整的例子:

# Just pass the wrapped tools and the get_date utilityagent = OpenAIAgent.from_tools(  [*wrapped_retrieve.to_tool_list(), metaphor_tool_list[4]],  verbose=True,)print(agent.chat('Can you summarize everything published in the last month regarding news on superconductors'))

        下面看一个agent调用多个工具的详细过程:

=== Calling Function ===Calling function: current_date with args: {}Got output: 2023-08-20=========================== Calling Function ===Calling function: search_and_retrieve_documents with args: {  "query": "superconductors",  "start_published_date": "2023-07-20",  "end_published_date": "2023-08-20"}[Metaphor Tool] Autoprompt: "Here is an interesting article about superconductors:Got output: Content loaded! You can now search the information using read_search_and_retrieve_documents=========================== Calling Function ===Calling function: read_search_and_retrieve_documents with args: {  "query": "superconductors"}Got output: Superconductors are materials that can perfectly conduct electricity. They are used in a variety of applications, such as particle accelerators, nuclear fusion devices, MRI machines, and maglev trains. However, so far, no superconductor has been proven to work at ambient pressures and temperatures. On July 22, scientists in South Korea published research claiming to have solved this problem with a material called LK-99, which has an electrical resistivity that drops to near zero at 30 degrees Celsius (86 degrees Fahrenheit).========================In the last month, there have been developments in the field of superconductors. Scientists in South Korea have published research on a material called LK-99, which has the ability to conduct electricity with near-zero resistance at a temperature of 30 degrees Celsius (86 degrees Fahrenheit). This breakthrough could potentially lead to the development of superconductors that work at ambient pressures and temperatures, opening up new possibilities for various applications such as particle accelerators, nuclear fusion devices, MRI machines, and maglev trains.

      agent使用get_date工具来确定当前月份,然后在调用search时,根据发布日期应用Metaphor中的过滤器。使用retrieve_documents加载文档,并使用read_retrieve_documents读取这些文档。

参考文献:

[1] https://blog.llamaindex.ai/llamaindex-metaphor-towards-automating-knowledge-work-with-llms-5520a32efa2f

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/205636.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[论文阅读]BEVFusion

BEVFusion BEVFusion: A Simple and Robust LiDAR-Camera Fusion Framework BEVFusion:简单而强大的激光雷达相机融合框架 论文网址:BEVFusion 论文代码:BEVFusion 简读论文 论文背景:激光雷达和摄像头是自动驾驶系统中常用的两…

UE Http笔记

c参考链接 UE4 开发如何使用 Http 请求_wx61ae2f5191643的技术博客_51CTO博客 虚幻引擎:UEC如何对JSON文件进行读写?-CSDN博客 UE4 HTTP使用 官方免费插件 VaRest 在代码插件创建的VaRest - 虚幻引擎商城 UE5在蓝图中使用Varest插件Get,Post两种常见请求方式…

webpack学习-3.管理输出

webpack学习-3.管理输出 1.简单练手2.设置 HtmlWebpackPlugin3.清理 /dist 文件夹4.manifest5.总结 1.简单练手 官网的第一个预先准备,是多入口的。 const path require(path);module.exports {entry: {index: ./src/index.js,print: ./src/print.js,},output: …

axios调接口传参特殊字符丢失的问题(encodeURI 和 encodeURIComponent)

1、axios调接口特殊字符丢失的问题 项目开发过程中遇到一个接口传参,参数带特殊字符,axios调接口特殊字符丢失的问题 例如接口: get/user/detail/{name} name是个参数直接调接口的时候拼到接口上,get/user/detail/test123#$%&am…

计算机网络中的通信子网:架构、协议与技术简介

在计算机网络中,通信子网是负责实现主机之间以及主机与终端之间数据传输的核心部分。它由一系列硬件设备和通信协议组成,为上层应用提供可靠、高效和透明的数据传输服务。本文将详细介绍通信子网的架构、协议与技术。 一、通信子网的架构 星型拓扑 星…

华为配置Smart Link负载分担示例

Smart Link基本概念 Smart Link通过两个端口相互配合工作来实现功能。这样的一对端口组成了一个Smart Link组。为了区别一个Smart Link组中的两个端口,我们将其中的一个叫做主端口,另一个叫做从端口。同时我们利用Flush报文、Smart Link实例和控制VLAN等…

Matlab 点云曲线探测(算法不稳定,仅用于学习)

文章目录 一、简介二、实现代码三、实现效果参考文献一、简介 这是一个很有趣的曲线探测的方法,不过我没有复现出论文中那样的效果,可能是理解有误,但这个算法仍然是很有意思,故这里也对其进行记录。 按照论文中的思路,首先我们需要通过一种线性强度图来计算确定每个点的法…

js事件流模型

js 事件流模型js 事件循环 js 事件流模型 JavaScript的事件流模型可以被概括为三个阶段:捕获阶段,目标阶段和冒泡阶段。这个模型是在DOM(文档对象模型)中定义的,用于描述事件如何在DOM元素中传播。 捕获阶段&#xf…

C语言实现水仙花

水仙花定义:指一个3位数,其各位数字立方和等于改数本身。若:153 1* 1* 1 5 * 5* 53* 3* 3 依次类推,四 、五 … 十全十美呀。 解题思路 从这句:其各位数字立方和等于改数本身 我们将这位数拆分出来: num num1 ^3 n…

提高图片分辨率的方法与实践

引言 在图像处理和计算机视觉领域,提高图片分辨率是一个常见的问题。随着高分辨率显示设备的普及,如4K、8K电视以及高像素手机摄像头的应用,用户对高质量图片的需求也越来越高。本文将介绍使用Golang语言提高图片分辨率的方法与实践。 1. 图…

服务器如何修改密码

首先先远程登录服务器。 1、右键我的电脑,点击“管理”。 2、在“本地用户和组”中打开“用户”,在右侧找到 Administrator 账户。 3、在 Administrator 账户上点击右键,选择“修改密码”设置您的新密码。 4、修改后请牢记您的系统管理员密…

Java网络编程,使用UDP实现TCP(一), 基本实现三次握手

简介: 首先我们需要知道TCP传输和UDP传输的区别,UDP相当于只管发送不管对方是否接收到了,而TCP相当于打电话,需要进行3次握手,4次挥手,所以我们就需要在应用层上做一些功能添加,如:…

Bounding boxes augmentation for object detection

Different annotations formats Bounding boxes are rectangles that mark objects on an image. There are multiple formats of bounding boxes annotations. Each format uses its specific representation of bouning boxes coordinates 每种格式都使用其特定的边界框坐标…

案例060:基于微信小程序考试系统

文末获取源码 开发语言:Java 框架:SSM JDK版本:JDK1.8 数据库:mysql 5.7 开发软件:eclipse/myeclipse/idea Maven包:Maven3.5.4 小程序框架:uniapp 小程序开发软件:HBuilder X 小程序…

01-SDV软件定义汽车思考

前言: 随着汽车产业“新四化”(电动化、网联化、智能化、共享化)的加速推动,智能汽车已成为各国科技发展战略重点,在社会数字化转型的浪潮下逐渐形成跨领域协作、多技术融合的汽车产业新赛道。 软件定义汽车已成为行业趋势与共识&#xff…

gcc安全特性之FORTIFY_SOURCE

GCC 4.0引入了FORTIFY_SOURCE特性,旨在加强程序的安全性,特别是对于字符串和内存操作函数的使用。下面是对FORTIFY_SOURCE机制的深入分析: 1. 功能 FORTIFY_SOURCE旨在检测和防止缓冲区溢出,格式化字符串漏洞以及其他与内存操作…

Django回顾的第三天

1.视图层 响应——本质都是HttpResponse——字符串 render——放个模板——模板渲染是在后端完成 js代码是在客户端浏览器里执行的 模板语法是在后端执行的 redirect——重定向 字符串参数不是是空的 状态码是 3开头 JsonResponse——json格式数据 return JsonRespons…

被淘汰的服务器如何回收利用_Maizyun

被淘汰的服务器如何回收利用 随着技术的不断进步,服务器作为IT基础设施的核心组件,其生命周期也在不断缩短。当服务器达到一定的使用年限或者技术更新换代时,便会被淘汰。如何有效地回收利用这些被淘汰的服务器,减少资源浪费&…

【开题报告】基于SpringBoot的抑郁症科普平台的设计与实现

1.研究背景 抑郁症是一种常见的精神障碍,严重影响了患者的生活质量和社会功能。随着社会的快速发展和生活压力的增加,抑郁症的发病率逐渐上升,成为全球范围内的健康问题。然而,对抑郁症的认知和理解仍存在许多误解和偏见&#xf…

class037 二叉树高频题目-下-不含树型dp【算法】

class037 二叉树高频题目-下-不含树型dp【算法】 code1 236. 二叉树的最近公共祖先 // 普通二叉树上寻找两个节点的最近公共祖先 // 测试链接 : https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-tree/ package class037;// 普通二叉树上寻找两个节点的最近…