AI
Usage with the OpenAI SDK
The thirdweb AI model can be accessed using the standard OpenAI Python/Typescript SDK by configuring it to use thirdweb's API endpoint. This allows you to leverage familiar OpenAI patterns while accessing thirdweb's blockchain-specific AI capabilities.
First, install the OpenAI Python SDK and configure it to use thirdweb's AI endpoint:
Configure the client with your thirdweb secret key:
Important: Store your THIRDWEB_SECRET_KEY
as an environment variable for security. You can obtain your secret key from the thirdweb dashboard.
The thirdweb AI model (t0-latest
) can understand and execute blockchain operations through natural language. Use the extra_body
parameter to provide blockchain context:
from
: The wallet address that will execute transactionschain_ids
: Array of blockchain network IDs to operate on (e.g.,[1]
for Ethereum,[8453]
for Base)auto_execute_transactions
: Set totrue
to automatically execute transactions, orfalse
to return transaction data for manual execution
The thirdweb AI model supports streaming responses with different event types. Here's how to handle them:
init
: Initializes the stream and provides session informationpresence
: Indicates the AI is thinking or processingimage
: Contains image datacontext
: Contains context dataerror
: Contains error information if something goes wrong- Action events:
sign_transaction
: Contains transaction datasign_swap
: Contains swap datamonitor_transaction
: Contains queued transaction id
- Message content: The actual response text from the AI
The AI will return structured data for blockchain operations, including transaction details, gas estimates, and execution status when auto_execute_transactions
is enabled.