-
Notifications
You must be signed in to change notification settings - Fork 619
[Docs] Add AI Chat API documentation and endpoint metadata #7828
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
joaquim-verges
merged 1 commit into
main
from
_Docs_Add_AI_Chat_API_documentation_and_endpoint_metadata
Aug 12, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.", | ||
| 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
145
apps/portal/src/app/ai/chat/handling-responses/page.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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", | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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", | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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").
Spotted by Diamond
Is this helpful? React 👍 or 👎 to let us know.