Skip to content

Commit d06a15e

Browse files
[Docs] Add OpenAI SDK documentation page (#7997)
1 parent 61fddef commit d06a15e

File tree

3 files changed

+116
-5
lines changed

3 files changed

+116
-5
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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.

apps/portal/src/app/ai/chat/page.mdx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ You can use the API with the HTTP API directly, the AI SDK or with any OpenAI-co
2020
<TabsList>
2121
<TabsTrigger value="api">HTTP API</TabsTrigger>
2222
<TabsTrigger value="ai-sdk">Vercel AI SDK</TabsTrigger>
23-
<TabsTrigger value="openai">OpenAI Client</TabsTrigger>
23+
<TabsTrigger value="openai">OpenAI SDK</TabsTrigger>
2424
</TabsList>
2525
<TabsContent value="api">
2626
<OpenApiEndpoint path="/ai/chat" method="POST" />
@@ -60,9 +60,9 @@ You can use the API with the HTTP API directly, the AI SDK or with any OpenAI-co
6060

6161
<TabsContent value="openai">
6262

63-
### Using the OpenAI Client
63+
### Using the OpenAI SDK
6464

65-
You can use the standard OpenAI client library to interact with the thirdweb AI model.
65+
You can use the standard OpenAI SDK library to interact with the thirdweb AI model.
6666

6767
```python
6868
from openai import OpenAI
@@ -72,8 +72,8 @@ client = OpenAI(
7272
api_key="<your-project-secret-key>",
7373
)
7474
chat_completion = client.chat.completions.create(
75-
model="t0",
76-
messages=[{"role": "user", "content": "Transfer 10 USDC to vitalik.eth"}],
75+
model="t0-latest",
76+
messages=[{ "role": "user", "content": "Transfer 10 USDC to vitalik.eth" }],
7777
stream=False,
7878
extra_body={ "context": { "from": "0x...", "chain_ids": [8453] }}
7979
)

apps/portal/src/app/ai/sidebar.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ export const sidebar: SideBar = {
2929
name: "Vercel AI SDK",
3030
href: "/ai/chat/ai-sdk",
3131
},
32+
{
33+
name: "OpenAI SDK",
34+
href: "/ai/chat/openai-sdk",
35+
},
3236
{
3337
name: "API Reference",
3438
href: "/reference#tag/ai/post/ai/chat",

0 commit comments

Comments
 (0)