Skip to main content

Radius adapter for LangChain

Project description

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
pip install radius-ai-agent-sdk-adapter-langchain

# Required dependencies
pip install radius-ai-agent-sdk
pip install langchain langchain-core

Prerequisites

  • Python >=3.10
  • LangChain installed in your project:
    • pip install langchain langchain-core langchain-openai
  • OpenAI API key or other LLM provider credentials
  • Radius wallet setup with a funded account

Usage

import os
from dotenv import load_dotenv

from langchain_openai import ChatOpenAI
from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain_core.prompts import ChatPromptTemplate
from web3 import Web3
from eth_account import Account

from radius_adapters.langchain import get_on_chain_tools
from radius_wallets.evm import send_eth
from radius_wallets.web3 import Web3EVMWalletClient

# Load environment variables
load_dotenv()

# 1. Setup Web3 and wallet
w3 = Web3(Web3.HTTPProvider(os.getenv("RPC_PROVIDER_URL")))
private_key = os.getenv("WALLET_PRIVATE_KEY")
assert private_key is not None, "You must set WALLET_PRIVATE_KEY environment variable"

account = Account.from_key(private_key)
print(f"Wallet address: {account.address}")

# 2. Configure the tools for LangChain
tools = get_on_chain_tools(
    wallet=Web3EVMWalletClient(w3),
    plugins=[
        send_eth(),  # Enable ETH transfers
    ],
)

print(f"Configured {len(tools)} tools for LangChain")
for tool in tools:
    print(f" - {tool.name}: {tool.description}")

# 3. Create a LangChain agent with the tools
llm = ChatOpenAI(temperature=0)
prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful AI assistant with blockchain capabilities."),
    ("human", "{input}"),
    ("placeholder", "{agent_scratchpad}"),
])

# Create and run the agent
agent = create_tool_calling_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

# Example usage
result = agent_executor.invoke({
    "input": "What is my wallet address?"
})

print(result["output"])

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

from radius_plugins.erc20 import erc20, ERC20PluginOptions
from radius_plugins.erc20.token import USDC

# Then in the plugins array:
tools = get_on_chain_tools(
    wallet=Web3EVMWalletClient(w3),
    plugins=[
        send_eth(),
        erc20(options=ERC20PluginOptions(tokens=[USDC]))  # Add ERC20 token support
    ],
)

API Reference

get_on_chain_tools(wallet, plugins)

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

Parameters:

  • wallet (WalletClientBase): A Radius wallet instance
  • plugins (List[Any]): List of plugin instances to enable

Returns:

  • (List[BaseTool]): An array of LangChain-compatible tools that can be used with LangChain agents

Default Tools:

The get_on_chain_tools 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:

tools = get_on_chain_tools(
    wallet=Web3EVMWalletClient(w3),
    plugins=[
        send_eth(),
        erc20(options=ERC20PluginOptions(tokens=[USDC]))
    ],
)

Integration Examples

For complete examples integrating this adapter with LangChain, see:

Development Setup

1. Clone the Repository

git clone git@github.com:radiustechsystems/ai-agent-toolkit.git
cd ai-agent-toolkit/python/src/adapters/langchain

2. Install Development Dependencies

pip install -e ".[dev]"

3. Run Tests

pytest

Troubleshooting

Error: Missing LangChain Dependencies

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

pip install langchain langchain-core langchain-openai

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 function)
  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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file radius_ai_agent_sdk_adapter_langchain-1.0.0.tar.gz.

File metadata

File hashes

Hashes for radius_ai_agent_sdk_adapter_langchain-1.0.0.tar.gz
Algorithm Hash digest
SHA256 c2246f5bbceb21c07bf17c3d4728d0e2c3100cca8fde3cd59fa39474561cc344
MD5 b6c7cebf8513a526502c8fb8bb982757
BLAKE2b-256 39c6b6fd0478e99938a9ecdf8d24454ed78a8bafc9e2ae31b53795a6d3fccfa8

See more details on using hashes here.

File details

Details for the file radius_ai_agent_sdk_adapter_langchain-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for radius_ai_agent_sdk_adapter_langchain-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7b831a39c2583e8e013043fd603a9ff04154e52d9710a403492249ed2364f02f
MD5 14fe594c6014bc981804d3c51266311e
BLAKE2b-256 d5ceafab20dcda6bf6754e2c3f36bf1023d3462cab4d8fcea144d5e7f0bfafd6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page