# Google ADK

## Integrating Google ADK with Agentipy

This guide demonstrates how to integrate Google's Agent Development Kit (ADK) with Agentipy to create intelligent agents that can interact with blockchain networks.

### Prerequisites

* Python 3.9+
* A Solana wallet private key
* Google API key

### Setup

1. **Install required packages**

```bash
pip install agentipy google-adk python-dotenv
```

2. **Create an environment file**

Create a `.env` file in your project root with the following variables:

```
PRIVATE_KEY=your_solana_private_key
GOOGLE_GENAI_USE_VERTEXAI=FALSE
GOOGLE_API_KEY=your_google_api_key
```

### Folder Structure

```
.
├── agent/
│   ├── __init__.py     # Agent configuration definitions
│   └── agent.py        # Task configuration definitions
└── .env
```

#### Basic Structure

The integration follows these key steps:

1. Create your AgentiPy kit for the specific blockchain
2. Define domain-specific tools/functions
3. Create a Google LlmAgent and provide your tools
4. Set the LlmAgent as your root agent

#### Example: Solana Balance Checker

```python
from agentipy.agent import SolanaAgentKit
from agentipy.tools.get_balance import BalanceFetcher
import os
from dotenv import load_dotenv
from google.adk.agents import LlmAgent
from google.adk.agents import Agent
from typing import Optional

# Load environment variables
load_dotenv()

# Initialize Solana agent kit
agent = SolanaAgentKit(
    private_key=os.getenv("PRIVATE_KEY"),
    rpc_url="https://api.mainnet-beta.solana.com"  # Mainnet RPC endpoint
)

# Define a tool/function for your agent
async def get_balance(token_address: Optional[str] = None) -> str:
    """Get the current balance of the wallet.
    Args:
        token_address (Optional[str]): The token address to get the balance of.
    Returns:
        str: The balance of the wallet.
    """
    try:
        balance = await BalanceFetcher.get_balance(agent, token_address)
        return f"Current balance: {balance}"
    except Exception as e:
        return f"Error getting balance: {str(e)}"

# Create a Google LlmAgent with your tool
check_balance = LlmAgent(
    name="Check_Balance",
    description="Check the balance of the wallet for a specific token or SOL",
    tools=[get_balance],
    model="gemini-2.0-flash",
)

# Set as root agent
root_agent = check_balance
```

### Running Your Agent

Create a file named `main.py` to run your agent:

To ensure proper execution, utilize either `adk web` or `adk run` commands. Confirm execution from the root folder

```
// adk run or adk web 
```

### Extending Your Agent

You can add more tools to your agent by:

1. Creating additional functions that use the AgentiPy kit
2. Adding these functions to your LlmAgent's tools list

Example of adding a token transfer tool:

```python
async def transfer_token(recipient: str, amount: float, token_address: Optional[str] = None) -> str:
    """Transfer tokens to another wallet.
    Args:
        recipient (str): The recipient's wallet address.
        amount (float): The amount to transfer.
        token_address (Optional[str]): The token address to transfer. Default is SOL.
    Returns:
        str: The transaction result.
    """
    try:
        # Implement transfer logic using AgentiPy
        return f"Transferred {amount} to {recipient}"
    except Exception as e:
        return f"Error transferring tokens: {str(e)}"

# Update your agent with multiple tools
wallet_agent = LlmAgent(
    name="Wallet_Agent",
    description="Manage wallet balance and transfers",
    tools=[get_balance, transfer_token],
    model="gemini-2.0-flash",
)
```

### Best Practices

1. **Error Handling**: Always include proper error handling in your tool functions
2. **Documentation**: Provide clear docstrings for all tools
3. **Environment Variables**: Keep sensitive information in environment variables
4. **Modular Design**: Create specific agents for different functionalities
5. **Testing**: Test your agents with various inputs before deployment

### Resources

* [Google ADK Documentation](https://google.github.io/adk-docs/)
* [AgentiPy Documentation](https://agentipy.fun/)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.agentipy.fun/integrations/google-adk.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
