Infobip
這個筆記本展示了如何使用 Infobip API 封裝器發送簡訊和電子郵件。
Infobip 提供許多服務,但這個筆記本將專注於簡訊和電子郵件服務。您可以在這裡找到更多關於 API 和其他管道的資訊。
設定
要使用這個工具,您需要有一個 Infobip 帳戶。您可以建立免費試用帳戶。
InfobipAPIWrapper
使用名稱參數,您可以在其中提供憑證
infobip_api_key
- 您可以在您的開發人員工具中找到的 API 金鑰infobip_base_url
- Infobip API 的 基礎 URL。您可以使用預設值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",
)
API 參考資料:InfobipAPIWrapper
發送電子郵件
from langchain_community.utilities.infobip import InfobipAPIWrapper
infobip: InfobipAPIWrapper = InfobipAPIWrapper()
infobip.run(
to="test@example.com",
sender="test@example.com",
subject="example",
body="example",
channel="email",
)
API 參考資料:InfobipAPIWrapper
如何在代理程式內使用它
from langchain import hub
from langchain.agents import AgentExecutor, create_openai_functions_agent
from langchain_community.utilities.infobip import InfobipAPIWrapper
from langchain_core.tools import StructuredTool
from langchain_openai import ChatOpenAI
from pydantic import BaseModel, Field
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="Email body text")
to: str = Field(description="Email address to send to. Example: email@example.com")
sender: str = Field(
description="Email address to send from, must be 'validemail@example.com'"
)
subject: str = Field(description="Email subject")
channel: str = Field(description="Email channel, must be 'email'")
infobip_api_wrapper: InfobipAPIWrapper = InfobipAPIWrapper()
infobip_tool = StructuredTool.from_function(
name="infobip_email",
description="Send Email via Infobip. If you need to send email, use 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@example.com"
}
)
API 參考資料:hub | AgentExecutor | create_openai_functions_agent | InfobipAPIWrapper | StructuredTool | ChatOpenAI
> 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@example.com', 'sender': 'validemail@example.com', 'subject': 'Python Recursion Example', 'channel': 'email'}`
I have sent an example of Python recursion to your email. Please check your inbox.
> Finished chain.