1.0.4 • Published 8 months ago

@radiustechsystems/ai-agent-adapter-langchain v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

Radius AI Agent Toolkit - LangChain Adapter

This adapter provides integration between the Radius AI Agent Toolkit and LangChain, allowing you to easily add Radius capabilities to AI agents built with LangChain.

This package is part of the Radius AI Agent Toolkit, which provides tools for integrating AI agents with the Radius platform.

Installation

# Install this specific package
npm install @radiustechsystems/ai-agent-adapter-langchain

# Required peer dependencies
npm install @radiustechsystems/ai-agent-core
npm install @langchain/core

Prerequisites

  • Node.js >=20.12.2 <23
  • LangChain installed in your project:
    • npm install @langchain/core @langchain/openai langchain
  • OpenAI API key or other LLM provider credentials
  • Radius wallet setup with a funded account

Usage

import { getOnChainTools } from "@radiustechsystems/ai-agent-adapter-langchain";
import { createRadiusWallet, sendETH } from "@radiustechsystems/ai-agent-wallet";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { ChatOpenAI } from "@langchain/openai";
import * as dotenv from "dotenv";

// Load environment variables
dotenv.config();

// 1. Create a Radius wallet (always validate your environment variables)
const rpcUrl = process.env.RPC_PROVIDER_URL;
const privateKey = process.env.WALLET_PRIVATE_KEY;

if (!rpcUrl || !privateKey) {
  console.warn("WARNING: Missing required environment variables");
  console.warn("RPC_PROVIDER_URL and WALLET_PRIVATE_KEY must be set");
}

const wallet = await createRadiusWallet({
  rpcUrl,
  privateKey
});

// Retrieve the wallet address
const address = await wallet.getAddress();
console.log(`Wallet address: ${address}`);

// 2. Configure the tools for LangChain
const tools = await getOnChainTools({
  wallet,
  plugins: [
    sendETH(), // Enable ETH transfers
  ]
});

console.log(`Configured ${tools.length} tools for LangChain`);
tools.forEach(tool => {
  console.log(` - ${tool.name}: ${tool.description}`);
});

// 3. Create a LangChain agent with the tools
const llm = new ChatOpenAI({ temperature: 0 });
const prompt = ChatPromptTemplate.fromMessages([
  ["system", "You are a helpful AI assistant with blockchain capabilities."],
  ["human", "{input}"]
]);

// Verify we have the components for creating a LangChain agent
if (
  llm && typeof llm.invoke === 'function' &&
  tools && Array.isArray(tools) && tools.length > 0 &&
  prompt && typeof prompt.format === 'function'
) {
  console.log("All components for creating a LangChain agent are valid");
  
  // For production use, you would create an agent executor like this:
  // const executor = await createToolAgentExecutor({
  //   llm,
  //   tools,
  //   prompt
  // });
  //
  // const result = await executor.invoke({
  //   input: "Send 0.01 ETH to 0x1234..."
  // });
  //
  // console.log(result.output);
}

If you want to include ERC20 token support, you would add:

import { erc20, USDC } from "@radiustechsystems/ai-agent-plugin-erc20";

// Then in the plugins array:
const tools = await getOnChainTools({
  wallet,
  plugins: [
    sendETH(), 
    erc20({ tokens: [USDC] }) // Add ERC20 token support
  ]
});

API Reference

getOnChainTools(options)

Creates a collection of tools compatible with LangChain that provides access to configured Radius features.

Parameters:

  • options.wallet (WalletClientBase): A Radius wallet instance
  • options.plugins (Array): Array of plugin instances to enable

Returns:

  • (Array): An array of LangChain-compatible tools that can be used with LangChain agents

Default Tools:

The getOnChainTools function automatically provides the following wallet-related tools:

  • get_address: Get the address of the wallet
  • get_chain: Get the chain of the wallet
  • get_balance: Get the balance of an address
  • sign_message: Sign a message with the wallet
  • simulate_transaction: Simulate a transaction to check if it would succeed
  • resolve_address: Resolve an ENS name to an address
  • get_transaction_status: Check the status of a transaction

Example:

const tools = await getOnChainTools({
  wallet,
  plugins: [
    sendETH(),
    erc20({ tokens: [USDC] })
  ]
});

Integration Examples

For complete examples integrating this adapter with LangChain, see:

Troubleshooting

Error: Missing LangChain Dependencies

If you encounter errors related to missing LangChain dependencies, ensure you have installed all required packages:

npm install @langchain/core @langchain/openai langchain

Error: Cannot Create Agent Executor

The LangChain agent creation API may change between versions. If you encounter errors creating the agent executor:

  1. Check your LangChain version and ensure compatibility
  2. Verify that your tools have the expected structure (name, description, and call method)
  3. Check the LangChain documentation for the latest API patterns

Related Packages

Resources

Contributing

Please see the Contributing Guide for detailed information about contributing to this toolkit.

License

This project is licensed under the MIT License.

1.0.4

8 months ago

1.0.3

8 months ago

1.0.2

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago