Skip to content

You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert

{{ message }}

goat-sdk/ goat Public

File tree

Expand file treeCollapse file tree

Open diff view settings

Filter options

Expand file treeCollapse file tree

Open diff view settings

Collapse file

‎README.md‎

Copy file name to clipboardExpand all lines: README.md

+1Lines changed: 1 addition & 0 deletions

  • Display the source diff
  • Display the rich diff
Original file line number Diff line number Diff line change
@@ -219,6 +219,7 @@ GOAT is free software, MIT licensed.<br>
219 219 `
220 220 `
221 221 `
`` 222 `+
222 223 `
223 224 `
224 225 `
<br>

Collapse file

‎python/README.md‎

Copy file name to clipboardExpand all lines: python/README.md

+1Lines changed: 1 addition & 0 deletions

  • Display the source diff
  • Display the rich diff
Original file line number Diff line number Diff line change
@@ -83,6 +83,7 @@ GOAT is free software, MIT licensed.<br>
83 83 - [OpenAI Agents SDK](/content/goat-sdk/goat/tree/main/python/examples/by-framework/openai-agents-sdk/index.html)<br>
84 84 - [AG2](/content/goat-sdk/goat/tree/main/python/examples/by-framework/ag2/index.html)<br>
85 85 - [CrewAI](/content/goat-sdk/goat/tree/main/python/examples/by-framework/crewai/index.html)<br>
`` 86 + - [Smolagents](/content/goat-sdk/goat/tree/main/python/examples/by-framework/smolagents/index.html)<br>
86 87 - [GAME Agent](/content/game-by-virtuals/game-python/tree/main/plugins/onchain_actions/index.html)<br>
87 88 - [ZerePy](/content/blorm-network/ZerePy/blob/main/src/connections/goat_connection.py)<br>
88 89 - **By wallet**<br>
<br>

Collapse file

‎python/examples/by-framework/smolagents/.env.template‎

Copy file name to clipboard

+20Lines changed: 20 additions & 0 deletions

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@<br>
`` 1 +# Solana configuration<br>
`` 2 +SOLANA_RPC_ENDPOINT=https://api.mainnet-beta.solana.com<br>
`` 3 +SOLANA_WALLET_SEED=your_wallet_private_key_in_base58<br>
`` 4 +<br>
`` 5 +# Smolagents model configuration<br>
`` 6 +# Choose ONE of the following model options:<br>
`` 7 +<br>
`` 8 +# Option 1: OpenAI API (used in the example by default)<br>
`` 9 +OPENAI_API_KEY=your_openai_api_key<br>
`` 10 +<br>
`` 11 +# Option 2: HuggingFace API<br>
`` 12 +# Comment out the OPENAI_API_KEY above and uncomment:<br>
`` 13 +#HF_MODEL_NAME=microsoft/phi-3-mini-4k-instruct<br>
`` 14 +#HF_API_TOKEN=your_huggingface_token_if_needed_for_model_access<br>
`` 15 +<br>
`` 16 +# Option 3: Local models - requires code changes in example.py<br>
`` 17 +# Use HfLocalModel instead of OpenAIServerModel<br>
`` 18 +<br>
`` 19 +# Option 4: VLLM deployment - requires code changes in example.py<br>
`` 20 +# Use VLLMModel instead of OpenAIServerModel<br>

Collapse file

‎python/examples/by-framework/smolagents/README.md‎

Copy file name to clipboard

+131Lines changed: 131 additions & 0 deletions

  • Display the source diff
  • Display the rich diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@<br>
`` 1 +<div align="center"><br>
`` 2 +<img src="https://github.com/user-attachments/assets/5fc7f121-259c-492c-8bca-f15fe7eb830c" alt="GOAT" width="100px" height="auto" style="object-fit: contain;"><br>
`` 3 +</div><br>
`` 4 +<br>
`` 5 +# GOAT SDK with Smolagents Example<br>
`` 6 +<br>
`` 7 +This example demonstrates how to use the GOAT SDK with [Smolagents](/content/huggingface/smolagents/index.html) to build an agent that can interact with blockchain tools.<br>
`` 8 +<br>
`` 9 +## Prerequisites<br>
`` 10 +<br>
`` 11 +Before running this example, you need to:<br>
`` 12 +<br>
`` 13 +1. Have a Solana wallet with a valid keypair<br>
`` 14 +2. Access to a Solana RPC endpoint<br>
`` 15 +3. Have [Poetry](https://python-poetry.org/docs/#installation) installed<br>
`` 16 +<br>
`` 17 +## Installation<br>
`` 18 +<br>
`` 19 +bash
```
`` 20 +# Install dependencies using Poetry<br>
`` 21 +poetry install<br>
`` 22 +
```
`` 23 +<br>
`` 24 +If you're not using the provided Poetry environment, you can install the dependencies manually:<br>
`` 25 +<br>
`` 26 +bash
```
`` 27 +# Install with Poetry<br>
`` 28 +poetry add goat-sdk goat-sdk-adapter-smolagents smolagents solders solana-sdk python-dotenv<br>
`` 29 +
```
`` 30 +<br>
`` 31 +## Configuration<br>
`` 32 +<br>
`` 33 +Create a `.env` file in this directory with the following variables:<br>
`` 34 +<br>
`` 35 +
```
`` 36 +SOLANA_RPC_ENDPOINT=<your-solana-rpc-endpoint><br>
`` 37 +SOLANA_WALLET_SEED=<your-wallet-private-key><br>
`` 38 +OPENAI_API_KEY=<your-openai-api-key> # For using OpenAI models<br>
`` 39 +# HF_MODEL_NAME=microsoft/phi-3-mini-4k-instruct # For using HuggingFace models<br>
`` 40 +# HF_API_TOKEN=<your-huggingface-token> # Optional, for accessing gated models<br>
`` 41 +
```
`` 42 +<br>
`` 43 +The `SOLANA_WALLET_SEED` should be a base58-encoded private key of your Solana wallet.<br>
`` 44 +<br>
`` 45 +### Model Selection<br>
`` 46 +<br>
`` 47 +SmolaGents provides multiple model providers that you can use with GOAT:<br>
`` 48 +<br>
`` 49 +#### 1. OpenAI Models (Default in this example)<br>
`` 50 +By default, this example uses OpenAI's GPT-4o model, which offers excellent capabilities for agent interaction. To use OpenAI models:<br>
`` 51 +
```
`` 52 +OPENAI_API_KEY=<your-openai-api-key><br>
`` 53 +
```
`` 54 +<br>
`` 55 +#### 2. HuggingFace API Models<br>
`` 56 +You can use models hosted on HuggingFace's Inference API by providing:<br>
`` 57 +
```
`` 58 +# Comment out or remove OPENAI_API_KEY<br>
`` 59 +HF_MODEL_NAME=mistralai/Mistral-7B-Instruct-v0.2<br>
`` 60 +HF_API_TOKEN=<your-huggingface-token> # Optional, for accessing gated models<br>
`` 61 +
```
`` 62 +<br>
`` 63 +#### 3. Local Models<br>
`` 64 +SmolaGents supports running models locally (requires modifying the example script):<br>
`` 65 +python
```
`` 66 +from smolagents import TransformersModel, ToolCallingAgent<br>
`` 67 +<br>
`` 68 +model = TransformersModel(<br>
`` 69 + model_id="microsoft/phi-3-mini-4k-instruct",<br>
`` 70 + # Additional configuration like device, quantization, etc.<br>
`` 71 +)<br>
`` 72 +
```
`` 73 +<br>
`` 74 +#### 4. VLLM Deployment<br>
`` 75 +For high-performance inference with VLLM:<br>
`` 76 +python
```
`` 77 +from smolagents import VLLMModel, ToolCallingAgent<br>
`` 78 +<br>
`` 79 +model = VLLMModel(<br>
`` 80 + model_id="mistralai/Mistral-7B-Instruct-v0.2",<br>
`` 81 + server_url="http://localhost:8000" # Your VLLM server<br>
`` 82 +)<br>
`` 83 +
```
`` 84 +<br>
`` 85 +Recommended models include:<br>
`` 86 +- OpenAI: `gpt-4o` or `gpt-3.5-turbo` for highest capability<br>
`` 87 +- HuggingFace larger models: `mistralai/Mistral-7B-Instruct-v0.2`<br>
`` 88 +- HuggingFace smaller models: `microsoft/phi-3-mini-4k-instruct` (3.8B parameters)<br>
`` 89 +- Very small models: `TinyLlama/TinyLlama-1.1B-Chat-v1.0`<br>
`` 90 +<br>
`` 91 +## Running the Example<br>
`` 92 +<br>
`` 93 +bash
```
`` 94 +# Using Poetry<br>
`` 95 +poetry run python example.py<br>
`` 96 +
```
`` 97 +<br>
`` 98 +This will start an interactive session where you can ask questions about SPL tokens related to your wallet. The agent will use the appropriate tools from GOAT SDK to fetch information from the blockchain and provide answers.<br>
`` 99 +<br>
`` 100 +## Example Queries<br>
`` 101 +<br>
`` 102 +Here are some example questions you can ask:<br>
`` 103 +<br>
`` 104 +- "What is the balance of USDC in my wallet?"<br>
`` 105 +- "Can you tell me more about the SOL token?"<br>
`` 106 +- "How many different tokens do I have in my wallet?"<br>
`` 107 +<br>
`` 108 +## How It Works<br>
`` 109 +<br>
`` 110 +1. The script initializes a Solana wallet and the SPL Token plugin using GOAT SDK<br>
`` 111 +2. It creates Smolagents-compatible tools using the GOAT adapter<br>
`` 112 +3. It sets up a Smolagents CodeAgent with these tools<br>
`` 113 +4. It runs an interactive loop that processes user questions using the agent<br>
`` 114 +<br>
`` 115 +The agent uses the right tools automatically based on the user's query.<br>
`` 116 +<br>
`` 117 +### Agent Wallets<br>
`` 118 +[Crossmint](https://docs.crossmint.com/wallets/quickstarts/agent-wallets) offers one of the most advanced solutions for agent developers and launchpads: [Agent Wallets](https://docs.crossmint.com/wallets/quickstarts/agent-wallets).<br>
`` 119 +<br>
`` 120 +To integrate Agent Wallets with GOAT, check out the following quickstarts:<br>
`` 121 +1. Agent Wallets Quickstart [[EVM](/content/goat-sdk/goat/tree/main/python/examples/by-wallet/crossmint/index.html), [Solana](/content/goat-sdk/goat/tree/main/python/examples/by-wallet/crossmint/index.html)]<br>
`` 122 +2. [Agent Launchpad Starter Kit](/content/Crossmint/agent-launchpad-starter-kit/index.html)<br>
`` 123 +<br>
`` 124 +<footer><br>
`` 125 +<br/><br>
`` 126 +<br/><br>
`` 127 +<div><br>
`` 128 + <img src="https://github.com/user-attachments/assets/59fa5ddc-9d47-4d41-a51a-64f6798f94bd" alt="GOAT" width="100%" height="auto" style="object-fit: contain; max-width: 800px;"><br>
`` 129 +<br>
`` 130 +<div><br>
`` 131 +</footer><br>

Collapse file

‎python/examples/by-framework/smolagents/example.py‎

Copy file name to clipboard

+106Lines changed: 106 additions & 0 deletions

Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@<br>
`` 1 +import os<br>
`` 2 +from dotenv import load_dotenv<br>
`` 3 +from solders.keypair import Keypair # Import Keypair<br>
`` 4 +from solana.rpc.api import Client as SolanaClient # Import Solana Client<br>
`` 5 +<br>
`` 6 +# Smolagents imports<br>
`` 7 +from smolagents import OpenAIServerModel, ToolCallingAgent<br>
`` 8 +<br>
`` 9 +# GOAT imports<br>
`` 10 +from goat_adapters.smolagents import get_smolagents_tools<br>
`` 11 +from goat_plugins.spl_token import spl_token, SplTokenPluginOptions<br>
`` 12 +from goat_plugins.spl_token.tokens import SPL_TOKENS<br>
`` 13 +from goat_wallets.solana import solana<br>
`` 14 +<br>
`` 15 +# Load environment variables<br>
`` 16 +load_dotenv()<br>
`` 17 +<br>
`` 18 +# --- Setup GOAT Wallet and Plugins ---<br>
`` 19 +print("Initializing Solana wallet and GOAT plugins...")<br>
`` 20 +solana_rpc_endpoint = os.getenv("SOLANA_RPC_ENDPOINT")<br>
`` 21 +solana_wallet_seed = os.getenv("SOLANA_WALLET_SEED")<br>
`` 22 +openai_api_key = os.getenv("OPENAI_API_KEY")<br>
`` 23 +<br>
`` 24 +if not solana_rpc_endpoint or not solana_wallet_seed:<br>
`` 25 + print("Error: Please set SOLANA_RPC_ENDPOINT and SOLANA_WALLET_SEED in your .env file.")<br>
`` 26 + exit(1)<br>
`` 27 +<br>
`` 28 +if not openai_api_key:<br>
`` 29 + print("Error: Please set OPENAI_API_KEY in your .env file to use GPT-4o.")<br>
`` 30 + exit(1)<br>
`` 31 +<br>
`` 32 +try:<br>
`` 33 + client = SolanaClient(solana_rpc_endpoint)<br>
`` 34 + keypair = Keypair.from_base58_string(solana_wallet_seed)<br>
`` 35 + wallet = solana(client, keypair)<br>
`` 36 +<br>
`` 37 + spl_token_plugin = spl_token(<br>
`` 38 + SplTokenPluginOptions(<br>
`` 39 + network="mainnet", # Make sure this matches your .env settings and seed key network<br>
`` 40 + tokens=SPL_TOKENS,<br>
`` 41 + )<br>
`` 42 + )<br>
`` 43 +<br>
`` 44 + # Get Smolagents-compatible tools from GOAT adapter<br>
`` 45 + goat_smolagents_tools = get_smolagents_tools(wallet=wallet, plugins=[spl_token_plugin])<br>
`` 46 + if not goat_smolagents_tools:<br>
`` 47 + print("Warning: No GOAT tools were loaded. Check adapter/plugin setup or tool compatibility.")<br>
`` 48 + # Optionally exit if tools are essential<br>
`` 49 + # exit(1)<br>
`` 50 + else:<br>
`` 51 + print(f"Successfully loaded {len(goat_smolagents_tools)} GOAT tools for Smolagents.")<br>
`` 52 +<br>
`` 53 +except Exception as e:<br>
`` 54 + print(f"Error during initialization: {e}")<br>
`` 55 + exit(1)<br>
`` 56 +<br>
`` 57 +# --- Define Smolagents Agent ---<br>
`` 58 +print(f"Initializing Smolagents agent with model: GPT-4o...")<br>
`` 59 +model = OpenAIServerModel(<br>
`` 60 + model_id="gpt-4o",<br>
`` 61 +)<br>
`` 62 +<br>
`` 63 +agent = ToolCallingAgent(<br>
`` 64 + tools=goat_smolagents_tools,<br>
`` 65 + model=model,<br>
`` 66 + add_base_tools=False<br>
`` 67 +)<br>
`` 68 +<br>
`` 69 +# Set custom system prompt after initialization<br>
`` 70 +agent.prompt_templates["system_prompt"] = (<br>
`` 71 + "You are an expert analyst focused on the Solana blockchain and its SPL token ecosystem. "<br>
`` 72 + "You have access to specialized tools that can query SPL token balances and potentially other relevant information directly from the blockchain. "<br>
`` 73 + "Your task is to understand the user's question, use the appropriate tool(s) if necessary, and provide a clear, concise answer based *only* on the information retrieved from the tools. "<br>
`` 74 + "If the tools cannot provide the information needed to answer the question, clearly state that."<br>
`` 75 +)<br>
`` 76 +<br>
`` 77 +# --- Interactive Loop ---<br>
`` 78 +def run_interactive_loop():<br>
`` 79 + print("\n--- Solana SPL Token Analyst (Smolagents) ---")<br>
`` 80 + print("Ask questions about SPL tokens (using configured wallet and network). Type 'quit' to exit.")<br>
`` 81 + while True:<br>
`` 82 + try:<br>
`` 83 + user_query = input("\nYour question: ")<br>
`` 84 + if user_query.lower() == "quit":<br>
`` 85 + print("Exiting...")<br>
`` 86 + break<br>
`` 87 + if not user_query.strip():<br>
`` 88 + continue<br>
`` 89 +<br>
`` 90 + print("\nProcessing your request with Smolagents...")<br>
`` 91 + # Run the agent with the user's question<br>
`` 92 + result = agent.run(user_query)<br>
`` 93 +<br>
`` 94 + print("\n✅ --- Agent Finished ---")<br>
`` 95 + print("Final Answer:")<br>
`` 96 + print(result)<br>
`` 97 + print("--------------------\n")<br>
`` 98 +<br>
`` 99 + except Exception as e:<br>
`` 100 + print(f"\n❌ An error occurred during agent execution: {e}")<br>
`` 101 + except KeyboardInterrupt:<br>
`` 102 + print("\nExiting...")<br>
`` 103 + break<br>
`` 104 +<br>
`` 105 +if __name__ == "__main__":<br>
`` 106 + run_interactive_loop()<br>

Collapse file

‎python/examples/by-framework/smolagents/poetry.lock‎

Copy file name to clipboardExpand all lines: python/examples/by-framework/smolagents/poetry.lock

+3,339Lines changed: 3339 additions & 0 deletions

Load DiffSome generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Collapse file

‎python/examples/by-framework/smolagents/poetry.toml‎

Copy file name to clipboard

+2Lines changed: 2 additions & 0 deletions

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@<br>
`` 1 +[virtualenvs]<br>
`` 2 +in-project = true<br>

Collapse file

‎python/examples/by-framework/smolagents/pyproject.toml‎

Copy file name to clipboard

+51Lines changed: 51 additions & 0 deletions

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@<br>
`` 1 +[tool.poetry]<br>
`` 2 +name = "goat-sdk-example-by-framework-smolagents"<br>
`` 3 +version = "0.1.0"<br>
`` 4 +description = "GOAT example for Smolagents with Solana SPL token plugin"<br>
`` 5 +authors = ["Andrea Villa <andreakarimodm@gmail.com>"]<br>
`` 6 +readme = "README.md"<br>
`` 7 +keywords = ["goat", "sdk", "web3", "agents", "ai"]<br>
`` 8 +homepage = "https://ohmygoat.dev/"<br>
`` 9 +repository = "https://github.com/goat-sdk/goat"<br>
`` 10 +packages = [<br>
`` 11 + { include = "example.py" },<br>
`` 12 +]<br>
`` 13 +<br>
`` 14 +[tool.poetry.dependencies]<br>
`` 15 +python = ">=3.11,<3.13"<br>
`` 16 +smolagents = { version = ">=1.10.0", extras = ["openai"] }<br>
`` 17 +python-dotenv = "^1.0.1"<br>
`` 18 +goat-sdk = "^0.1.0"<br>
`` 19 +goat-sdk-wallet-solana = "^0.1.0"<br>
`` 20 +goat-sdk-plugin-spl-token = "^0.1.0"<br>
`` 21 +goat-sdk-adapter-smolagents = ">=0.1.0"<br>
`` 22 +goat-sdk-wallet-crossmint = "*"<br>
`` 23 +<br>
`` 24 +[tool.poetry.group.test.dependencies]<br>
`` 25 +pytest = "^8.3.4"<br>
`` 26 +pytest-asyncio = "^0.25.0"<br>
`` 27 +<br>
`` 28 +[tool.poetry.urls]<br>
`` 29 +"Bug Tracker" = "https://github.com/goat-sdk/goat/issues"<br>
`` 30 +<br>
`` 31 +[tool.pytest.ini_options]<br>
`` 32 +addopts = [<br>
`` 33 + "--import-mode=importlib",<br>
`` 34 +]<br>
`` 35 +pythonpath = "src"<br>
`` 36 +asyncio_default_fixture_loop_scope = "function"<br>
`` 37 +<br>
`` 38 +[build-system]<br>
`` 39 +requires = ["poetry-core"]<br>
`` 40 +build-backend = "poetry.core.masonry.api"<br>
`` 41 +[tool.poetry.group.dev.dependencies]<br>
`` 42 +ruff = "^0.8.6"<br>
`` 43 +goat-sdk = { path = "../../../src/goat-sdk", develop = true }<br>
`` 44 +goat-sdk-wallet-solana = { path = "../../../src/wallets/solana", develop = true }<br>
`` 45 +goat-sdk-plugin-spl-token = { path = "../../../src/plugins/spl_token", develop = true }<br>
`` 46 +goat-sdk-adapter-smolagents = { path = "../../../src/adapters/smolagents", develop = true }<br>
`` 47 +goat-sdk-wallet-crossmint = { path = "../../../src/wallets/crossmint", develop = true }<br>
`` 48 +[tool.ruff]<br>
`` 49 +line-length = 120<br>
`` 50 +target-version = "py312"<br>
`` 51 +<br>

Collapse file

‎python/src/adapters/smolagents/README.md‎

Copy file name to clipboard

+115Lines changed: 115 additions & 0 deletions

  • Display the source diff
  • Display the rich diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@<br>
`` 1 +<div align="center"><br>
`` 2 +<a href="https://github.com/goat-sdk/goat"><br>
`` 3 +<br>
`` 4 +<img src="https://github.com/user-attachments/assets/5fc7f121-259c-492c-8bca-f15fe7eb830c" alt="GOAT" width="100px" height="auto" style="object-fit: contain;"><br>
`` 5 +</a><br>
`` 6 +</div><br>
`` 7 +<br>
`` 8 +# Smolagents Adapter for GOAT<br>
`` 9 +<br>
`` 10 +Integrate the more than +200 onchain tools of GOAT with [Smolagents](/content/huggingface/smolagents/index.html), a framework for building agentic systems with small language models.<br>
`` 11 +<br>
`` 12 +## Installation<br>
`` 13 +bash
```
`` 14 +poetry add goat-sdk-adapter-smolagents<br>
`` 15 +
```
`` 16 +<br>
`` 17 +## Usage<br>
`` 18 +<br>
`` 19 +python
```
`` 20 +# --- Setup GOAT Wallet and Plugins (Example: Solana + SPL Token) ---<br>
`` 21 +import os<br>
`` 22 +from dotenv import load_dotenv<br>
`` 23 +from solders.keypair import Keypair<br>
`` 24 +from solana.rpc.api import Client as SolanaClient<br>
`` 25 +from goat_plugins.spl_token import spl_token, SplTokenPluginOptions<br>
`` 26 +from goat_plugins.spl_token.tokens import SPL_TOKENS<br>
`` 27 +from goat_wallets.solana import solana<br>
`` 28 +<br>
`` 29 +# Load environment variables<br>
`` 30 +load_dotenv()<br>
`` 31 +solana_rpc_endpoint = os.getenv("SOLANA_RPC_ENDPOINT")<br>
`` 32 +solana_wallet_seed = os.getenv("SOLANA_WALLET_SEED")<br>
`` 33 +openai_api_key = os.getenv("OPENAI_API_KEY")<br>
`` 34 +<br>
`` 35 +# Make sure environment variables are set<br>
`` 36 +if not solana_rpc_endpoint or not solana_wallet_seed:<br>
`` 37 + raise ValueError("SOLANA_RPC_ENDPOINT and SOLANA_WALLET_SEED must be set in .env")<br>
`` 38 +<br>
`` 39 +if not openai_api_key:<br>
`` 40 + raise ValueError("OPENAI_API_KEY must be set in .env to use GPT-4o")<br>
`` 41 +<br>
`` 42 +client = SolanaClient(solana_rpc_endpoint)<br>
`` 43 +keypair = Keypair.from_base58_string(solana_wallet_seed)<br>
`` 44 +wallet = solana(client, keypair)<br>
`` 45 +<br>
`` 46 +spl_token_plugin = spl_token(SplTokenPluginOptions(<br>
`` 47 + network="mainnet",<br>
`` 48 + tokens=SPL_TOKENS<br>
`` 49 +))<br>
`` 50 +<br>
`` 51 +# --- Import Smolagents and the GOAT Adapter ---<br>
`` 52 +from smolagents import OpenAIServerModel, ToolCallingAgent<br>
`` 53 +from goat_adapters.smolagents import get_smolagents_tools<br>
`` 54 +<br>
`` 55 +# --- Generate Smolagents Tools from GOAT ---<br>
`` 56 +goat_smolagents_tools = get_smolagents_tools(<br>
`` 57 + wallet=wallet,<br>
`` 58 + plugins=[spl_token_plugin]<br>
`` 59 +)<br>
`` 60 +<br>
`` 61 +# --- Define Smolagents Agent using GOAT Tools ---<br>
`` 62 +model = OpenAIServerModel(<br>
`` 63 + model_id="gpt-4o",<br>
`` 64 +)<br>
`` 65 +<br>
`` 66 +agent = ToolCallingAgent(<br>
`` 67 + tools=goat_smolagents_tools,<br>
`` 68 + model=model,<br>
`` 69 + add_base_tools=False # Don't include Smolagents default tools<br>
`` 70 +)<br>
`` 71 +<br>
`` 72 +# Set custom system prompt<br>
`` 73 +agent.prompt_templates["system_prompt"] = (<br>
`` 74 + "You are an expert analyst focused on the Solana blockchain and its SPL token ecosystem. "<br>
`` 75 + "You have access to specialized tools that can query SPL token balances and potentially other relevant information directly from the blockchain. "<br>
`` 76 + "Your task is to understand the user's question, use the appropriate tool(s) if necessary, and provide a clear, concise answer."<br>
`` 77 +)<br>
`` 78 +<br>
`` 79 +# --- Run the agent with a specific task ---<br>
`` 80 +result = agent.run("What is the balance of USDC for my wallet?")<br>
`` 81 +print(result)<br>
`` 82 +
```
`` 83 +<br>
`` 84 +### Tool Collection<br>
`` 85 +<br>
`` 86 +You can also create a Smolagents ToolCollection to manage your GOAT tools:<br>
`` 87 +<br>
`` 88 +python
```
`` 89 +from smolagents import ToolCollection, ToolCallingAgent<br>
`` 90 +<br>
`` 91 +# Create the tools first<br>
`` 92 +goat_tools = get_smolagents_tools(wallet=wallet, plugins=[spl_token_plugin])<br>
`` 93 +<br>
`` 94 +# Create a custom tool collection with your GOAT tools<br>
`` 95 +my_tool_collection = ToolCollection(<br>
`` 96 + tools=goat_tools<br>
`` 97 +)<br>
`` 98 +<br>
`` 99 +# Now use this collection with your agent<br>
`` 100 +agent = ToolCallingAgent(<br>
`` 101 + tools=my_tool_collection.tools,<br>
`` 102 + model=model,<br>
`` 103 + add_base_tools=False<br>
`` 104 +)<br>
`` 105 +
```
`` 106 +<br>
`` 107 +<footer><br>
`` 108 +<br/><br>
`` 109 +<br/><br>
`` 110 +<div><br>
`` 111 +<a href="https://github.com/goat-sdk/goat"><br>
`` 112 + <img src="https://github.com/user-attachments/assets/59fa5ddc-9d47-4d41-a51a-64f6798f94bd" alt="GOAT" width="100%" height="auto" style="object-fit: contain; max-width: 800px;"><br>
`` 113 +</a><br>
`` 114 +<div><br>
`` 115 +</footer><br>

Collapse file

‎python/src/adapters/smolagents/goat_adapters/__init__.py‎

Copy file name to clipboard

+3Lines changed: 3 additions & 0 deletions

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@<br>
`` 1 +from . import smolagents<br>
`` 2 +<br>
`` 3 +__all__ = ["smolagents"]<br>

Collapse file

‎python/src/adapters/smolagents/goat_adapters/smolagents/__init__.py‎

Copy file name to clipboard

+6Lines changed: 6 additions & 0 deletions

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@<br>
`` 1 +from .adapter import get_smolagents_tools, GoatToolWrapper<br>
`` 2 +<br>
`` 3 +__all__ = [<br>
`` 4 + "get_smolagents_tools",<br>
`` 5 + "GoatToolWrapper",<br>
`` 6 +]<br>

Collapse file

‎python/src/adapters/smolagents/goat_adapters/smolagents/adapter.py‎

Copy file name to clipboard

+110Lines changed: 110 additions & 0 deletions

Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@<br>
`` 1 +from typing import List, Any, Dict, get_origin, Optional, Type<br>
`` 2 +import inspect<br>
`` 3 +import traceback<br>
`` 4 +from goat.classes.plugin_base import PluginBase<br>
`` 5 +from pydantic import BaseModel<br>
`` 6 +from goat import WalletClientBase, get_tools<br>
`` 7 +from goat.classes.tool_base import ToolBase<br>
`` 8 +from smolagents import Tool<br>
`` 9 +<br>
`` 10 +def python_type_to_json_schema_type(python_type: Type) -> str:<br>
`` 11 + """Convert Python type to JSON Schema type string."""<br>
`` 12 + if python_type is str or python_type is Optional[str]:<br>
`` 13 + return "string"<br>
`` 14 + elif python_type is int or python_type is Optional[int]:<br>
`` 15 + return "integer"<br>
`` 16 + elif python_type is float or python_type is Optional[float]:<br>
`` 17 + return "number"<br>
`` 18 + elif python_type is bool or python_type is Optional[bool]:<br>
`` 19 + return "boolean"<br>
`` 20 + elif get_origin(python_type) is list or get_origin(python_type) is List:<br>
`` 21 + return "array"<br>
`` 22 + elif get_origin(python_type) is dict or get_origin(python_type) is Dict:<br>
`` 23 + return "object"<br>
`` 24 + elif python_type is None or python_type is type(None):<br>
`` 25 + return "null"<br>
`` 26 + else:<br>
`` 27 + # For complex types or when unsure, use string as fallback<br>
`` 28 + return "string"<br>
`` 29 +<br>
`` 30 +class GoatToolWrapper(Tool):<br>
`` 31 + """A wrapper for executing GOAT SDK tools within a Smolagents environment."""<br>
`` 32 +<br>
`` 33 + def __init__(self, goat_tool: ToolBase):<br>
`` 34 + if not hasattr(goat_tool, 'parameters') or not issubclass(goat_tool.parameters, BaseModel):<br>
`` 35 + raise ValueError(f"GOAT tool '{goat_tool.name}' has no Pydantic parameters model defined.")<br>
`` 36 +<br>
`` 37 + self.goat_tool = goat_tool<br>
`` 38 + self.name = goat_tool.name<br>
`` 39 + self.description = goat_tool.description<br>
`` 40 +<br>
`` 41 + # We have a dynamic forward method, with our own validation<br>
`` 42 + self.skip_forward_signature_validation = True<br>
`` 43 +<br>
`` 44 + # Convert GOAT tool parameters to Smolagents inputs format<br>
`` 45 + self.inputs = {}<br>
`` 46 + if hasattr(goat_tool, 'parameters'):<br>
`` 47 + # Using Pydantic v2 model_fields<br>
`` 48 + model_fields = getattr(goat_tool.parameters, "model_fields", {})<br>
`` 49 + for field_name, field_info in model_fields.items():<br>
`` 50 + # Get field type from annotation<br>
`` 51 + python_type = field_info.annotation<br>
`` 52 +<br>
`` 53 + # Convert Python type to JSON schema type<br>
`` 54 + field_type = python_type_to_json_schema_type(python_type)<br>
`` 55 +<br>
`` 56 + # Get field description<br>
`` 57 + field_description = getattr(field_info, "description", "") or f"Parameter {field_name}"<br>
`` 58 +<br>
`` 59 + self.inputs[field_name] = {<br>
`` 60 + "type": field_type,<br>
`` 61 + "description": field_description<br>
`` 62 + }<br>
`` 63 +<br>
`` 64 + # Try to determine output_type from the execute method's return annotation<br>
`` 65 + try:<br>
`` 66 + return_type = inspect.signature(goat_tool.execute).return_annotation<br>
`` 67 + if return_type is not inspect.Signature.empty:<br>
`` 68 + self.output_type = python_type_to_json_schema_type(return_type)<br>
`` 69 + else:<br>
`` 70 + # Default to string if no return annotation<br>
`` 71 + self.output_type = "string"<br>
`` 72 + except (ValueError, TypeError):<br>
`` 73 + # If we can't determine the return type, default to string<br>
`` 74 + self.output_type = "string"<br>
`` 75 +<br>
`` 76 + super().__init__()<br>
`` 77 +<br>
`` 78 + def forward(self, **kwargs: Any) -> Any:<br>
`` 79 + """Executes the wrapped GOAT tool."""<br>
`` 80 + try:<br>
`` 81 + return self.goat_tool.execute(kwargs)<br>
`` 82 + except Exception as e:<br>
`` 83 + # Get the full traceback<br>
`` 84 + error_details = traceback.format_exc()<br>
`` 85 + raise Exception(f"Error executing tool {self.name}: {error_details}")<br>
`` 86 +<br>
`` 87 +def get_smolagents_tools(wallet: WalletClientBase, plugins: List[PluginBase]) -> List[Tool]:<br>
`` 88 + """Create Smolagents-compatible tools from GOAT tools.<br>
`` 89 +<br>
`` 90 + Args:<br>
`` 91 + wallet: A wallet client instance<br>
`` 92 + plugins: List of plugin instances<br>
`` 93 +<br>
`` 94 + Returns:<br>
`` 95 + List of Tool instances ready for Smolagents Agents.<br>
`` 96 + """<br>
`` 97 + raw_tools: List[ToolBase] = get_tools(wallet=wallet, plugins=plugins)<br>
`` 98 + smolagents_tools: List[Tool] = []<br>
`` 99 +<br>
`` 100 + for raw_tool in raw_tools:<br>
`` 101 + if hasattr(raw_tool, 'parameters') and raw_tool.parameters and issubclass(raw_tool.parameters, BaseModel):<br>
`` 102 + try:<br>
`` 103 + wrapper = GoatToolWrapper(goat_tool=raw_tool)<br>
`` 104 + smolagents_tools.append(wrapper)<br>
`` 105 + except Exception as e:<br>
`` 106 + print(f"Warning: Could not initialize wrapper for GOAT tool '{raw_tool.name}': {e}")<br>
`` 107 + else:<br>
`` 108 + print(f"Info: Skipping GOAT tool '{raw_tool.name}' as it lacks a Pydantic parameters model.")<br>
`` 109 +<br>
`` 110 + return smolagents_tools<br>

Collapse file

‎python/src/adapters/smolagents/poetry.lock‎

Copy file name to clipboardExpand all lines: python/src/adapters/smolagents/poetry.lock

+1,322Lines changed: 1322 additions & 0 deletions

Collapse file

‎python/src/adapters/smolagents/poetry.toml‎

Copy file name to clipboard

+2Lines changed: 2 additions & 0 deletions

Collapse file

‎python/src/adapters/smolagents/pyproject.toml‎

Copy file name to clipboard

+57Lines changed: 57 additions & 0 deletions

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@<br>
`` 1 +[tool.poetry]<br>
`` 2 +name = "goat-sdk-adapter-smolagents"<br>
`` 3 +version = "0.1.0"<br>
`` 4 +description = "Goat adapter for SmolAgents"<br>
`` 5 +authors = [<br>
`` 6 + "Andrea Villa <andreakarimodm@gmail.com>",<br>
`` 7 +]<br>
`` 8 +readme = "README.md"<br>
`` 9 +keywords = [<br>
`` 10 + "goat",<br>
`` 11 + "sdk",<br>
`` 12 + "web3",<br>
`` 13 + "agents",<br>
`` 14 + "ai",<br>
`` 15 + "smolagents",<br>
`` 16 +]<br>
`` 17 +homepage = "https://ohmygoat.dev/"<br>
`` 18 +repository = "https://github.com/goat-sdk/goat"<br>
`` 19 +packages = [<br>
`` 20 + { include = "goat_adapters/smolagents" },<br>
`` 21 +]<br>
`` 22 +<br>
`` 23 +[tool.poetry.dependencies]<br>
`` 24 +python = "^3.10,<3.13"<br>
`` 25 +goat-sdk = ">=0.1.6"<br>
`` 26 +smolagents = ">=1.10.0"<br>
`` 27 +<br>
`` 28 +[tool.poetry.group.test.dependencies]<br>
`` 29 +pytest = "^8.3.4"<br>
`` 30 +pytest-asyncio = "^0.25.0"<br>
`` 31 +<br>
`` 32 +[tool.poetry.group.dev.dependencies]<br>
`` 33 +ruff = "^0.8.6"<br>
`` 34 +<br>
`` 35 +[tool.poetry.group.dev.dependencies.goat-sdk]<br>
`` 36 +path = "../../goat-sdk"<br>
`` 37 +develop = true<br>
`` 38 +<br>
`` 39 +[tool.poetry.urls]<br>
`` 40 +"Bug Tracker" = "https://github.com/goat-sdk/goat/issues"<br>
`` 41 +<br>
`` 42 +[tool.pytest.ini_options]<br>
`` 43 +addopts = [<br>
`` 44 + "--import-mode=importlib",<br>
`` 45 +]<br>
`` 46 +pythonpath = "src"<br>
`` 47 +asyncio_default_fixture_loop_scope = "function"<br>
`` 48 +<br>
`` 49 +[tool.ruff]<br>
`` 50 +line-length = 120<br>
`` 51 +target-version = "py312"<br>
`` 52 +<br>
`` 53 +[build-system]<br>
`` 54 +requires = [<br>
`` 55 + "poetry-core",<br>
`` 56 +]<br>
`` 57 +build-backend = "poetry.core.masonry.api"<br>

0 commit comments

Comments

0 (0)

Please sign in to comment.

You can’t perform that action at this time.

5 files remain