"This notebook demonstrates how to create an AI Email agent using Composio’s Gmail tool with autogen to create an agent that will automatically respond to emails based on provided instructions.\n",
"\n",
"[Composio](https://composio.dev/) allows an AI agent or LLM to easily connect to apps like Gmail, Slack, Trello etc. The key features of Composio are:"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "UI78uqxyCLQu"
},
"source": [
"- Repository of Tools: Composio allows LLMs and agents to integrate with 100+ apps (Github, Salesforce, File Manager, Code Execution & More) to perform actions & subscribe to triggers(events).\n",
"\n",
"- Frameworks & LLM Agnostic: Composio provides out of box support for 10+ popular agentic frameworks and works with all the LLM providers using function calling.\n",
"\n",
"- Managed Auth: Composio helps manage authentication for all users/agents from a single dashboard."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Visit [Composio Docs](https://docs.composio.dev/introduction/intro/overview) to learn more."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "16kFQX0kCLQv"
},
"source": [
"The notebook demonstrates how to create a Gmail integration with Composio, set up a trigger for new emails, initialize agents with tools and finally we'll see the agent in action."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ixe3kpQrCLQv"
},
"source": [
"````{=mdx}\n",
":::info Requirements\n",
"Some extra dependencies are needed for this notebook, which can be installed via pip:\n",
"For more information, please refer to the [installation guide](/docs/installation/).\n",
":::\n",
"````"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "XK6_H749CLQv"
},
"source": [
"## Composio Setup\n",
"\n",
"To get started with using Composio's Gmail tool, we need to create an integration between Composio and Gmail. This can be done using a simple command -"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "-1XLfYJRCLQv"
},
"outputs": [],
"source": [
"!composio add gmail"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "T-YxALkYCLQw"
},
"source": [
"To set up a trigger(basically a listener) for new emails -"
"Here, we get the `GMAIL_REPLY_TO_THREAD` action, which is just a function that can be used to reply to an email. We'll be using this action to reply to emails automatically when they arrive."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ZsgE3qm9CLQx"
},
"source": [
"## Create trigger listener"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "fU6TmawGCLQx"
},
"source": [
"Now, we create a listener for the trigger that we created above. This listener will listen for new emails and when a new email arrives, it'll provide data associated with the email like the sender email, email content etc. This data will be used by the attached callback function to invoke the agent and to send a reply to the email."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `@listener.callback` decorator registers the function it decorates as a callback for a specific event trigger, in this case, when a new Gmail message is received (`GMAIL_NEW_GMAIL_MESSAGE`). It listens for the specified trigger and invokes the decorated function (`callback_new_message`) when the event occurs.\n",
"\n",
"After extracting the relevant data from the trigger payload, we start a conversation between `user_proxy` and `chatbot` to send a reply to the received email."
"{\"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\"}\n",
" # Get the payload and extract relevant information\n",
" payload = event.payload # Email payload\n",
" thread_id = payload.get(\"threadId\")\n",
" message = payload.get(\"messageText\")\n",
" sender_mail = payload.get(\"sender\")\n",
" if sender_mail is None:\n",
" print(\"No sender email found\")\n",
" return\n",
"\n",
" analyze_email_task = f\"\"\"\n",
" Analyze the email content and create an appropriate reply.\n",
" a. The email was received from {sender_mail}\n",
" b. The content of the email is: {message}\n",
" c. The thread id is: {thread_id}.\n",
" \"\"\"\n",
" # Initiate the conversation\n",
" res = user_proxy.initiate_chat(chatbot, message=analyze_email_task)\n",
" print(res.summary)\n",
"\n",
"\n",
"print(\"Subscribed to triggers!\")\n",
"# Start listening\n",
"listener.listen()"
]
}
],
"metadata": {
"colab": {
"provenance": []
},
"front_matter": {
"description": "Use Composio to create AI agents that seamlessly connect with external tools, Apps, and APIs to perform actions and receive triggers. With built-in support for AutoGen, Composio enables the creation of highly capable and adaptable AI agents that can autonomously execute complex tasks and deliver personalized experiences.",