portkey_sdk/model/prompts.rs
1use std::collections::HashMap;
2
3use serde::{Deserialize, Serialize};
4
5/// Request to execute a prompt template with completions.
6///
7/// This allows you to execute saved prompt templates on Portkey, substituting
8/// variables and optionally overriding hyperparameters.
9///
10/// # Example
11///
12/// ```rust
13/// use portkey_sdk::model::PromptCompletionRequest;
14/// use std::collections::HashMap;
15///
16/// let mut variables = HashMap::new();
17/// variables.insert("user_input".to_string(), serde_json::json!("Hello world"));
18///
19/// let request = PromptCompletionRequest {
20/// variables,
21/// stream: Some(false),
22/// max_tokens: Some(250),
23/// temperature: Some(0.7),
24/// presence_penalty: Some(0.2),
25/// frequency_penalty: None,
26/// top_p: None,
27/// stop: None,
28/// n: None,
29/// logprobs: None,
30/// echo: None,
31/// best_of: None,
32/// logit_bias: None,
33/// user: None,
34/// };
35/// ```
36#[derive(Debug, Clone, Serialize, Deserialize)]
37pub struct PromptCompletionRequest {
38 /// Variables to substitute in the prompt template
39 pub variables: HashMap<String, serde_json::Value>,
40
41 /// Whether to stream the response (default: false)
42 #[serde(skip_serializing_if = "Option::is_none")]
43 pub stream: Option<bool>,
44
45 // Hyperparameters - these are passed at root level, not nested
46 /// Maximum number of tokens to generate
47 #[serde(skip_serializing_if = "Option::is_none")]
48 pub max_tokens: Option<i32>,
49
50 /// Sampling temperature (0-2)
51 #[serde(skip_serializing_if = "Option::is_none")]
52 pub temperature: Option<f32>,
53
54 /// Presence penalty (-2 to 2)
55 #[serde(skip_serializing_if = "Option::is_none")]
56 pub presence_penalty: Option<f32>,
57
58 /// Frequency penalty (-2 to 2)
59 #[serde(skip_serializing_if = "Option::is_none")]
60 pub frequency_penalty: Option<f32>,
61
62 /// Nucleus sampling parameter (0-1)
63 #[serde(skip_serializing_if = "Option::is_none")]
64 pub top_p: Option<f32>,
65
66 /// Stop sequences
67 #[serde(skip_serializing_if = "Option::is_none")]
68 pub stop: Option<Vec<String>>,
69
70 /// Number of completions to generate
71 #[serde(skip_serializing_if = "Option::is_none")]
72 pub n: Option<i32>,
73
74 /// Include log probabilities
75 #[serde(skip_serializing_if = "Option::is_none")]
76 pub logprobs: Option<bool>,
77
78 /// Echo back the prompt
79 #[serde(skip_serializing_if = "Option::is_none")]
80 pub echo: Option<bool>,
81
82 /// Generate best_of completions server-side
83 #[serde(skip_serializing_if = "Option::is_none")]
84 pub best_of: Option<i32>,
85
86 /// Logit bias map
87 #[serde(skip_serializing_if = "Option::is_none")]
88 pub logit_bias: Option<HashMap<String, i32>>,
89
90 /// User identifier
91 #[serde(skip_serializing_if = "Option::is_none")]
92 pub user: Option<String>,
93}
94
95/// Response from executing a prompt completion.
96///
97/// The response contains the status, headers, and body. The body can be
98/// either a chat completion or text completion response depending on the
99/// prompt template configuration.
100#[derive(Debug, Clone, Serialize, Deserialize)]
101pub struct PromptCompletionResponse {
102 /// Response status
103 #[serde(skip_serializing_if = "Option::is_none")]
104 pub status: Option<String>,
105
106 /// Response headers
107 #[serde(skip_serializing_if = "Option::is_none")]
108 pub headers: Option<HashMap<String, serde_json::Value>>,
109
110 /// Response body - can be chat completion or text completion
111 pub body: serde_json::Value,
112}
113
114/// Request to render a prompt template with variable substitution.
115///
116/// This endpoint renders a prompt template by substituting variables and
117/// applying hyperparameters, returning the fully rendered prompt configuration
118/// without executing it.
119#[derive(Debug, Clone, Serialize, Deserialize)]
120pub struct PromptRenderRequest {
121 /// Variables to substitute in the prompt template
122 pub variables: HashMap<String, serde_json::Value>,
123
124 // Hyperparameters - these are passed at root level, not nested
125 /// Maximum number of tokens to generate
126 #[serde(skip_serializing_if = "Option::is_none")]
127 pub max_tokens: Option<i32>,
128
129 /// Sampling temperature (0-2)
130 #[serde(skip_serializing_if = "Option::is_none")]
131 pub temperature: Option<f32>,
132
133 /// Presence penalty (-2 to 2)
134 #[serde(skip_serializing_if = "Option::is_none")]
135 pub presence_penalty: Option<f32>,
136
137 /// Frequency penalty (-2 to 2)
138 #[serde(skip_serializing_if = "Option::is_none")]
139 pub frequency_penalty: Option<f32>,
140
141 /// Nucleus sampling parameter (0-1)
142 #[serde(skip_serializing_if = "Option::is_none")]
143 pub top_p: Option<f32>,
144
145 /// Stop sequences
146 #[serde(skip_serializing_if = "Option::is_none")]
147 pub stop: Option<Vec<String>>,
148
149 /// Number of completions to generate
150 #[serde(skip_serializing_if = "Option::is_none")]
151 pub n: Option<i32>,
152
153 /// Include log probabilities
154 #[serde(skip_serializing_if = "Option::is_none")]
155 pub logprobs: Option<bool>,
156
157 /// Echo back the prompt
158 #[serde(skip_serializing_if = "Option::is_none")]
159 pub echo: Option<bool>,
160
161 /// Generate best_of completions server-side
162 #[serde(skip_serializing_if = "Option::is_none")]
163 pub best_of: Option<i32>,
164
165 /// Logit bias map
166 #[serde(skip_serializing_if = "Option::is_none")]
167 pub logit_bias: Option<HashMap<String, i32>>,
168
169 /// User identifier
170 #[serde(skip_serializing_if = "Option::is_none")]
171 pub user: Option<String>,
172}
173
174/// Response from rendering a prompt template.
175///
176/// Contains the rendered prompt configuration with variables substituted
177/// and hyperparameters applied.
178#[derive(Debug, Clone, Serialize, Deserialize)]
179pub struct PromptRenderResponse {
180 /// Whether the render was successful
181 pub success: bool,
182
183 /// The rendered prompt data - can be chat completion or text completion request
184 pub data: serde_json::Value,
185}