新增: F-05多模态Phase2a后端(ContentPart+parts字段+provider图片块+truncate防撑爆)
This commit is contained in:
@@ -16,6 +16,9 @@ use crate::provider::{
|
||||
CompletionRequest, CompletionResponse, LlmProvider, StreamChunk, StreamResult,
|
||||
TokenUsage, ToolCall, ToolCallDelta,
|
||||
};
|
||||
// ChatMessage 仅单测构造 CompletionRequest 用,避免非 test 构建的 unused import 警告。
|
||||
#[cfg(test)]
|
||||
use crate::provider::ChatMessage;
|
||||
use crate::retry::{
|
||||
retry_with_backoff, AttemptOutcome, is_reqwest_error_retryable, is_status_retryable,
|
||||
};
|
||||
@@ -44,10 +47,13 @@ struct OpenAiRequest {
|
||||
}
|
||||
|
||||
/// OpenAI 消息格式
|
||||
///
|
||||
/// `content` 为 `serde_json::Value`:纯文本消息走字符串简写(与老端点兼容),
|
||||
/// 含图消息走 `[{type:"text",text},{type:"image_url",image_url:{url}}]` 数组。
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
struct OpenAiMessage {
|
||||
role: String,
|
||||
content: String,
|
||||
content: serde_json::Value,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
tool_call_id: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
@@ -308,6 +314,38 @@ impl OpenAICompatProvider {
|
||||
crate::provider::MessageRole::Assistant => "assistant",
|
||||
crate::provider::MessageRole::Tool => "tool",
|
||||
};
|
||||
// F-260614-05 Phase 2a: 多模态 content(须在 move m.tool_calls 之前算,借用 m)。
|
||||
// 含图消息走 content 数组(text/image_url);纯文本走字符串简写
|
||||
// (保持与现有纯文本端点零回归)。image_url 支持 data URI(base64)与 http(s) URL。
|
||||
let content = if m.has_image() {
|
||||
let parts: Vec<serde_json::Value> = m
|
||||
.flattened_parts()
|
||||
.into_iter()
|
||||
.map(|p| match p {
|
||||
crate::provider::ContentPart::Text { text } => serde_json::json!({
|
||||
"type": "text",
|
||||
"text": text,
|
||||
}),
|
||||
crate::provider::ContentPart::Image { url, base64, media_type, alt: _ } => {
|
||||
let final_url = match (base64, url, media_type) {
|
||||
(Some(b), _, Some(mt)) => {
|
||||
format!("data:{};base64,{}", mt, b)
|
||||
}
|
||||
(None, Some(u), _) => u,
|
||||
// 兜底:缺数据时退化为占位,避免发空 url 触发 400
|
||||
_ => String::new(),
|
||||
};
|
||||
serde_json::json!({
|
||||
"type": "image_url",
|
||||
"image_url": { "url": final_url },
|
||||
})
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
serde_json::Value::Array(parts)
|
||||
} else {
|
||||
serde_json::Value::String(m.content.clone())
|
||||
};
|
||||
let tool_calls = m.tool_calls.map(|calls| {
|
||||
calls
|
||||
.into_iter()
|
||||
@@ -325,7 +363,7 @@ impl OpenAICompatProvider {
|
||||
});
|
||||
OpenAiMessage {
|
||||
role: role.to_string(),
|
||||
content: m.content,
|
||||
content,
|
||||
tool_call_id: m.tool_call_id,
|
||||
tool_calls,
|
||||
}
|
||||
@@ -691,4 +729,58 @@ mod tests {
|
||||
assert_eq!(tcs[0].function_arguments.as_deref(), Some("{\"q\":"));
|
||||
assert!(!c.finished);
|
||||
}
|
||||
|
||||
// ---------- F-260614-05 Phase 2a 多模态 convert_request ----------
|
||||
|
||||
/// 含图消息 → content 数组(text + image_url data URI);纯文本 → 字符串简写
|
||||
#[test]
|
||||
fn openai_convert_multimodal_content() {
|
||||
let provider = OpenAICompatProvider::new("https://api.openai.com", "k", "gpt-4o");
|
||||
let req = CompletionRequest {
|
||||
model: "gpt-4o".into(),
|
||||
messages: vec![ChatMessage::user_parts(
|
||||
"看图",
|
||||
vec![
|
||||
crate::provider::ContentPart::image_base64("image/png", "iVBOR"),
|
||||
crate::provider::ContentPart::image_url("https://x/a.png"),
|
||||
],
|
||||
)],
|
||||
temperature: None,
|
||||
max_tokens: None,
|
||||
stream: false,
|
||||
tools: None,
|
||||
tool_choice: None,
|
||||
};
|
||||
let out = provider.convert_request(req);
|
||||
let msg = &out.messages[0];
|
||||
// content 是数组:[text "看图", image_url(data URI), image_url(http url)]
|
||||
let arr = msg.content.as_array().expect("含图 → content 数组");
|
||||
assert_eq!(arr.len(), 3);
|
||||
assert_eq!(arr[0]["type"], "text");
|
||||
assert_eq!(arr[0]["text"], "看图");
|
||||
assert_eq!(arr[1]["type"], "image_url");
|
||||
assert_eq!(
|
||||
arr[1]["image_url"]["url"],
|
||||
"data:image/png;base64,iVBOR"
|
||||
);
|
||||
assert_eq!(arr[2]["image_url"]["url"], "https://x/a.png");
|
||||
}
|
||||
|
||||
/// 纯文本消息 → content 仍是字符串简写(无图不数组化,对齐纯文本端点兼容)
|
||||
#[test]
|
||||
fn openai_convert_text_only_remains_string() {
|
||||
let provider = OpenAICompatProvider::new("https://api.openai.com", "k", "gpt-4o");
|
||||
let req = CompletionRequest {
|
||||
model: "gpt-4o".into(),
|
||||
messages: vec![ChatMessage::user("hello")],
|
||||
temperature: None,
|
||||
max_tokens: None,
|
||||
stream: false,
|
||||
tools: None,
|
||||
tool_choice: None,
|
||||
};
|
||||
let out = provider.convert_request(req);
|
||||
let msg = &out.messages[0];
|
||||
assert_eq!(msg.content, serde_json::Value::String("hello".into()));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user