File size: 5,015 Bytes
a21c316 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | // OpenAI 数据模型
use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct OpenAIRequest {
pub model: String,
#[serde(default)]
pub messages: Vec<OpenAIMessage>,
#[serde(default)]
pub prompt: Option<String>,
#[serde(default)]
pub stream: bool,
#[serde(default)]
pub n: Option<u32>, // [NEW] 支持多候选结果数量
#[serde(rename = "max_tokens")]
pub max_tokens: Option<u32>,
pub temperature: Option<f64>,
#[serde(rename = "top_p")]
pub top_p: Option<f64>,
pub stop: Option<Value>,
pub response_format: Option<ResponseFormat>,
#[serde(default)]
pub tools: Option<Vec<Value>>,
#[serde(rename = "tool_choice")]
pub tool_choice: Option<Value>,
#[serde(rename = "parallel_tool_calls")]
pub parallel_tool_calls: Option<bool>,
// Codex proprietary fields
pub instructions: Option<String>,
pub input: Option<Value>,
// [NEW] Image generation parameters (for Chat API compatibility)
#[serde(default)]
pub size: Option<String>,
#[serde(default)]
pub quality: Option<String>,
#[serde(default, rename = "personGeneration")]
pub person_generation: Option<String>,
// [NEW] Thinking/Extended Thinking 支持 (兼容 Anthropic/Claude 协议)
#[serde(default)]
pub thinking: Option<ThinkingConfig>,
// [NEW] Direct imageSize support (for Gemini native parameter)
#[serde(default, rename = "imageSize")]
pub image_size: Option<String>,
}
/// Thinking 配置 (兼容 Anthropic 和 OpenAI 扩展协议)
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ThinkingConfig {
#[serde(rename = "type")]
pub thinking_type: Option<String>, // "enabled", "disabled", or "adaptive"
#[serde(rename = "budget_tokens", alias = "budgetTokens")]
pub budget_tokens: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub effort: Option<String>, // "low", "high", or "max"
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResponseFormat {
pub r#type: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(untagged)]
pub enum OpenAIContent {
String(String),
Array(Vec<OpenAIContentBlock>),
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(tag = "type")]
pub enum OpenAIContentBlock {
#[serde(rename = "text", alias = "input_text")]
Text { text: String },
#[serde(rename = "image_url")]
ImageUrl { image_url: OpenAIImageUrl },
#[serde(rename = "audio_url")]
AudioUrl { audio_url: AudioUrlContent },
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct OpenAIImageUrl {
pub url: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub detail: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct AudioUrlContent {
pub url: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OpenAIMessage {
pub role: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub content: Option<OpenAIContent>,
#[serde(skip_serializing_if = "Option::is_none")]
pub reasoning_content: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tool_calls: Option<Vec<ToolCall>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tool_call_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolCall {
pub id: String,
pub r#type: String,
pub function: ToolFunction,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolFunction {
pub name: String,
pub arguments: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OpenAIResponse {
pub id: String,
pub object: String,
pub created: u64,
pub model: String,
pub choices: Vec<Choice>,
#[serde(skip_serializing_if = "Option::is_none")]
pub usage: Option<OpenAIUsage>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Choice {
pub index: u32,
pub message: OpenAIMessage,
pub finish_reason: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OpenAIUsage {
pub prompt_tokens: u32,
pub completion_tokens: u32,
pub total_tokens: u32,
#[serde(skip_serializing_if = "Option::is_none")]
pub prompt_tokens_details: Option<PromptTokensDetails>,
#[serde(skip_serializing_if = "Option::is_none")]
pub completion_tokens_details: Option<CompletionTokensDetails>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PromptTokensDetails {
#[serde(skip_serializing_if = "Option::is_none")]
pub cached_tokens: Option<u32>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompletionTokensDetails {
#[serde(skip_serializing_if = "Option::is_none")]
pub reasoning_tokens: Option<u32>,
}
|