|
| 1 | +# Usage with the OpenAI SDK |
| 2 | + |
| 3 | +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. |
| 4 | + |
| 5 | +## Setup and Configuration |
| 6 | + |
| 7 | +First, install the OpenAI Python SDK and configure it to use thirdweb's AI endpoint: |
| 8 | + |
| 9 | +```bash |
| 10 | +pip install openai |
| 11 | +``` |
| 12 | + |
| 13 | +Configure the client with your thirdweb secret key: |
| 14 | + |
| 15 | +```python |
| 16 | +import os |
| 17 | +from openai import OpenAI |
| 18 | + |
| 19 | +client = OpenAI( |
| 20 | + base_url="https://api.thirdweb.com/ai", |
| 21 | + api_key=os.environ.get("THIRDWEB_SECRET_KEY") |
| 22 | +) |
| 23 | +``` |
| 24 | + |
| 25 | +**Important**: Store your `THIRDWEB_SECRET_KEY` as an environment variable for security. You can obtain your secret key from the [thirdweb dashboard](https://thirdweb.com/dashboard). |
| 26 | + |
| 27 | +## Making AI Requests |
| 28 | + |
| 29 | +The thirdweb AI model (`t0-latest`) can understand and execute blockchain operations through natural language. Use the `extra_body` parameter to provide blockchain context: |
| 30 | + |
| 31 | +```python |
| 32 | +chat_completion = client.chat.completions.create( |
| 33 | + model="t0-latest", |
| 34 | + messages=[{ |
| 35 | + "role": "user", |
| 36 | + "content": "Swap 0.01 ETH to USDC, then transfer 10 USDC to vitalik.eth", |
| 37 | + }], |
| 38 | + stream=True, |
| 39 | + extra_body={ |
| 40 | + "context": { |
| 41 | + "from": "0x2247d5d238d0f9d37184d8332aE0289d1aD9991b", |
| 42 | + "chain_ids": [8453], |
| 43 | + "auto_execute_transactions": False |
| 44 | + } |
| 45 | + }, |
| 46 | +) |
| 47 | +``` |
| 48 | + |
| 49 | +### Context Parameters |
| 50 | + |
| 51 | +- **`from`**: The wallet address that will execute transactions |
| 52 | +- **`chain_ids`**: Array of blockchain network IDs to operate on (e.g., `[1]` for Ethereum, `[8453]` for Base) |
| 53 | +- **`auto_execute_transactions`**: Set to `true` to automatically execute transactions, or `false` to return transaction data for manual execution |
| 54 | + |
| 55 | +## Handling Streaming Responses |
| 56 | + |
| 57 | +The thirdweb AI model supports streaming responses with different event types. Here's how to handle them: |
| 58 | + |
| 59 | +```python |
| 60 | +import json |
| 61 | + |
| 62 | +# Buffer message chunks and print once at the end |
| 63 | +message_parts = [] |
| 64 | + |
| 65 | +for event in chat_completion: |
| 66 | + event_type = getattr(event, "type", None) |
| 67 | + |
| 68 | + # Handle thinking steps |
| 69 | + if event_type == "presence": |
| 70 | + data = getattr(event, "data", None) |
| 71 | + print(f"[presence] {data}" if data is not None else "[presence]") |
| 72 | + continue |
| 73 | + |
| 74 | + # Handle actions (transaction preparations, chain changes, etc.) |
| 75 | + if isinstance(event_type, str): |
| 76 | + data = getattr(event, "data", None) |
| 77 | + try: |
| 78 | + data_str = json.dumps(data, separators=(",", ":")) |
| 79 | + except Exception: |
| 80 | + data_str = str(data) |
| 81 | + print(f"\n\n[{event_type}] - {data_str if data_str is not None else ""}") |
| 82 | + continue |
| 83 | + |
| 84 | + # Handle message content |
| 85 | + v = getattr(event, "v", None) |
| 86 | + if v is not None: |
| 87 | + message_parts.append(str(v)) |
| 88 | + |
| 89 | +# Print the final message |
| 90 | +if message_parts: |
| 91 | + print("\n\n[message] " + "".join(message_parts)) |
| 92 | +``` |
| 93 | + |
| 94 | +### Event Types |
| 95 | + |
| 96 | +- **`init`**: Initializes the stream and provides session information |
| 97 | +- **`presence`**: Indicates the AI is thinking or processing |
| 98 | +- **`image`**: Contains image data |
| 99 | +- **`context`**: Contains context data |
| 100 | +- **`error`**: Contains error information if something goes wrong |
| 101 | +- **Action events**: |
| 102 | + - **`sign_transaction`**: Contains transaction data |
| 103 | + - **`sign_swap`**: Contains swap data |
| 104 | + - **`monitor_transaction`**: Contains queued transaction id |
| 105 | +- **Message content**: The actual response text from the AI |
| 106 | + |
| 107 | +The AI will return structured data for blockchain operations, including transaction details, gas estimates, and execution status when `auto_execute_transactions` is enabled. |
0 commit comments