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 #3499

Merged
merged 13 commits into from
Dec 5, 2023
25 changes: 25 additions & 0 deletions docs/core_docs/docs/integrations/toolkits/connery.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import CodeBlock from "@theme/CodeBlock";
import Example from "@examples/tools/connery_mrkl.ts";

# Connery Actions Toolkit

Using this toolkit, you can integrate Connery actions into your LangChain agents and chains.

## What is Connery?

Connery is an open-source plugin infrastructure for AI.

With Connery, you can easily create a custom plugin, which is essentially a set of actions, and use them in your LangChain agents and chains.
Connery will handle the rest: runtime, authorization, secret management, access management, audit logs, and other vital features.
Also, you can find a lot of ready-to-use plugins from our community.

Learn more about Connery:

- GitHub repository: https://github.com/connery-io/connery-platform
- Documentation: https://docs.connery.io

## Usage

This example shows how to create an agent with Connery actions using the Connery Actions Toolkit.

<CodeBlock language="typescript">{Example}</CodeBlock>
25 changes: 25 additions & 0 deletions docs/core_docs/docs/integrations/tools/connery.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import CodeBlock from "@theme/CodeBlock";
import Example from "@examples/agents/connery.ts";

# Connery Actions Tool

Using this tool, you can integrate individual Connery actions into your LangChain agents and chains.

## What is Connery?

Connery is an open-source plugin infrastructure for AI.

With Connery, you can easily create a custom plugin, which is essentially a set of actions, and use them in your LangChain agents and chains.
Connery will handle the rest: runtime, authorization, secret management, access management, audit logs, and other vital features.
Also, you can find a lot of ready-to-use plugins from our community.

Learn more about Connery:

- GitHub repository: https://github.com/connery-io/connery-platform
- Documentation: https://docs.connery.io

## Usage

This example shows how to create a tool for one specific Connery action and call it.

<CodeBlock language="typescript">{Example}</CodeBlock>
2 changes: 2 additions & 0 deletions examples/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ AZURE_OPENAI_API_COMPLETIONS_DEPLOYMENT_NAME=ADD_YOURS_HERE # Azure Portal -> Co
AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME=ADD_YOURS_HERE # Azure Portal -> Cognitive Services -> OpenAI -> Choose your instance -> Go to Azure OpenAI Studio -> Deployments
AZURE_OPENAI_API_VERSION=ADD_YOURS_HERE # Azure Portal -> Cognitive Services -> OpenAI -> Choose your instance -> Go to Azure OpenAI Studio -> Completions/Chat -> Choose Deployment -> View Code
AZURE_OPENAI_BASE_PATH=ADD_YOURS_HERE # Azure Portal -> Cognitive Services -> OpenAI -> Choose your instance -> Endpoint (optional)
CONNERY_RUNNER_URL=ADD_YOURS_HERE
CONNERY_RUNNER_API_KEY=ADD_YOURS_HERE
ELASTIC_URL=ADD_YOURS_HERE # http://127.0.0.1:9200
ELASTIC_USERNAME="elastic"
ELASTIC_PASSWORD=ADD_YOURS_HERE # Password for the 'elastic' user (at least 6 characters)
Expand Down
45 changes: 45 additions & 0 deletions examples/src/agents/connery_mrkl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {
ConneryToolkit,
initializeAgentExecutorWithOptions,
} from "langchain/agents";
import { OpenAI } from "langchain/llms/openai";
import { ConneryService } from "langchain/tools";

/**
* This example shows how to create an agent with Connery actions using the Connery Actions Toolkit.
*
* Connery is an open-source plugin infrastructure for AI.
* Source code: https://github.com/connery-io/connery-platform
*
* To run this example, you need to do some preparation:
* 1. Set up the Connery runner. See a quick start guide here: https://docs.connery.io/docs/platform/quick-start/
* 2. Intsall the "Summarization" plugin (https://github.com/connery-io/summarization-plugin) on the runner.
* 3. Install the "Gmail" plugin (https://github.com/connery-io/gmail) on the runner.
* 4. Set environment variables CONNERY_RUNNER_URL and CONNERY_RUNNER_API_KEY in the ./examples/.env file of this repository.
*
* If you want to use only one particular Connery action in your agent,
* check out an example here: ./examples/src/tools/connery.ts
*/

const model = new OpenAI({ temperature: 0 });
const conneryService = new ConneryService();
const conneryToolkit = await ConneryToolkit.createInstance(conneryService);

const executor = await initializeAgentExecutorWithOptions(
conneryToolkit.tools,
model,
{
agentType: "zero-shot-react-description",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not blocking for this but the OpenAI Functions agent probably has the best performance right now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! I will add another example later.

verbose: true,
}
);

/**
* In this example we use two Connery actions:
* 1. "Summarize public webpage" from the "Summarization" plugin.
* 2. "Send email" from the "Gmail" plugin.
*/
const input =
"Make a short summary of the webpage http://www.paulgraham.com/vb.html in three sentences and send it to [email protected]. Include the link to the webpage into the body of the email.";
const result = await executor.invoke({ input });
console.log(result.output);
37 changes: 37 additions & 0 deletions examples/src/tools/connery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { ConneryService } from "langchain/tools";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey team, just a heads up that I've flagged a change in the PR for review. The added code is accessing environment variables via process.env to fetch an action from the Connery runner, so it's important to ensure everything is handled correctly. Keep up the great work!


/**
* This example shows how to create a tool for one specific Connery action and call it.
*
* Connery is an open-source plugin infrastructure for AI.
* Source code: https://github.com/connery-io/connery-platform
*
* To run this example, you need to do some preparation:
* 1. Set up the Connery runner. See a quick start guide here: https://docs.connery.io/docs/platform/quick-start/
* 2. Install the "Gmail" plugin (https://github.com/connery-io/gmail) on the runner.
* 3. Set environment variables CONNERY_RUNNER_URL and CONNERY_RUNNER_API_KEY in the ./examples/.env file of this repository.
*
* If you want to use several Connery actions in your agent, check out the Connery Toolkit.
* Example of using Connery Toolkit: ./examples/src/agents/connery_mrkl.ts
*/

const conneryService = new ConneryService();

/**
* The "getAction" method fetches the action from the Connery runner by ID,
* constructs a LangChain tool object from it, and returns it to the caller.
*
* In this example, we use the ID of the "Send email" action from the "Gmail" plugin.
* You can find action IDs in the Connery runner.
*/
const tool = await conneryService.getAction("CABC80BB79C15067CA983495324AE709");

/**
* The "call" method of the tool takes a plain English prompt
* with all the information needed to run the Connery action behind the scenes.
*/
const result = await tool.call(
"Send an email to [email protected] with the subject 'Test email' and the body 'This is a test email sent from Langchain Connery tool.'"
);

console.log(result);
2 changes: 2 additions & 0 deletions langchain/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ AZURE_OPENAI_API_COMPLETIONS_DEPLOYMENT_NAME=ADD_YOURS_HERE
AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME=ADD_YOURS_HERE
AZURE_OPENAI_API_VERSION=ADD_YOURS_HERE
AZURE_OPENAI_BASE_PATH=ADD_YOURS_HERE
CONNERY_RUNNER_URL=ADD_YOURS_HERE
CONNERY_RUNNER_API_KEY=ADD_YOURS_HERE
CONVEX_URL=ADD_YOURS_HERE
ELASTIC_URL=http://127.0.0.1:9200
OPENSEARCH_URL=http://127.0.0.1:9200
Expand Down
1 change: 1 addition & 0 deletions langchain/src/agents/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export {
type OutputParserArgs,
} from "./agent.js";
export {
ConneryToolkit,
JsonToolkit,
OpenApiToolkit,
RequestsToolkit,
Expand Down
35 changes: 35 additions & 0 deletions langchain/src/agents/toolkits/connery/connery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Tool } from "@langchain/core/tools";
import { Toolkit } from "../base.js";
import { ConneryService } from "../../../tools/connery.js";

/**
* A toolkit for working with Connery actions.
*
* Connery is an open-source plugin infrastructure for AI.
* Source code: https://github.com/connery-io/connery-platform
*
* See an example of using this toolkit here: `./examples/src/agents/connery_mrkl.ts`
* @extends Toolkit
*/
export class ConneryToolkit extends Toolkit {
tools: Tool[];

/**
* Creates a ConneryToolkit instance based on the provided ConneryService instance.
* It populates the tools property of the ConneryToolkit instance with the list of
* available tools from the ConneryService instance.
* @param conneryService The ConneryService instance.
* @returns A Promise that resolves to a ConneryToolkit instance.
*/
static async createInstance(
conneryService: ConneryService
): Promise<ConneryToolkit> {
const toolkit = new ConneryToolkit();
toolkit.tools = [];

const actions = await conneryService.listActions();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat!

toolkit.tools.push(...actions);

return toolkit;
}
}
1 change: 1 addition & 0 deletions langchain/src/agents/toolkits/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { JsonToolkit, createJsonAgent } from "./json/json.js";
export { ConneryToolkit } from "./connery/connery.js";
export {
RequestsToolkit,
OpenApiToolkit,
Expand Down
Loading