async_openai/types/completions/completion.rs
1use std::collections::HashMap;
2
3use derive_builder::Builder;
4use serde::{Deserialize, Serialize};
5
6use crate::error::OpenAIError;
7
8use crate::types::chat::{
9 ChatCompletionStreamOptions, Choice, CompletionUsage, Prompt, StopConfiguration,
10};
11
12#[derive(Clone, Serialize, Deserialize, Default, Debug, Builder, PartialEq)]
13#[builder(name = "CreateCompletionRequestArgs")]
14#[builder(pattern = "mutable")]
15#[builder(setter(into, strip_option), default)]
16#[builder(derive(Debug))]
17#[builder(build_fn(error = "OpenAIError"))]
18#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
19#[cfg_attr(feature = "openapi", schema(as = async_openai::CreateCompletionRequest))]
20pub struct CreateCompletionRequest {
21 /// ID of the model to use. You can use the [List models](https://platform.openai.com/docs/api-reference/models/list) API to see all of your available models, or see our [Model overview](https://platform.openai.com/docs/models/overview) for descriptions of them.
22 pub model: String,
23
24 /// The prompt(s) to generate completions for, encoded as a string, array of strings, array of tokens, or array of token arrays.
25 ///
26 /// Note that <|endoftext|> is the document separator that the model sees during training, so if a prompt is not specified the model will generate as if from the beginning of a new document.
27 pub prompt: Prompt,
28
29 /// The suffix that comes after a completion of inserted text.
30 ///
31 /// This parameter is only supported for `gpt-3.5-turbo-instruct`.
32 #[serde(skip_serializing_if = "Option::is_none")]
33 pub suffix: Option<String>, // default: null
34
35 /// The maximum number of [tokens](https://platform.openai.com/tokenizer) that can be generated in the completion.
36 ///
37 /// The token count of your prompt plus `max_tokens` cannot exceed the model's context length. [Example Python code](https://cookbook.openai.com/examples/how_to_count_tokens_with_tiktoken) for counting tokens.
38 #[serde(skip_serializing_if = "Option::is_none")]
39 pub max_tokens: Option<u32>,
40
41 /// What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
42 ///
43 /// We generally recommend altering this or `top_p` but not both.
44 #[serde(skip_serializing_if = "Option::is_none")]
45 #[cfg_attr(feature = "openapi", schema(minimum = 0, maximum = 2))]
46 pub temperature: Option<f32>, // min: 0, max: 2, default: 1,
47
48 /// An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.
49 ///
50 /// We generally recommend altering this or `temperature` but not both.
51 #[serde(skip_serializing_if = "Option::is_none")]
52 #[cfg_attr(feature = "openapi", schema(minimum = 0, maximum = 1))]
53 pub top_p: Option<f32>, // min: 0, max: 1, default: 1
54
55 /// How many completions to generate for each prompt.
56
57 /// **Note:** Because this parameter generates many completions, it can quickly consume your token quota. Use carefully and ensure that you have reasonable settings for `max_tokens` and `stop`.
58 ///
59 #[serde(skip_serializing_if = "Option::is_none")]
60 #[cfg_attr(feature = "openapi", schema(minimum = 1, maximum = 128))]
61 pub n: Option<u8>, // min:1 max: 128, default: 1
62
63 /// Whether to stream back partial progress. If set, tokens will be sent as data-only [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format)
64 /// as they become available, with the stream terminated by a `data: [DONE]` message.
65 #[serde(skip_serializing_if = "Option::is_none")]
66 pub stream: Option<bool>, // nullable: true
67
68 #[serde(skip_serializing_if = "Option::is_none")]
69 pub stream_options: Option<ChatCompletionStreamOptions>,
70
71 /// Include the log probabilities on the `logprobs` most likely output tokens, as well the chosen tokens. For example, if `logprobs` is 5, the API will return a list of the 5 most likely tokens. The API will always return the `logprob` of the sampled token, so there may be up to `logprobs+1` elements in the response.
72 ///
73 /// The maximum value for `logprobs` is 5.
74 #[serde(skip_serializing_if = "Option::is_none")]
75 #[cfg_attr(feature = "openapi", schema(minimum = 0, maximum = 5))]
76 pub logprobs: Option<u8>, // min:0 , max: 5, default: null, nullable: true
77
78 /// Echo back the prompt in addition to the completion
79 #[serde(skip_serializing_if = "Option::is_none")]
80 pub echo: Option<bool>,
81
82 /// Up to 4 sequences where the API will stop generating further tokens. The returned text will not contain the stop sequence.
83 #[serde(skip_serializing_if = "Option::is_none")]
84 pub stop: Option<StopConfiguration>,
85
86 /// Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics.
87 ///
88 /// [See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/text-generation/parameter-details)
89 #[serde(skip_serializing_if = "Option::is_none")]
90 #[cfg_attr(feature = "openapi", schema(minimum = -2, maximum = 2))]
91 pub presence_penalty: Option<f32>, // min: -2.0, max: 2.0, default 0
92
93 /// Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.
94 ///
95 /// [See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/text-generation/parameter-details)
96 #[serde(skip_serializing_if = "Option::is_none")]
97 #[cfg_attr(feature = "openapi", schema(minimum = -2, maximum = 2))]
98 pub frequency_penalty: Option<f32>, // min: -2.0, max: 2.0, default: 0
99
100 /// Generates `best_of` completions server-side and returns the "best" (the one with the highest log probability per token). Results cannot be streamed.
101 ///
102 /// When used with `n`, `best_of` controls the number of candidate completions and `n` specifies how many to return – `best_of` must be greater than `n`.
103 ///
104 /// **Note:** Because this parameter generates many completions, it can quickly consume your token quota. Use carefully and ensure that you have reasonable settings for `max_tokens` and `stop`.
105 #[serde(skip_serializing_if = "Option::is_none")]
106 #[cfg_attr(feature = "openapi", schema(minimum = 0, maximum = 20))]
107 pub best_of: Option<u8>, //min: 0, max: 20, default: 1
108
109 /// Modify the likelihood of specified tokens appearing in the completion.
110 ///
111 /// Accepts a json object that maps tokens (specified by their token ID in the GPT tokenizer) to an associated bias value from -100 to 100. You can use this [tokenizer tool](/tokenizer?view=bpe) (which works for both GPT-2 and GPT-3) to convert text to token IDs. Mathematically, the bias is added to the logits generated by the model prior to sampling. The exact effect will vary per model, but values between -1 and 1 should decrease or increase likelihood of selection; values like -100 or 100 should result in a ban or exclusive selection of the relevant token.
112 ///
113 /// As an example, you can pass `{"50256": -100}` to prevent the <|endoftext|> token from being generated.
114 #[serde(skip_serializing_if = "Option::is_none")]
115 #[cfg_attr(feature = "openapi", schema(schema_with = crate::types::shared::schema::logit_bias))]
116 pub logit_bias: Option<HashMap<String, serde_json::Value>>, // default: null
117
118 /// A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/usage-policies/end-user-ids).
119 #[serde(skip_serializing_if = "Option::is_none")]
120 pub user: Option<String>,
121
122 /// If specified, our system will make a best effort to sample deterministically, such that repeated requests with the same `seed` and parameters should return the same result.
123 ///
124 /// Determinism is not guaranteed, and you should refer to the `system_fingerprint` response parameter to monitor changes in the backend.
125 #[serde(skip_serializing_if = "Option::is_none")]
126 pub seed: Option<i64>,
127}
128
129#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)]
130#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
131#[cfg_attr(feature = "openapi", schema(as = async_openai::CreateCompletionResponse))]
132pub struct CreateCompletionResponse {
133 /// A unique identifier for the completion.
134 pub id: String,
135 pub choices: Vec<Choice>,
136 /// The Unix timestamp (in seconds) of when the completion was created.
137 pub created: u32,
138
139 /// The model used for completion.
140 pub model: String,
141 /// This fingerprint represents the backend configuration that the model runs with.
142 ///
143 /// Can be used in conjunction with the `seed` request parameter to understand when backend changes have been
144 /// made that might impact determinism.
145 pub system_fingerprint: Option<String>,
146
147 /// The object type, which is always "text_completion"
148 pub object: String,
149 pub usage: Option<CompletionUsage>,
150}
151
152/// Parsed server side events stream until an \[DONE\] is received from server.
153#[cfg(feature = "_api")]
154pub type CompletionResponseStream = crate::types::stream::StreamResponse<CreateCompletionResponse>;