Skip to content
Start here

Chat Completions

Deprecated
client.AISearch.Instances.ChatCompletions(ctx, id, params) (*InstanceChatCompletionsResponse, error)
POST/accounts/{account_id}/ai-search/instances/{id}/chat/completions

Performs a chat completion request against an AI Search instance, using indexed content as context for generating responses.

Deprecated: use /accounts/{account_id}/ai-search/namespaces/{namespace}/instances (and descendant paths) instead.

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
ParametersExpand Collapse
id string

AI Search instance ID. Lowercase alphanumeric, hyphens, and underscores.

maxLength64
minLength1
params InstanceChatCompletionsParams
AccountID param.Field[string]

Path param

Messages param.Field[[]InstanceChatCompletionsParamsMessage]

Body param

Content InstanceChatCompletionsParamsMessagesContentUnion
One of the following:
UnionString
type InstanceChatCompletionsParamsMessagesContentArray []InstanceChatCompletionsParamsMessagesContentArray
One of the following:
type InstanceChatCompletionsParamsMessagesContentArrayObject struct{…}
Text string
minLength1
Type InstanceChatCompletionsParamsMessagesContentArrayObjectType
type InstanceChatCompletionsParamsMessagesContentArrayObject struct{…}
ImageURL InstanceChatCompletionsParamsMessagesContentArrayObjectImageURL
URL string
maxLength20971520
minLength1
Type InstanceChatCompletionsParamsMessagesContentArrayObjectType
type InstanceChatCompletionsParamsMessagesContentArrayObject struct{…}
File InstanceChatCompletionsParamsMessagesContentArrayObjectFile
Filename string
maxLength255
minLength1
FileData stringOptional
maxLength13981144
minLength1
FileID stringOptional
Type InstanceChatCompletionsParamsMessagesContentArrayObjectType
UnionString
Role InstanceChatCompletionsParamsMessagesRole
One of the following:
const InstanceChatCompletionsParamsMessagesRoleSystem InstanceChatCompletionsParamsMessagesRole = "system"
const InstanceChatCompletionsParamsMessagesRoleDeveloper InstanceChatCompletionsParamsMessagesRole = "developer"
const InstanceChatCompletionsParamsMessagesRoleUser InstanceChatCompletionsParamsMessagesRole = "user"
const InstanceChatCompletionsParamsMessagesRoleAssistant InstanceChatCompletionsParamsMessagesRole = "assistant"
const InstanceChatCompletionsParamsMessagesRoleTool InstanceChatCompletionsParamsMessagesRole = "tool"
AISearchOptions param.Field[InstanceChatCompletionsParamsAISearchOptions]Optional

Body param

Cache InstanceChatCompletionsParamsAISearchOptionsCacheOptional
CacheThreshold InstanceChatCompletionsParamsAISearchOptionsCacheCacheThresholdOptional
One of the following:
const InstanceChatCompletionsParamsAISearchOptionsCacheCacheThresholdSuperStrictMatch InstanceChatCompletionsParamsAISearchOptionsCacheCacheThreshold = "super_strict_match"
const InstanceChatCompletionsParamsAISearchOptionsCacheCacheThresholdCloseEnough InstanceChatCompletionsParamsAISearchOptionsCacheCacheThreshold = "close_enough"
const InstanceChatCompletionsParamsAISearchOptionsCacheCacheThresholdFlexibleFriend InstanceChatCompletionsParamsAISearchOptionsCacheCacheThreshold = "flexible_friend"
const InstanceChatCompletionsParamsAISearchOptionsCacheCacheThresholdAnythingGoes InstanceChatCompletionsParamsAISearchOptionsCacheCacheThreshold = "anything_goes"
Enabled boolOptional
CustomMetadata map[string, InstanceChatCompletionsParamsAISearchOptionsCustomMetadataUnion]Optional

Metadata added to AI Gateway logs for requests this operation triggers. Accepts up to 2 string, number, or boolean entries. Reserved keys: ‘ai-search’, ‘task’, ‘origin’, and any key beginning with ‘cf.’.

One of the following:
UnionString
UnionFloat
UnionBool
QueryRewrite InstanceChatCompletionsParamsAISearchOptionsQueryRewriteOptional
Enabled boolOptional
Model stringOptional

A Workers AI model ID or an AI Gateway model ID compatible with the OpenAI Chat Completions API. An empty string uses the configured or default model.

RewritePrompt stringOptional
Reranking InstanceChatCompletionsParamsAISearchOptionsRerankingOptional
Enabled boolOptional
MatchThreshold float64Optional
maximum1
minimum0
Model stringOptional
Retrieval InstanceChatCompletionsParamsAISearchOptionsRetrievalOptional
BoostBy []InstanceChatCompletionsParamsAISearchOptionsRetrievalBoostByOptional

Metadata fields to boost search results by. Overrides the instance-level boost_by config. Direction defaults to ‘asc’ for numeric/datetime fields, ‘exists’ for text/boolean fields. Fields must match ‘timestamp’ or a defined custom_metadata field.

Field string

Metadata field name to boost by. Use ‘timestamp’ for document freshness, or any custom_metadata field. Numeric and datetime fields support all four directions (asc, desc, exists, not_exists); text/boolean fields only support exists/not_exists.

maxLength64
minLength1
Direction InstanceChatCompletionsParamsAISearchOptionsRetrievalBoostByDirectionOptional

Boost direction. ‘desc’ = higher values rank higher (e.g. newer timestamps). ‘asc’ = lower values rank higher. ‘exists’ = boost chunks that have the field. ‘not_exists’ = boost chunks that lack the field. Optional — defaults to ‘asc’ for numeric/datetime fields, ‘exists’ for text/boolean fields.

One of the following:
const InstanceChatCompletionsParamsAISearchOptionsRetrievalBoostByDirectionAsc InstanceChatCompletionsParamsAISearchOptionsRetrievalBoostByDirection = "asc"
const InstanceChatCompletionsParamsAISearchOptionsRetrievalBoostByDirectionDesc InstanceChatCompletionsParamsAISearchOptionsRetrievalBoostByDirection = "desc"
const InstanceChatCompletionsParamsAISearchOptionsRetrievalBoostByDirectionExists InstanceChatCompletionsParamsAISearchOptionsRetrievalBoostByDirection = "exists"
const InstanceChatCompletionsParamsAISearchOptionsRetrievalBoostByDirectionNotExists InstanceChatCompletionsParamsAISearchOptionsRetrievalBoostByDirection = "not_exists"
ContextExpansion int64Optional
maximum3
minimum0
Filters map[string, unknown]Optional
FusionMethod InstanceChatCompletionsParamsAISearchOptionsRetrievalFusionMethodOptional
One of the following:
const InstanceChatCompletionsParamsAISearchOptionsRetrievalFusionMethodMax InstanceChatCompletionsParamsAISearchOptionsRetrievalFusionMethod = "max"
const InstanceChatCompletionsParamsAISearchOptionsRetrievalFusionMethodRrf InstanceChatCompletionsParamsAISearchOptionsRetrievalFusionMethod = "rrf"
KeywordMatchMode InstanceChatCompletionsParamsAISearchOptionsRetrievalKeywordMatchModeOptional

Controls which documents become candidates for BM25 scoring. ‘and’ restricts candidates to documents containing all query terms; ‘or’ includes any document containing at least one term and ranks results according to BM25 relevance. When omitted, falls back to the instance-level retrieval_options.keyword_match_mode, then to ‘and’.

One of the following:
const InstanceChatCompletionsParamsAISearchOptionsRetrievalKeywordMatchModeAnd InstanceChatCompletionsParamsAISearchOptionsRetrievalKeywordMatchMode = "and"
const InstanceChatCompletionsParamsAISearchOptionsRetrievalKeywordMatchModeOr InstanceChatCompletionsParamsAISearchOptionsRetrievalKeywordMatchMode = "or"
MatchThreshold float64Optional

Minimum similarity score a chunk must reach to be returned. When omitted, falls back to the instance-level score_threshold, then to 0.4.

maximum1
minimum0
MaxNumResults int64Optional

Maximum number of results to return. When omitted, falls back to the instance-level max_num_results, then to 10.

maximum50
minimum1
RetrievalType InstanceChatCompletionsParamsAISearchOptionsRetrievalRetrievalTypeOptional
One of the following:
const InstanceChatCompletionsParamsAISearchOptionsRetrievalRetrievalTypeVector InstanceChatCompletionsParamsAISearchOptionsRetrievalRetrievalType = "vector"
const InstanceChatCompletionsParamsAISearchOptionsRetrievalRetrievalTypeKeyword InstanceChatCompletionsParamsAISearchOptionsRetrievalRetrievalType = "keyword"
const InstanceChatCompletionsParamsAISearchOptionsRetrievalRetrievalTypeHybrid InstanceChatCompletionsParamsAISearchOptionsRetrievalRetrievalType = "hybrid"
ReturnOnFailure boolOptional
Model param.Field[string]Optional

Body param: A Workers AI model ID or an AI Gateway model ID compatible with the OpenAI Chat Completions API. An empty string uses the configured or default model.

Stream param.Field[bool]Optional

Body param

ReturnsExpand Collapse
type InstanceChatCompletionsResponse struct{…}
Choices []InstanceChatCompletionsResponseChoice
Message InstanceChatCompletionsResponseChoicesMessage
Content InstanceChatCompletionsResponseChoicesMessageContentUnion
One of the following:
UnionString
type InstanceChatCompletionsResponseChoicesMessageContentArray []InstanceChatCompletionsResponseChoicesMessageContentArrayItem
One of the following:
type InstanceChatCompletionsResponseChoicesMessageContentArrayObject struct{…}
Text string
minLength1
Type InstanceChatCompletionsResponseChoicesMessageContentArrayObjectType
type InstanceChatCompletionsResponseChoicesMessageContentArrayObject struct{…}
ImageURL InstanceChatCompletionsResponseChoicesMessageContentArrayObjectImageURL
URL string
maxLength20971520
minLength1
Type InstanceChatCompletionsResponseChoicesMessageContentArrayObjectType
type InstanceChatCompletionsResponseChoicesMessageContentArrayObject struct{…}
File InstanceChatCompletionsResponseChoicesMessageContentArrayObjectFile
Filename string
maxLength255
minLength1
FileData stringOptional
maxLength13981144
minLength1
FileID stringOptional
Type InstanceChatCompletionsResponseChoicesMessageContentArrayObjectType
UnionString
Role InstanceChatCompletionsResponseChoicesMessageRole
One of the following:
const InstanceChatCompletionsResponseChoicesMessageRoleSystem InstanceChatCompletionsResponseChoicesMessageRole = "system"
const InstanceChatCompletionsResponseChoicesMessageRoleDeveloper InstanceChatCompletionsResponseChoicesMessageRole = "developer"
const InstanceChatCompletionsResponseChoicesMessageRoleUser InstanceChatCompletionsResponseChoicesMessageRole = "user"
const InstanceChatCompletionsResponseChoicesMessageRoleAssistant InstanceChatCompletionsResponseChoicesMessageRole = "assistant"
const InstanceChatCompletionsResponseChoicesMessageRoleTool InstanceChatCompletionsResponseChoicesMessageRole = "tool"
Index int64Optional
Chunks []InstanceChatCompletionsResponseChunk
ID string
Score float64
maximum1
minimum0
Text string
Type string
Item InstanceChatCompletionsResponseChunksItemOptional
Key string
Metadata map[string, unknown]Optional
Timestamp float64Optional
MediaEndMs int64Optional

Audio/video transcript chunks only: end, in milliseconds from the start of the media (whole seconds), of the last transcript section the matched chunk covers. Describes the matched chunk before any context expansion, not the expanded text.

minimum0
MediaStartMs int64Optional

Audio/video transcript chunks only: start, in milliseconds from the start of the media (whole seconds), of the transcript section the matched chunk begins in. Describes the matched chunk before any context expansion, not the expanded text.

minimum0
ScoringDetails InstanceChatCompletionsResponseChunksScoringDetailsOptional
FusionMethod InstanceChatCompletionsResponseChunksScoringDetailsFusionMethodOptional
One of the following:
const InstanceChatCompletionsResponseChunksScoringDetailsFusionMethodRrf InstanceChatCompletionsResponseChunksScoringDetailsFusionMethod = "rrf"
const InstanceChatCompletionsResponseChunksScoringDetailsFusionMethodMax InstanceChatCompletionsResponseChunksScoringDetailsFusionMethod = "max"
KeywordRank float64Optional
KeywordScore float64Optional
minimum0
RerankingScore float64Optional
maximum1
minimum0
VectorRank float64Optional
VectorScore float64Optional
maximum1
minimum0
SourceMediaType InstanceChatCompletionsResponseChunksSourceMediaTypeOptional

Audio/video transcript chunks only: type of the source media file. Omitted for all other chunks.

One of the following:
const InstanceChatCompletionsResponseChunksSourceMediaTypeAudio InstanceChatCompletionsResponseChunksSourceMediaType = "audio"
const InstanceChatCompletionsResponseChunksSourceMediaTypeVideo InstanceChatCompletionsResponseChunksSourceMediaType = "video"
ID stringOptional
Model stringOptional
Object stringOptional

Chat Completions

package main

import (
  "context"
  "fmt"

  "github.com/cloudflare/cloudflare-go"
  "github.com/cloudflare/cloudflare-go/ai_search"
  "github.com/cloudflare/cloudflare-go/option"
  "github.com/cloudflare/cloudflare-go/shared"
)

func main() {
  client := cloudflare.NewClient(
    option.WithAPIToken("Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY"),
  )
  response, err := client.AISearch.Instances.ChatCompletions(
    context.TODO(),
    "my-ai-search",
    ai_search.InstanceChatCompletionsParams{
      AccountID: cloudflare.F("c3dc5f0b34a14ff8e1b3ec04895e1b22"),
      Messages: cloudflare.F([]ai_search.InstanceChatCompletionsParamsMessage{ai_search.InstanceChatCompletionsParamsMessage{
        Content: cloudflare.F[ai_search.InstanceChatCompletionsParamsMessagesContentUnion](shared.UnionString("string")),
        Role: cloudflare.F(ai_search.InstanceChatCompletionsParamsMessagesRoleSystem),
      }}),
    },
  )
  if err != nil {
    panic(err.Error())
  }
  fmt.Printf("%+v\n", response.ID)
}
{
  "choices": [
    {
      "message": {
        "content": "string",
        "role": "system"
      },
      "index": 0
    }
  ],
  "chunks": [
    {
      "id": "id",
      "score": 0,
      "text": "text",
      "type": "type",
      "item": {
        "key": "key",
        "metadata": {
          "foo": "bar"
        },
        "timestamp": 0
      },
      "media_end_ms": 0,
      "media_start_ms": 0,
      "scoring_details": {
        "fusion_method": "rrf",
        "keyword_rank": 0,
        "keyword_score": 0,
        "reranking_score": 0,
        "vector_rank": 0,
        "vector_score": 0
      },
      "source_media_type": "audio"
    }
  ],
  "id": "id",
  "model": "model",
  "object": "object"
}
Returns Examples
{
  "choices": [
    {
      "message": {
        "content": "string",
        "role": "system"
      },
      "index": 0
    }
  ],
  "chunks": [
    {
      "id": "id",
      "score": 0,
      "text": "text",
      "type": "type",
      "item": {
        "key": "key",
        "metadata": {
          "foo": "bar"
        },
        "timestamp": 0
      },
      "media_end_ms": 0,
      "media_start_ms": 0,
      "scoring_details": {
        "fusion_method": "rrf",
        "keyword_rank": 0,
        "keyword_score": 0,
        "reranking_score": 0,
        "vector_rank": 0,
        "vector_score": 0
      },
      "source_media_type": "audio"
    }
  ],
  "id": "id",
  "model": "model",
  "object": "object"
}