Using Connery Service in LangChain to Run Plugin Actions

Posted: Feb 1, 2025.

Connery is a powerful open-source plugin infrastructure for AI that allows you to integrate custom actions into your LangChain applications. In this guide, we'll explore how to use the ConneryService class to interact with the Connery Runner API.

What is ConneryService?

ConneryService is a class that provides an interface to interact with the Connery Runner API. It allows you to:

  • Get a list of available plugin actions
  • Retrieve specific actions by ID
  • Execute actions with custom inputs

The service acts as a bridge between your LangChain application and Connery's plugin ecosystem, enabling you to leverage pre-built actions or custom plugins in your AI agents.

Reference

Here are the main methods provided by ConneryService:

MethodDescription
list_actions()Returns a list of all available actions from the Connery Runner
get_action(action_id)Retrieves a specific action by its ID
run_action(action_id, input)Executes an action with the given input parameters

How to Use ConneryService

Setting Up ConneryService

First, you'll need to initialize ConneryService with your credentials:

from langchain_community.tools.connery import ConneryService
import os

# Set your Connery credentials
os.environ["CONNERY_RUNNER_URL"] = "your-runner-url"
os.environ["CONNERY_RUNNER_API_KEY"] = "your-api-key"

# Initialize the service
connery_service = ConneryService()

Listing Available Actions

To get all available actions from your Connery Runner:

# Get all available actions
actions = connery_service.list_actions()

Getting a Specific Action

If you know the ID of an action you want to use:

# Get a specific action by ID
send_email_action = connery_service.get_action("CABC80BB79C15067CA983495324AE709")

Running Actions

You can run actions directly through the service:

# Run an action with input parameters
result = connery_service.run_action(
    action_id="CABC80BB79C15067CA983495324AE709",
    input={
        "recipient": "test@example.com",
        "subject": "Test Email",
        "body": "This is a test email from Connery"
    }
)
print(result)

Integration with LangChain Agents

ConneryService works particularly well with LangChain agents that support structured tools. Here's how to use it with an OpenAI Functions agent:

from langchain.agents import AgentType, initialize_agent
from langchain_openai import ChatOpenAI

# Create the LLM
llm = ChatOpenAI(temperature=0)

# Get a specific action to use
send_email_action = connery_service.get_action("CABC80BB79C15067CA983495324AE709")

# Initialize the agent with the action
agent = initialize_agent(
    [send_email_action], 
    llm, 
    AgentType.OPENAI_FUNCTIONS, 
    verbose=True
)

# Run the agent
result = agent.run(
    "Send an email to test@example.com saying I'll be late for the meeting"
)
print(result)

Using Multiple Actions

You can also use multiple actions together in a workflow:

# Get multiple actions
summarize_action = connery_service.get_action("CA72DFB0AB4DF6C830B43E14B0782F70")
email_action = connery_service.get_action("CABC80BB79C15067CA983495324AE709")

# Use them in sequence
summary_result = connery_service.run_action(
    action_id="CA72DFB0AB4DF6C830B43E14B0782F70",
    input={"publicWebpageUrl": "http://example.com"}
)

email_result = connery_service.run_action(
    action_id="CABC80BB79C15067CA983495324AE709",
    input={
        "recipient": "test@example.com",
        "subject": "Page Summary",
        "body": summary_result['summary']
    }
)

Remember that Connery actions are structured tools, so they're best used with agents that support structured tool formats, like the OpenAI Functions agent.

An alternative to LangSmith

Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.

LangChain Docs

Join 10,000+ subscribers

Every 2 weeks, latest model releases and industry news.

An alternative to LangSmith

Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.

LangChain Docs