portkey_sdk/model/
completions.rs

1//! Completions API models.
2//!
3//! This module contains models for the legacy completions endpoint.
4
5use std::collections::HashMap;
6
7use serde::{Deserialize, Serialize};
8
9/// Request body for creating a completion.
10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct CreateCompletionRequest {
12    /// ID of the model to use.
13    pub model: String,
14
15    /// The prompt(s) to generate completions for.
16    #[serde(skip_serializing_if = "Option::is_none")]
17    pub prompt: Option<CompletionPrompt>,
18
19    /// Maximum number of tokens to generate.
20    #[serde(skip_serializing_if = "Option::is_none")]
21    pub max_tokens: Option<u32>,
22
23    /// Sampling temperature (0-2).
24    #[serde(skip_serializing_if = "Option::is_none")]
25    pub temperature: Option<f32>,
26
27    /// Nucleus sampling parameter (0-1).
28    #[serde(skip_serializing_if = "Option::is_none")]
29    pub top_p: Option<f32>,
30
31    /// Number of completions to generate (1-128).
32    #[serde(skip_serializing_if = "Option::is_none")]
33    pub n: Option<u32>,
34
35    /// Whether to stream back partial progress.
36    #[serde(skip_serializing_if = "Option::is_none")]
37    pub stream: Option<bool>,
38
39    /// Include log probabilities.
40    #[serde(skip_serializing_if = "Option::is_none")]
41    pub logprobs: Option<u32>,
42
43    /// Echo back the prompt in the response.
44    #[serde(skip_serializing_if = "Option::is_none")]
45    pub echo: Option<bool>,
46
47    /// Stop sequences.
48    #[serde(skip_serializing_if = "Option::is_none")]
49    pub stop: Option<CompletionStop>,
50
51    /// Frequency penalty (-2 to 2).
52    #[serde(skip_serializing_if = "Option::is_none")]
53    pub frequency_penalty: Option<f32>,
54
55    /// Presence penalty (-2 to 2).
56    #[serde(skip_serializing_if = "Option::is_none")]
57    pub presence_penalty: Option<f32>,
58
59    /// Generate best_of completions server-side (0-20).
60    #[serde(skip_serializing_if = "Option::is_none")]
61    pub best_of: Option<u32>,
62
63    /// Modify the likelihood of specified tokens.
64    #[serde(skip_serializing_if = "Option::is_none")]
65    pub logit_bias: Option<HashMap<String, i32>>,
66
67    /// A unique identifier representing your end-user.
68    #[serde(skip_serializing_if = "Option::is_none")]
69    pub user: Option<String>,
70
71    /// Suffix to append after completion.
72    #[serde(skip_serializing_if = "Option::is_none")]
73    pub suffix: Option<String>,
74
75    /// Seed for deterministic sampling.
76    #[serde(skip_serializing_if = "Option::is_none")]
77    pub seed: Option<i64>,
78}
79
80/// Prompt can be a string or array of strings.
81#[derive(Debug, Clone, Serialize, Deserialize)]
82#[serde(untagged)]
83pub enum CompletionPrompt {
84    /// Single string prompt.
85    String(String),
86    /// Array of string prompts.
87    Array(Vec<String>),
88}
89
90/// Stop sequences can be a string or array of strings.
91#[derive(Debug, Clone, Serialize, Deserialize)]
92#[serde(untagged)]
93pub enum CompletionStop {
94    /// Single stop sequence.
95    String(String),
96    /// Multiple stop sequences (up to 4).
97    Array(Vec<String>),
98}
99
100/// Response from the completions endpoint.
101#[derive(Debug, Clone, Serialize, Deserialize)]
102pub struct CompletionResponse {
103    /// Unique identifier for the completion.
104    pub id: String,
105
106    /// Object type (always "text_completion").
107    pub object: String,
108
109    /// Unix timestamp of when the completion was created.
110    pub created: i64,
111
112    /// Model used for completion.
113    pub model: String,
114
115    /// List of completion choices.
116    pub choices: Vec<CompletionChoice>,
117
118    /// Usage statistics for the request.
119    #[serde(skip_serializing_if = "Option::is_none")]
120    pub usage: Option<CompletionUsage>,
121
122    /// System fingerprint for the backend configuration.
123    #[serde(skip_serializing_if = "Option::is_none")]
124    pub system_fingerprint: Option<String>,
125}
126
127/// A single completion choice.
128#[derive(Debug, Clone, Serialize, Deserialize)]
129pub struct CompletionChoice {
130    /// Generated completion text.
131    pub text: String,
132
133    /// Index of this choice in the list.
134    pub index: u32,
135
136    /// Reason for completion finishing.
137    #[serde(skip_serializing_if = "Option::is_none")]
138    pub finish_reason: Option<String>,
139
140    /// Log probabilities for tokens.
141    #[serde(skip_serializing_if = "Option::is_none")]
142    pub logprobs: Option<CompletionLogprobs>,
143}
144
145/// Log probability information for tokens.
146#[derive(Debug, Clone, Serialize, Deserialize)]
147pub struct CompletionLogprobs {
148    /// List of tokens.
149    #[serde(skip_serializing_if = "Option::is_none")]
150    pub tokens: Option<Vec<String>>,
151
152    /// Log probabilities for each token.
153    #[serde(skip_serializing_if = "Option::is_none")]
154    pub token_logprobs: Option<Vec<f32>>,
155
156    /// Text offset for each token.
157    #[serde(skip_serializing_if = "Option::is_none")]
158    pub text_offset: Option<Vec<u32>>,
159
160    /// Top log probabilities for each position.
161    #[serde(skip_serializing_if = "Option::is_none")]
162    pub top_logprobs: Option<Vec<HashMap<String, f32>>>,
163}
164
165/// Token usage statistics.
166#[derive(Debug, Clone, Serialize, Deserialize)]
167pub struct CompletionUsage {
168    /// Number of tokens in the prompt.
169    pub prompt_tokens: u32,
170
171    /// Number of tokens in the completion.
172    pub completion_tokens: u32,
173
174    /// Total tokens used (prompt + completion).
175    pub total_tokens: u32,
176}