|
|
|
@ -311,7 +311,7 @@ public func sendMessageStream(_ sessionId: String, messages: [[String: Any]]) { |
|
|
|
tools: chatTools |
|
|
|
) |
|
|
|
|
|
|
|
// print("[ChatApiService] 发起AI问答 问题:\(messages[0]) 工具数量: \(chatTools.count)") |
|
|
|
print("[ChatApiService] 发起AI问答 问题:\(messages) 工具数量: \(chatTools.count)") |
|
|
|
} else { |
|
|
|
query = ChatQuery( |
|
|
|
messages: chatMessages, |
|
|
|
@ -761,7 +761,44 @@ private func convertMapToJsonObject(_ map: [String: Any]) -> [String: Any] { |
|
|
|
} |
|
|
|
return false |
|
|
|
} |
|
|
|
|
|
|
|
// 解析并构建正确的图片消息 |
|
|
|
func parseImageData(_ data: [String: Any]) -> ChatQuery.ChatCompletionMessageParam? { |
|
|
|
// 1. 校验基础字段 |
|
|
|
guard let type = data["type"] as? String, type == "image_url", |
|
|
|
let imageUrlDict = data["image_url"] as? [String: Any], |
|
|
|
let imageUrlString = imageUrlDict["url"] as? String else { |
|
|
|
print("数据结构错误:缺少type或image_url.url") |
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
// 2. 补充默认的detail(原结构体必须,默认auto) |
|
|
|
let detailString = imageUrlDict["detail"] as? String ?? "auto" |
|
|
|
let imageDetail: ChatQuery.ChatCompletionMessageParam.UserMessageParam.Content.VisionContent.ChatCompletionContentPartImageParam.ImageURL.Detail |
|
|
|
switch detailString.lowercased() { |
|
|
|
case "low": |
|
|
|
imageDetail = .low |
|
|
|
case "high": |
|
|
|
imageDetail = .high |
|
|
|
default: |
|
|
|
imageDetail = .auto |
|
|
|
} |
|
|
|
|
|
|
|
// 3. 逐层构建结构体(按原结构体要求映射) |
|
|
|
let imageUrlModel = ChatQuery.ChatCompletionMessageParam.UserMessageParam.Content.VisionContent.ChatCompletionContentPartImageParam.ImageURL( |
|
|
|
url: imageUrlString, |
|
|
|
detail: imageDetail |
|
|
|
) |
|
|
|
|
|
|
|
let imageContentPart = ChatQuery.ChatCompletionMessageParam.UserMessageParam.Content.VisionContent.ChatCompletionContentPartImageParam( |
|
|
|
imageUrl: imageUrlModel |
|
|
|
) |
|
|
|
|
|
|
|
let visionContent = ChatQuery.ChatCompletionMessageParam.UserMessageParam.Content.VisionContent.chatCompletionContentPartImageParam(imageContentPart) |
|
|
|
let userContent = ChatQuery.ChatCompletionMessageParam.UserMessageParam.Content.vision([visionContent]) |
|
|
|
let userMessageParam = ChatQuery.ChatCompletionMessageParam.UserMessageParam(content: userContent) |
|
|
|
|
|
|
|
return .user(userMessageParam) |
|
|
|
} |
|
|
|
/// 转换为OpenAI Message格式 |
|
|
|
private func convertToChatMessages(_ messages: [[String: Any]]) throws -> [ChatQuery.ChatCompletionMessageParam] { |
|
|
|
var chatMessages: [ChatQuery.ChatCompletionMessageParam] = [] |
|
|
|
@ -851,17 +888,24 @@ private func convertMapToJsonObject(_ map: [String: Any]) -> [String: Any] { |
|
|
|
var textContent = "" |
|
|
|
|
|
|
|
for item in contentArray { |
|
|
|
if let type = item["type"] as? String, type == "text", |
|
|
|
let text = item["text"] as? String { |
|
|
|
textContent += text |
|
|
|
if let type = item["type"] as? String { |
|
|
|
switch type { |
|
|
|
case "text": |
|
|
|
if let text = item["text"] as? String { |
|
|
|
if let chatMessage = ChatQuery.ChatCompletionMessageParam(role: messageRole, content: text) { |
|
|
|
chatMessages.append(chatMessage) |
|
|
|
} |
|
|
|
} |
|
|
|
case "image_url": |
|
|
|
if let imageMessage = parseImageData(item) { |
|
|
|
chatMessages.append(imageMessage) |
|
|
|
print("图片消息解析成功") |
|
|
|
} |
|
|
|
default: |
|
|
|
break |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
let finalContent = textContent.isEmpty ? "多媒体内容" : textContent |
|
|
|
|
|
|
|
if let chatMessage = ChatQuery.ChatCompletionMessageParam(role: messageRole, content: finalContent) { |
|
|
|
chatMessages.append(chatMessage) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|