使用Composio的AI邮件代理
本笔记本展示了如何使用Composio的Gmail工具与autogen创建一个AI电子邮件代理,该代理将根据提供的指示自动响应邮件。
Composio 允许AI代理或LLM轻松连接到Gmail、Slack、Trello等应用程序。Composio的主要功能包括:
-
工具仓库:Composio 允许 LLMs 和代理与 100 多个应用程序(Github、Salesforce、文件管理器、代码执行等)集成,以执行操作并订阅触发器(事件)。
-
框架与LLM无关:Composio 提供了对10+流行代理框架的开箱即用支持,并与所有使用函数调用的LLM提供商兼容。
-
托管认证:Composio 帮助从单一仪表板管理所有用户/代理的认证。
访问 Composio Docs 了解更多。
该笔记本展示了如何通过Composio创建Gmail集成,设置新邮件的触发器,使用工具初始化代理,最后我们将看到代理的实际运作。
Composio 设置
要开始使用Composio的Gmail工具,我们需要在Composio和Gmail之间创建集成。这可以通过一个简单的命令来完成 -
!composio add gmail
要为新邮件设置一个触发器(基本上是一个监听器) -
!composio triggers enable gmail_new_gmail_message
这使得gmail_new_gmail_message
触发器启用,当连接的账户收到新邮件时,该触发器会被触发。
import os
from composio_autogen import Action, ComposioToolSet
from autogen.agentchat import AssistantAgent, UserProxyAgent
os.environ["OPENAI_API_KEY"] = "YOUR_API_KEY"
初始化代理
llm_config = {"config_list": [{"model": "gpt-4o", "api_key": os.environ.get("OPENAI_API_KEY")}]}
# Prompt for email assistant
email_assistant_prompt = """
You are an AI email assistant specialized in drafting replies to emails.
You create appropriate and professional replies to emails based on the content of the email.
After executing the GMAIL_REPLY_TO_THREAD action and sending the email to the user, respond with TERMINATE.
"""
# Initialize AssistantAgent
chatbot = AssistantAgent(
"chatbot",
system_message=email_assistant_prompt,
llm_config=llm_config,
)
# Initialize UserProxyAgent
user_proxy = UserProxyAgent(
"user_proxy",
is_termination_msg=lambda x: x.get("content", "") and "TERMINATE" in x.get("content", ""),
human_input_mode="NEVER",
code_execution_config=False,
llm_config=llm_config,
)
初始化Composio的工具集
现在,我们初始化Composio的工具集并获取代理所需的工具和操作。然后,我们将这些工具注册到UserProxyAgent
中。
代理随后可以通过函数调用来使用这些工具。
# Initialize Composio Toolset
composio_toolset = ComposioToolSet()
# Get the required tools and register them with the agents
email_tools = composio_toolset.register_tools(
caller=user_proxy,
executor=chatbot,
actions=[
Action.GMAIL_REPLY_TO_THREAD,
],
)
这里,我们得到了GMAIL_REPLY_TO_THREAD
操作,这是一个可以用来回复邮件的函数。我们将使用这个操作在邮件到达时自动回复。
创建触发监听器
现在,我们为上面创建的触发器创建一个监听器。该监听器将监听新邮件,当新邮件到达时,它会提供与邮件相关的数据,如发件人邮箱、邮件内容等。这些数据将由附加的回调函数用于调用代理并发送邮件回复。
@listener.callback
装饰器将其修饰的函数注册为特定事件触发时的回调函数,在这种情况下,当接收到新的Gmail邮件时(GMAIL_NEW_GMAIL_MESSAGE
)。它会监听指定的触发器,并在事件发生时调用修饰的函数(callback_new_message
)。
从触发器负载中提取相关数据后,我们开始一个user_proxy
和chatbot
之间的对话,以发送回复到收到的电子邮件。
# Create a trigger listener
listener = composio_toolset.create_trigger_listener()
@listener.callback(filters={"trigger_name": "GMAIL_NEW_GMAIL_MESSAGE"})
def callback_new_message(event) -> None:
# Get the payload and extract relevant information
payload = event.payload # Email payload
thread_id = payload.get("threadId")
message = payload.get("messageText")
sender_mail = payload.get("sender")
if sender_mail is None:
print("No sender email found")
return
analyze_email_task = f"""
Analyze the email content and create an appropriate reply.
a. The email was received from {sender_mail}
b. The content of the email is: {message}
c. The thread id is: {thread_id}.
"""
# Initiate the conversation
res = user_proxy.initiate_chat(chatbot, message=analyze_email_task)
print(res.summary)
print("Subscribed to triggers!")
# Start listening
listener.listen()
INFO:composio.utils.shared:Creating trigger subscription
INFO:composio.utils.shared:Received trigger event with trigger ID: ea36d63f-5cc9-4581-9a19-b647e7468697 and trigger name: GMAIL_NEW_GMAIL_MESSAGE
INFO:composio.utils.shared:Executing `GMAIL_REPLY_TO_THREAD` with params={'thread_id': '1922811a78db4...', 'message_body': "Hi John,\n\nI'm doing well, thank you! How about you?\n\nBest,\n[Your Name]", 'recipient_email': 'example_email@gmail.com'} and metadata={} connected_account_i...
INFO:composio.utils.shared:Got response={'successfull': True, 'data': {'response_data': {'id': '1922811c1b3ed...', 'threadId': '1922811a78db4...', 'labelIds': ['SENT']}}, 'error': None} from action=<composio.client.enums._action.Action object at 0x7d50554c4310> with params={'thread_...
Subscribed to triggers!
Analyze the email content and create an appropriate reply.
a. The email was received from John Doe <example_email@gmail.com>
b. The content of the email is: hey, how are you?
c. The thread id is: 1922811a78db4....
--------------------------------------------------------------------------------
GMAIL_REPLY_TO_THREAD thread_id: 1922811a78db4... message:
Hi John,
I'm doing well, thank you! How about you?
Best,
[Your Name]
--------------------------------------------------------------------------------
***** Suggested tool call (call_qGQzJ6XgyO8LKSSFnwkQhSCz): GMAIL_REPLY_TO_THREAD_8c4b19f45c *****
Arguments:
{"thread_id":"1922811a78db4...","message_body":"Hi John,\n\nI'm doing well, thank you! How about you?\n\nBest,\n[Your Name]","recipient_email":"example_email@gmail.com"}
*************************************************************************************************
--------------------------------------------------------------------------------
>>>>>>>> EXECUTING FUNCTION GMAIL_REPLY_TO_THREAD_8c4b19f45c...
***** Response from calling tool (call_qGQzJ6XgyO8LKSSFnwkQhSCz) *****
{"successfull": true, "data": {"response_data": {"id": "1922811c1b3ed...", "threadId": "1922811a78db4...", "labelIds": ["SENT"]}}, "error": null}
**********************************************************************
--------------------------------------------------------------------------------
I've replied to the email with the following message:
Hi John,
I'm doing well, thank you! How about you?
Best,
[Your Name]
Is there anything else you need?
--------------------------------------------------------------------------------
TERMINATE
--------------------------------------------------------------------------------