Skip to content
Start here

Execute AI model

client.AI.Run(ctx, modelName, params) (*AIRunResponseUnion, error)
POST/accounts/{account_id}/ai/run/{model_name}

This endpoint provides users with the capability to run specific AI models on-demand.

By submitting the required input data, users can receive real-time predictions or results generated by the chosen AI model. The endpoint supports various AI model types, ensuring flexibility and adaptability for diverse use cases.

Model specific inputs available in Cloudflare Docs.

Security
API Token

The preferred authorization scheme for interacting with the Cloudflare API. Create a token.

Example:Authorization: Bearer Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY
API Email + API Key

The previous authorization scheme for interacting with the Cloudflare API, used in conjunction with a Global API key.

Example:X-Auth-Email: user@example.com

The previous authorization scheme for interacting with the Cloudflare API. When possible, use API tokens instead of Global API keys.

Example:X-Auth-Key: 144c9defac04969c7bfad8efaa8ea194
Accepted Permissions (at least one required)
Workers AI WriteWorkers AI Read
ParametersExpand Collapse
modelName string
params AIRunParams
AccountID param.Field[string]

Path param

Text param.Field[string]

Body param: The text that you want to classify

minLength1
ReturnsExpand Collapse
type AIRunResponseUnion interface{…}

An array of classification results for the input text

One of the following:
type AIRunResponseTextClassification []AIRunResponseTextClassificationItem

An array of classification results for the input text

Label stringoptional

The classification label assigned to the text (e.g., 'POSITIVE' or 'NEGATIVE')

Score float64optional

Confidence score indicating the likelihood that the text belongs to the specified label

UnionString
type AIRunResponseAudio struct{…}
Audio stringoptional

The generated audio in MP3 format, base64-encoded

UnionString
type AIRunResponseTextEmbeddings struct{…}
Data [][]float64optional

Embeddings of the requested text values

Shape []float64optional
type AIRunResponseAutomaticSpeechRecognition struct{…}
Text string

The transcription

Vtt stringoptional
WordCount float64optional
Words []AIRunResponseAutomaticSpeechRecognitionWordoptional
End float64optional

The ending second when the word completes

Start float64optional

The second this word begins in the recording

Word stringoptional
type AIRunResponseImageClassification []AIRunResponseImageClassificationItem
Label stringoptional

The predicted category or class for the input image based on analysis

Score float64optional

A confidence value, between 0 and 1, indicating how certain the model is about the predicted label

type AIRunResponseObjectDetection []AIRunResponseObjectDetectionItem

An array of detected objects within the input image

Box AIRunResponseObjectDetectionBoxoptional

Coordinates defining the bounding box around the detected object

Xmax float64optional

The x-coordinate of the bottom-right corner of the bounding box

Xmin float64optional

The x-coordinate of the top-left corner of the bounding box

Ymax float64optional

The y-coordinate of the bottom-right corner of the bounding box

Ymin float64optional

The y-coordinate of the top-left corner of the bounding box

Label stringoptional

The class label or name of the detected object

Score float64optional

Confidence score indicating the likelihood that the detection is correct

type AIRunResponseObject struct{…}
Response string

The generated text response from the model

ToolCalls []AIRunResponseObjectToolCalloptional

An array of tool calls requests made during the response generation

Arguments unknownoptional

The arguments passed to be passed to the tool call request

Name stringoptional

The name of the tool to be called

Usage AIRunResponseObjectUsageoptional

Usage statistics for the inference request

CompletionTokens float64optional

Total number of tokens in output

PromptTokens float64optional

Total number of tokens in input

TotalTokens float64optional

Total number of input and output tokens

UnionString
type AIRunResponseTranslation struct{…}
TranslatedText stringoptional

The translated text in the target language

type AIRunResponseSummarization struct{…}
Summary stringoptional

The summarized version of the input text

type AIRunResponseImageToText struct{…}
Description stringoptional
type AIRunResponseImageTextToText struct{…}
Description stringoptional
type AIRunResponseMultimodalEmbeddings struct{…}
Data [][]float64optional
Shape []float64optional

Execute 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(),
    "model_name",
    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
    }
  ]
}