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 NamespaceInstanceChatCompletionsParams
Messages param.Field[[]NamespaceInstanceChatCompletionsParamsMessage]Body param
Body param
Role NamespaceInstanceChatCompletionsParamsMessagesRole
Body param
Body param
Cache NamespaceInstanceChatCompletionsParamsAISearchOptionsCacheOptional
CacheThreshold NamespaceInstanceChatCompletionsParamsAISearchOptionsCacheCacheThresholdOptional
QueryRewrite NamespaceInstanceChatCompletionsParamsAISearchOptionsQueryRewriteOptional
Model NamespaceInstanceChatCompletionsParamsAISearchOptionsQueryRewriteModelOptional
Reranking NamespaceInstanceChatCompletionsParamsAISearchOptionsRerankingOptional
Retrieval NamespaceInstanceChatCompletionsParamsAISearchOptionsRetrievalOptional
BoostBy []NamespaceInstanceChatCompletionsParamsAISearchOptionsRetrievalBoostByOptionalMetadata 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 asc/desc directions; text/boolean fields support exists/not_exists.
Direction NamespaceInstanceChatCompletionsParamsAISearchOptionsRetrievalBoostByDirectionOptionalBoost 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 NamespaceInstanceChatCompletionsParamsAISearchOptionsRetrievalFusionMethodOptional
KeywordMatchMode NamespaceInstanceChatCompletionsParamsAISearchOptionsRetrievalKeywordMatchModeOptionalControls 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. Defaults 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. Defaults to ‘and’.
RetrievalType NamespaceInstanceChatCompletionsParamsAISearchOptionsRetrievalRetrievalTypeOptional
Body param
Body param
ReturnsExpand Collapse
type NamespaceInstanceChatCompletionsResponse struct{…}
Choices []NamespaceInstanceChatCompletionsResponseChoice
Message NamespaceInstanceChatCompletionsResponseChoicesMessage
Role NamespaceInstanceChatCompletionsResponseChoicesMessageRole
Chunks []NamespaceInstanceChatCompletionsResponseChunk
ScoringDetails NamespaceInstanceChatCompletionsResponseChunksScoringDetailsOptional
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"
)
func main() {
client := cloudflare.NewClient(
option.WithAPIToken("Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY"),
)
response, err := client.AISearch.Namespaces.Instances.ChatCompletions(
context.TODO(),
"my-namespace",
"my-ai-search",
ai_search.NamespaceInstanceChatCompletionsParams{
AccountID: cloudflare.F("c3dc5f0b34a14ff8e1b3ec04895e1b22"),
Messages: cloudflare.F([]ai_search.NamespaceInstanceChatCompletionsParamsMessage{ai_search.NamespaceInstanceChatCompletionsParamsMessage{
Content: cloudflare.F("content"),
Role: cloudflare.F(ai_search.NamespaceInstanceChatCompletionsParamsMessagesRoleSystem),
}}),
},
)
if err != nil {
panic(err.Error())
}
fmt.Printf("%+v\n", response.ID)
}
{
"choices": [
{
"message": {
"content": "content",
"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": "content",
"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"
}