Knit LangChain SDK Guide

Currently in Beta

Welcome to the Knit's LangChain SDK, a powerful toolkit designed to integrate AI-powered agents built using LangChain with a wide range of SaaS applications.

As an embedded integration platform, Knit provides a white-labeled solution empowering SaaS companies to effortlessly scale integrations within their own products, enriching customer experiences with dynamic, out-of-the-box functionality.

The Knit LangChain SDK is designed to facilitate seamless integration between LLM agents and SaaS applications by leveraging Knit's platform and its wide range of connectors.

Installation

Kickstart your journey with the Knit LangChain SDK by installing it via pip:

pip install knit-langchain

Quick Start

First, get your Knit API Key by signing up at https://dashboard.getknit.dev/signup

❗️

Your Knit API key should either be declared as an environment variable with the key, KNIT_API_KEY or be declared while initializing the SDK as
knit = KnitLangChain(api_key="YOUR_KNIT_API_KEY")

Now, we're ready to start using the SDK. Here's a simple guide to help you start integrating with the Knit LangChain SDK:

import logging
from langchain_openai import ChatOpenAI
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain_core.messages import HumanMessage
from langchain_core.prompts import ChatPromptTemplate
from knit_langchain import KnitLangChain, ToolFilter

# Initialize the Knit SDK with your API key
knit = KnitLangChain(api_key="YOUR_KNIT_API_KEY")

# Initialize your Large Language Model (LLM)
model = ChatOpenAI(
    api_key="YOUR_OPENAI_API_KEY",
    model="gpt-4o",  # Choose the appropriate model, e.g., gpt-4o
    temperature=0,   # Set temperature for response variability
)

# Discover available tools from a specific app.
# Use app_id to filter tools for a particular application
tools = knit.find_tools(app_id="charliehr")

# Retrieve specific tools you want to pass to the LLM model.
# Use ToolFilter to select the required tools by their IDs
tool_defs = knit.get_tools(tools=[ToolFilter(app_id="charliehr", tool_ids=[tool.tool_id for tool in tools])])

# Define the conversation prompt template with predefined roles and placeholders
prompt = ChatPromptTemplate.from_messages(
    [
        ("system", "You are a helpful assistant"),  # System message defining the agent role
        ("human", "{input}"),  # Placeholder for human input
        ("placeholder", "{agent_scratchpad}"),  # Placeholder for intermediate steps
    ]
)

# Create the agent capable of using the defined tools and prompt
agent = create_tool_calling_agent(model, tool_defs, prompt)

# Create an executor to run the agent with detailed output
agent_executor = AgentExecutor(agent=agent, tools=tool_defs, verbose=True)

# Configuration settings required for tool execution
config = {"knit_integration_id": "b29fcTlZc2IwSzViSUF1NXI5SmhqOHdydTpjaGFybGllaHI="}

# Invoke the agent to perform a task, supplying necessary inputs and configuration
agent_executor.invoke(
    {"input": "I want to get the list of offices of the company"},
    config={"configurable": config}
)

That's it! It's that easy to get started and add hundreds of SaaS applications to your AI Agent.


How to use Tools

After discovering tools using find_tools, use get_tools to fetch the actual tool definitions that will be passed to your LLM model. This function converts the tool summaries into usable tool objects that your LangChain agents can interact with.

  1. Find Tools First: Typically, you'll first use find_tools to discover available tools.

  2. Create Tool Filters: Use the ToolFilter class to specify which tools you want to retrieve.

  3. Call get_tools: Pass your filters to retrieve the actual tool definitions.

  4. Use with Your LLM Agent: These tool definitions can be directly passed to your LangChain agent.

Let's break each step down:

Discover Tools for an App or Unified Tools for a Category

📘

You can discover and filter tools on the basis of the App ID, Category ID, Entities, Operation, or Usecase. To read more about it in depth, refer to the detailed guide here

The find_tools function in the Knit LangChain SDK allows you to discover tools that are available for integration with a specific app.

The function will return a list of tools. You can then select and use these tools in your AI agents.

Here's a quick example to demonstrate how you can use the find_tools function:

# You can discover and filter tools on the basis of the App ID, Category ID, Entities, Operation, or
# Usecase. To read more about it in depth, refer to the detailed guide:
# https://developers.getknit.dev/docs/find-and-discover-tools-langchain

from knit_langchain import KnitLangChain

# Initialize the Knit SDK with your API key
knit = KnitLangChain(api_key="YOUR_KNIT_API_KEY")

# Find tools related to a specific app and operation
tools = knit.find_tools(app_id="charliehr", operation="read", usecase="I want to fetch the list of employees")

# Display tool information
for tool in tools:
    print(f"Tool ID: {tool.tool_id}")
    print(f"Title: {tool.title}")
    print(f"Description: {tool.description}")

To read more about it in depth, refer to the detailed guide here

Get Tools and Use with Your LLM Agent

The get_tools function in the Knit LangChain SDK allows you to retrieve specific tools based on your filtering criteria. This guide explains how to use this function to fetch the exact tools you need for your LLM agent integrations.

The function returns a list of tool definitions that can be directly used with LangChain agents. These are fully functional tool objects that your LLM can use to interact with external applications.

Here's a quick example to demonstrate how you can use the get_tools function:

from knit_langchain import KnitLangChain, ToolFilter

# Initialize the Knit SDK with your API key
knit = KnitLangChain(api_key="YOUR_KNIT_API_KEY")

# First, discover available tools
discovered_tools = knit.find_tools(app_id="charliehr")

# Create a filter to get specific tools
tool_filter = ToolFilter(
    app_id="charliehr",
    tool_ids=[tool.tool_id for tool in discovered_tools[:3]]  # Get the first 3 tools
)

# Get the actual tool definitions
tool_definitions = knit.get_tools(tools=[tool_filter])

# These tool definitions can now be passed to your LLM agent
# For example:
agent_executor = AgentExecutor(agent=agent, tools=tool_defs, verbose=True)

To read more about it in depth, refer to the detailed guide here


That's it! With just a few lines of code, you've unlocked the ability to add hundreds of SaaS integrations to your AI agents. The possibilities are endless - from automating workflows across multiple platforms to creating seamless experiences for your users.

We can't wait to see what you build with the Knit LangChain SDK! 🚀 As you explore and create, remember we're here to help - reach out anytime at [email protected].