Skip to content

Commit 60ed453

Browse files
authored
split 'resolve' into multiple tools and add more descriptive information for LLMs (#19)
* split 'resolve' into multiple tools and add more descriptive information for LLMs * decode "data" and remove "logs_bloom" * correct description for resolve using ens/wallets * remove data if it's encoded
1 parent 1e9a26f commit 60ed453

File tree

2 files changed

+118
-12
lines changed

2 files changed

+118
-12
lines changed

python/thirdweb-ai/src/thirdweb_ai/common/utils.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import re
2+
from typing import Any
23

34

45
def extract_digits(value: int | str) -> int:
@@ -27,3 +28,23 @@ def normalize_chain_id(
2728
return [extract_digits(c) for c in in_value]
2829

2930
return extract_digits(in_value)
31+
32+
33+
def is_encoded(encoded_data: str) -> bool:
34+
encoded_data = encoded_data.removeprefix("0x")
35+
36+
try:
37+
bytes.fromhex(encoded_data)
38+
return True
39+
except ValueError:
40+
return False
41+
42+
43+
def clean_resolve(out: dict[str, Any]):
44+
if "transactions" in out["data"]:
45+
for transaction in out["data"]["transactions"]:
46+
if "data" in transaction and is_encoded(transaction["data"]):
47+
transaction.pop("data")
48+
if "logs_bloom" in transaction:
49+
transaction.pop("logs_bloom")
50+
return out

python/thirdweb-ai/src/thirdweb_ai/services/insight.py

Lines changed: 97 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Annotated, Any
22

3-
from thirdweb_ai.common.utils import normalize_chain_id
3+
from thirdweb_ai.common.utils import clean_resolve, normalize_chain_id
44
from thirdweb_ai.services.service import Service
55
from thirdweb_ai.tools.tool import tool
66

@@ -12,15 +12,15 @@ def __init__(self, secret_key: str, chain_id: int | str | list[int | str]):
1212
self.chain_ids = normalized if isinstance(normalized, list) else [normalized]
1313

1414
@tool(
15-
description="Retrieve blockchain events with flexible filtering options. Use this to search for specific events or to analyze event patterns across multiple blocks."
15+
description="Retrieve blockchain events with flexible filtering options. Use this to search for specific events or to analyze event patterns across multiple blocks. Do not use this tool to simply look up a single transaction."
1616
)
1717
def get_all_events(
1818
self,
1919
chain: Annotated[
2020
list[int | str] | int | str | None,
2121
"Chain ID(s) to query (e.g., 1 for Ethereum Mainnet, 137 for Polygon). Specify multiple IDs as a list [1, 137] for cross-chain queries (max 5).",
2222
] = None,
23-
address: Annotated[
23+
contract_address: Annotated[
2424
str | None,
2525
"Contract address to filter events by (e.g., '0x1234...'). Only return events emitted by this contract.",
2626
] = None,
@@ -55,8 +55,8 @@ def get_all_events(
5555
normalized_chain = normalize_chain_id(chain) if chain is not None else self.chain_ids
5656
if normalized_chain:
5757
params["chain"] = normalized_chain
58-
if address:
59-
params["filter_address"] = address
58+
if contract_address:
59+
params["filter_address"] = contract_address
6060
if block_number_gte:
6161
params["filter_block_number_gte"] = block_number_gte
6262
if block_number_lt:
@@ -294,13 +294,13 @@ def get_token_prices(
294294
return self._get("tokens/price", params)
295295

296296
@tool(
297-
description="Get contract ABI and metadata about a smart contract, including name, symbol, decimals, and other contract-specific information. This tool also retrieve the Application Binary Interface (ABI) for a smart contract. Essential for decoding contract data and interacting with the contract"
297+
description="Get contract ABI and metadata about a smart contract, including name, symbol, decimals, and other contract-specific information. Use this when asked about a contract's functions, interface, or capabilities. This tool specifically retrieves details about deployed smart contracts (NOT regular wallet addresses or transaction hashes)."
298298
)
299299
def get_contract_metadata(
300300
self,
301301
contract_address: Annotated[
302302
str,
303-
"The contract address to get metadata for (e.g., '0x1234...'). Works for tokens and other contract types.",
303+
"The contract address to get metadata for (e.g., '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2' for WETH). Must be a deployed smart contract address (not a regular wallet). Use this for queries like 'what functions does this contract have' or 'get the ABI for contract 0x1234...'.",
304304
],
305305
chain: Annotated[
306306
list[int | str] | int | str | None,
@@ -425,13 +425,55 @@ def get_nft_transfers(
425425
return self._get(f"nfts/transfers/{contract_address}", params)
426426

427427
@tool(
428-
description="Search and analyze blockchain input data: block number, transaction or block hash, wallet or contract address, event signature or function selector. It returns a detailed analyzed information about the input data."
428+
description="Get detailed information about a specific block by its number or hash. Use this when asked about blockchain blocks (e.g., 'What's in block 12345678?' or 'Tell me about this block: 0xabc123...'). This tool is specifically for block data, NOT transactions, addresses, or contracts."
429+
)
430+
def get_block_details(
431+
self,
432+
block_identifier: Annotated[
433+
str,
434+
"Block number or block hash to look up. Can be either a simple number (e.g., '12345678') or a block hash (e.g., '0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3' for Ethereum block 0). Use for queries like 'what happened in block 14000000' or 'show me block 0xd4e56...'.",
435+
],
436+
chain: Annotated[
437+
list[int | str] | int | str | None,
438+
"Chain ID(s) to query (e.g., 1 for Ethereum). Specify the blockchain network where the block exists.",
439+
] = None,
440+
) -> dict[str, Any]:
441+
params = {}
442+
normalized_chain = normalize_chain_id(chain) if chain is not None else self.chain_ids
443+
if normalized_chain:
444+
params["chain"] = normalized_chain
445+
out = self._get(f"resolve/{block_identifier}", params)
446+
return clean_resolve(out)
447+
448+
@tool(
449+
description="Look up transactions for a wallet or contract address. Use this when asked about a specific Ethereum address (e.g., '0x1234...') to get account details including balance, transaction count, and contract verification status. This tool is specifically for addresses (accounts and contracts), NOT transaction hashes or ENS names."
450+
)
451+
def get_address_transactions(
452+
self,
453+
address: Annotated[
454+
str,
455+
"Wallet or contract address to look up (e.g., '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045' for Vitalik's address). Must be a valid blockchain address starting with 0x and 42 characters long.",
456+
],
457+
chain: Annotated[
458+
list[int | str] | int | str | None,
459+
"Chain ID(s) to query (e.g., 1 for Ethereum). Specify the blockchain network for the address.",
460+
] = None,
461+
) -> dict[str, Any]:
462+
params = {}
463+
normalized_chain = normalize_chain_id(chain) if chain is not None else self.chain_ids
464+
if normalized_chain:
465+
params["chain"] = normalized_chain
466+
out = self._get(f"resolve/{address}", params)
467+
return clean_resolve(out)
468+
469+
@tool(
470+
description="Look up transactions associated with an ENS domain name (anything ending in .eth like 'vitalik.eth'). This tool is specifically for ENS domains, NOT addresses, transaction hashes, or contract queries."
429471
)
430-
def resolve(
472+
def get_ens_transactions(
431473
self,
432-
input_data: Annotated[
474+
ens_name: Annotated[
433475
str,
434-
"Any blockchain input data: block number, transaction or block hash, address, event signature or function selector",
476+
"ENS name to resolve (e.g., 'vitalik.eth', 'thirdweb.eth'). Must be a valid ENS domain ending with .eth.",
435477
],
436478
chain: Annotated[
437479
list[int | str] | int | str | None,
@@ -442,4 +484,47 @@ def resolve(
442484
normalized_chain = normalize_chain_id(chain) if chain is not None else self.chain_ids
443485
if normalized_chain:
444486
params["chain"] = normalized_chain
445-
return self._get(f"resolve/{input_data}", params)
487+
out = self._get(f"resolve/{ens_name}", params)
488+
return clean_resolve(out)
489+
490+
@tool(
491+
description="Get detailed information about a specific transaction by its hash. Use this when asked to analyze, look up, check, or get details about a transaction hash (e.g., 'What can you tell me about this transaction: 0x5407ea41...'). This tool specifically deals with transaction hashes (txid/txhash), NOT addresses, contracts, or ENS names."
492+
)
493+
def get_transaction_details(
494+
self,
495+
transaction_hash: Annotated[
496+
str,
497+
"Transaction hash to look up (e.g., '0x5407ea41de24b7353d70eab42d72c92b42a44e140f930e349973cfc7b8c9c1d7'). Must be a valid transaction hash beginning with 0x and typically 66 characters long. Use this for queries like 'tell me about this transaction' or 'what happened in transaction 0x1234...'.",
498+
],
499+
chain: Annotated[
500+
list[int | str] | int | str | None,
501+
"Chain ID(s) to query (e.g., 1 for Ethereum). Specify the blockchain network where the transaction exists.",
502+
] = None,
503+
) -> dict[str, Any]:
504+
params = {}
505+
normalized_chain = normalize_chain_id(chain) if chain is not None else self.chain_ids
506+
if normalized_chain:
507+
params["chain"] = normalized_chain
508+
out = self._get(f"resolve/{transaction_hash}", params)
509+
return clean_resolve(out)
510+
511+
@tool(
512+
description="Decode a function or event signature. Use this when you need to understand what a specific function selector or event signature does and what parameters it accepts."
513+
)
514+
def decode_signature(
515+
self,
516+
signature: Annotated[
517+
str,
518+
"Function or event signature to decode (e.g., '0x095ea7b3' for the approve function). Usually begins with 0x.",
519+
],
520+
chain: Annotated[
521+
list[int | str] | int | str | None,
522+
"Chain ID(s) to query (e.g., 1 for Ethereum). Specify to improve signature lookup accuracy.",
523+
] = None,
524+
) -> dict[str, Any]:
525+
params = {}
526+
normalized_chain = normalize_chain_id(chain) if chain is not None else self.chain_ids
527+
if normalized_chain:
528+
params["chain"] = normalized_chain
529+
out = self._get(f"resolve/{signature}", params)
530+
return clean_resolve(out)

0 commit comments

Comments
 (0)