Run a Workers AI model
client.AI.Run(ctx, modelName, params) (*AIRunResponseUnion, error)
POST/accounts/{account_id}/ai/run/{model_name}
Runs the Workers AI model specified in the URL path. Send the model’s inputs directly in the request body, without a model/input/options wrapper.
Accepts model-specific JSON or binary input. The response format depends on the model and the requested output.
See the model catalog for supported models and their input formats.
Security
API Token
The preferred authorization scheme for interacting with the Cloudflare API. Create a token.
Example:
API Email + API Key
The previous authorization scheme for interacting with the Cloudflare API, used in conjunction with a Global API key.
Example:
The previous authorization scheme for interacting with the Cloudflare API. When possible, use API tokens instead of Global API keys.
Example:
Accepted Permissions (at least one required)
Run a Workers AI model
package main
import (
"context"
"fmt"
"github.com/cloudflare/cloudflare-go"
"github.com/cloudflare/cloudflare-go/ai"
"github.com/cloudflare/cloudflare-go/option"
)
func main() {
client := cloudflare.NewClient(
option.WithAPIToken("Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY"),
)
response, err := client.AI.Run(
context.TODO(),
"@cf/meta/llama-3.1-8b-instruct",
ai.AIRunParams{
AccountID: cloudflare.F("023e105f4ecef8ad9ca31a8372d0c353"),
},
)
if err != nil {
panic(err.Error())
}
fmt.Printf("%+v\n", response)
}
{
"result": [
{
"label": "label",
"score": 0
}
]
}Returns Examples
{
"result": [
{
"label": "label",
"score": 0
}
]
}