# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. from __future__ import annotations from typing import Union, Iterable, Optional from typing_extensions import Literal import httpx from ...._types import Body, Omit, Query, Headers, NotGiven, SequenceNotStr, omit, not_given from ...._utils import 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 ...._base_client import make_request_options from ....types.ai.openai import chat_create_completion_params from ....types.ai.openai.chat_create_completion_response import ChatCreateCompletionResponse __all__ = ["ChatResource", "AsyncChatResource"] class ChatResource(SyncAPIResource): @cached_property def with_raw_response(self) -> ChatResourceWithRawResponse: """ 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/team-telnyx/telnyx-python#accessing-raw-response-data-eg-headers """ return ChatResourceWithRawResponse(self) @cached_property def with_streaming_response(self) -> ChatResourceWithStreamingResponse: """ An alternative to `.with_raw_response` that doesn't eagerly read the response body. For more information, see https://www.github.com/team-telnyx/telnyx-python#with_streaming_response """ return ChatResourceWithStreamingResponse(self) def create_completion( self, *, messages: Iterable[chat_create_completion_params.Message], api_key_ref: str | Omit = omit, best_of: int | Omit = omit, early_stopping: bool | Omit = omit, enable_thinking: bool | Omit = omit, frequency_penalty: float | Omit = omit, length_penalty: float | Omit = omit, logprobs: bool | Omit = omit, max_tokens: Optional[int] | Omit = omit, min_p: float | Omit = omit, mode: Literal["preferred", "strict"] | Omit = omit, model: str | Omit = omit, n: float | Omit = omit, presence_penalty: float | Omit = omit, reasoning_effort: Literal["none", "minimal", "low", "medium", "high", "xhigh", "max"] | Omit = omit, region: Literal["USA", "EU", "AUS", "UAE"] | Omit = omit, response_format: chat_create_completion_params.ResponseFormat | Omit = omit, seed: int | Omit = omit, service_tier: str | Omit = omit, stop: Union[str, SequenceNotStr[str]] | Omit = omit, stream: bool | Omit = omit, temperature: float | Omit = omit, tool_choice: Literal["none", "auto", "required"] | Omit = omit, tools: Iterable[chat_create_completion_params.Tool] | Omit = omit, top_logprobs: int | Omit = omit, top_p: float | Omit = omit, use_beam_search: 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, ) -> ChatCreateCompletionResponse: """Chat with a language model. This endpoint is consistent with the [OpenAI Chat Completions API](https://platform.openai.com/docs/api-reference/chat) and may be used with the OpenAI JS or Python SDK by setting the base URL to `https://api.telnyx.com/v2/ai/openai`. Args: messages: A list of the previous chat messages for context. api_key_ref: If you are using an external inference provider like xAI or OpenAI, this field allows you to pass along a reference to your API key. After creating an [integration secret](https://developers.telnyx.com/api-reference/integration-secrets/create-a-secret) for you API key, pass the secret's `identifier` in this field. best_of: This is used with `use_beam_search` to determine how many candidate beams to explore. early_stopping: This is used with `use_beam_search`. If `true`, generation stops as soon as there are `best_of` complete candidates; if `false`, a heuristic is applied and the generation stops when is it very unlikely to find better candidates. enable_thinking: Whether to enable the thinking/reasoning phase for models that support it (e.g., QwQ, Qwen3). When set to false, the model will skip the internal reasoning step and respond directly, which can reduce latency. Defaults to true. frequency_penalty: Higher values will penalize the model from repeating the same output tokens. length_penalty: This is used with `use_beam_search` to prefer shorter or longer completions. logprobs: Whether to return log probabilities of the output tokens or not. If true, returns the log probabilities of each output token returned in the `content` of `message`. max_tokens: Maximum number of completion (output) tokens, including reasoning and answer tokens combined. An omitted or null limit defaults to 8192 unless the model is excluded from that default; excluded models leave the limit to the serving engine. Explicit values and applied defaults are clamped to the model's max_completion_tokens metadata when that ceiling is set (see GET /v2/ai/openai/models). An omitted limit on an excluded model remains unset even when metadata declares a ceiling. Input and output together must fit the model's context window. Set an explicit limit when a bounded generation budget is required. For example, omitted or null limits default to 8192 for `zai-org/GLM-5.3` and `zai-org/GLM-5.3-Flash`. Hosted exclusions include `moonshotai/Kimi-K3`, `moonshotai/Kimi-K2.6`, `moonshotai/Kimi-K2.5`, `zai-org/GLM-5.2`, `zai-org/GLM-5.1-FP8`, `MiniMaxAI/MiniMax-M3-MXFP8`, `MiniMaxAI/MiniMax-M2.7`, `Qwen/Qwen3.8-27B`, `Qwen/Qwen3-235B-A22B`, `meta-llama/Llama-3.3-70B-Instruct`, `meta-llama/Meta-Llama-3.1-70B-Instruct`, `meta-llama/Meta-Llama-3.1-8B-Instruct` and `google/gemma-2b-it`. Legacy external and fine-tuned model IDs can also bypass the default; model discovery reports completion ceilings, not whether this default applies. Set an explicit limit for predictable behavior across models. min_p: This is an alternative to `top_p` that [many prefer](https://github.com/huggingface/transformers/issues/27670). Must be in [0, 1]. mode: How strictly `region` is applied. `preferred` (the default when `region` is set) tries that region first and falls back to another when the model cannot be served there, so a request that would have succeeded still succeeds. `strict` pins the request: it is served from that region or it fails with a 422, never redirected to another region. Requires `region`. model: The language model to chat with. n: This will return multiple choices for you instead of a single chat completion. presence_penalty: Higher values will penalize the model from repeating the same output tokens. reasoning_effort: Controls the reasoning effort for models that support it. When set, the model spends more or less compute on internal reasoning before generating its response. Supported values: none, minimal, low, medium, high, xhigh, max. Not all models support all values; unsupported values are rejected with a 400 error. When omitted, reasoning models use their default effort level. region: Optional data-residency region the request should be served from, using the same vocabulary as your account's Data Locality setting. Behavior depends on `mode`. Supported for Telnyx-hosted models only: a request routed to an external provider never passes through Telnyx model routing, so a region cannot be enforced for it. Omit for today's latency-based routing. response_format: Controls the format of the model output. `json_object` guarantees valid JSON output without defining a schema; `json_schema` constrains the output to the JSON schema you supply via the `json_schema` property and is the supported way to get guaranteed structured output on Telnyx-hosted models. seed: If specified, the system will make a best effort to sample deterministically, such that repeated requests with the same `seed` and parameters should return the same result. service_tier: The service tier to use for this request. Supported values vary by model; use `GET /v2/ai/openai/models` and inspect the model's `service_tiers` field. If omitted, Telnyx-hosted models use `default`. stop: Up to 4 sequences where the API will stop generating further tokens. The returned text will not contain the stop sequence. stream: Whether or not to stream data-only server-sent events as they become available. temperature: Adjusts the "creativity" of the model. Lower values make the model more deterministic and repetitive, while higher values make the model more random and creative. tools: The `function` tool type follows the same schema as the [OpenAI Chat Completions API](https://platform.openai.com/docs/api-reference/chat). The `retrieval` tool type is unique to Telnyx. You may pass a list of [embedded storage buckets](https://developers.telnyx.com/api-reference/embeddings/embed-documents) for retrieval-augmented generation. top_logprobs: This is used with `logprobs`. An integer between 0 and 20 specifying the number of most likely tokens to return at each token position, each with an associated log probability. top_p: An alternative or complement to `temperature`. This adjusts how many of the top possibilities to consider. use_beam_search: Setting this to `true` will allow the model to [explore more completion options](https://huggingface.co/blog/how-to-generate#beam-search). This is not supported by OpenAI. 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 """ return self._post( "/ai/openai/chat/completions", body=maybe_transform( { "messages": messages, "api_key_ref": api_key_ref, "best_of": best_of, "early_stopping": early_stopping, "enable_thinking": enable_thinking, "frequency_penalty": frequency_penalty, "length_penalty": length_penalty, "logprobs": logprobs, "max_tokens": max_tokens, "min_p": min_p, "mode": mode, "model": model, "n": n, "presence_penalty": presence_penalty, "reasoning_effort": reasoning_effort, "region": region, "response_format": response_format, "seed": seed, "service_tier": service_tier, "stop": stop, "stream": stream, "temperature": temperature, "tool_choice": tool_choice, "tools": tools, "top_logprobs": top_logprobs, "top_p": top_p, "use_beam_search": use_beam_search, }, chat_create_completion_params.ChatCreateCompletionParams, ), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), cast_to=ChatCreateCompletionResponse, ) class AsyncChatResource(AsyncAPIResource): @cached_property def with_raw_response(self) -> AsyncChatResourceWithRawResponse: """ 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/team-telnyx/telnyx-python#accessing-raw-response-data-eg-headers """ return AsyncChatResourceWithRawResponse(self) @cached_property def with_streaming_response(self) -> AsyncChatResourceWithStreamingResponse: """ An alternative to `.with_raw_response` that doesn't eagerly read the response body. For more information, see https://www.github.com/team-telnyx/telnyx-python#with_streaming_response """ return AsyncChatResourceWithStreamingResponse(self) async def create_completion( self, *, messages: Iterable[chat_create_completion_params.Message], api_key_ref: str | Omit = omit, best_of: int | Omit = omit, early_stopping: bool | Omit = omit, enable_thinking: bool | Omit = omit, frequency_penalty: float | Omit = omit, length_penalty: float | Omit = omit, logprobs: bool | Omit = omit, max_tokens: Optional[int] | Omit = omit, min_p: float | Omit = omit, mode: Literal["preferred", "strict"] | Omit = omit, model: str | Omit = omit, n: float | Omit = omit, presence_penalty: float | Omit = omit, reasoning_effort: Literal["none", "minimal", "low", "medium", "high", "xhigh", "max"] | Omit = omit, region: Literal["USA", "EU", "AUS", "UAE"] | Omit = omit, response_format: chat_create_completion_params.ResponseFormat | Omit = omit, seed: int | Omit = omit, service_tier: str | Omit = omit, stop: Union[str, SequenceNotStr[str]] | Omit = omit, stream: bool | Omit = omit, temperature: float | Omit = omit, tool_choice: Literal["none", "auto", "required"] | Omit = omit, tools: Iterable[chat_create_completion_params.Tool] | Omit = omit, top_logprobs: int | Omit = omit, top_p: float | Omit = omit, use_beam_search: 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, ) -> ChatCreateCompletionResponse: """Chat with a language model. This endpoint is consistent with the [OpenAI Chat Completions API](https://platform.openai.com/docs/api-reference/chat) and may be used with the OpenAI JS or Python SDK by setting the base URL to `https://api.telnyx.com/v2/ai/openai`. Args: messages: A list of the previous chat messages for context. api_key_ref: If you are using an external inference provider like xAI or OpenAI, this field allows you to pass along a reference to your API key. After creating an [integration secret](https://developers.telnyx.com/api-reference/integration-secrets/create-a-secret) for you API key, pass the secret's `identifier` in this field. best_of: This is used with `use_beam_search` to determine how many candidate beams to explore. early_stopping: This is used with `use_beam_search`. If `true`, generation stops as soon as there are `best_of` complete candidates; if `false`, a heuristic is applied and the generation stops when is it very unlikely to find better candidates. enable_thinking: Whether to enable the thinking/reasoning phase for models that support it (e.g., QwQ, Qwen3). When set to false, the model will skip the internal reasoning step and respond directly, which can reduce latency. Defaults to true. frequency_penalty: Higher values will penalize the model from repeating the same output tokens. length_penalty: This is used with `use_beam_search` to prefer shorter or longer completions. logprobs: Whether to return log probabilities of the output tokens or not. If true, returns the log probabilities of each output token returned in the `content` of `message`. max_tokens: Maximum number of completion (output) tokens, including reasoning and answer tokens combined. An omitted or null limit defaults to 8192 unless the model is excluded from that default; excluded models leave the limit to the serving engine. Explicit values and applied defaults are clamped to the model's max_completion_tokens metadata when that ceiling is set (see GET /v2/ai/openai/models). An omitted limit on an excluded model remains unset even when metadata declares a ceiling. Input and output together must fit the model's context window. Set an explicit limit when a bounded generation budget is required. For example, omitted or null limits default to 8192 for `zai-org/GLM-5.3` and `zai-org/GLM-5.3-Flash`. Hosted exclusions include `moonshotai/Kimi-K3`, `moonshotai/Kimi-K2.6`, `moonshotai/Kimi-K2.5`, `zai-org/GLM-5.2`, `zai-org/GLM-5.1-FP8`, `MiniMaxAI/MiniMax-M3-MXFP8`, `MiniMaxAI/MiniMax-M2.7`, `Qwen/Qwen3.8-27B`, `Qwen/Qwen3-235B-A22B`, `meta-llama/Llama-3.3-70B-Instruct`, `meta-llama/Meta-Llama-3.1-70B-Instruct`, `meta-llama/Meta-Llama-3.1-8B-Instruct` and `google/gemma-2b-it`. Legacy external and fine-tuned model IDs can also bypass the default; model discovery reports completion ceilings, not whether this default applies. Set an explicit limit for predictable behavior across models. min_p: This is an alternative to `top_p` that [many prefer](https://github.com/huggingface/transformers/issues/27670). Must be in [0, 1]. mode: How strictly `region` is applied. `preferred` (the default when `region` is set) tries that region first and falls back to another when the model cannot be served there, so a request that would have succeeded still succeeds. `strict` pins the request: it is served from that region or it fails with a 422, never redirected to another region. Requires `region`. model: The language model to chat with. n: This will return multiple choices for you instead of a single chat completion. presence_penalty: Higher values will penalize the model from repeating the same output tokens. reasoning_effort: Controls the reasoning effort for models that support it. When set, the model spends more or less compute on internal reasoning before generating its response. Supported values: none, minimal, low, medium, high, xhigh, max. Not all models support all values; unsupported values are rejected with a 400 error. When omitted, reasoning models use their default effort level. region: Optional data-residency region the request should be served from, using the same vocabulary as your account's Data Locality setting. Behavior depends on `mode`. Supported for Telnyx-hosted models only: a request routed to an external provider never passes through Telnyx model routing, so a region cannot be enforced for it. Omit for today's latency-based routing. response_format: Controls the format of the model output. `json_object` guarantees valid JSON output without defining a schema; `json_schema` constrains the output to the JSON schema you supply via the `json_schema` property and is the supported way to get guaranteed structured output on Telnyx-hosted models. seed: If specified, the system will make a best effort to sample deterministically, such that repeated requests with the same `seed` and parameters should return the same result. service_tier: The service tier to use for this request. Supported values vary by model; use `GET /v2/ai/openai/models` and inspect the model's `service_tiers` field. If omitted, Telnyx-hosted models use `default`. stop: Up to 4 sequences where the API will stop generating further tokens. The returned text will not contain the stop sequence. stream: Whether or not to stream data-only server-sent events as they become available. temperature: Adjusts the "creativity" of the model. Lower values make the model more deterministic and repetitive, while higher values make the model more random and creative. tools: The `function` tool type follows the same schema as the [OpenAI Chat Completions API](https://platform.openai.com/docs/api-reference/chat). The `retrieval` tool type is unique to Telnyx. You may pass a list of [embedded storage buckets](https://developers.telnyx.com/api-reference/embeddings/embed-documents) for retrieval-augmented generation. top_logprobs: This is used with `logprobs`. An integer between 0 and 20 specifying the number of most likely tokens to return at each token position, each with an associated log probability. top_p: An alternative or complement to `temperature`. This adjusts how many of the top possibilities to consider. use_beam_search: Setting this to `true` will allow the model to [explore more completion options](https://huggingface.co/blog/how-to-generate#beam-search). This is not supported by OpenAI. 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 """ return await self._post( "/ai/openai/chat/completions", body=await async_maybe_transform( { "messages": messages, "api_key_ref": api_key_ref, "best_of": best_of, "early_stopping": early_stopping, "enable_thinking": enable_thinking, "frequency_penalty": frequency_penalty, "length_penalty": length_penalty, "logprobs": logprobs, "max_tokens": max_tokens, "min_p": min_p, "mode": mode, "model": model, "n": n, "presence_penalty": presence_penalty, "reasoning_effort": reasoning_effort, "region": region, "response_format": response_format, "seed": seed, "service_tier": service_tier, "stop": stop, "stream": stream, "temperature": temperature, "tool_choice": tool_choice, "tools": tools, "top_logprobs": top_logprobs, "top_p": top_p, "use_beam_search": use_beam_search, }, chat_create_completion_params.ChatCreateCompletionParams, ), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), cast_to=ChatCreateCompletionResponse, ) class ChatResourceWithRawResponse: def __init__(self, chat: ChatResource) -> None: self._chat = chat self.create_completion = to_raw_response_wrapper( chat.create_completion, ) class AsyncChatResourceWithRawResponse: def __init__(self, chat: AsyncChatResource) -> None: self._chat = chat self.create_completion = async_to_raw_response_wrapper( chat.create_completion, ) class ChatResourceWithStreamingResponse: def __init__(self, chat: ChatResource) -> None: self._chat = chat self.create_completion = to_streamed_response_wrapper( chat.create_completion, ) class AsyncChatResourceWithStreamingResponse: def __init__(self, chat: AsyncChatResource) -> None: self._chat = chat self.create_completion = async_to_streamed_response_wrapper( chat.create_completion, )