宣布 LlamaCloud 正式可用(以及我们的 1900 万美元 A 轮融资)!
LlamaIndex

MultiOn 2024-05-23

使用 MultiOn 和 LlamaIndex 自动化在线任务

引言

MultiOn 是一个 AI 智能体平台,旨在促进在任何 Web 环境中自主完成任务。它使开发人员能够构建 AI 智能体,从头到尾管理在线活动,处理从简单数据检索到复杂交互的一切事务。

LlamaIndex 通过提供一个协调框架来补充这一点,该框架弥合了使用大型语言模型构建应用所需的私有数据和公共数据之间的差距。它有助于数据摄取、索引和查询,对于希望利用生成式 AI 的开发人员来说是必不可少的。

在本文中,我们将演示 MultiOn 的功能如何无缝集成到 LlamaIndex 框架中,展示一个利用这两种技术来自动化和简化 Web 交互的实际应用。

技术演练:将 MultiOn 与 LlamaIndex 集成

让我们探讨一个 MultiOn 和 LlamaIndex 协同工作以管理电子邮件交互和网页浏览的实际示例。

步骤 1:设置环境 我们首先为我们的 AI 智能体设置必要的配置和 API 密钥

import openai
from llama_index.agent.openai import OpenAIAgent
openai.api_key = "sk-your-key"

from llama_index.tools.multion import MultionToolSpec
multion_tool = MultionToolSpec(api_key="your-multion-key")

步骤 2:集成 Gmail 搜索工具 接下来,我们集成一个 Gmail 搜索工具,帮助我们的智能体获取和分析电子邮件,为后续操作提供必要的上下文

from llama_index.tools.google import GmailToolSpec
from llama_index.core.tools.ondemand_loader_tool import OnDemandLoaderTool

gmail_tool = GmailToolSpec()
gmail_loader_tool = OnDemandLoaderTool.from_tool(
    gmail_tool.to_tool_list()[1],
    name="gmail_search",
    description="""
         This tool allows you to search the users gmail inbox and give directions for how to summarize or process the emails

        You must always provide a query to filter the emails, as well as a query_str to process the retrieved emails.
        All parameters are required
        
        If you need to reply to an email, ask this tool to build the reply directly
        Examples:
            query='from:adam subject:dinner', max_results=5, query_str='Where are adams favourite places to eat'
            query='dentist appointment', max_results=1, query_str='When is the next dentist appointment'
            query='to:jerry', max_results=1, query_str='summarize and then create a response email to jerrys latest email'
            query='is:inbox', max_results=5, query_str='Summarize these emails'
    """
)

步骤 3:初始化智能体

使用工具和系统提示初始化智能体

agent = OpenAIAgent.from_tools(
    [*multion_tool.to_tool_list(), gmail_loader_tool],
    system_prompt="""
	    You are an AI agent that assists the user in crafting email responses based on previous conversations.
	    
	    The gmail_search tool connects directly to an API to search and retrieve emails, and answer questions based on the content.
	    The browse tool allows you to control a web browser with natural language to complete arbitrary actions on the web.
	    
	    Use these two tools together to gain context on past emails and respond to conversations for the user.
    """
)

步骤 4:智能体执行流程 集成了工具后,智能体现在具备执行一系列任务的能力

1. 搜索和总结电子邮件:智能体使用 LlamaIndex 的 Gmail 工具获取相关电子邮件并总结内容,为起草回复提供基础。

print(agent.chat("browse to the latest email from Julian and open the email"))
Added user message to memory: browse to the latest email from Julian and open the email
=== Calling Function ===
Calling function: gmail_search with args: {"query":"from:Julian","max_results":1,"query_str":"Browse to the latest email from Julian and open the email"}
Please visit this URL to authorize this application: https://#/o/oauth2/auth?response_type=code&client_id=1054044249014.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2F&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fgmail.compose+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fgmail.readonly&state=JSdsfdsi990sddsd&access_type=offline
Got output: Open the email from Julian to view the latest communication.
========================
 
I have opened the latest email from Julian for you to view. If you need any specific information or action to be taken, please let me know.

2. 生成回复:基于总结的信息,智能体起草针对电子邮件链的适当回复。

print(agent.chat(
	"Summarize the email chain with julian and create a response to the last email that confirms all the details"
))
Added user message to memory: Summarize the email chain with julian and create a response to the last email that confirms all the details
=== Calling Function ===
Calling function: gmail_search with args: {"query":"from:Julian","max_results":1,"query_str":"Summarize the email chain with Julian and create a response to the last email confirming all the details"}
Got output: The email chain with Julian involved a change in an event scheduled for Friday, August 6, 2021, from 15:30 to 16:00 United Kingdom Time on Google Meet. The instructions for joining were provided in the description. The email also included contact information for joining the meeting. Julian and Nassar were listed as attendees, with Julian being the organizer. The email was authenticated and passed SPF and DKIM checks.

In response to the last email, I would confirm all the details of the event change, reiterating the date, time, platform (Google Meet), and any specific instructions provided. I would express gratitude for the update and confirm attendance at the revised event timing.
========================

Based on the email chain with Julian, here is a summary:
- The event scheduled for Friday, August 6, 2021, has been changed from 15:30 to 16:00 United Kingdom Time on Google Meet.
- Instructions for joining the meeting were provided in the email.
- Attendees included Julian and Nassar, with Julian as the organizer.
- The email passed SPF and DKIM checks.

To respond and confirm all the details, you can mention the revised event date and time, the platform (Google Meet), and express gratitude for the update. Confirm your attendance at the new timing. Let me know if you would like me to draft the response email for you.

3. 通过 MultiOn 发送电子邮件:最后,生成的回复传递给 MultiOn 智能体,该智能体通过 Web 浏览器管理发送电子邮件的操作。

print(agent.chat(
	"pass the entire generated email to the browser and have it send the email as a reply to the chain"
))
Added user message to memory: pass the entire generated email to the browser and have it send the email as a reply to the chain
=== Calling Function ===
Calling function: browse with args: {"cmd": "Compose a reply email to Julian confirming the event change to Fri 6 Aug 2021 from 15:30 to 16:00 UK Time on Google Meet. Express readiness to attend and thank Julian for the details."}
Got output: Email response sent to Julian
========================

后续步骤

MultiOn 是 LlamaHub 上官方支持的工具,LlamaHub 是所有 LlamaIndex 集成(从工具到 LLM 再到向量存储)的中心页面。在这里查看 LlamaHub 页面

如果您有兴趣自己通过本教程构建一个浏览器 + Gmail 驱动的智能体,请查看我们的notebook

MultiOn 和 LlamaIndex 的集成提供了一个强大的工具包,供旨在自动化和简化在线任务的开发人员使用。随着这些技术的发展,它们将继续释放 AI 应用的新潜力,显着影响开发人员与数字环境的交互方式以及数据管理方式。