Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/portal/src/app/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ const apisLinks = [
];

const aiLinks = [
{
href: "/ai/chat",
name: "Chat API",
},
{
href: "/ai/mcp",
name: "MCP",
Expand Down
91 changes: 91 additions & 0 deletions apps/portal/src/app/ai/chat/EndpointMetadata.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import {
type APIParameter,
ApiEndpoint,
} from "@/components/Document/APIEndpointMeta/ApiEndpoint";
import { secretKeyHeaderParameter } from "../../../components/Document/APIEndpointMeta/common";

const contextFilterType = `\
{
from: string | null;
chain_ids: string[] | null;
session_id: string | null;
}`;

const contextParameter: APIParameter = {
description: "Provide additional context information along with the message",
name: "context",
required: false,
type: contextFilterType,
example: {
from: "0x2247d5d238d0f9d37184d8332aE0289d1aD9991b",
chain_ids: [8453],
},
};

const response200Example = `\
{
"message": "I've prepared a native ETH transfer as requested. Would you like to proceed with executing this transfer?",
"session_id": "123",
"request_id": "456",
"actions": [
{
"type": "sign_transaction",
"data": {
"chainId": 8453,
"to": "0x1234567890123456789012345678901234567890",
"value": "10000000000000000",
"data": "0x"
},
"session_id": "123",
"request_id": "456",
"source": "model",
"tool_name": null,
"description": null,
"kwargs": null
}
]
}`;

export function EndpointMetadata() {
return (
<ApiEndpoint
metadata={{
description:
"Send requests to the thirdweb AI model and receive a responses.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a grammatical error in the description text. It should be "Send requests to the thirdweb AI model and receive responses." (without the article "a" before the plural noun "responses").

Suggested change
"Send requests to the thirdweb AI model and receive a responses.",
"Send requests to the thirdweb AI model and receive responses.",

Spotted by Diamond

Is this helpful? React 👍 or 👎 to let us know.

method: "POST",
origin: "https://api.thirdweb.com",
path: "/ai/chat",
request: {
bodyParameters: [
{
description: "The message to be processed.",
example: [
{
role: "user",
content: "Transfer 10 USDC to vitalik.eth",
},
],
name: "messages",
required: true,
type: "array",
},
{
description: "Whether to stream the response or not",
example: false,
name: "stream",
required: false,
type: "boolean",
},
contextParameter,
],
headers: [secretKeyHeaderParameter],
pathParameters: [],
},
responseExamples: {
200: response200Example,
},
title: "Chat via HTTP API",
}}
/>
);
}
145 changes: 145 additions & 0 deletions apps/portal/src/app/ai/chat/handling-responses/page.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
# Response Handling

## Streamed vs non-streamed responses

- **Streamed Responses**: This method streams data in real-time, providing immediate feedback as the response is generated. Set the `stream` parameter to `true` in your request, the API delivers responses via Server-Sent Events (SSE).
- **Non-Streamed Responses**: When the `stream` parameter is set to `false`, the API returns the complete response in a single JSON payload after processing is complete.

## Handling Streamed Responses

For `stream:true`, you'll need to handle the following event types:

- `init`: Initializes the stream and provides session information
- `presence`: Provides backend status updates about worker processing
- `delta`: Contains chunks of the response message text
- `action`: Contains blockchain transaction or action data
- `image`: Contains image data
- `context`: Contains context data
- `error`: Contains error information if something goes wrong

**Example SSE Stream:**

```http
event: init
data: {
"session_id": "f4b45429-9570-4ee8-8c8f-8b267429915a",
"request_id": "9efc7f6a-8576-4d9c-8603-f6c72aa72164",
"type": "init",
"source": "model",
}

event: presence
data: {
"session_id": "c238c379-9875-4729-afb2-bf3c26081e24",
"request_id": "8b0e733a-89fd-4585-8fac-b4334208eada",
"source": "model",
"type": "presence",
"data": "Need to convert 0.00001 ETH to wei, then prepare native transfer to `0x2247d5d238d0f9d37184d8332aE0289d1aD9991b`."
}

event: presence
data: {
"session_id": "f4b45429-9570-4ee8-8c8f-8b267429915a",
"request_id": "9efc7f6a-8576-4d9c-8603-f6c72aa72164",
"type": "presence",
"source": "model",
"data": "Now that I have the wei value (10,000,000,000,000), I will prepare a native transfer to the recipient."
}

event: delta
data: {"v": "To send 0.0001 ETH on the Sepolia network"}

event: delta
data: {"v": " to the address associated with"}

event: action
data: {
"session_id": "f4b45429-9570-4ee8-8c8f-8b267429915a",
"request_id": "9efc7f6a-8576-4d9c-8603-f6c72aa72164",
"type": "sign_transaction",
"source": "executor",
"data": "{\"chainId\": 11155111, \"to\": \"0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045\", \"data\": \"0x\", \"value\": \"0x5af3107a4000\"}"
}
```

### TypeScript Example

We recommend using the `fetch-event-stream` package to handle the event stream in TypeScript. This gives you an easy way to handle each event type.

```jsx
import { stream } from "fetch-event-stream";

const events = await stream(`https://api.thirdweb.com/ai/chat`, {
body: JSON.stringify({
messages: [
{
role: "user",
content: "Transfer 10 USDC to vitalik.eth",
},
],
}),
headers: {
"Content-Type": "application/json",
"x-secret-key": "<your-secret-key>",
},
method: "POST",
});

// process the event stream
for await (const event of events) {
if (!event.data) {
continue;
}
switch (event.event) {
case "init": {
console.log("Init event", event.data);
// handle init event (session id and request id)
break;
}
case "presence": {
console.log("Presence event", event.data);
// handle intermediate thinking steps
break;
}
case "delta": {
console.log("Delta event", event.data);
// handle delta event (streamed output text response)
break;
}
case "action": {
const data = JSON.parse(event.data);

// handle transaction signing
if (data.type === "sign_transaction") {
const transactionData = JSON.parse(data.data);
console.log("Sign transaction event", transactionData);
}

// handle swap signing
if (data.type === "sign_swap") {
const swapData = JSON.parse(data.data);
console.log("Sign swap event", swapData);
}

break;
}
case "image": {
const data = JSON.parse(event.data);
// handle image rendering
console.log("Image data", data);
break;
}
case "context": {
console.log("Context event", event.data);
// handle context changes (chain ids, wallet address, etc)
break;
}
case "error": {
console.log("Error event", event.data);
// handle error event (error message)
break;
}
}
}
```

53 changes: 53 additions & 0 deletions apps/portal/src/app/ai/chat/page.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { EndpointMetadata } from "./EndpointMetadata";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@doc";

# Chat API

The thirdweb AI chat API is a standard OpenAI-compatible chat completion API that allows you to interact with the thirdweb AI model, specialized for blockchain interactions.

The thirdweb proprietary model is optimized for blockchain interactions and can:

- Query real-time data from the blockchain
- Analyze transactions
- Fetch token balances, prices and metadata
- Prepare any contract call or transaction for signing
- Prepare swaps from any token pair
- Deploy contracts
- Generate images
- Search the web for information
- And more!

You can use the API with the API directly, or with any OpenAI-compatible client library.

<Tabs defaultValue="api">
<TabsList>
<TabsTrigger value="api">HTTP API</TabsTrigger>
<TabsTrigger value="openai">OpenAI Client</TabsTrigger>
</TabsList>
<TabsContent value="api">
<EndpointMetadata />
</TabsContent>
<TabsContent value="openai">

### Using the OpenAI Client

You can use the standard OpenAI client library to interact with the thirdweb AI model.

```python
from openai import OpenAI

client = OpenAI(
base_url="https://api.thirdweb.com/ai",
api_key="<your-project-secret-key>",
)
chat_completion = client.chat.completions.create(
model="t0",
messages=[{"role": "user", "content": "Transfer 10 USDC to vitalik.eth"}],
stream=False,
extra_body={ "context": { "from": "0x...", "chain_ids": [8453] }}
)

print(chat_completion)
```
</TabsContent>
</Tabs>
23 changes: 3 additions & 20 deletions apps/portal/src/app/ai/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,16 @@
import { createMetadata } from "@doc";
import { BookIcon, ZapIcon } from "lucide-react";
import { DocLayout } from "@/components/Layouts/DocLayout";
import { sidebar } from "./sidebar";

export default async function Layout(props: { children: React.ReactNode }) {
return (
<DocLayout
editPageButton={true}
sideBar={{
links: [
{
name: "MCP Server",
icon: <ZapIcon />,
href: "/ai/mcp",
},
{
name: "llms.txt",
icon: <BookIcon />,
href: "/ai/llm-txt",
},
],
name: "AI",
}}
>
<DocLayout editPageButton={true} sideBar={sidebar}>
<div>{props.children}</div>
</DocLayout>
);
}

export const metadata = createMetadata({
description: "AI tools for agents and LLM clients.",
description: "AI tools for apps, agents and LLM clients.",
title: "thirdweb AI",
});
38 changes: 38 additions & 0 deletions apps/portal/src/app/ai/sidebar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { BookIcon, ZapIcon } from "lucide-react";
import type { SideBar } from "@/components/Layouts/DocLayout";

export const sidebar: SideBar = {
links: [
{
name: "Chat API",
isCollapsible: false,
links: [
{
name: "Get Started",
href: "/ai/chat",
icon: <ZapIcon />,
},
{
name: "Response Handling",
href: "/ai/chat/handling-responses",
},
{
name: "API Reference",
href: "https://api.thirdweb-dev.com/reference#tag/ai/post/ai/chat",
},
],
},
{ separator: true },
{
name: "MCP Server",
icon: <ZapIcon />,
href: "/ai/mcp",
},
{
name: "llms.txt",
icon: <BookIcon />,
href: "/ai/llm-txt",
},
],
name: "AI",
};
Loading
Loading