From 6085fe18d4f8e3647060f940b99de2594efdb372 Mon Sep 17 00:00:00 2001 From: Harrison Chase Date: Wed, 22 Feb 2023 22:29:43 -0800 Subject: [PATCH] add ifttt tool (#1244) --- docs/modules/utils/examples/ifttt.ipynb | 121 ++++++++++++++++++++++++ langchain/tools/__init__.py | 3 +- langchain/tools/ifttt.py | 57 +++++++++++ 3 files changed, 180 insertions(+), 1 deletion(-) create mode 100644 docs/modules/utils/examples/ifttt.ipynb create mode 100644 langchain/tools/ifttt.py diff --git a/docs/modules/utils/examples/ifttt.ipynb b/docs/modules/utils/examples/ifttt.ipynb new file mode 100644 index 00000000..ab21d190 --- /dev/null +++ b/docs/modules/utils/examples/ifttt.ipynb @@ -0,0 +1,121 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "16763ed3", + "metadata": {}, + "source": [ + "# IFTTT WebHooks\n", + "\n", + "This notebook shows how to use IFTTT Webhooks.\n", + "\n", + "From https://github.com/SidU/teams-langchain-js/wiki/Connecting-IFTTT-Services.\n", + "\n", + "# Creating a webhook\n", + "- Go to https://ifttt.com/create\n", + "\n", + "# Configuring the \"If This\"\n", + "- Click on the \"If This\" button in the IFTTT interface.\n", + "- Search for \"Webhooks\" in the search bar.\n", + "- Choose the first option for \"Receive a web request with a JSON payload.\"\n", + "- Choose an Event Name that is specific to the service you plan to connect to.\n", + "This will make it easier for you to manage the webhook URL.\n", + "For example, if you're connecting to Spotify, you could use \"Spotify\" as your\n", + "Event Name.\n", + "- Click the \"Create Trigger\" button to save your settings and create your webhook.\n", + "\n", + "# Configuring the \"Then That\"\n", + "- Tap on the \"Then That\" button in the IFTTT interface.\n", + "- Search for the service you want to connect, such as Spotify.\n", + "- Choose an action from the service, such as \"Add track to a playlist\".\n", + "- Configure the action by specifying the necessary details, such as the playlist name,\n", + "e.g., \"Songs from AI\".\n", + "- Reference the JSON Payload received by the Webhook in your action. For the Spotify\n", + "scenario, choose \"{{JsonPayload}}\" as your search query.\n", + "- Tap the \"Create Action\" button to save your action settings.\n", + "- Once you have finished configuring your action, click the \"Finish\" button to\n", + "complete the setup.\n", + "- Congratulations! You have successfully connected the Webhook to the desired\n", + "service, and you're ready to start receiving data and triggering actions 🎉\n", + "\n", + "# Finishing up\n", + "- To get your webhook URL go to https://ifttt.com/maker_webhooks/settings\n", + "- Copy the IFTTT key value from there. The URL is of the form\n", + "https://maker.ifttt.com/use/YOUR_IFTTT_KEY. Grab the YOUR_IFTTT_KEY value.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "10a46e7e", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain.tools.ifttt import IFTTTWebhook" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "12003d72", + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "key = os.environ[\"IFTTTKey\"]\n", + "url = f\"https://maker.ifttt.com/trigger/spotify/json/with/key/{key}\"\n", + "tool = IFTTTWebhook(name=\"Spotify\", description=\"Add a song to spotify playlist\", url=url)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "6e68f846", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\"Congratulations! You've fired the spotify JSON event\"" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tool.run(\"taylor swift\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a7e599c9", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.1" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/langchain/tools/__init__.py b/langchain/tools/__init__.py index 31d0c58b..fcacd361 100644 --- a/langchain/tools/__init__.py +++ b/langchain/tools/__init__.py @@ -1,5 +1,6 @@ """Core toolkit implementations.""" from langchain.tools.base import BaseTool +from langchain.tools.ifttt import IFTTTWebhook -__all__ = ["BaseTool"] +__all__ = ["BaseTool", "IFTTTWebhook"] diff --git a/langchain/tools/ifttt.py b/langchain/tools/ifttt.py new file mode 100644 index 00000000..8d3d943a --- /dev/null +++ b/langchain/tools/ifttt.py @@ -0,0 +1,57 @@ +"""From https://github.com/SidU/teams-langchain-js/wiki/Connecting-IFTTT-Services. + +# Creating a webhook +- Go to https://ifttt.com/create + +# Configuring the "If This" +- Click on the "If This" button in the IFTTT interface. +- Search for "Webhooks" in the search bar. +- Choose the first option for "Receive a web request with a JSON payload." +- Choose an Event Name that is specific to the service you plan to connect to. +This will make it easier for you to manage the webhook URL. +For example, if you're connecting to Spotify, you could use "Spotify" as your +Event Name. +- Click the "Create Trigger" button to save your settings and create your webhook. + +# Configuring the "Then That" +- Tap on the "Then That" button in the IFTTT interface. +- Search for the service you want to connect, such as Spotify. +- Choose an action from the service, such as "Add track to a playlist". +- Configure the action by specifying the necessary details, such as the playlist name, +e.g., "Songs from AI". +- Reference the JSON Payload received by the Webhook in your action. For the Spotify +scenario, choose "{{JsonPayload}}" as your search query. +- Tap the "Create Action" button to save your action settings. +- Once you have finished configuring your action, click the "Finish" button to +complete the setup. +- Congratulations! You have successfully connected the Webhook to the desired +service, and you're ready to start receiving data and triggering actions 🎉 + +# Finishing up +- To get your webhook URL go to https://ifttt.com/maker_webhooks/settings +- Copy the IFTTT key value from there. The URL is of the form +https://maker.ifttt.com/use/YOUR_IFTTT_KEY. Grab the YOUR_IFTTT_KEY value. +""" +import requests + +from langchain.tools.base import BaseTool + + +class IFTTTWebhook(BaseTool): + """IFTTT Webhook. + + Args: + name: name of the tool + description: description of the tool + url: url to hit with the json event. + """ + + url: str + + def _run(self, tool_input: str) -> str: + body = {"this": tool_input} + response = requests.post(self.url, data=body) + return response.text + + async def _arun(self, tool_input: str) -> str: + raise NotImplementedError("Not implemented.")