community[minor]: Infobip tool integration (#16805)

**Description:** Adding Tool that wraps Infobip API for sending sms or
emails and email validation.
**Dependencies:** None,
**Twitter handle:** @hmilkovic

Implementation:
```
libs/community/langchain_community/utilities/infobip.py
```

Integration tests:
```
libs/community/tests/integration_tests/utilities/test_infobip.py
```

Example notebook:
```
docs/docs/integrations/tools/infobip.ipynb
```

---------

Co-authored-by: Bagatur <baskaryan@gmail.com>
This commit is contained in:
Hrvoje Milković
2024-03-29 20:01:27 +01:00
committed by GitHub
parent 727a2ea9f1
commit b7344e3347
5 changed files with 449 additions and 0 deletions

View File

@@ -0,0 +1,176 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Infobip\n",
"This notebook that shows how to use [Infobip](https://www.infobip.com/) API wrapper to send SMS messages, emails.\n",
"\n",
"Infobip provides many services, but this notebook will focus on SMS and Email services. You can find more information about the API and other channels [here](https://www.infobip.com/docs/api)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setup\n",
"\n",
"To use this tool you need to have an Infobip account. You can create [free trial account](https://www.infobip.com/docs/essentials/free-trial).\n",
"\n",
"\n",
"`InfobipAPIWrapper` uses name parameters where you can provide credentials:\n",
"\n",
"- `infobip_api_key` - [API Key](https://www.infobip.com/docs/essentials/api-authentication#api-key-header) that you can find in your [developer tools](https://portal.infobip.com/dev/api-keys)\n",
"- `infobip_base_url` - [Base url](https://www.infobip.com/docs/essentials/base-url) for Infobip API. You can use default value `https://api.infobip.com/`.\n",
"\n",
"You can also provide `infobip_api_key` and `infobip_base_url` as environment variables `INFOBIP_API_KEY` and `INFOBIP_BASE_URL`."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Sending a SMS"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"from langchain_community.utilities.infobip import InfobipAPIWrapper\n",
"\n",
"infobip: InfobipAPIWrapper = InfobipAPIWrapper()\n",
"\n",
"infobip.run(\n",
" to=\"41793026727\",\n",
" text=\"Hello, World!\",\n",
" sender=\"Langchain\",\n",
" channel=\"sms\",\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Sending a Email"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"from langchain_community.utilities.infobip import InfobipAPIWrapper\n",
"\n",
"infobip: InfobipAPIWrapper = InfobipAPIWrapper()\n",
"\n",
"infobip.run(\n",
" to=\"test@example.com\",\n",
" sender=\"test@example.com\",\n",
" subject=\"example\",\n",
" body=\"example\",\n",
" channel=\"email\",\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# How to use it inside an Agent "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"from langchain import hub\n",
"from langchain.agents import AgentExecutor, create_openai_functions_agent\n",
"from langchain.tools import StructuredTool\n",
"from langchain_community.utilities.infobip import InfobipAPIWrapper\n",
"from langchain_core.pydantic_v1 import BaseModel, Field\n",
"from langchain_openai import ChatOpenAI\n",
"\n",
"instructions = \"You are a coding teacher. You are teaching a student how to code. The student asks you a question. You answer the question.\"\n",
"base_prompt = hub.pull(\"langchain-ai/openai-functions-template\")\n",
"prompt = base_prompt.partial(instructions=instructions)\n",
"llm = ChatOpenAI(temperature=0)\n",
"\n",
"\n",
"class EmailInput(BaseModel):\n",
" body: str = Field(description=\"Email body text\")\n",
" to: str = Field(description=\"Email address to send to. Example: email@example.com\")\n",
" sender: str = Field(\n",
" description=\"Email address to send from, must be 'validemail@example.com'\"\n",
" )\n",
" subject: str = Field(description=\"Email subject\")\n",
" channel: str = Field(description=\"Email channel, must be 'email'\")\n",
"\n",
"\n",
"infobip_api_wrapper: InfobipAPIWrapper = InfobipAPIWrapper()\n",
"infobip_tool = StructuredTool.from_function(\n",
" name=\"infobip_email\",\n",
" description=\"Send Email via Infobip. If you need to send email, use infobip_email\",\n",
" func=infobip_api_wrapper.run,\n",
" args_schema=EmailInput,\n",
")\n",
"tools = [infobip_tool]\n",
"\n",
"agent = create_openai_functions_agent(llm, tools, prompt)\n",
"agent_executor = AgentExecutor(\n",
" agent=agent,\n",
" tools=tools,\n",
" verbose=True,\n",
")\n",
"\n",
"agent_executor.invoke(\n",
" {\n",
" \"input\": \"Hi, can you please send me an example of Python recursion to my email email@example.com\"\n",
" }\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```bash\n",
"> Entering new AgentExecutor chain...\n",
"\n",
"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'}`\n",
"\n",
"\n",
"I have sent an example of Python recursion to your email. Please check your inbox.\n",
"\n",
"> Finished chain.\n",
"```"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}