# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

from __future__ import annotations

from typing import Type, Iterable, Optional, cast
from typing_extensions import Literal

import httpx

from .jobs import (
    JobsResource,
    AsyncJobsResource,
    JobsResourceWithRawResponse,
    AsyncJobsResourceWithRawResponse,
    JobsResourceWithStreamingResponse,
    AsyncJobsResourceWithStreamingResponse,
)
from .items import (
    ItemsResource,
    AsyncItemsResource,
    ItemsResourceWithRawResponse,
    AsyncItemsResourceWithRawResponse,
    ItemsResourceWithStreamingResponse,
    AsyncItemsResourceWithStreamingResponse,
)
from ....._types import Body, Omit, Query, Headers, NotGiven, omit, not_given
from ....._utils import path_template, maybe_transform, async_maybe_transform
from ....._compat import cached_property
from ....._resource import SyncAPIResource, AsyncAPIResource
from ....._response import (
    to_raw_response_wrapper,
    to_streamed_response_wrapper,
    async_to_raw_response_wrapper,
    async_to_streamed_response_wrapper,
)
from ....._wrappers import ResultWrapper
from .....pagination import SyncV4PagePaginationArray, AsyncV4PagePaginationArray
from ....._base_client import AsyncPaginator, make_request_options
from .....types.aisearch.namespaces import (
    instance_list_params,
    instance_create_params,
    instance_search_params,
    instance_update_params,
    instance_chat_completions_params,
)
from .....types.aisearch.namespaces.instance_list_response import InstanceListResponse
from .....types.aisearch.namespaces.instance_read_response import InstanceReadResponse
from .....types.aisearch.namespaces.instance_stats_response import InstanceStatsResponse
from .....types.aisearch.namespaces.instance_create_response import InstanceCreateResponse
from .....types.aisearch.namespaces.instance_delete_response import InstanceDeleteResponse
from .....types.aisearch.namespaces.instance_search_response import InstanceSearchResponse
from .....types.aisearch.namespaces.instance_update_response import InstanceUpdateResponse
from .....types.aisearch.namespaces.instance_chat_completions_response import InstanceChatCompletionsResponse

__all__ = ["InstancesResource", "AsyncInstancesResource"]


class InstancesResource(SyncAPIResource):
    @cached_property
    def jobs(self) -> JobsResource:
        return JobsResource(self._client)

    @cached_property
    def items(self) -> ItemsResource:
        return ItemsResource(self._client)

    @cached_property
    def with_raw_response(self) -> InstancesResourceWithRawResponse:
        """
        This property can be used as a prefix for any HTTP method call to return
        the raw response object instead of the parsed content.

        For more information, see https://www.github.com/cloudflare/cloudflare-python#accessing-raw-response-data-eg-headers
        """
        return InstancesResourceWithRawResponse(self)

    @cached_property
    def with_streaming_response(self) -> InstancesResourceWithStreamingResponse:
        """
        An alternative to `.with_raw_response` that doesn't eagerly read the response body.

        For more information, see https://www.github.com/cloudflare/cloudflare-python#with_streaming_response
        """
        return InstancesResourceWithStreamingResponse(self)

    def create(
        self,
        name: str,
        *,
        account_id: str,
        id: str,
        ai_gateway_id: Optional[str] | Omit = omit,
        aisearch_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",
                "@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",
                "@cf/google/gemma-4-26b-a4b-it",
                "@cf/moonshotai/kimi-k2.5",
                "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",
                "",
            ]
        ]
        | Omit = omit,
        cache: bool | Omit = omit,
        cache_threshold: Literal["super_strict_match", "close_enough", "flexible_friend", "anything_goes"]
        | Omit = omit,
        cache_ttl: Literal[600, 1800, 3600, 7200, 21600, 43200, 86400, 172800, 259200, 518400] | Omit = omit,
        chunk: bool | Omit = omit,
        chunk_overlap: int | Omit = omit,
        chunk_size: int | Omit = omit,
        custom_metadata: Iterable[instance_create_params.CustomMetadata] | Omit = omit,
        embedding_model: Optional[
            Literal[
                "@cf/qwen/qwen3-embedding-0.6b",
                "@cf/baai/bge-m3",
                "@cf/baai/bge-large-en-v1.5",
                "@cf/google/embeddinggemma-300m",
                "google-ai-studio/gemini-embedding-001",
                "google-ai-studio/gemini-embedding-2-preview",
                "openai/text-embedding-3-small",
                "openai/text-embedding-3-large",
                "",
            ]
        ]
        | Omit = omit,
        fusion_method: Literal["max", "rrf"] | Omit = omit,
        hybrid_search_enabled: bool | Omit = omit,
        index_method: instance_create_params.IndexMethod | Omit = omit,
        indexing_options: Optional[instance_create_params.IndexingOptions] | Omit = omit,
        max_num_results: int | Omit = omit,
        metadata: instance_create_params.Metadata | Omit = omit,
        public_endpoint_params: instance_create_params.PublicEndpointParams | Omit = omit,
        reranking: bool | Omit = omit,
        reranking_model: Optional[Literal["@cf/baai/bge-reranker-base", ""]] | Omit = omit,
        retrieval_options: Optional[instance_create_params.RetrievalOptions] | Omit = omit,
        rewrite_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",
                "@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",
                "@cf/google/gemma-4-26b-a4b-it",
                "@cf/moonshotai/kimi-k2.5",
                "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",
                "",
            ]
        ]
        | Omit = omit,
        rewrite_query: bool | Omit = omit,
        score_threshold: float | Omit = omit,
        source: Optional[str] | Omit = omit,
        source_params: Optional[instance_create_params.SourceParams] | Omit = omit,
        sync_interval: Literal[900, 1800, 3600, 7200, 14400, 21600, 43200, 86400] | Omit = omit,
        token_id: str | Omit = omit,
        type: Optional[Literal["r2", "web-crawler"]] | Omit = omit,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> InstanceCreateResponse:
        """Create a new instance.

        Args:
          id: AI Search instance ID.

        Lowercase alphanumeric, hyphens, and underscores.

          cache_ttl: Cache entry TTL in seconds. Allowed values: 600 (10min), 1800 (30min), 3600
              (1h), 7200 (2h), 21600 (6h), 43200 (12h), 86400 (24h), 172800 (48h), 259200
              (72h), 518400 (6d).

          hybrid_search_enabled: Deprecated — use index_method instead.

          index_method: Controls which storage backends are used during indexing. Defaults to
              vector-only.

          sync_interval: Interval between automatic syncs, in seconds. Allowed values: 900 (15min), 1800
              (30min), 3600 (1h), 7200 (2h), 14400 (4h), 21600 (6h), 43200 (12h), 86400 (24h).

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not account_id:
            raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
        if not name:
            raise ValueError(f"Expected a non-empty value for `name` but received {name!r}")
        return self._post(
            path_template(
                "/accounts/{account_id}/ai-search/namespaces/{name}/instances", account_id=account_id, name=name
            ),
            body=maybe_transform(
                {
                    "id": id,
                    "ai_gateway_id": ai_gateway_id,
                    "aisearch_model": aisearch_model,
                    "cache": cache,
                    "cache_threshold": cache_threshold,
                    "cache_ttl": cache_ttl,
                    "chunk": chunk,
                    "chunk_overlap": chunk_overlap,
                    "chunk_size": chunk_size,
                    "custom_metadata": custom_metadata,
                    "embedding_model": embedding_model,
                    "fusion_method": fusion_method,
                    "hybrid_search_enabled": hybrid_search_enabled,
                    "index_method": index_method,
                    "indexing_options": indexing_options,
                    "max_num_results": max_num_results,
                    "metadata": metadata,
                    "public_endpoint_params": public_endpoint_params,
                    "reranking": reranking,
                    "reranking_model": reranking_model,
                    "retrieval_options": retrieval_options,
                    "rewrite_model": rewrite_model,
                    "rewrite_query": rewrite_query,
                    "score_threshold": score_threshold,
                    "source": source,
                    "source_params": source_params,
                    "sync_interval": sync_interval,
                    "token_id": token_id,
                    "type": type,
                },
                instance_create_params.InstanceCreateParams,
            ),
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                post_parser=ResultWrapper[InstanceCreateResponse]._unwrapper,
            ),
            cast_to=cast(Type[InstanceCreateResponse], ResultWrapper[InstanceCreateResponse]),
        )

    def update(
        self,
        id: str,
        *,
        account_id: str,
        name: str,
        ai_gateway_id: Optional[str] | Omit = omit,
        aisearch_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",
                "@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",
                "@cf/google/gemma-4-26b-a4b-it",
                "@cf/moonshotai/kimi-k2.5",
                "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",
                "",
            ]
        ]
        | Omit = omit,
        cache: bool | Omit = omit,
        cache_threshold: Literal["super_strict_match", "close_enough", "flexible_friend", "anything_goes"]
        | Omit = omit,
        cache_ttl: Literal[600, 1800, 3600, 7200, 21600, 43200, 86400, 172800, 259200, 518400] | Omit = omit,
        chunk: bool | Omit = omit,
        chunk_overlap: int | Omit = omit,
        chunk_size: int | Omit = omit,
        custom_metadata: Iterable[instance_update_params.CustomMetadata] | Omit = omit,
        embedding_model: Optional[
            Literal[
                "@cf/qwen/qwen3-embedding-0.6b",
                "@cf/baai/bge-m3",
                "@cf/baai/bge-large-en-v1.5",
                "@cf/google/embeddinggemma-300m",
                "google-ai-studio/gemini-embedding-001",
                "google-ai-studio/gemini-embedding-2-preview",
                "openai/text-embedding-3-small",
                "openai/text-embedding-3-large",
                "",
            ]
        ]
        | Omit = omit,
        fusion_method: Literal["max", "rrf"] | Omit = omit,
        index_method: instance_update_params.IndexMethod | Omit = omit,
        indexing_options: Optional[instance_update_params.IndexingOptions] | Omit = omit,
        max_num_results: int | Omit = omit,
        metadata: instance_update_params.Metadata | Omit = omit,
        paused: bool | Omit = omit,
        public_endpoint_params: instance_update_params.PublicEndpointParams | Omit = omit,
        reranking: bool | Omit = omit,
        reranking_model: Optional[Literal["@cf/baai/bge-reranker-base", ""]] | Omit = omit,
        retrieval_options: Optional[instance_update_params.RetrievalOptions] | Omit = omit,
        rewrite_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",
                "@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",
                "@cf/google/gemma-4-26b-a4b-it",
                "@cf/moonshotai/kimi-k2.5",
                "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",
                "",
            ]
        ]
        | Omit = omit,
        rewrite_query: bool | Omit = omit,
        score_threshold: float | Omit = omit,
        source_params: Optional[instance_update_params.SourceParams] | Omit = omit,
        summarization: bool | Omit = omit,
        summarization_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",
                "@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",
                "@cf/google/gemma-4-26b-a4b-it",
                "@cf/moonshotai/kimi-k2.5",
                "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",
                "",
            ]
        ]
        | Omit = omit,
        sync_interval: Literal[900, 1800, 3600, 7200, 14400, 21600, 43200, 86400] | Omit = omit,
        system_prompt_aisearch: Optional[str] | Omit = omit,
        system_prompt_index_summarization: Optional[str] | Omit = omit,
        system_prompt_rewrite_query: Optional[str] | Omit = omit,
        token_id: str | Omit = omit,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> InstanceUpdateResponse:
        """Update instance.

        Args:
          cache_ttl: Cache entry TTL in seconds.

        Allowed values: 600 (10min), 1800 (30min), 3600
              (1h), 7200 (2h), 21600 (6h), 43200 (12h), 86400 (24h), 172800 (48h), 259200
              (72h), 518400 (6d).

          index_method: Controls which storage backends are used during indexing. Defaults to
              vector-only.

          sync_interval: Interval between automatic syncs, in seconds. Allowed values: 900 (15min), 1800
              (30min), 3600 (1h), 7200 (2h), 14400 (4h), 21600 (6h), 43200 (12h), 86400 (24h).

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not account_id:
            raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
        if not name:
            raise ValueError(f"Expected a non-empty value for `name` but received {name!r}")
        if not id:
            raise ValueError(f"Expected a non-empty value for `id` but received {id!r}")
        return self._put(
            path_template(
                "/accounts/{account_id}/ai-search/namespaces/{name}/instances/{id}",
                account_id=account_id,
                name=name,
                id=id,
            ),
            body=maybe_transform(
                {
                    "ai_gateway_id": ai_gateway_id,
                    "aisearch_model": aisearch_model,
                    "cache": cache,
                    "cache_threshold": cache_threshold,
                    "cache_ttl": cache_ttl,
                    "chunk": chunk,
                    "chunk_overlap": chunk_overlap,
                    "chunk_size": chunk_size,
                    "custom_metadata": custom_metadata,
                    "embedding_model": embedding_model,
                    "fusion_method": fusion_method,
                    "index_method": index_method,
                    "indexing_options": indexing_options,
                    "max_num_results": max_num_results,
                    "metadata": metadata,
                    "paused": paused,
                    "public_endpoint_params": public_endpoint_params,
                    "reranking": reranking,
                    "reranking_model": reranking_model,
                    "retrieval_options": retrieval_options,
                    "rewrite_model": rewrite_model,
                    "rewrite_query": rewrite_query,
                    "score_threshold": score_threshold,
                    "source_params": source_params,
                    "summarization": summarization,
                    "summarization_model": summarization_model,
                    "sync_interval": sync_interval,
                    "system_prompt_aisearch": system_prompt_aisearch,
                    "system_prompt_index_summarization": system_prompt_index_summarization,
                    "system_prompt_rewrite_query": system_prompt_rewrite_query,
                    "token_id": token_id,
                },
                instance_update_params.InstanceUpdateParams,
            ),
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                post_parser=ResultWrapper[InstanceUpdateResponse]._unwrapper,
            ),
            cast_to=cast(Type[InstanceUpdateResponse], ResultWrapper[InstanceUpdateResponse]),
        )

    def list(
        self,
        name: str,
        *,
        account_id: str,
        namespace: str | Omit = omit,
        order_by: Literal["created_at"] | Omit = omit,
        order_by_direction: Literal["asc", "desc"] | Omit = omit,
        page: int | Omit = omit,
        per_page: int | Omit = omit,
        search: str | Omit = omit,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> SyncV4PagePaginationArray[InstanceListResponse]:
        """
        List instances.

        Args:
          namespace: Filter by namespace.

          order_by: Field to order results by.

          order_by_direction: Order direction.

          page: Page number (1-indexed).

          per_page: Number of results per page.

          search: Filter instances whose id contains this string (case-insensitive).

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not account_id:
            raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
        if not name:
            raise ValueError(f"Expected a non-empty value for `name` but received {name!r}")
        return self._get_api_list(
            path_template(
                "/accounts/{account_id}/ai-search/namespaces/{name}/instances", account_id=account_id, name=name
            ),
            page=SyncV4PagePaginationArray[InstanceListResponse],
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                query=maybe_transform(
                    {
                        "namespace": namespace,
                        "order_by": order_by,
                        "order_by_direction": order_by_direction,
                        "page": page,
                        "per_page": per_page,
                        "search": search,
                    },
                    instance_list_params.InstanceListParams,
                ),
            ),
            model=InstanceListResponse,
        )

    def delete(
        self,
        id: str,
        *,
        account_id: str,
        name: str,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> InstanceDeleteResponse:
        """
        Delete instance.

        Args:
          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not account_id:
            raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
        if not name:
            raise ValueError(f"Expected a non-empty value for `name` but received {name!r}")
        if not id:
            raise ValueError(f"Expected a non-empty value for `id` but received {id!r}")
        return self._delete(
            path_template(
                "/accounts/{account_id}/ai-search/namespaces/{name}/instances/{id}",
                account_id=account_id,
                name=name,
                id=id,
            ),
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                post_parser=ResultWrapper[InstanceDeleteResponse]._unwrapper,
            ),
            cast_to=cast(Type[InstanceDeleteResponse], ResultWrapper[InstanceDeleteResponse]),
        )

    def chat_completions(
        self,
        id: str,
        *,
        account_id: str,
        name: str,
        messages: Iterable[instance_chat_completions_params.Message],
        aisearch_options: instance_chat_completions_params.AISearchOptions | Omit = omit,
        model: 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",
            "@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",
            "@cf/google/gemma-4-26b-a4b-it",
            "@cf/moonshotai/kimi-k2.5",
            "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",
            "",
        ]
        | Omit = omit,
        stream: bool | Omit = omit,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> InstanceChatCompletionsResponse:
        """
        Performs a chat completion request against an AI Search instance, using indexed
        content as context for generating responses.

        Args:
          id: AI Search instance ID. Lowercase alphanumeric, hyphens, and underscores.

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not account_id:
            raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
        if not name:
            raise ValueError(f"Expected a non-empty value for `name` but received {name!r}")
        if not id:
            raise ValueError(f"Expected a non-empty value for `id` but received {id!r}")
        return self._post(
            path_template(
                "/accounts/{account_id}/ai-search/namespaces/{name}/instances/{id}/chat/completions",
                account_id=account_id,
                name=name,
                id=id,
            ),
            body=maybe_transform(
                {
                    "messages": messages,
                    "aisearch_options": aisearch_options,
                    "model": model,
                    "stream": stream,
                },
                instance_chat_completions_params.InstanceChatCompletionsParams,
            ),
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=InstanceChatCompletionsResponse,
        )

    def read(
        self,
        id: str,
        *,
        account_id: str,
        name: str,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> InstanceReadResponse:
        """
        Read instance.

        Args:
          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not account_id:
            raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
        if not name:
            raise ValueError(f"Expected a non-empty value for `name` but received {name!r}")
        if not id:
            raise ValueError(f"Expected a non-empty value for `id` but received {id!r}")
        return self._get(
            path_template(
                "/accounts/{account_id}/ai-search/namespaces/{name}/instances/{id}",
                account_id=account_id,
                name=name,
                id=id,
            ),
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                post_parser=ResultWrapper[InstanceReadResponse]._unwrapper,
            ),
            cast_to=cast(Type[InstanceReadResponse], ResultWrapper[InstanceReadResponse]),
        )

    def search(
        self,
        id: str,
        *,
        account_id: str,
        name: str,
        aisearch_options: instance_search_params.AISearchOptions | Omit = omit,
        messages: Iterable[instance_search_params.Message] | Omit = omit,
        query: str | Omit = omit,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> InstanceSearchResponse:
        """
        Executes a semantic search query against an AI Search instance to find relevant
        indexed content.

        Args:
          id: AI Search instance ID. Lowercase alphanumeric, hyphens, and underscores.

          query: A simple text query string. Alternative to 'messages' — provide either this or
              'messages', not both.

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not account_id:
            raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
        if not name:
            raise ValueError(f"Expected a non-empty value for `name` but received {name!r}")
        if not id:
            raise ValueError(f"Expected a non-empty value for `id` but received {id!r}")
        return self._post(
            path_template(
                "/accounts/{account_id}/ai-search/namespaces/{name}/instances/{id}/search",
                account_id=account_id,
                name=name,
                id=id,
            ),
            body=maybe_transform(
                {
                    "aisearch_options": aisearch_options,
                    "messages": messages,
                    "query": query,
                },
                instance_search_params.InstanceSearchParams,
            ),
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                post_parser=ResultWrapper[InstanceSearchResponse]._unwrapper,
            ),
            cast_to=cast(Type[InstanceSearchResponse], ResultWrapper[InstanceSearchResponse]),
        )

    def stats(
        self,
        id: str,
        *,
        account_id: str,
        name: str,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> InstanceStatsResponse:
        """
        Retrieves usage statistics for AI Search instances.

        Args:
          id: AI Search instance ID. Lowercase alphanumeric, hyphens, and underscores.

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not account_id:
            raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
        if not name:
            raise ValueError(f"Expected a non-empty value for `name` but received {name!r}")
        if not id:
            raise ValueError(f"Expected a non-empty value for `id` but received {id!r}")
        return self._get(
            path_template(
                "/accounts/{account_id}/ai-search/namespaces/{name}/instances/{id}/stats",
                account_id=account_id,
                name=name,
                id=id,
            ),
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                post_parser=ResultWrapper[InstanceStatsResponse]._unwrapper,
            ),
            cast_to=cast(Type[InstanceStatsResponse], ResultWrapper[InstanceStatsResponse]),
        )


class AsyncInstancesResource(AsyncAPIResource):
    @cached_property
    def jobs(self) -> AsyncJobsResource:
        return AsyncJobsResource(self._client)

    @cached_property
    def items(self) -> AsyncItemsResource:
        return AsyncItemsResource(self._client)

    @cached_property
    def with_raw_response(self) -> AsyncInstancesResourceWithRawResponse:
        """
        This property can be used as a prefix for any HTTP method call to return
        the raw response object instead of the parsed content.

        For more information, see https://www.github.com/cloudflare/cloudflare-python#accessing-raw-response-data-eg-headers
        """
        return AsyncInstancesResourceWithRawResponse(self)

    @cached_property
    def with_streaming_response(self) -> AsyncInstancesResourceWithStreamingResponse:
        """
        An alternative to `.with_raw_response` that doesn't eagerly read the response body.

        For more information, see https://www.github.com/cloudflare/cloudflare-python#with_streaming_response
        """
        return AsyncInstancesResourceWithStreamingResponse(self)

    async def create(
        self,
        name: str,
        *,
        account_id: str,
        id: str,
        ai_gateway_id: Optional[str] | Omit = omit,
        aisearch_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",
                "@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",
                "@cf/google/gemma-4-26b-a4b-it",
                "@cf/moonshotai/kimi-k2.5",
                "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",
                "",
            ]
        ]
        | Omit = omit,
        cache: bool | Omit = omit,
        cache_threshold: Literal["super_strict_match", "close_enough", "flexible_friend", "anything_goes"]
        | Omit = omit,
        cache_ttl: Literal[600, 1800, 3600, 7200, 21600, 43200, 86400, 172800, 259200, 518400] | Omit = omit,
        chunk: bool | Omit = omit,
        chunk_overlap: int | Omit = omit,
        chunk_size: int | Omit = omit,
        custom_metadata: Iterable[instance_create_params.CustomMetadata] | Omit = omit,
        embedding_model: Optional[
            Literal[
                "@cf/qwen/qwen3-embedding-0.6b",
                "@cf/baai/bge-m3",
                "@cf/baai/bge-large-en-v1.5",
                "@cf/google/embeddinggemma-300m",
                "google-ai-studio/gemini-embedding-001",
                "google-ai-studio/gemini-embedding-2-preview",
                "openai/text-embedding-3-small",
                "openai/text-embedding-3-large",
                "",
            ]
        ]
        | Omit = omit,
        fusion_method: Literal["max", "rrf"] | Omit = omit,
        hybrid_search_enabled: bool | Omit = omit,
        index_method: instance_create_params.IndexMethod | Omit = omit,
        indexing_options: Optional[instance_create_params.IndexingOptions] | Omit = omit,
        max_num_results: int | Omit = omit,
        metadata: instance_create_params.Metadata | Omit = omit,
        public_endpoint_params: instance_create_params.PublicEndpointParams | Omit = omit,
        reranking: bool | Omit = omit,
        reranking_model: Optional[Literal["@cf/baai/bge-reranker-base", ""]] | Omit = omit,
        retrieval_options: Optional[instance_create_params.RetrievalOptions] | Omit = omit,
        rewrite_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",
                "@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",
                "@cf/google/gemma-4-26b-a4b-it",
                "@cf/moonshotai/kimi-k2.5",
                "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",
                "",
            ]
        ]
        | Omit = omit,
        rewrite_query: bool | Omit = omit,
        score_threshold: float | Omit = omit,
        source: Optional[str] | Omit = omit,
        source_params: Optional[instance_create_params.SourceParams] | Omit = omit,
        sync_interval: Literal[900, 1800, 3600, 7200, 14400, 21600, 43200, 86400] | Omit = omit,
        token_id: str | Omit = omit,
        type: Optional[Literal["r2", "web-crawler"]] | Omit = omit,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> InstanceCreateResponse:
        """Create a new instance.

        Args:
          id: AI Search instance ID.

        Lowercase alphanumeric, hyphens, and underscores.

          cache_ttl: Cache entry TTL in seconds. Allowed values: 600 (10min), 1800 (30min), 3600
              (1h), 7200 (2h), 21600 (6h), 43200 (12h), 86400 (24h), 172800 (48h), 259200
              (72h), 518400 (6d).

          hybrid_search_enabled: Deprecated — use index_method instead.

          index_method: Controls which storage backends are used during indexing. Defaults to
              vector-only.

          sync_interval: Interval between automatic syncs, in seconds. Allowed values: 900 (15min), 1800
              (30min), 3600 (1h), 7200 (2h), 14400 (4h), 21600 (6h), 43200 (12h), 86400 (24h).

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not account_id:
            raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
        if not name:
            raise ValueError(f"Expected a non-empty value for `name` but received {name!r}")
        return await self._post(
            path_template(
                "/accounts/{account_id}/ai-search/namespaces/{name}/instances", account_id=account_id, name=name
            ),
            body=await async_maybe_transform(
                {
                    "id": id,
                    "ai_gateway_id": ai_gateway_id,
                    "aisearch_model": aisearch_model,
                    "cache": cache,
                    "cache_threshold": cache_threshold,
                    "cache_ttl": cache_ttl,
                    "chunk": chunk,
                    "chunk_overlap": chunk_overlap,
                    "chunk_size": chunk_size,
                    "custom_metadata": custom_metadata,
                    "embedding_model": embedding_model,
                    "fusion_method": fusion_method,
                    "hybrid_search_enabled": hybrid_search_enabled,
                    "index_method": index_method,
                    "indexing_options": indexing_options,
                    "max_num_results": max_num_results,
                    "metadata": metadata,
                    "public_endpoint_params": public_endpoint_params,
                    "reranking": reranking,
                    "reranking_model": reranking_model,
                    "retrieval_options": retrieval_options,
                    "rewrite_model": rewrite_model,
                    "rewrite_query": rewrite_query,
                    "score_threshold": score_threshold,
                    "source": source,
                    "source_params": source_params,
                    "sync_interval": sync_interval,
                    "token_id": token_id,
                    "type": type,
                },
                instance_create_params.InstanceCreateParams,
            ),
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                post_parser=ResultWrapper[InstanceCreateResponse]._unwrapper,
            ),
            cast_to=cast(Type[InstanceCreateResponse], ResultWrapper[InstanceCreateResponse]),
        )

    async def update(
        self,
        id: str,
        *,
        account_id: str,
        name: str,
        ai_gateway_id: Optional[str] | Omit = omit,
        aisearch_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",
                "@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",
                "@cf/google/gemma-4-26b-a4b-it",
                "@cf/moonshotai/kimi-k2.5",
                "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",
                "",
            ]
        ]
        | Omit = omit,
        cache: bool | Omit = omit,
        cache_threshold: Literal["super_strict_match", "close_enough", "flexible_friend", "anything_goes"]
        | Omit = omit,
        cache_ttl: Literal[600, 1800, 3600, 7200, 21600, 43200, 86400, 172800, 259200, 518400] | Omit = omit,
        chunk: bool | Omit = omit,
        chunk_overlap: int | Omit = omit,
        chunk_size: int | Omit = omit,
        custom_metadata: Iterable[instance_update_params.CustomMetadata] | Omit = omit,
        embedding_model: Optional[
            Literal[
                "@cf/qwen/qwen3-embedding-0.6b",
                "@cf/baai/bge-m3",
                "@cf/baai/bge-large-en-v1.5",
                "@cf/google/embeddinggemma-300m",
                "google-ai-studio/gemini-embedding-001",
                "google-ai-studio/gemini-embedding-2-preview",
                "openai/text-embedding-3-small",
                "openai/text-embedding-3-large",
                "",
            ]
        ]
        | Omit = omit,
        fusion_method: Literal["max", "rrf"] | Omit = omit,
        index_method: instance_update_params.IndexMethod | Omit = omit,
        indexing_options: Optional[instance_update_params.IndexingOptions] | Omit = omit,
        max_num_results: int | Omit = omit,
        metadata: instance_update_params.Metadata | Omit = omit,
        paused: bool | Omit = omit,
        public_endpoint_params: instance_update_params.PublicEndpointParams | Omit = omit,
        reranking: bool | Omit = omit,
        reranking_model: Optional[Literal["@cf/baai/bge-reranker-base", ""]] | Omit = omit,
        retrieval_options: Optional[instance_update_params.RetrievalOptions] | Omit = omit,
        rewrite_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",
                "@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",
                "@cf/google/gemma-4-26b-a4b-it",
                "@cf/moonshotai/kimi-k2.5",
                "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",
                "",
            ]
        ]
        | Omit = omit,
        rewrite_query: bool | Omit = omit,
        score_threshold: float | Omit = omit,
        source_params: Optional[instance_update_params.SourceParams] | Omit = omit,
        summarization: bool | Omit = omit,
        summarization_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",
                "@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",
                "@cf/google/gemma-4-26b-a4b-it",
                "@cf/moonshotai/kimi-k2.5",
                "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",
                "",
            ]
        ]
        | Omit = omit,
        sync_interval: Literal[900, 1800, 3600, 7200, 14400, 21600, 43200, 86400] | Omit = omit,
        system_prompt_aisearch: Optional[str] | Omit = omit,
        system_prompt_index_summarization: Optional[str] | Omit = omit,
        system_prompt_rewrite_query: Optional[str] | Omit = omit,
        token_id: str | Omit = omit,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> InstanceUpdateResponse:
        """Update instance.

        Args:
          cache_ttl: Cache entry TTL in seconds.

        Allowed values: 600 (10min), 1800 (30min), 3600
              (1h), 7200 (2h), 21600 (6h), 43200 (12h), 86400 (24h), 172800 (48h), 259200
              (72h), 518400 (6d).

          index_method: Controls which storage backends are used during indexing. Defaults to
              vector-only.

          sync_interval: Interval between automatic syncs, in seconds. Allowed values: 900 (15min), 1800
              (30min), 3600 (1h), 7200 (2h), 14400 (4h), 21600 (6h), 43200 (12h), 86400 (24h).

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not account_id:
            raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
        if not name:
            raise ValueError(f"Expected a non-empty value for `name` but received {name!r}")
        if not id:
            raise ValueError(f"Expected a non-empty value for `id` but received {id!r}")
        return await self._put(
            path_template(
                "/accounts/{account_id}/ai-search/namespaces/{name}/instances/{id}",
                account_id=account_id,
                name=name,
                id=id,
            ),
            body=await async_maybe_transform(
                {
                    "ai_gateway_id": ai_gateway_id,
                    "aisearch_model": aisearch_model,
                    "cache": cache,
                    "cache_threshold": cache_threshold,
                    "cache_ttl": cache_ttl,
                    "chunk": chunk,
                    "chunk_overlap": chunk_overlap,
                    "chunk_size": chunk_size,
                    "custom_metadata": custom_metadata,
                    "embedding_model": embedding_model,
                    "fusion_method": fusion_method,
                    "index_method": index_method,
                    "indexing_options": indexing_options,
                    "max_num_results": max_num_results,
                    "metadata": metadata,
                    "paused": paused,
                    "public_endpoint_params": public_endpoint_params,
                    "reranking": reranking,
                    "reranking_model": reranking_model,
                    "retrieval_options": retrieval_options,
                    "rewrite_model": rewrite_model,
                    "rewrite_query": rewrite_query,
                    "score_threshold": score_threshold,
                    "source_params": source_params,
                    "summarization": summarization,
                    "summarization_model": summarization_model,
                    "sync_interval": sync_interval,
                    "system_prompt_aisearch": system_prompt_aisearch,
                    "system_prompt_index_summarization": system_prompt_index_summarization,
                    "system_prompt_rewrite_query": system_prompt_rewrite_query,
                    "token_id": token_id,
                },
                instance_update_params.InstanceUpdateParams,
            ),
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                post_parser=ResultWrapper[InstanceUpdateResponse]._unwrapper,
            ),
            cast_to=cast(Type[InstanceUpdateResponse], ResultWrapper[InstanceUpdateResponse]),
        )

    def list(
        self,
        name: str,
        *,
        account_id: str,
        namespace: str | Omit = omit,
        order_by: Literal["created_at"] | Omit = omit,
        order_by_direction: Literal["asc", "desc"] | Omit = omit,
        page: int | Omit = omit,
        per_page: int | Omit = omit,
        search: str | Omit = omit,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> AsyncPaginator[InstanceListResponse, AsyncV4PagePaginationArray[InstanceListResponse]]:
        """
        List instances.

        Args:
          namespace: Filter by namespace.

          order_by: Field to order results by.

          order_by_direction: Order direction.

          page: Page number (1-indexed).

          per_page: Number of results per page.

          search: Filter instances whose id contains this string (case-insensitive).

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not account_id:
            raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
        if not name:
            raise ValueError(f"Expected a non-empty value for `name` but received {name!r}")
        return self._get_api_list(
            path_template(
                "/accounts/{account_id}/ai-search/namespaces/{name}/instances", account_id=account_id, name=name
            ),
            page=AsyncV4PagePaginationArray[InstanceListResponse],
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                query=maybe_transform(
                    {
                        "namespace": namespace,
                        "order_by": order_by,
                        "order_by_direction": order_by_direction,
                        "page": page,
                        "per_page": per_page,
                        "search": search,
                    },
                    instance_list_params.InstanceListParams,
                ),
            ),
            model=InstanceListResponse,
        )

    async def delete(
        self,
        id: str,
        *,
        account_id: str,
        name: str,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> InstanceDeleteResponse:
        """
        Delete instance.

        Args:
          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not account_id:
            raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
        if not name:
            raise ValueError(f"Expected a non-empty value for `name` but received {name!r}")
        if not id:
            raise ValueError(f"Expected a non-empty value for `id` but received {id!r}")
        return await self._delete(
            path_template(
                "/accounts/{account_id}/ai-search/namespaces/{name}/instances/{id}",
                account_id=account_id,
                name=name,
                id=id,
            ),
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                post_parser=ResultWrapper[InstanceDeleteResponse]._unwrapper,
            ),
            cast_to=cast(Type[InstanceDeleteResponse], ResultWrapper[InstanceDeleteResponse]),
        )

    async def chat_completions(
        self,
        id: str,
        *,
        account_id: str,
        name: str,
        messages: Iterable[instance_chat_completions_params.Message],
        aisearch_options: instance_chat_completions_params.AISearchOptions | Omit = omit,
        model: 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",
            "@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",
            "@cf/google/gemma-4-26b-a4b-it",
            "@cf/moonshotai/kimi-k2.5",
            "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",
            "",
        ]
        | Omit = omit,
        stream: bool | Omit = omit,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> InstanceChatCompletionsResponse:
        """
        Performs a chat completion request against an AI Search instance, using indexed
        content as context for generating responses.

        Args:
          id: AI Search instance ID. Lowercase alphanumeric, hyphens, and underscores.

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not account_id:
            raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
        if not name:
            raise ValueError(f"Expected a non-empty value for `name` but received {name!r}")
        if not id:
            raise ValueError(f"Expected a non-empty value for `id` but received {id!r}")
        return await self._post(
            path_template(
                "/accounts/{account_id}/ai-search/namespaces/{name}/instances/{id}/chat/completions",
                account_id=account_id,
                name=name,
                id=id,
            ),
            body=await async_maybe_transform(
                {
                    "messages": messages,
                    "aisearch_options": aisearch_options,
                    "model": model,
                    "stream": stream,
                },
                instance_chat_completions_params.InstanceChatCompletionsParams,
            ),
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=InstanceChatCompletionsResponse,
        )

    async def read(
        self,
        id: str,
        *,
        account_id: str,
        name: str,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> InstanceReadResponse:
        """
        Read instance.

        Args:
          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not account_id:
            raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
        if not name:
            raise ValueError(f"Expected a non-empty value for `name` but received {name!r}")
        if not id:
            raise ValueError(f"Expected a non-empty value for `id` but received {id!r}")
        return await self._get(
            path_template(
                "/accounts/{account_id}/ai-search/namespaces/{name}/instances/{id}",
                account_id=account_id,
                name=name,
                id=id,
            ),
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                post_parser=ResultWrapper[InstanceReadResponse]._unwrapper,
            ),
            cast_to=cast(Type[InstanceReadResponse], ResultWrapper[InstanceReadResponse]),
        )

    async def search(
        self,
        id: str,
        *,
        account_id: str,
        name: str,
        aisearch_options: instance_search_params.AISearchOptions | Omit = omit,
        messages: Iterable[instance_search_params.Message] | Omit = omit,
        query: str | Omit = omit,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> InstanceSearchResponse:
        """
        Executes a semantic search query against an AI Search instance to find relevant
        indexed content.

        Args:
          id: AI Search instance ID. Lowercase alphanumeric, hyphens, and underscores.

          query: A simple text query string. Alternative to 'messages' — provide either this or
              'messages', not both.

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not account_id:
            raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
        if not name:
            raise ValueError(f"Expected a non-empty value for `name` but received {name!r}")
        if not id:
            raise ValueError(f"Expected a non-empty value for `id` but received {id!r}")
        return await self._post(
            path_template(
                "/accounts/{account_id}/ai-search/namespaces/{name}/instances/{id}/search",
                account_id=account_id,
                name=name,
                id=id,
            ),
            body=await async_maybe_transform(
                {
                    "aisearch_options": aisearch_options,
                    "messages": messages,
                    "query": query,
                },
                instance_search_params.InstanceSearchParams,
            ),
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                post_parser=ResultWrapper[InstanceSearchResponse]._unwrapper,
            ),
            cast_to=cast(Type[InstanceSearchResponse], ResultWrapper[InstanceSearchResponse]),
        )

    async def stats(
        self,
        id: str,
        *,
        account_id: str,
        name: str,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> InstanceStatsResponse:
        """
        Retrieves usage statistics for AI Search instances.

        Args:
          id: AI Search instance ID. Lowercase alphanumeric, hyphens, and underscores.

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not account_id:
            raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
        if not name:
            raise ValueError(f"Expected a non-empty value for `name` but received {name!r}")
        if not id:
            raise ValueError(f"Expected a non-empty value for `id` but received {id!r}")
        return await self._get(
            path_template(
                "/accounts/{account_id}/ai-search/namespaces/{name}/instances/{id}/stats",
                account_id=account_id,
                name=name,
                id=id,
            ),
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                post_parser=ResultWrapper[InstanceStatsResponse]._unwrapper,
            ),
            cast_to=cast(Type[InstanceStatsResponse], ResultWrapper[InstanceStatsResponse]),
        )


class InstancesResourceWithRawResponse:
    def __init__(self, instances: InstancesResource) -> None:
        self._instances = instances

        self.create = to_raw_response_wrapper(
            instances.create,
        )
        self.update = to_raw_response_wrapper(
            instances.update,
        )
        self.list = to_raw_response_wrapper(
            instances.list,
        )
        self.delete = to_raw_response_wrapper(
            instances.delete,
        )
        self.chat_completions = to_raw_response_wrapper(
            instances.chat_completions,
        )
        self.read = to_raw_response_wrapper(
            instances.read,
        )
        self.search = to_raw_response_wrapper(
            instances.search,
        )
        self.stats = to_raw_response_wrapper(
            instances.stats,
        )

    @cached_property
    def jobs(self) -> JobsResourceWithRawResponse:
        return JobsResourceWithRawResponse(self._instances.jobs)

    @cached_property
    def items(self) -> ItemsResourceWithRawResponse:
        return ItemsResourceWithRawResponse(self._instances.items)


class AsyncInstancesResourceWithRawResponse:
    def __init__(self, instances: AsyncInstancesResource) -> None:
        self._instances = instances

        self.create = async_to_raw_response_wrapper(
            instances.create,
        )
        self.update = async_to_raw_response_wrapper(
            instances.update,
        )
        self.list = async_to_raw_response_wrapper(
            instances.list,
        )
        self.delete = async_to_raw_response_wrapper(
            instances.delete,
        )
        self.chat_completions = async_to_raw_response_wrapper(
            instances.chat_completions,
        )
        self.read = async_to_raw_response_wrapper(
            instances.read,
        )
        self.search = async_to_raw_response_wrapper(
            instances.search,
        )
        self.stats = async_to_raw_response_wrapper(
            instances.stats,
        )

    @cached_property
    def jobs(self) -> AsyncJobsResourceWithRawResponse:
        return AsyncJobsResourceWithRawResponse(self._instances.jobs)

    @cached_property
    def items(self) -> AsyncItemsResourceWithRawResponse:
        return AsyncItemsResourceWithRawResponse(self._instances.items)


class InstancesResourceWithStreamingResponse:
    def __init__(self, instances: InstancesResource) -> None:
        self._instances = instances

        self.create = to_streamed_response_wrapper(
            instances.create,
        )
        self.update = to_streamed_response_wrapper(
            instances.update,
        )
        self.list = to_streamed_response_wrapper(
            instances.list,
        )
        self.delete = to_streamed_response_wrapper(
            instances.delete,
        )
        self.chat_completions = to_streamed_response_wrapper(
            instances.chat_completions,
        )
        self.read = to_streamed_response_wrapper(
            instances.read,
        )
        self.search = to_streamed_response_wrapper(
            instances.search,
        )
        self.stats = to_streamed_response_wrapper(
            instances.stats,
        )

    @cached_property
    def jobs(self) -> JobsResourceWithStreamingResponse:
        return JobsResourceWithStreamingResponse(self._instances.jobs)

    @cached_property
    def items(self) -> ItemsResourceWithStreamingResponse:
        return ItemsResourceWithStreamingResponse(self._instances.items)


class AsyncInstancesResourceWithStreamingResponse:
    def __init__(self, instances: AsyncInstancesResource) -> None:
        self._instances = instances

        self.create = async_to_streamed_response_wrapper(
            instances.create,
        )
        self.update = async_to_streamed_response_wrapper(
            instances.update,
        )
        self.list = async_to_streamed_response_wrapper(
            instances.list,
        )
        self.delete = async_to_streamed_response_wrapper(
            instances.delete,
        )
        self.chat_completions = async_to_streamed_response_wrapper(
            instances.chat_completions,
        )
        self.read = async_to_streamed_response_wrapper(
            instances.read,
        )
        self.search = async_to_streamed_response_wrapper(
            instances.search,
        )
        self.stats = async_to_streamed_response_wrapper(
            instances.stats,
        )

    @cached_property
    def jobs(self) -> AsyncJobsResourceWithStreamingResponse:
        return AsyncJobsResourceWithStreamingResponse(self._instances.jobs)

    @cached_property
    def items(self) -> AsyncItemsResourceWithStreamingResponse:
        return AsyncItemsResourceWithStreamingResponse(self._instances.items)
