Chat Completions
Performs a chat completion request against an AI Search instance, using indexed content as context for generating responses.
Security
API Token
The preferred authorization scheme for interacting with the Cloudflare API. Create a token.
Authorization: Bearer Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYYAPI Email + API Key
The previous authorization scheme for interacting with the Cloudflare API, used in conjunction with a Global API key.
X-Auth-Email: user@example.comThe previous authorization scheme for interacting with the Cloudflare API. When possible, use API tokens instead of Global API keys.
X-Auth-Key: 144c9defac04969c7bfad8efaa8ea194ParametersExpand Collapse
AI Search instance ID. Lowercase alphanumeric, hyphens, and underscores.
params InstanceChatCompletionsParams
Messages param.Field[[]InstanceChatCompletionsParamsMessage]Body param
Body param
Content InstanceChatCompletionsParamsMessagesContentUnion
type InstanceChatCompletionsParamsMessagesContentArray []InstanceChatCompletionsParamsMessagesContentArray
Role InstanceChatCompletionsParamsMessagesRole
Body param
Body param
Cache InstanceChatCompletionsParamsAISearchOptionsCacheOptional
CacheThreshold InstanceChatCompletionsParamsAISearchOptionsCacheCacheThresholdOptional
QueryRewrite InstanceChatCompletionsParamsAISearchOptionsQueryRewriteOptional
Model InstanceChatCompletionsParamsAISearchOptionsQueryRewriteModelOptional
Reranking InstanceChatCompletionsParamsAISearchOptionsRerankingOptional
Retrieval InstanceChatCompletionsParamsAISearchOptionsRetrievalOptional
BoostBy []InstanceChatCompletionsParamsAISearchOptionsRetrievalBoostByOptionalMetadata 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.
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.
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.
Direction InstanceChatCompletionsParamsAISearchOptionsRetrievalBoostByDirectionOptionalBoost 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.
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.
FusionMethod InstanceChatCompletionsParamsAISearchOptionsRetrievalFusionMethodOptional
KeywordMatchMode InstanceChatCompletionsParamsAISearchOptionsRetrievalKeywordMatchModeOptionalControls which documents are candidates for BM25 scoring. ‘and’ restricts candidates to documents containing all query terms; ‘or’ includes any document containing at least one term, ranked by BM25 relevance. When omitted, falls back to the instance-level retrieval_options.keyword_match_mode, then to ‘and’.
Controls which documents are candidates for BM25 scoring. ‘and’ restricts candidates to documents containing all query terms; ‘or’ includes any document containing at least one term, ranked by BM25 relevance. When omitted, falls back to the instance-level retrieval_options.keyword_match_mode, then to ‘and’.
RetrievalType InstanceChatCompletionsParamsAISearchOptionsRetrievalRetrievalTypeOptional
Body param
Body param
ReturnsExpand Collapse
type InstanceChatCompletionsResponse struct{…}
Choices []InstanceChatCompletionsResponseChoice
Message InstanceChatCompletionsResponseChoicesMessage
Content InstanceChatCompletionsResponseChoicesMessageContentUnion
type InstanceChatCompletionsResponseChoicesMessageContentArray []InstanceChatCompletionsResponseChoicesMessageContentArrayItem
Role InstanceChatCompletionsResponseChoicesMessageRole
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
},
"scoring_details": {
"fusion_method": "rrf",
"keyword_rank": 0,
"keyword_score": 0,
"reranking_score": 0,
"vector_rank": 0,
"vector_score": 0
}
}
],
"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
},
"scoring_details": {
"fusion_method": "rrf",
"keyword_rank": 0,
"keyword_score": 0,
"reranking_score": 0,
"vector_rank": 0,
"vector_score": 0
}
}
],
"id": "id",
"model": "model",
"object": "object"
}