Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Connery Tool and Toolkit #14506

Merged
merged 36 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
a147b61
Add ConneryAPIWrapper and integration tests
machulav Dec 10, 2023
61f453d
Merge branch 'master' into connery-tool
machulav Dec 10, 2023
19f2e3a
Refactor ConneryAPIWrapper class and add docstrings
machulav Dec 10, 2023
bd3ad4d
Add ConneryAction Tool and ConneryService
machulav Dec 10, 2023
a47db02
Update Connery Action Tool, add docs
machulav Dec 11, 2023
93698ce
Update Connery service and tool
machulav Dec 11, 2023
c487483
Refactor ConneryService and ConneryAction classes
machulav Dec 11, 2023
6b22255
Add ConneryToolkit, docs and examples
machulav Dec 12, 2023
7a78e0f
Merge branch 'master' into connery-tool
machulav Dec 12, 2023
e8531b7
Update name of the test file
machulav Dec 12, 2023
db07f40
Merge branch 'master' into connery-tool
machulav Dec 12, 2023
09ba23a
Fix linter
machulav Dec 12, 2023
625cf52
Fix linter
machulav Dec 12, 2023
3254e86
Fix linter
machulav Dec 12, 2023
51fa9b9
Merge branch 'master' into connery-tool
machulav Dec 12, 2023
6e943c1
Add ConneryAction to list of expected imports
machulav Dec 12, 2023
fd43147
Update docs
machulav Dec 12, 2023
f26e636
Update docs
machulav Dec 12, 2023
565d453
Merge branch 'master' into connery-tool
machulav Dec 12, 2023
9b25ce0
Merge branch 'master' into connery-tool
machulav Dec 13, 2023
d63b6ea
Add _arun implementation
machulav Dec 13, 2023
2fdeb94
Move Connery tool and toolkit to the community library
machulav Dec 13, 2023
39d8418
Fix linter
machulav Dec 13, 2023
e4e1f7a
Update docs and examples
machulav Dec 18, 2023
85f4acc
Fix formatting
machulav Dec 18, 2023
a599267
Update wording
machulav Dec 22, 2023
2e4bd99
Merge branch 'master' into connery-tool
machulav Jan 2, 2024
fd0f920
Merge branch 'master' into connery-tool
machulav Jan 2, 2024
d106d0f
Update URLs
machulav Jan 3, 2024
178f6f8
Merge branch 'connery-tool' of github.com:connery-io/langchain into c…
machulav Jan 3, 2024
755a5f7
Merge branch 'master' into connery-tool
machulav Jan 3, 2024
6f90a50
Merge branch 'master' into connery-tool
machulav Jan 15, 2024
24dbfa0
Merge branch 'master' into connery-tool
machulav Jan 29, 2024
cf0c634
Fix imports
machulav Jan 29, 2024
9840543
Fix tests
machulav Jan 29, 2024
5a1ec90
Fix format
machulav Jan 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions docs/docs/integrations/toolkits/connery.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Connery Toolkit\n",
"\n",
"Using this toolkit, you can integrate Connery Actions into your LangChain agent.\n",
"\n",
"If you want to use only one particular Connery Action in your agent,\n",
"check out the [Connery Action Tool](/docs/integrations/tools/connery) documentation.\n",
"\n",
"## What is Connery?\n",
"\n",
"Connery is an open-source plugin infrastructure for AI.\n",
"\n",
"With Connery, you can easily create a custom plugin with a set of actions and seamlessly integrate them into your LangChain agent.\n",
"Connery will take care of critical aspects such as runtime, authorization, secret management, access management, audit logs, and other vital features.\n",
"\n",
"Furthermore, Connery, supported by our community, provides a diverse collection of ready-to-use open-source plugins for added convenience.\n",
"\n",
"Learn more about Connery:\n",
"\n",
"- GitHub: https://github.com/connery-io/connery-platform\n",
"- Documentation: https://docs.connery.io\n",
"\n",
"## Prerequisites\n",
"\n",
"To use Connery Actions in your LangChain agent, you need to do some preparation:\n",
"\n",
"1. Set up the Connery runner using the [Quickstart](https://docs.connery.io/docs/platform/quick-start/) guide.\n",
"2. Install all the plugins with the actions you want to use in your agent.\n",
"3. Set environment variables `CONNERY_RUNNER_URL` and `CONNERY_RUNNER_API_KEY` so the toolkit can communicate with the Connery Runner.\n",
"\n",
"## Example of using Connery Toolkit\n",
"\n",
"In the example below, we create an agent that uses two Connery Actions to summarize a public webpage and send the summary by email:\n",
"\n",
"1. **Summarize public webpage** action from the [Summarization](https://github.com/connery-io/summarization-plugin) plugin.\n",
"2. **Send email** action from the [Gmail](https://github.com/connery-io/gmail) plugin.\n",
"\n",
"You can see a LangSmith trace of this example [here](https://smith.langchain.com/public/4af5385a-afe9-46f6-8a53-57fe2d63c5bc/r)."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
"\u001b[32;1m\u001b[1;3m\n",
"Invoking: `CA72DFB0AB4DF6C830B43E14B0782F70` with `{'publicWebpageUrl': 'http://www.paulgraham.com/vb.html'}`\n",
"\n",
"\n",
"\u001b[0m\u001b[33;1m\u001b[1;3m{'summary': 'The author reflects on the concept of life being short and how having children made them realize the true brevity of life. They discuss how time can be converted into discrete quantities and how limited certain experiences are. The author emphasizes the importance of prioritizing and eliminating unnecessary things in life, as well as actively pursuing meaningful experiences. They also discuss the negative impact of getting caught up in online arguments and the need to be aware of how time is being spent. The author suggests pruning unnecessary activities, not waiting to do things that matter, and savoring the time one has.'}\u001b[0m\u001b[32;1m\u001b[1;3m\n",
"Invoking: `CABC80BB79C15067CA983495324AE709` with `{'recipient': '[email protected]', 'subject': 'Summary of the webpage', 'body': 'Here is a short summary of the webpage http://www.paulgraham.com/vb.html:\\n\\nThe author reflects on the concept of life being short and how having children made them realize the true brevity of life. They discuss how time can be converted into discrete quantities and how limited certain experiences are. The author emphasizes the importance of prioritizing and eliminating unnecessary things in life, as well as actively pursuing meaningful experiences. They also discuss the negative impact of getting caught up in online arguments and the need to be aware of how time is being spent. The author suggests pruning unnecessary activities, not waiting to do things that matter, and savoring the time one has.\\n\\nYou can find the full webpage [here](http://www.paulgraham.com/vb.html).'}`\n",
"\n",
"\n",
"\u001b[0m\u001b[33;1m\u001b[1;3m{'messageId': '<[email protected]>'}\u001b[0m\u001b[32;1m\u001b[1;3mI have sent the email with the summary of the webpage to [email protected]. Please check your inbox.\u001b[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n",
"I have sent the email with the summary of the webpage to [email protected]. Please check your inbox.\n"
]
}
],
"source": [
"import os\n",
"\n",
"from langchain.agents import AgentType, initialize_agent\n",
"from langchain.chat_models import ChatOpenAI\n",
"from langchain_community.agent_toolkits.connery import ConneryToolkit\n",
"from langchain_community.tools.connery import ConneryService\n",
"\n",
"# Specify your Connery Runner credentials.\n",
"os.environ[\"CONNERY_RUNNER_URL\"] = \"\"\n",
"os.environ[\"CONNERY_RUNNER_API_KEY\"] = \"\"\n",
"\n",
"# Specify OpenAI API key.\n",
"os.environ[\"OPENAI_API_KEY\"] = \"\"\n",
"\n",
"# Specify your email address to receive the email with the summary from example below.\n",
"recepient_email = \"[email protected]\"\n",
"\n",
"# Create a Connery Toolkit with all the available actions from the Connery Runner.\n",
"connery_service = ConneryService()\n",
"connery_toolkit = ConneryToolkit.create_instance(connery_service)\n",
"\n",
"# Use OpenAI Functions agent to execute the prompt using actions from the Connery Toolkit.\n",
"llm = ChatOpenAI(temperature=0)\n",
"agent = initialize_agent(\n",
" connery_toolkit.get_tools(), llm, AgentType.OPENAI_FUNCTIONS, verbose=True\n",
")\n",
"result = agent.run(\n",
" f\"\"\"Make a short summary of the webpage http://www.paulgraham.com/vb.html in three sentences\n",
"and send it to {recepient_email}. Include the link to the webpage into the body of the email.\"\"\"\n",
")\n",
"print(result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"NOTE: Connery Action is a structured tool, so you can only use it in the agents supporting structured tools."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
165 changes: 165 additions & 0 deletions docs/docs/integrations/tools/connery.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Connery Action Tool\n",
"\n",
"Using this tool, you can integrate individual Connery Action into your LangChain agent.\n",
"\n",
"If you want to use more than one Connery Action in your agent,\n",
"check out the [Connery Toolkit](/docs/integrations/toolkits/connery) documentation.\n",
"\n",
"## What is Connery?\n",
"\n",
"Connery is an open-source plugin infrastructure for AI.\n",
"\n",
"With Connery, you can easily create a custom plugin with a set of actions and seamlessly integrate them into your LangChain agent.\n",
"Connery will take care of critical aspects such as runtime, authorization, secret management, access management, audit logs, and other vital features.\n",
"\n",
"Furthermore, Connery, supported by our community, provides a diverse collection of ready-to-use open-source plugins for added convenience.\n",
"\n",
"Learn more about Connery:\n",
"\n",
"- GitHub: https://github.com/connery-io/connery-platform\n",
"- Documentation: https://docs.connery.io\n",
"\n",
"## Prerequisites\n",
"\n",
"To use Connery Actions in your LangChain agent, you need to do some preparation:\n",
"\n",
"1. Set up the Connery runner using the [Quickstart](https://docs.connery.io/docs/platform/quick-start/) guide.\n",
"2. Install all the plugins with the actions you want to use in your agent.\n",
"3. Set environment variables `CONNERY_RUNNER_URL` and `CONNERY_RUNNER_API_KEY` so the toolkit can communicate with the Connery Runner.\n",
"\n",
"## Example of using Connery Action Tool\n",
"\n",
"In the example below, we fetch action by its ID from the Connery Runner and then call it with the specified parameters.\n",
"\n",
"Here, we use the ID of the **Send email** action from the [Gmail](https://github.com/connery-io/gmail) plugin."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"from langchain.agents import AgentType, initialize_agent\n",
"from langchain.chat_models import ChatOpenAI\n",
"from langchain_community.tools.connery import ConneryService\n",
"\n",
"# Specify your Connery Runner credentials.\n",
"os.environ[\"CONNERY_RUNNER_URL\"] = \"\"\n",
"os.environ[\"CONNERY_RUNNER_API_KEY\"] = \"\"\n",
"\n",
"# Specify OpenAI API key.\n",
"os.environ[\"OPENAI_API_KEY\"] = \"\"\n",
"\n",
"# Specify your email address to receive the emails from examples below.\n",
"recepient_email = \"[email protected]\"\n",
"\n",
"# Get the SendEmail action from the Connery Runner by ID.\n",
"connery_service = ConneryService()\n",
"send_email_action = connery_service.get_action(\"CABC80BB79C15067CA983495324AE709\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Run the action manually."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"manual_run_result = send_email_action.run(\n",
" {\n",
" \"recipient\": recepient_email,\n",
" \"subject\": \"Test email\",\n",
" \"body\": \"This is a test email sent from Connery.\",\n",
" }\n",
")\n",
"print(manual_run_result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Run the action using the OpenAI Functions agent.\n",
"\n",
"You can see a LangSmith trace of this example [here](https://smith.langchain.com/public/a37d216f-c121-46da-a428-0e09dc19b1dc/r)."
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
"\u001b[32;1m\u001b[1;3m\n",
"Invoking: `CABC80BB79C15067CA983495324AE709` with `{'recipient': '[email protected]', 'subject': 'Late for Meeting', 'body': 'Dear Team,\\n\\nI wanted to inform you that I will be late for the meeting today. I apologize for any inconvenience caused. Please proceed with the meeting without me and I will join as soon as I can.\\n\\nBest regards,\\n[Your Name]'}`\n",
"\n",
"\n",
"\u001b[0m\u001b[36;1m\u001b[1;3m{'messageId': '<[email protected]>'}\u001b[0m\u001b[32;1m\u001b[1;3mI have sent an email to [email protected] informing them that you will be late for the meeting.\u001b[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n",
"I have sent an email to [email protected] informing them that you will be late for the meeting.\n"
]
}
],
"source": [
"llm = ChatOpenAI(temperature=0)\n",
"agent = initialize_agent(\n",
" [send_email_action], llm, AgentType.OPENAI_FUNCTIONS, verbose=True\n",
")\n",
"agent_run_result = agent.run(\n",
" f\"Send an email to the {recepient_email} and say that I will be late for the meeting.\"\n",
")\n",
"print(agent_run_result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"NOTE: Connery Action is a structured tool, so you can only use it in the agents supporting structured tools."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
2 changes: 2 additions & 0 deletions libs/community/langchain_community/agent_toolkits/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from langchain_community.agent_toolkits.azure_cognitive_services import (
AzureCognitiveServicesToolkit,
)
from langchain_community.agent_toolkits.connery import ConneryToolkit
from langchain_community.agent_toolkits.file_management.toolkit import (
FileManagementToolkit,
)
Expand Down Expand Up @@ -49,6 +50,7 @@
"AINetworkToolkit",
"AmadeusToolkit",
"AzureCognitiveServicesToolkit",
"ConneryToolkit",
"FileManagementToolkit",
"GmailToolkit",
"JiraToolkit",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""
This module contains the ConneryToolkit.
"""

from .toolkit import ConneryToolkit

__all__ = ["ConneryToolkit"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from typing import List

from langchain_core.pydantic_v1 import root_validator
from langchain_core.tools import BaseTool

from langchain_community.agent_toolkits.base import BaseToolkit
from langchain_community.tools.connery import ConneryService


class ConneryToolkit(BaseToolkit):
"""
A LangChain Toolkit with a list of Connery Actions as tools.
"""

tools: List[BaseTool]

def get_tools(self) -> List[BaseTool]:
"""
Returns the list of Connery Actions.
"""
return self.tools

@root_validator()
def validate_attributes(cls, values: dict) -> dict:
"""
Validate the attributes of the ConneryToolkit class.
Parameters:
values (dict): The arguments to validate.
Returns:
dict: The validated arguments.
"""

if not values.get("tools"):
raise ValueError("The attribute 'tools' must be set.")

return values

@classmethod
def create_instance(cls, connery_service: ConneryService) -> "ConneryToolkit":
"""
Creates a Connery Toolkit using a Connery Service.
Parameters:
connery_service (ConneryService): The Connery Service
to to get the list of Connery Actions.
Returns:
ConneryToolkit: The Connery Toolkit.
"""

instance = cls(tools=connery_service.list_actions())

return instance
Loading
Loading