docs: Adding notebook for cdp agentkit toolkit (#27910)

- **Description:** Adding in the first pass of documentation for the CDP
Agentkit Toolkit
    - **Issue:** N/a
    - **Dependencies:** cdp-langchain
    - **Twitter handle:** @CoinbaseDev

---------

Co-authored-by: Erick Friis <erick@langchain.dev>
Co-authored-by: John Peterson <john.peterson@coinbase.com>
This commit is contained in:
murrlincoln 2024-11-07 04:28:27 +07:00 committed by GitHub
parent ea0ad917b0
commit 14f1827953
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -0,0 +1,332 @@
{
"cells": [
{
"cell_type": "raw",
"id": "afaf8039",
"metadata": {},
"source": [
"---\n",
"sidebar_label: CDP\n",
"---"
]
},
{
"cell_type": "markdown",
"id": "e49f1e0d",
"metadata": {},
"source": [
"# CDP Agentkit Toolkit\n",
"\n",
"The `CDP Agentkit` toolkit contains tools that enable an LLM agent to interact with the [Coinbase Developer Platform](https://docs.cdp.coinbase.com/). The toolkit provides a wrapper around the CDP SDK, allowing agents to perform onchain operations like transfers, trades, and smart contract interactions.\n",
"\n",
"## Overview\n",
"\n",
"### Integration details\n",
"\n",
"| Class | Package | Serializable | JS support | Package latest |\n",
"| :--- | :--- | :---: | :---: | :---: |\n",
"| CdpToolkit | `cdp-langchain` | ❌ | ❌ | ![PyPI - Version](https://img.shields.io/pypi/v/cdp-langchain?style=flat-square&label=%20) |\n",
"\n",
"### Tool features\n",
"\n",
"The toolkit provides the following tools:\n",
"\n",
"1. **get_wallet_details** - Get details about the MPC Wallet\n",
"2. **get_balance** - Get balance for specific assets\n",
"3. **request_faucet_funds** - Request test tokens from faucet\n",
"4. **transfer** - Transfer assets between addresses\n",
"5. **trade** - Trade assets (Mainnet only)\n",
"6. **deploy_token** - Deploy ERC-20 token contracts\n",
"7. **mint_nft** - Mint NFTs from existing contracts\n",
"8. **deploy_nft** - Deploy new NFT contracts\n",
"9. **register_basename** - Register a basename for the wallet\n",
"\n",
"We encourage you to add your own tools, both using CDP and web2 APIs, to create an agent that is tailored to your needs.\n",
"\n",
"## Setup\n",
"\n",
"At a high-level, we will:\n",
"\n",
"1. Install the langchain package\n",
"2. Set up your CDP API credentials\n",
"3. Initialize the CDP wrapper and toolkit\n",
"4. Pass the tools to your agent with `toolkit.get_tools()`"
]
},
{
"cell_type": "markdown",
"id": "72ee0c4b",
"metadata": {},
"source": [
"If you want to get automated tracing from runs of individual tools, you can also set your [LangSmith](https://docs.smith.langchain.com/) API key by uncommenting below:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a15d341e",
"metadata": {},
"outputs": [],
"source": [
"# os.environ[\"LANGSMITH_API_KEY\"] = getpass.getpass(\"Enter your LangSmith API key: \")\n",
"# os.environ[\"LANGSMITH_TRACING\"] = \"true\""
]
},
{
"cell_type": "markdown",
"id": "0730d6a1",
"metadata": {},
"source": [
"### Installation\n",
"\n",
"This toolkit lives in the `cdp-langchain` package:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "652d6238",
"metadata": {},
"outputs": [],
"source": [
"%pip install -qU cdp-langchain"
]
},
{
"cell_type": "markdown",
"id": "a38cde65",
"metadata": {},
"source": [
"#### Set Environment Variables\n",
"\n",
"To use this toolkit, you must first set the following environment variables to access the [CDP APIs](https://docs.cdp.coinbase.com/mpc-wallet/docs/quickstart) to create wallets and interact onchain. You can sign up for an API key for free on the [CDP Portal](https://cdp.coinbase.com/):"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cb09c344",
"metadata": {},
"outputs": [],
"source": [
"import getpass\n",
"import os\n",
"\n",
"for env_var in [\n",
" \"CDP_API_KEY_NAME\",\n",
" \"CDP_API_KEY_PRIVATE_KEY\",\n",
"]:\n",
" if not os.getenv(env_var):\n",
" os.environ[env_var] = getpass.getpass(f\"Enter your {env_var}: \")\n",
"\n",
"# Optional: Set network (defaults to base-sepolia)\n",
"os.environ[\"NETWORK_ID\"] = \"base-sepolia\" # or \"base-mainnet\""
]
},
{
"cell_type": "markdown",
"id": "5c5f2839",
"metadata": {},
"source": [
"## Instantiation\n",
"\n",
"Now we can instantiate our toolkit:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "51a60dbe",
"metadata": {},
"outputs": [],
"source": [
"from cdp_langchain.agent_toolkits import CdpToolkit\n",
"from cdp_langchain.utils import CdpAgentkitWrapper\n",
"\n",
"# Initialize CDP wrapper\n",
"cdp = CdpAgentkitWrapper()\n",
"\n",
"# Create toolkit from wrapper\n",
"toolkit = CdpToolkit.from_cdp_agentkit_wrapper(cdp)"
]
},
{
"cell_type": "markdown",
"id": "d11245ad",
"metadata": {},
"source": [
"## Tools\n",
"\n",
"View [available tools](#tool-features):"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "310bf18e",
"metadata": {},
"outputs": [],
"source": [
"tools = toolkit.get_tools()\n",
"for tool in tools:\n",
" print(tool.name)"
]
},
{
"cell_type": "markdown",
"id": "23e11cc9",
"metadata": {},
"source": [
"## Use within an agent\n",
"\n",
"We will need a LLM or chat model:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d1ee55bc",
"metadata": {},
"outputs": [],
"source": [
"from langchain_openai import ChatOpenAI\n",
"\n",
"llm = ChatOpenAI(model=\"gpt-4o-mini\")"
]
},
{
"cell_type": "markdown",
"id": "3a5bb5ca",
"metadata": {},
"source": [
"Initialize the agent with the tools:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f8a2c4b1",
"metadata": {},
"outputs": [],
"source": [
"from langgraph.prebuilt import create_react_agent\n",
"\n",
"tools = toolkit.get_tools()\n",
"agent_executor = create_react_agent(llm, tools)"
]
},
{
"cell_type": "markdown",
"id": "b4a7c9d2",
"metadata": {},
"source": [
"Example usage:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c9a8e4f3",
"metadata": {},
"outputs": [],
"source": [
"example_query = \"Send 0.005 ETH to john2879.base.eth\"\n",
"\n",
"events = agent_executor.stream(\n",
" {\"messages\": [(\"user\", example_query)]},\n",
" stream_mode=\"values\",\n",
")\n",
"for event in events:\n",
" event[\"messages\"][-1].pretty_print()"
]
},
{
"cell_type": "markdown",
"id": "e5a7c9d4",
"metadata": {},
"source": [
"Expected output:\n",
"```\n",
"Transferred 0.005 of eth to john2879.base.eth.\n",
"Transaction hash for the transfer: 0x78c7c2878659a0de216d0764fc87eff0d38b47f3315fa02ba493a83d8e782d1e\n",
"Transaction link for the transfer: https://sepolia.basescan.org/tx/0x78c7c2878659a0de216d0764fc87eff0d38b47f3315fa02ba493a83d8e782d1\n",
"```"
]
},
{
"cell_type": "markdown",
"id": "f5a7c9d5",
"metadata": {},
"source": [
"## CDP Toolkit Specific Features\n",
"\n",
"### Wallet Management\n",
"\n",
"The toolkit maintains an MPC wallet. The wallet data can be exported and imported to persist between sessions:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "g5a7c9d6",
"metadata": {},
"outputs": [],
"source": [
"# Export wallet data\n",
"wallet_data = cdp.export_wallet()\n",
"\n",
"# Import wallet data\n",
"values = {\"cdp_wallet_data\": wallet_data}\n",
"cdp = CdpAgentkitWrapper(**values)"
]
},
{
"cell_type": "markdown",
"id": "h5a7c9d7",
"metadata": {},
"source": [
"### Network Support\n",
"\n",
"The toolkit supports [multiple networks](https://docs.cdp.coinbase.com/cdp-sdk/docs/networks)\n",
"\n",
"### Gasless Transactions\n",
"\n",
"Some operations support gasless transactions on Base Mainnet:\n",
"- USDC transfers\n",
"- EURC transfers\n",
"- cbBTC transfers"
]
},
{
"cell_type": "markdown",
"id": "i5a7c9d8",
"metadata": {},
"source": [
"## API reference\n",
"\n",
"For detailed documentation of all CDP features and configurations head to the [CDP docs](https://docs.cdp.coinbase.com/mpc-wallet/docs/welcome)."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}