mirror of
https://github.com/hwchase17/langchain.git
synced 2025-11-20 12:09:46 +00:00
```python
"""python scripts/update_mypy_ruff.py"""
import glob
import tomllib
from pathlib import Path
import toml
import subprocess
import re
ROOT_DIR = Path(__file__).parents[1]
def main():
for path in glob.glob(str(ROOT_DIR / "libs/**/pyproject.toml"), recursive=True):
print(path)
with open(path, "rb") as f:
pyproject = tomllib.load(f)
try:
pyproject["tool"]["poetry"]["group"]["typing"]["dependencies"]["mypy"] = (
"^1.10"
)
pyproject["tool"]["poetry"]["group"]["lint"]["dependencies"]["ruff"] = (
"^0.5"
)
except KeyError:
continue
with open(path, "w") as f:
toml.dump(pyproject, f)
cwd = "/".join(path.split("/")[:-1])
completed = subprocess.run(
"poetry lock --no-update; poetry install --with typing; poetry run mypy . --no-color",
cwd=cwd,
shell=True,
capture_output=True,
text=True,
)
logs = completed.stdout.split("\n")
to_ignore = {}
for l in logs:
if re.match("^(.*)\:(\d+)\: error:.*\[(.*)\]", l):
path, line_no, error_type = re.match(
"^(.*)\:(\d+)\: error:.*\[(.*)\]", l
).groups()
if (path, line_no) in to_ignore:
to_ignore[(path, line_no)].append(error_type)
else:
to_ignore[(path, line_no)] = [error_type]
print(len(to_ignore))
for (error_path, line_no), error_types in to_ignore.items():
all_errors = ", ".join(error_types)
full_path = f"{cwd}/{error_path}"
try:
with open(full_path, "r") as f:
file_lines = f.readlines()
except FileNotFoundError:
continue
file_lines[int(line_no) - 1] = (
file_lines[int(line_no) - 1][:-1] + f" # type: ignore[{all_errors}]\n"
)
with open(full_path, "w") as f:
f.write("".join(file_lines))
subprocess.run(
"poetry run ruff format .; poetry run ruff --select I --fix .",
cwd=cwd,
shell=True,
capture_output=True,
text=True,
)
if __name__ == "__main__":
main()
```
84 lines
3.3 KiB
Python
84 lines
3.3 KiB
Python
"""Util that calls Twilio."""
|
|
|
|
from typing import Any, Dict, Optional
|
|
|
|
from langchain_core.pydantic_v1 import BaseModel, Extra, root_validator
|
|
from langchain_core.utils import get_from_dict_or_env
|
|
|
|
|
|
class TwilioAPIWrapper(BaseModel):
|
|
"""Messaging Client using Twilio.
|
|
|
|
To use, you should have the ``twilio`` python package installed,
|
|
and the environment variables ``TWILIO_ACCOUNT_SID``, ``TWILIO_AUTH_TOKEN``, and
|
|
``TWILIO_FROM_NUMBER``, or pass `account_sid`, `auth_token`, and `from_number` as
|
|
named parameters to the constructor.
|
|
|
|
Example:
|
|
.. code-block:: python
|
|
|
|
from langchain_community.utilities.twilio import TwilioAPIWrapper
|
|
twilio = TwilioAPIWrapper(
|
|
account_sid="ACxxx",
|
|
auth_token="xxx",
|
|
from_number="+10123456789"
|
|
)
|
|
twilio.run('test', '+12484345508')
|
|
"""
|
|
|
|
client: Any #: :meta private:
|
|
account_sid: Optional[str] = None
|
|
"""Twilio account string identifier."""
|
|
auth_token: Optional[str] = None
|
|
"""Twilio auth token."""
|
|
from_number: Optional[str] = None
|
|
"""A Twilio phone number in [E.164](https://www.twilio.com/docs/glossary/what-e164)
|
|
format, an
|
|
[alphanumeric sender ID](https://www.twilio.com/docs/sms/send-messages#use-an-alphanumeric-sender-id),
|
|
or a [Channel Endpoint address](https://www.twilio.com/docs/sms/channels#channel-addresses)
|
|
that is enabled for the type of message you want to send. Phone numbers or
|
|
[short codes](https://www.twilio.com/docs/sms/api/short-code) purchased from
|
|
Twilio also work here. You cannot, for example, spoof messages from a private
|
|
cell phone number. If you are using `messaging_service_sid`, this parameter
|
|
must be empty.
|
|
"""
|
|
|
|
class Config:
|
|
"""Configuration for this pydantic object."""
|
|
|
|
extra = Extra.forbid
|
|
arbitrary_types_allowed = False
|
|
|
|
@root_validator(pre=True)
|
|
def validate_environment(cls, values: Dict) -> Dict:
|
|
"""Validate that api key and python package exists in environment."""
|
|
try:
|
|
from twilio.rest import Client
|
|
except ImportError:
|
|
raise ImportError(
|
|
"Could not import twilio python package. "
|
|
"Please install it with `pip install twilio`."
|
|
)
|
|
account_sid = get_from_dict_or_env(values, "account_sid", "TWILIO_ACCOUNT_SID")
|
|
auth_token = get_from_dict_or_env(values, "auth_token", "TWILIO_AUTH_TOKEN")
|
|
values["from_number"] = get_from_dict_or_env(
|
|
values, "from_number", "TWILIO_FROM_NUMBER"
|
|
)
|
|
values["client"] = Client(account_sid, auth_token)
|
|
return values
|
|
|
|
def run(self, body: str, to: str) -> str:
|
|
"""Run body through Twilio and respond with message sid.
|
|
|
|
Args:
|
|
body: The text of the message you want to send. Can be up to 1,600
|
|
characters in length.
|
|
to: The destination phone number in
|
|
[E.164](https://www.twilio.com/docs/glossary/what-e164) format for
|
|
SMS/MMS or
|
|
[Channel user address](https://www.twilio.com/docs/sms/channels#channel-addresses)
|
|
for other 3rd-party channels.
|
|
"""
|
|
message = self.client.messages.create(to, from_=self.from_number, body=body)
|
|
return message.sid
|