langchain icon indicating copy to clipboard operation
langchain copied to clipboard

New Tool: Twilio

Open cyzanfar opened this issue 3 years ago • 5 comments

I am considering implementing a new tool to give LLMs the ability to send SMS text using the Twilio API. @hwchase17 is this worth implementing? if so I'll submit a PR shortly.

cyzanfar avatar Apr 19 '23 14:04 cyzanfar

I plan on doing something similar

dakolli avatar Apr 29 '23 18:04 dakolli

If you want to just send sms, heres a simple util

"""Util that calls Twilio."""
from typing import Any, Dict, Optional

from pydantic import BaseModel, Extra, root_validator

from langchain.utils import get_from_dict_or_env

class Sms(BaseModel):
    """Sms Client using Twilio.

    To use, you should have the ``twilio`` python package installed,
    and the environment variables ``ACCOUNT_SID``, ``AUTH_TOKEN`, and ``FROM_NUMBER``, or pass
    `account_sid`, `auth_token`, and `from_number` as named parameters to the constructor.

    Example:
        .. code-block:: python

            from langchain.utilities.sms import Sms
            client = Sms(account_sid="ACxxx", auth_token="xxx", from_number="+15551234567")
            client.run('test', '+15551234567')
    """

    sms_client: Any  #: :meta private:
    account_sid: Optional[str] = None
    auth_token: Optional[str] = None
    from_number: Optional[str] = None

    class Config:
        """Configuration for this pydantic object."""

        extra = Extra.forbid
        arbitrary_types_allowed = False

    @root_validator()
    def validate_environment(cls, values: Dict) -> Dict:
        """Validate that api key and python package exists in environment."""
        values["account_sid"] = get_from_dict_or_env(
            values, "account_sid", "ACCOUNT_SID"
        )
        values["auth_token"] = get_from_dict_or_env(
            values, "auth_token", "AUTH_TOKEN"
        )
        values["from_number"] = get_from_dict_or_env(
            values, "from_number", "FROM_NUMBER"
        )
        try:
            from twilio.rest import Client
            values["sms_client"] = Client
        except ImportError:
            raise ValueError(
                "Could not import twilio python package. "
                "Please install it with `pip3 install twilio`."
            )
        return values

    def run(self, body: str, to: str, **kwargs: Any) -> str:
        """Run body through Twilio and respond with message sid."""
        return self.sms(body, to)

    async def arun(self, body: str, to: str, **kwargs: Any) -> str:
        """Run message through Twilio and respond with message sid async."""
        raise NotImplementedError("Sms does not yet support async")

    def sms(self, body: str, to: str) -> str:
        """Send message through Twilio and return the message sid."""
        client = self.sms_client(self.account_sid, self.auth_token)
        message = client.messages.create(
                to=to, 
                from_=self.from_number,
                body=body
        )
        return message.sid

tedma4 avatar May 23 '23 14:05 tedma4

@tedma4 great stuff. Could import that into https://github.com/hwchase17/langchain/blob/master/langchain/agents/load_tools.py and make it available as a tool

cyzanfar avatar May 23 '23 14:05 cyzanfar

@cyzanfar Done, added to #5136

tedma4 avatar May 23 '23 16:05 tedma4

@cyzanfar Is there any kind of special setup needed for when the run function has multiple args? Or does the agent figure it out through the signature?

tedma4 avatar May 23 '23 16:05 tedma4

Everything needed to support sending messages over WhatsApp Business Platform (GA) was present. Just added some details on leveraging it in https://github.com/hwchase17/langchain/pull/6562

movaid7 avatar Jun 21 '23 20:06 movaid7

Hi, @cyzanfar! I'm Dosu, and I'm here to help the LangChain team manage their backlog. I wanted to let you know that we are marking this issue as stale.

From what I understand, you are considering implementing a new tool to allow LLMs to send SMS text using the Twilio API. You have sought input from @hwchase17 on whether this is worth implementing. Additionally, @tedma4 suggests a simple utility for sending SMS using Twilio, which you plan to import into the repository. On a related note, @movaid7 mentions that support for sending messages over WhatsApp Business Platform is already present and provides details in a pull request.

Before we proceed, we would like to confirm if this issue is still relevant to the latest version of the LangChain repository. If it is, please let us know by commenting on this issue. Otherwise, feel free to close the issue yourself, or it will be automatically closed in 7 days.

Thank you for your understanding and cooperation. Let us know if you have any further questions or concerns!

Best regards, Dosu

dosubot[bot] avatar Sep 20 '23 16:09 dosubot[bot]