Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-sovereign-core-mv/blob/main/crates/aien-serve-http/src/openai.rs # aien-sovereign-core-mv AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-sovereign-core-mv / crates / aien-serve-http / src / openai.rs` 240 lines ยท 7316 bytes Raw Blame History 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 //! OpenAI chat-completions wire types: the subset Odysseus and the OpenAI SDKs 17 //! send and read. Unknown request fields are ignored (llama.cpp extras such as 18 //! `session_id` / `cache_prompt` arrive from Odysseus). 19 20 use serde::{Deserialize, Serialize}; 21 use serde_json::Value; 22 23 use crate::sampling::SamplingRecord; 24 25 #[derive(Debug, Deserialize)] 26 pub struct ChatRequest { 27 #[serde(default)] 28 pub model: Option, 29 #[serde(default)] 30 pub messages: Vec, 31 #[serde(default)] 32 pub stream: bool, 33 #[serde(default)] 34 pub temperature: Option, 35 #[serde(default)] 36 pub top_p: Option, 37 #[serde(default)] 38 pub top_k: Option, 39 #[serde(default)] 40 pub max_tokens: Option, 41 #[serde(default)] 42 pub max_completion_tokens: Option, 43 #[serde(default)] 44 pub stop: Option, 45 #[serde(default)] 46 pub seed: Option, 47 #[serde(default)] 48 pub n: Option, 49 #[serde(default)] 50 pub tools: Option>, 51 #[serde(default)] 52 pub functions: Option>, 53 #[serde(default)] 54 pub stream_options: Option, 55 /// Template switches (`{"enable_thinking": bool, "reasoning_effort": "low|medium|xhigh", 56 /// "preserve_thinking": bool}`), read only by templates that define them (Qwen3.8-27B: 57 /// thinking and effort; Qwen3.6-35B-A3B: thinking and preserve). 58 #[serde(default)] 59 pub chat_template_kwargs: Option, 60 /// Same as `chat_template_kwargs.reasoning_effort` (the OpenAI field name). 61 #[serde(default)] 62 pub reasoning_effort: Option, 63 /// AIEN extra: return the generated token ids in `usage.aien_token_ids` (correctness gates). 64 #[serde(default)] 65 pub aien_return_token_ids: bool, 66 /// AIEN extra, the merge train (L384): per-request test switches 67 /// (`{"stroke": bool, "exact_head": bool, "mutant": k, "row_swap": k, ...}`, see 68 /// [`crate::engine::TestSwitches`]). Honored only by a build with the cargo feature 69 /// `aien-test-switches`; every other build answers 400 when the field is present. 70 #[serde(default)] 71 pub aien_test: Option, 72 } 73 74 #[derive(Debug, Deserialize)] 75 pub struct StreamOptions { 76 #[serde(default)] 77 pub include_usage: bool, 78 } 79 80 #[derive(Debug, Deserialize)] 81 pub struct Message { 82 pub role: String, 83 /// A string, or an array of `{type: "text", text}` parts, or null. 84 #[serde(default)] 85 pub content: Value, 86 /// Assistant turn: the calls it made (OpenAI shape). 87 #[serde(default)] 88 pub tool_calls: Option>, 89 /// Tool turn: which call it answers (not rendered by the Qwen template). 90 #[serde(default)] 91 pub tool_call_id: Option, 92 } 93 94 #[derive(Debug, Serialize)] 95 pub struct ErrorBody { 96 pub error: ErrorDetail, 97 } 98 99 #[derive(Debug, Serialize)] 100 pub struct ErrorDetail { 101 pub message: String, 102 #[serde(rename = "type")] 103 pub kind: String, 104 pub param: Option, 105 pub code: Option, 106 } 107 108 impl ErrorBody { 109 pub fn new(kind: &str, message: impl Into) -> Self { 110 Self { 111 error: ErrorDetail { 112 message: message.into(), 113 kind: kind.to_string(), 114 param: None, 115 code: None, 116 }, 117 } 118 } 119 } 120 121 #[derive(Debug, Serialize)] 122 pub struct Usage { 123 pub prompt_tokens: usize, 124 pub completion_tokens: usize, 125 pub total_tokens: usize, 126 /// How the reply was sampled (OWN-8B stage S): an extra field, ignored by OpenAI clients. 127 /// Its `seed` replays the request. Absent for an engine that does not sample. 128 #[serde(skip_serializing_if = "Option::is_none")] 129 pub aien_sampling: Option, 130 /// The generated token ids, when the request set `aien_return_token_ids`. 131 #[serde(skip_serializing_if = "Option::is_none")] 132 pub aien_token_ids: Option>, 133 /// The merge train's switches this request set (`{"applied": [names]}`), when it set any. 134 #[serde(skip_serializing_if = "Option::is_none")] 135 pub aien_test: Option, 136 } 137 138 #[derive(Debug, Serialize)] 139 pub struct ModelList { 140 pub object: &'static str, 141 pub data: Vec, 142 } 143 144 #[derive(Debug, Serialize, Clone)] 145 pub struct ModelCard { 146 pub id: String, 147 pub object: &'static str, 148 pub created: u64, 149 pub owned_by: String, 150 } 151 152 /// Builds a non-streaming `chat.completion` object. 153 pub fn completion_json( 154 id: &str, 155 created: u64, 156 model: &str, 157 content: &str, 158 reasoning: Option<&str>, 159 finish_reason: &str, 160 usage: &Usage, 161 ) -> Value { 162 let mut message = serde_json::json!({"role": "assistant", "content": content}); 163 if let Some(r) = reasoning { 164 message["reasoning_content"] = Value::String(r.into()); 165 } 166 serde_json::json!({ 167 "id": id, 168 "object": "chat.completion", 169 "created": created, 170 "model": model, 171 "choices": [{ 172 "index": 0, 173 "message": message, 174 "finish_reason": finish_reason, 175 }], 176 "usage": usage, 177 }) 178 } 179 180 /// Builds one `chat.completion.chunk` object. `role` is set on the first chunk, 181 /// `finish_reason` and `usage` on the last. 182 pub fn chunk_json( 183 id: &str, 184 created: u64, 185 model: &str, 186 role: Option<&str>, 187 content: Option<&str>, 188 finish_reason: Option<&str>, 189 usage: Option<&Usage>, 190 ) -> Value { 191 chunk_json_with( 192 id, 193 created, 194 model, 195 role, 196 content, 197 None, 198 finish_reason, 199 usage, 200 ) 201 } 202 203 /// [`chunk_json`] with a `reasoning_content` delta (the thinking block of a thinking model). 204 #[allow(clippy::too_many_arguments)] 205 pub fn chunk_json_with( 206 id: &str, 207 created: u64, 208 model: &str, 209 role: Option<&str>, 210 content: Option<&str>, 211 reasoning: Option<&str>, 212 finish_reason: Option<&str>, 213 usage: Option<&Usage>, 214 ) -> Value { 215 let mut delta = serde_json::Map::new(); 216 if let Some(r) = role { 217 delta.insert("role".into(), Value::String(r.into())); 218 } 219 if let Some(c) = content { 220 delta.insert("content".into(), Value::String(c.into())); 221 } 222 if let Some(r) = reasoning { 223 delta.insert("reasoning_content".into(), Value::String(r.into())); 224 } 225 let mut v = serde_json::json!({ 226 "id": id, 227 "object": "chat.completion.chunk", 228 "created": created, 229 "model": model, 230 "choices": [{ 231 "index": 0, 232 "delta": Value::Object(delta), 233 "finish_reason": finish_reason, 234 }], 235 }); 236 if let Some(u) = usage { 237 v["usage"] = serde_json::to_value(u).expect("usage serializes"); 238 } 239 v 240 }