## Chat Completions `aisearch.instances.chat_completions(strid, InstanceChatCompletionsParams**kwargs) -> InstanceChatCompletionsResponse` **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. ### Parameters - `account_id: str` - `id: str` AI Search instance ID. Lowercase alphanumeric, hyphens, and underscores. - `messages: Iterable[Message]` - `content: Optional[str]` - `role: Literal["system", "developer", "user", 2 more]` - `"system"` - `"developer"` - `"user"` - `"assistant"` - `"tool"` - `aisearch_options: Optional[AISearchOptions]` - `cache: Optional[AISearchOptionsCache]` - `cache_threshold: Optional[Literal["super_strict_match", "close_enough", "flexible_friend", "anything_goes"]]` - `"super_strict_match"` - `"close_enough"` - `"flexible_friend"` - `"anything_goes"` - `enabled: Optional[bool]` - `query_rewrite: Optional[AISearchOptionsQueryRewrite]` - `enabled: Optional[bool]` - `model: Optional[Literal["@cf/meta/llama-3.3-70b-instruct-fp8-fast", "@cf/zai-org/glm-4.7-flash", "@cf/meta/llama-3.1-8b-instruct-fast", 25 more]]` - `"@cf/meta/llama-3.3-70b-instruct-fp8-fast"` - `"@cf/zai-org/glm-4.7-flash"` - `"@cf/meta/llama-3.1-8b-instruct-fast"` - `"@cf/meta/llama-3.1-8b-instruct-fp8"` - `"@cf/meta/llama-4-scout-17b-16e-instruct"` - `"@cf/qwen/qwen3-30b-a3b-fp8"` - `"@cf/deepseek-ai/deepseek-r1-distill-qwen-32b"` - `"@cf/moonshotai/kimi-k2-instruct"` - `"@cf/google/gemma-3-12b-it"` - `"anthropic/claude-3-7-sonnet"` - `"anthropic/claude-sonnet-4"` - `"anthropic/claude-opus-4"` - `"anthropic/claude-3-5-haiku"` - `"cerebras/qwen-3-235b-a22b-instruct"` - `"cerebras/qwen-3-235b-a22b-thinking"` - `"cerebras/llama-3.3-70b"` - `"cerebras/llama-4-maverick-17b-128e-instruct"` - `"cerebras/llama-4-scout-17b-16e-instruct"` - `"cerebras/gpt-oss-120b"` - `"google-ai-studio/gemini-2.5-flash"` - `"google-ai-studio/gemini-2.5-pro"` - `"grok/grok-4"` - `"groq/llama-3.3-70b-versatile"` - `"groq/llama-3.1-8b-instant"` - `"openai/gpt-5"` - `"openai/gpt-5-mini"` - `"openai/gpt-5-nano"` - `""` - `rewrite_prompt: Optional[str]` - `reranking: Optional[AISearchOptionsReranking]` - `enabled: Optional[bool]` - `match_threshold: Optional[float]` - `model: Optional[Literal["@cf/baai/bge-reranker-base", ""]]` - `"@cf/baai/bge-reranker-base"` - `""` - `retrieval: Optional[AISearchOptionsRetrieval]` - `boost_by: Optional[Iterable[AISearchOptionsRetrievalBoostBy]]` 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: str` 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: Optional[Literal["asc", "desc", "exists", "not_exists"]]` 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. - `"asc"` - `"desc"` - `"exists"` - `"not_exists"` - `context_expansion: Optional[int]` - `filters: Optional[Dict[str, object]]` - `fusion_method: Optional[Literal["max", "rrf"]]` - `"max"` - `"rrf"` - `keyword_match_mode: Optional[Literal["and", "or"]]` 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'. Legacy values 'exact_match' and 'fuzzy_match' are accepted and map to 'and' and 'or' respectively. - `"and"` - `"or"` - `match_threshold: Optional[float]` - `max_num_results: Optional[int]` - `retrieval_type: Optional[Literal["vector", "keyword", "hybrid"]]` - `"vector"` - `"keyword"` - `"hybrid"` - `return_on_failure: Optional[bool]` - `model: Optional[Literal["@cf/meta/llama-3.3-70b-instruct-fp8-fast", "@cf/zai-org/glm-4.7-flash", "@cf/meta/llama-3.1-8b-instruct-fast", 25 more]]` - `"@cf/meta/llama-3.3-70b-instruct-fp8-fast"` - `"@cf/zai-org/glm-4.7-flash"` - `"@cf/meta/llama-3.1-8b-instruct-fast"` - `"@cf/meta/llama-3.1-8b-instruct-fp8"` - `"@cf/meta/llama-4-scout-17b-16e-instruct"` - `"@cf/qwen/qwen3-30b-a3b-fp8"` - `"@cf/deepseek-ai/deepseek-r1-distill-qwen-32b"` - `"@cf/moonshotai/kimi-k2-instruct"` - `"@cf/google/gemma-3-12b-it"` - `"anthropic/claude-3-7-sonnet"` - `"anthropic/claude-sonnet-4"` - `"anthropic/claude-opus-4"` - `"anthropic/claude-3-5-haiku"` - `"cerebras/qwen-3-235b-a22b-instruct"` - `"cerebras/qwen-3-235b-a22b-thinking"` - `"cerebras/llama-3.3-70b"` - `"cerebras/llama-4-maverick-17b-128e-instruct"` - `"cerebras/llama-4-scout-17b-16e-instruct"` - `"cerebras/gpt-oss-120b"` - `"google-ai-studio/gemini-2.5-flash"` - `"google-ai-studio/gemini-2.5-pro"` - `"grok/grok-4"` - `"groq/llama-3.3-70b-versatile"` - `"groq/llama-3.1-8b-instant"` - `"openai/gpt-5"` - `"openai/gpt-5-mini"` - `"openai/gpt-5-nano"` - `""` - `stream: Optional[bool]` ### Returns - `class InstanceChatCompletionsResponse: …` - `choices: List[Choice]` - `message: ChoiceMessage` - `content: Optional[str]` - `role: Literal["system", "developer", "user", 2 more]` - `"system"` - `"developer"` - `"user"` - `"assistant"` - `"tool"` - `index: Optional[int]` - `chunks: List[Chunk]` - `id: str` - `score: float` - `text: str` - `type: str` - `item: Optional[ChunkItem]` - `key: str` - `metadata: Optional[Dict[str, object]]` - `timestamp: Optional[float]` - `scoring_details: Optional[ChunkScoringDetails]` - `fusion_method: Optional[Literal["rrf", "max"]]` - `"rrf"` - `"max"` - `keyword_rank: Optional[float]` - `keyword_score: Optional[float]` - `reranking_score: Optional[float]` - `vector_rank: Optional[float]` - `vector_score: Optional[float]` - `id: Optional[str]` - `model: Optional[str]` - `object: Optional[str]` ### Example ```python import os from cloudflare import Cloudflare client = Cloudflare( api_token=os.environ.get("CLOUDFLARE_API_TOKEN"), # This is the default and can be omitted ) response = client.aisearch.instances.chat_completions( id="my-ai-search", account_id="c3dc5f0b34a14ff8e1b3ec04895e1b22", messages=[{ "content": "content", "role": "system", }], ) print(response.id) ``` #### Response ```json { "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" } ```