// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. import { APIResource } from '../../core/resource'; import * as ChatCompletionsAPI from './completions'; import * as CompletionsAPI from '../completions'; import * as ModelsAPI from '../models'; import { APIPromise } from '../../core/api-promise'; import { Stream } from '../../core/streaming'; import { buildHeaders } from '../../internal/headers'; import { RequestOptions } from '../../internal/request-options'; export class Completions extends APIResource { /** * Generates a chat completion from an OpenAI-style `messages` array. * * Use this endpoint for standard messages-based chat inference; use * /v5/completions instead when you have a raw text `prompt` rather than messages, * /v5/responses for the OpenAI Responses API contract, and /v5/inference when the * payload does not follow any OpenAI schema. The request accepts the OpenAI Chat * Completions parameters (extra fields are allowed and forwarded), and the model * is selected from `model` given as `vendor/name`; most vendors are served through * the litellm proxy gateway, while OpenAI may use a native gateway. When `stream` * is set the response is delivered as server-sent events of * `chat.completion.chunk`; otherwise a single `chat.completion` object is * returned. Token usage is recorded for the account, and for streaming responses * it is read from the final chunk. */ create( params: CompletionCreateParamsNonStreaming, options?: RequestOptions, ): APIPromise; create( params: CompletionCreateParamsStreaming, options?: RequestOptions, ): APIPromise>; create( params: CompletionCreateParamsBase, options?: RequestOptions, ): APIPromise | CompletionCreateResponse>; create( params: CompletionCreateParams, options?: RequestOptions, ): APIPromise | APIPromise> { const { 'x-openai-api-key': xOpenAIAPIKey, ...body } = params; return this._client.post('/v5/chat/completions', { body, ...options, headers: buildHeaders([ { ...(xOpenAIAPIKey != null ? { 'x-openai-api-key': xOpenAIAPIKey } : undefined) }, options?.headers, ]), stream: params.stream ?? false, }) as APIPromise | APIPromise>; } /** * Lists the models available for use with /v5/chat/completions. * * Results are served directly from the stored inference-models catalog filtered to * the chat-completion model type, not from a live provider probe, so availability * reflects the catalog's recorded status. Pass the optional `model_vendor` query * parameter to restrict results to a single vendor. Results are paginated, and * each entry reports the model name, vendor, type, and availability. If the * underlying catalog query fails the endpoint returns an empty list rather than * raising an error. */ models( query: CompletionModelsParams | null | undefined = {}, options?: RequestOptions, ): APIPromise { return this._client.get('/v5/chat/completions/models', { query, ...options }); } } export interface ChatCompletion { id: string; choices: Array; created: number; model: string; object?: 'chat.completion'; service_tier?: 'auto' | 'default' | 'flex' | 'scale' | 'priority'; system_fingerprint?: string; /** * Usage statistics for the completion request. */ usage?: CompletionsAPI.CompletionUsage; [k: string]: unknown; } export namespace ChatCompletion { export interface Choice { finish_reason: 'stop' | 'length' | 'tool_calls' | 'content_filter' | 'function_call'; index: number; /** * A chat completion message generated by the model. */ message: Choice.Message; /** * Log probability information for the choice. */ logprobs?: ChatCompletionsAPI.ChoiceLogprobs; [k: string]: unknown; } export namespace Choice { /** * A chat completion message generated by the model. */ export interface Message { role: 'assistant'; annotations?: Array; /** * If the audio output modality is requested, this object contains data about the * audio response from the model. * [Learn more](https://platform.openai.com/docs/guides/audio). */ audio?: Message.Audio; content?: string; /** * Deprecated and replaced by `tool_calls`. * * The name and arguments of a function that should be called, as generated by the * model. */ function_call?: Message.FunctionCall; refusal?: string; tool_calls?: Array< Message.ChatCompletionMessageFunctionToolCall | Message.ChatCompletionMessageCustomToolCall >; [k: string]: unknown; } export namespace Message { /** * A URL citation when using web search. */ export interface Annotation { type: 'url_citation'; /** * A URL citation when using web search. */ url_citation: Annotation.URLCitation; [k: string]: unknown; } export namespace Annotation { /** * A URL citation when using web search. */ export interface URLCitation { end_index: number; start_index: number; title: string; url: string; [k: string]: unknown; } } /** * If the audio output modality is requested, this object contains data about the * audio response from the model. * [Learn more](https://platform.openai.com/docs/guides/audio). */ export interface Audio { id: string; data: string; expires_at: number; transcript: string; [k: string]: unknown; } /** * Deprecated and replaced by `tool_calls`. * * The name and arguments of a function that should be called, as generated by the * model. */ export interface FunctionCall { arguments: string; name: string; [k: string]: unknown; } /** * A call to a function tool created by the model. */ export interface ChatCompletionMessageFunctionToolCall { id: string; /** * The function that the model called. */ function: ChatCompletionMessageFunctionToolCall.Function; type: 'function'; [k: string]: unknown; } export namespace ChatCompletionMessageFunctionToolCall { /** * The function that the model called. */ export interface Function { arguments: string; name: string; [k: string]: unknown; } } /** * A call to a custom tool created by the model. */ export interface ChatCompletionMessageCustomToolCall { id: string; /** * The custom tool that the model called. */ custom: ChatCompletionMessageCustomToolCall.Custom; type: 'custom'; [k: string]: unknown; } export namespace ChatCompletionMessageCustomToolCall { /** * The custom tool that the model called. */ export interface Custom { input: string; name: string; [k: string]: unknown; } } } } } export interface ChatCompletionChunk { id: string; choices: Array; created: number; model: string; object?: 'chat.completion.chunk'; service_tier?: 'auto' | 'default' | 'flex' | 'scale' | 'priority'; system_fingerprint?: string; /** * Usage statistics for the completion request. */ usage?: CompletionsAPI.CompletionUsage; [k: string]: unknown; } export namespace ChatCompletionChunk { export interface Choice { /** * A chat completion delta generated by streamed model responses. */ delta: Choice.Delta; index: number; finish_reason?: 'stop' | 'length' | 'tool_calls' | 'content_filter' | 'function_call'; /** * Log probability information for the choice. */ logprobs?: ChatCompletionsAPI.ChoiceLogprobs; [k: string]: unknown; } export namespace Choice { /** * A chat completion delta generated by streamed model responses. */ export interface Delta { content?: string; /** * Deprecated and replaced by `tool_calls`. * * The name and arguments of a function that should be called, as generated by the * model. */ function_call?: Delta.FunctionCall; refusal?: string; role?: 'developer' | 'system' | 'user' | 'assistant' | 'tool'; tool_calls?: Array; [k: string]: unknown; } export namespace Delta { /** * Deprecated and replaced by `tool_calls`. * * The name and arguments of a function that should be called, as generated by the * model. */ export interface FunctionCall { arguments?: string; name?: string; [k: string]: unknown; } export interface ToolCall { index: number; id?: string; function?: ToolCall.Function; type?: 'function'; [k: string]: unknown; } export namespace ToolCall { export interface Function { arguments?: string; name?: string; [k: string]: unknown; } } } } } export interface ChatCompletionTokenLogprob { token: string; logprob: number; top_logprobs: Array; bytes?: Array; [k: string]: unknown; } export namespace ChatCompletionTokenLogprob { export interface TopLogprob { token: string; logprob: number; bytes?: Array; [k: string]: unknown; } } /** * Log probability information for the choice. */ export interface ChoiceLogprobs { content?: Array; refusal?: Array; [k: string]: unknown; } export type InferenceModelVendor = | 'openai' | 'cohere' | 'vertex_ai' | 'anthropic' | 'azure' | 'gemini' | 'launch' | 'llmengine' | 'model_zoo' | 'bedrock' | 'xai' | 'fireworks_ai'; export interface ModelDefinition { /** * model name, for example `gpt-4o` */ model_name: string; /** * model type, for example `chat_completion` */ model_type: ModelsAPI.InferenceModelType; /** * model vendor, for example `openai` */ model_vendor: InferenceModelVendor; /** * model availability indicating availability status, for example `available` */ model_availability?: ModelsAPI.InferenceModelAvailability; } export type SortOrder = 'asc' | 'desc'; export type CompletionCreateResponse = ChatCompletion | ChatCompletionChunk; export interface CompletionModelsResponse { items: Array; object?: 'list'; } export type CompletionCreateParams = CompletionCreateParamsNonStreaming | CompletionCreateParamsStreaming; export interface CompletionCreateParamsBase { /** * Body param: openai standard message format */ messages: Array<{ [key: string]: unknown }>; /** * Body param: model specified as `model_vendor/model`, for example `openai/gpt-4o` */ model: string; /** * Body param: Parameters for audio output. Required when audio output is requested * with modalities: ['audio']. */ audio?: { [key: string]: unknown }; /** * Body param: Number between -2.0 and 2.0. Positive values penalize new tokens * based on their existing frequency in the text so far. */ frequency_penalty?: number; /** * Body param: Deprecated in favor of tool_choice. Controls which function is * called by the model. */ function_call?: { [key: string]: unknown }; /** * Body param: Deprecated in favor of tools. A list of functions the model may * generate JSON inputs for. */ functions?: Array<{ [key: string]: unknown }>; /** * Body param: Modify the likelihood of specified tokens appearing in the * completion. Maps tokens to bias values from -100 to 100. */ logit_bias?: { [key: string]: number }; /** * Body param: Whether to return log probabilities of the output tokens or not. */ logprobs?: boolean; /** * Body param: An upper bound for the number of tokens that can be generated, * including visible output tokens and reasoning tokens. */ max_completion_tokens?: number; /** * Body param: Deprecated in favor of max_completion_tokens. The maximum number of * tokens to generate. */ max_tokens?: number; /** * Body param: Developer-defined tags and values used for filtering completions in * the dashboard. */ metadata?: { [key: string]: string }; /** * Body param: Output types that you would like the model to generate for this * request. */ modalities?: Array; /** * Body param: How many chat completion choices to generate for each input message. */ n?: number; /** * Body param: Whether to enable parallel function calling during tool use. */ parallel_tool_calls?: boolean; /** * Body param: Static predicted output content, such as the content of a text file * being regenerated. */ prediction?: { [key: string]: unknown }; /** * Body param: Number between -2.0 and 2.0. Positive values penalize tokens based * on whether they appear in the text so far. */ presence_penalty?: number; /** * Body param: For o1 models only. Constrains effort on reasoning. Values: low, * medium, high. */ reasoning_effort?: string; /** * Body param: An object specifying the format that the model must output. */ response_format?: { [key: string]: unknown }; /** * Body param: If specified, system will attempt to sample deterministically for * repeated requests with same seed. */ seed?: number; /** * Body param: Up to 4 sequences where the API will stop generating further tokens. */ stop?: string | Array; /** * Body param: Whether to store the output for use in model distillation or evals * products. */ store?: boolean; /** * Body param: If true, partial message deltas will be sent as server-sent events. */ stream?: boolean; /** * Body param: Options for streaming response. Only set this when stream is true. */ stream_options?: { [key: string]: unknown }; /** * Body param: What sampling temperature to use. Higher values make output more * random, lower more focused. */ temperature?: number; /** * Body param: Controls which tool is called by the model. Values: none, auto, * required, or specific tool. */ tool_choice?: string | { [key: string]: unknown }; /** * Body param: A list of tools the model may call. Currently, only functions are * supported. Max 128 functions. */ tools?: Array<{ [key: string]: unknown }>; /** * Body param: Only sample from the top K options for each subsequent token */ top_k?: number; /** * Body param: Number of most likely tokens to return at each position, with * associated log probability. */ top_logprobs?: number; /** * Body param: Alternative to temperature. Only tokens comprising top_p probability * mass are considered. */ top_p?: number; /** * Header param */ 'x-openai-api-key'?: string; [k: string]: unknown; } export namespace CompletionCreateParams { export type CompletionCreateParamsNonStreaming = ChatCompletionsAPI.CompletionCreateParamsNonStreaming; export type CompletionCreateParamsStreaming = ChatCompletionsAPI.CompletionCreateParamsStreaming; } export interface CompletionCreateParamsNonStreaming extends CompletionCreateParamsBase { /** * Body param: If true, partial message deltas will be sent as server-sent events. */ stream?: false; [k: string]: unknown; } export interface CompletionCreateParamsStreaming extends CompletionCreateParamsBase { /** * Body param: If true, partial message deltas will be sent as server-sent events. */ stream: true; [k: string]: unknown; } export interface CompletionModelsParams { ending_before?: string; limit?: number; model_vendor?: InferenceModelVendor; sort_by?: string; sort_order?: SortOrder; starting_after?: string; } export declare namespace Completions { export { type ChatCompletion as ChatCompletion, type ChatCompletionChunk as ChatCompletionChunk, type ChatCompletionTokenLogprob as ChatCompletionTokenLogprob, type ChoiceLogprobs as ChoiceLogprobs, type InferenceModelVendor as InferenceModelVendor, type ModelDefinition as ModelDefinition, type SortOrder as SortOrder, type CompletionCreateResponse as CompletionCreateResponse, type CompletionModelsResponse as CompletionModelsResponse, type CompletionCreateParams as CompletionCreateParams, type CompletionCreateParamsNonStreaming as CompletionCreateParamsNonStreaming, type CompletionCreateParamsStreaming as CompletionCreateParamsStreaming, type CompletionModelsParams as CompletionModelsParams, }; }