completions.d.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. import { APIResource } from "../resource.js";
  2. import { APIPromise } from "../core.js";
  3. import * as Core from "../core.js";
  4. import * as CompletionsAPI from "./completions.js";
  5. import * as CompletionsCompletionsAPI from "./chat/completions/completions.js";
  6. import { Stream } from "../streaming.js";
  7. export declare class Completions extends APIResource {
  8. /**
  9. * Creates a completion for the provided prompt and parameters.
  10. *
  11. * @example
  12. * ```ts
  13. * const completion = await client.completions.create({
  14. * model: 'string',
  15. * prompt: 'This is a test.',
  16. * });
  17. * ```
  18. */
  19. create(body: CompletionCreateParamsNonStreaming, options?: Core.RequestOptions): APIPromise<Completion>;
  20. create(body: CompletionCreateParamsStreaming, options?: Core.RequestOptions): APIPromise<Stream<Completion>>;
  21. create(body: CompletionCreateParamsBase, options?: Core.RequestOptions): APIPromise<Stream<Completion> | Completion>;
  22. }
  23. /**
  24. * Represents a completion response from the API. Note: both the streamed and
  25. * non-streamed response objects share the same shape (unlike the chat endpoint).
  26. */
  27. export interface Completion {
  28. /**
  29. * A unique identifier for the completion.
  30. */
  31. id: string;
  32. /**
  33. * The list of completion choices the model generated for the input prompt.
  34. */
  35. choices: Array<CompletionChoice>;
  36. /**
  37. * The Unix timestamp (in seconds) of when the completion was created.
  38. */
  39. created: number;
  40. /**
  41. * The model used for completion.
  42. */
  43. model: string;
  44. /**
  45. * The object type, which is always "text_completion"
  46. */
  47. object: 'text_completion';
  48. /**
  49. * This fingerprint represents the backend configuration that the model runs with.
  50. *
  51. * Can be used in conjunction with the `seed` request parameter to understand when
  52. * backend changes have been made that might impact determinism.
  53. */
  54. system_fingerprint?: string;
  55. /**
  56. * Usage statistics for the completion request.
  57. */
  58. usage?: CompletionUsage;
  59. }
  60. export interface CompletionChoice {
  61. /**
  62. * The reason the model stopped generating tokens. This will be `stop` if the model
  63. * hit a natural stop point or a provided stop sequence, `length` if the maximum
  64. * number of tokens specified in the request was reached, or `content_filter` if
  65. * content was omitted due to a flag from our content filters.
  66. */
  67. finish_reason: 'stop' | 'length' | 'content_filter';
  68. index: number;
  69. logprobs: CompletionChoice.Logprobs | null;
  70. text: string;
  71. }
  72. export declare namespace CompletionChoice {
  73. interface Logprobs {
  74. text_offset?: Array<number>;
  75. token_logprobs?: Array<number>;
  76. tokens?: Array<string>;
  77. top_logprobs?: Array<Record<string, number>>;
  78. }
  79. }
  80. /**
  81. * Usage statistics for the completion request.
  82. */
  83. export interface CompletionUsage {
  84. /**
  85. * Number of tokens in the generated completion.
  86. */
  87. completion_tokens: number;
  88. /**
  89. * Number of tokens in the prompt.
  90. */
  91. prompt_tokens: number;
  92. /**
  93. * Total number of tokens used in the request (prompt + completion).
  94. */
  95. total_tokens: number;
  96. /**
  97. * Breakdown of tokens used in a completion.
  98. */
  99. completion_tokens_details?: CompletionUsage.CompletionTokensDetails;
  100. /**
  101. * Breakdown of tokens used in the prompt.
  102. */
  103. prompt_tokens_details?: CompletionUsage.PromptTokensDetails;
  104. }
  105. export declare namespace CompletionUsage {
  106. /**
  107. * Breakdown of tokens used in a completion.
  108. */
  109. interface CompletionTokensDetails {
  110. /**
  111. * When using Predicted Outputs, the number of tokens in the prediction that
  112. * appeared in the completion.
  113. */
  114. accepted_prediction_tokens?: number;
  115. /**
  116. * Audio input tokens generated by the model.
  117. */
  118. audio_tokens?: number;
  119. /**
  120. * Tokens generated by the model for reasoning.
  121. */
  122. reasoning_tokens?: number;
  123. /**
  124. * When using Predicted Outputs, the number of tokens in the prediction that did
  125. * not appear in the completion. However, like reasoning tokens, these tokens are
  126. * still counted in the total completion tokens for purposes of billing, output,
  127. * and context window limits.
  128. */
  129. rejected_prediction_tokens?: number;
  130. }
  131. /**
  132. * Breakdown of tokens used in the prompt.
  133. */
  134. interface PromptTokensDetails {
  135. /**
  136. * Audio input tokens present in the prompt.
  137. */
  138. audio_tokens?: number;
  139. /**
  140. * Cached tokens present in the prompt.
  141. */
  142. cached_tokens?: number;
  143. }
  144. }
  145. export type CompletionCreateParams = CompletionCreateParamsNonStreaming | CompletionCreateParamsStreaming;
  146. export interface CompletionCreateParamsBase {
  147. /**
  148. * ID of the model to use. You can use the
  149. * [List models](https://platform.openai.com/docs/api-reference/models/list) API to
  150. * see all of your available models, or see our
  151. * [Model overview](https://platform.openai.com/docs/models) for descriptions of
  152. * them.
  153. */
  154. model: (string & {}) | 'gpt-3.5-turbo-instruct' | 'davinci-002' | 'babbage-002';
  155. /**
  156. * The prompt(s) to generate completions for, encoded as a string, array of
  157. * strings, array of tokens, or array of token arrays.
  158. *
  159. * Note that <|endoftext|> is the document separator that the model sees during
  160. * training, so if a prompt is not specified the model will generate as if from the
  161. * beginning of a new document.
  162. */
  163. prompt: string | Array<string> | Array<number> | Array<Array<number>> | null;
  164. /**
  165. * Generates `best_of` completions server-side and returns the "best" (the one with
  166. * the highest log probability per token). Results cannot be streamed.
  167. *
  168. * When used with `n`, `best_of` controls the number of candidate completions and
  169. * `n` specifies how many to return – `best_of` must be greater than `n`.
  170. *
  171. * **Note:** Because this parameter generates many completions, it can quickly
  172. * consume your token quota. Use carefully and ensure that you have reasonable
  173. * settings for `max_tokens` and `stop`.
  174. */
  175. best_of?: number | null;
  176. /**
  177. * Echo back the prompt in addition to the completion
  178. */
  179. echo?: boolean | null;
  180. /**
  181. * Number between -2.0 and 2.0. Positive values penalize new tokens based on their
  182. * existing frequency in the text so far, decreasing the model's likelihood to
  183. * repeat the same line verbatim.
  184. *
  185. * [See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/text-generation)
  186. */
  187. frequency_penalty?: number | null;
  188. /**
  189. * Modify the likelihood of specified tokens appearing in the completion.
  190. *
  191. * Accepts a JSON object that maps tokens (specified by their token ID in the GPT
  192. * tokenizer) to an associated bias value from -100 to 100. You can use this
  193. * [tokenizer tool](/tokenizer?view=bpe) to convert text to token IDs.
  194. * Mathematically, the bias is added to the logits generated by the model prior to
  195. * sampling. The exact effect will vary per model, but values between -1 and 1
  196. * should decrease or increase likelihood of selection; values like -100 or 100
  197. * should result in a ban or exclusive selection of the relevant token.
  198. *
  199. * As an example, you can pass `{"50256": -100}` to prevent the <|endoftext|> token
  200. * from being generated.
  201. */
  202. logit_bias?: Record<string, number> | null;
  203. /**
  204. * Include the log probabilities on the `logprobs` most likely output tokens, as
  205. * well the chosen tokens. For example, if `logprobs` is 5, the API will return a
  206. * list of the 5 most likely tokens. The API will always return the `logprob` of
  207. * the sampled token, so there may be up to `logprobs+1` elements in the response.
  208. *
  209. * The maximum value for `logprobs` is 5.
  210. */
  211. logprobs?: number | null;
  212. /**
  213. * The maximum number of [tokens](/tokenizer) that can be generated in the
  214. * completion.
  215. *
  216. * The token count of your prompt plus `max_tokens` cannot exceed the model's
  217. * context length.
  218. * [Example Python code](https://cookbook.openai.com/examples/how_to_count_tokens_with_tiktoken)
  219. * for counting tokens.
  220. */
  221. max_tokens?: number | null;
  222. /**
  223. * How many completions to generate for each prompt.
  224. *
  225. * **Note:** Because this parameter generates many completions, it can quickly
  226. * consume your token quota. Use carefully and ensure that you have reasonable
  227. * settings for `max_tokens` and `stop`.
  228. */
  229. n?: number | null;
  230. /**
  231. * Number between -2.0 and 2.0. Positive values penalize new tokens based on
  232. * whether they appear in the text so far, increasing the model's likelihood to
  233. * talk about new topics.
  234. *
  235. * [See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/text-generation)
  236. */
  237. presence_penalty?: number | null;
  238. /**
  239. * If specified, our system will make a best effort to sample deterministically,
  240. * such that repeated requests with the same `seed` and parameters should return
  241. * the same result.
  242. *
  243. * Determinism is not guaranteed, and you should refer to the `system_fingerprint`
  244. * response parameter to monitor changes in the backend.
  245. */
  246. seed?: number | null;
  247. /**
  248. * Not supported with latest reasoning models `o3` and `o4-mini`.
  249. *
  250. * Up to 4 sequences where the API will stop generating further tokens. The
  251. * returned text will not contain the stop sequence.
  252. */
  253. stop?: string | null | Array<string>;
  254. /**
  255. * Whether to stream back partial progress. If set, tokens will be sent as
  256. * data-only
  257. * [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format)
  258. * as they become available, with the stream terminated by a `data: [DONE]`
  259. * message.
  260. * [Example Python code](https://cookbook.openai.com/examples/how_to_stream_completions).
  261. */
  262. stream?: boolean | null;
  263. /**
  264. * Options for streaming response. Only set this when you set `stream: true`.
  265. */
  266. stream_options?: CompletionsCompletionsAPI.ChatCompletionStreamOptions | null;
  267. /**
  268. * The suffix that comes after a completion of inserted text.
  269. *
  270. * This parameter is only supported for `gpt-3.5-turbo-instruct`.
  271. */
  272. suffix?: string | null;
  273. /**
  274. * What sampling temperature to use, between 0 and 2. Higher values like 0.8 will
  275. * make the output more random, while lower values like 0.2 will make it more
  276. * focused and deterministic.
  277. *
  278. * We generally recommend altering this or `top_p` but not both.
  279. */
  280. temperature?: number | null;
  281. /**
  282. * An alternative to sampling with temperature, called nucleus sampling, where the
  283. * model considers the results of the tokens with top_p probability mass. So 0.1
  284. * means only the tokens comprising the top 10% probability mass are considered.
  285. *
  286. * We generally recommend altering this or `temperature` but not both.
  287. */
  288. top_p?: number | null;
  289. /**
  290. * A unique identifier representing your end-user, which can help OpenAI to monitor
  291. * and detect abuse.
  292. * [Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
  293. */
  294. user?: string;
  295. }
  296. export declare namespace CompletionCreateParams {
  297. type CompletionCreateParamsNonStreaming = CompletionsAPI.CompletionCreateParamsNonStreaming;
  298. type CompletionCreateParamsStreaming = CompletionsAPI.CompletionCreateParamsStreaming;
  299. }
  300. export interface CompletionCreateParamsNonStreaming extends CompletionCreateParamsBase {
  301. /**
  302. * Whether to stream back partial progress. If set, tokens will be sent as
  303. * data-only
  304. * [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format)
  305. * as they become available, with the stream terminated by a `data: [DONE]`
  306. * message.
  307. * [Example Python code](https://cookbook.openai.com/examples/how_to_stream_completions).
  308. */
  309. stream?: false | null;
  310. }
  311. export interface CompletionCreateParamsStreaming extends CompletionCreateParamsBase {
  312. /**
  313. * Whether to stream back partial progress. If set, tokens will be sent as
  314. * data-only
  315. * [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format)
  316. * as they become available, with the stream terminated by a `data: [DONE]`
  317. * message.
  318. * [Example Python code](https://cookbook.openai.com/examples/how_to_stream_completions).
  319. */
  320. stream: true;
  321. }
  322. export declare namespace Completions {
  323. export { type Completion as Completion, type CompletionChoice as CompletionChoice, type CompletionUsage as CompletionUsage, type CompletionCreateParams as CompletionCreateParams, type CompletionCreateParamsNonStreaming as CompletionCreateParamsNonStreaming, type CompletionCreateParamsStreaming as CompletionCreateParamsStreaming, };
  324. }
  325. //# sourceMappingURL=completions.d.ts.map