Infobip
本笔记本展示了如何使用 Infobip API 包来发送 SMS 消息和电子邮件。
Infobip 提供了许多服务,但本笔记本将重点介绍 SMS 和电子邮件服务。您可以在 这里 找到有关 API 和其他渠道的更多信息。
设置
要使用此工具,您需要拥有一个 Infobip 账户。您可以创建一个 免费试用账户。
InfobipAPIWrapper
使用命名参数,您可以提供凭据:
infobip_api_key
- API 密钥,您可以在 开发者工具 中找到infobip_base_url
- 基础 URL 用于 Infobip API。您可以使用默认值https://api.infobip.com/
。
您还可以将 infobip_api_key
和 infobip_base_url
作为环境变量 INFOBIP_API_KEY
和 INFOBIP_BASE_URL
提供。
发送短信
from langchain_community.utilities.infobip import InfobipAPIWrapper
infobip: InfobipAPIWrapper = InfobipAPIWrapper()
infobip.run(
to="41793026727",
text="Hello, World!",
sender="Langchain",
channel="sms",
)
发送电子邮件
from langchain_community.utilities.infobip import InfobipAPIWrapper
infobip: InfobipAPIWrapper = InfobipAPIWrapper()
infobip.run(
to="[email protected]",
sender="[email protected]",
subject="example",
body="example",
channel="email",
)
如何在代理中使用它
from langchain import hub
from langchain.agents import AgentExecutor, create_openai_functions_agent
from langchain_community.utilities.infobip import InfobipAPIWrapper
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_core.tools import StructuredTool
from langchain_openai import ChatOpenAI
instructions = "You are a coding teacher. You are teaching a student how to code. The student asks you a question. You answer the question."
base_prompt = hub.pull("langchain-ai/openai-functions-template")
prompt = base_prompt.partial(instructions=instructions)
llm = ChatOpenAI(temperature=0)
class EmailInput(BaseModel):
body: str = Field(description="电子邮件正文文本")
to: str = Field(description="要发送到的电子邮件地址。示例:[email protected]")
sender: str = Field(
description="发件人电子邮件地址,必须是 '[email protected]'"
)
subject: str = Field(description="电子邮件主题")
channel: str = Field(description="电子邮件渠道,必须是 'email'")
infobip_api_wrapper: InfobipAPIWrapper = InfobipAPIWrapper()
infobip_tool = StructuredTool.from_function(
name="infobip_email",
description="通过 Infobip 发送电子邮件。如果您需要发送电子邮件,请使用 infobip_email",
func=infobip_api_wrapper.run,
args_schema=EmailInput,
)
tools = [infobip_tool]
agent = create_openai_functions_agent(llm, tools, prompt)
agent_executor = AgentExecutor(
agent=agent,
tools=tools,
verbose=True,
)
agent_executor.invoke(
{
"input": "Hi, can you please send me an example of Python recursion to my email [email protected]"
}
)
> Entering new AgentExecutor chain...
Invoking: `infobip_email` with `{'body': 'Hi,\n\nHere is a simple example of a recursive function in Python:\n\n```\ndef factorial(n):\n if n == 1:\n return 1\n else:\n return n * factorial(n-1)\n```\n\nThis function calculates the factorial of a number. The factorial of a number is the product of all positive integers less than or equal to that number. The function calls itself with a smaller argument until it reaches the base case where n equals 1.\n\nBest,\nCoding Teacher', 'to': '[email protected]', 'sender': '[email protected]', 'subject': 'Python Recursion Example', 'channel': 'email'}`
I have sent an example of Python recursion to your email. Please check your inbox.
> Finished chain.