Merge branch 'fork/lottshin/pr/multimodal-chat-input'
Browse files
services/openai_backend_api.py
CHANGED
|
@@ -198,13 +198,71 @@ class OpenAIBackendAPI:
|
|
| 198 |
"""把标准 chat messages 转成 web conversation 所需的 messages。"""
|
| 199 |
conversation_messages = []
|
| 200 |
for item in messages:
|
|
|
|
| 201 |
content = item.get("content", "")
|
| 202 |
-
if
|
| 203 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
conversation_messages.append({
|
| 205 |
"id": new_uuid(),
|
| 206 |
-
"author": {"role":
|
| 207 |
-
"content": {"content_type": "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
})
|
| 209 |
return conversation_messages
|
| 210 |
|
|
|
|
| 198 |
"""把标准 chat messages 转成 web conversation 所需的 messages。"""
|
| 199 |
conversation_messages = []
|
| 200 |
for item in messages:
|
| 201 |
+
role = item.get("role", "user")
|
| 202 |
content = item.get("content", "")
|
| 203 |
+
if isinstance(content, str):
|
| 204 |
+
conversation_messages.append({
|
| 205 |
+
"id": new_uuid(),
|
| 206 |
+
"author": {"role": role},
|
| 207 |
+
"content": {"content_type": "text", "parts": [content]},
|
| 208 |
+
})
|
| 209 |
+
continue
|
| 210 |
+
if not isinstance(content, list):
|
| 211 |
+
raise RuntimeError("only string or list message content is supported")
|
| 212 |
+
text_parts: list[str] = []
|
| 213 |
+
image_inputs: list[tuple[bytes, str]] = []
|
| 214 |
+
for part in content:
|
| 215 |
+
if not isinstance(part, dict):
|
| 216 |
+
continue
|
| 217 |
+
part_type = str(part.get("type") or "")
|
| 218 |
+
if part_type == "text":
|
| 219 |
+
text_parts.append(str(part.get("text") or ""))
|
| 220 |
+
elif part_type == "image":
|
| 221 |
+
data = part.get("data")
|
| 222 |
+
mime = str(part.get("mime") or "image/png")
|
| 223 |
+
if isinstance(data, (bytes, bytearray)):
|
| 224 |
+
image_inputs.append((bytes(data), mime))
|
| 225 |
+
if not image_inputs:
|
| 226 |
+
conversation_messages.append({
|
| 227 |
+
"id": new_uuid(),
|
| 228 |
+
"author": {"role": role},
|
| 229 |
+
"content": {"content_type": "text", "parts": ["".join(text_parts)]},
|
| 230 |
+
})
|
| 231 |
+
continue
|
| 232 |
+
if not self.access_token:
|
| 233 |
+
raise RuntimeError("authenticated upstream account required for image input")
|
| 234 |
+
uploaded: list[Dict[str, Any]] = []
|
| 235 |
+
for idx, (data, mime) in enumerate(image_inputs, start=1):
|
| 236 |
+
ext_part = mime.split("/", 1)[1].split("+")[0] if "/" in mime else "png"
|
| 237 |
+
extension = "jpg" if ext_part == "jpeg" else (ext_part or "png")
|
| 238 |
+
b64 = base64.b64encode(data).decode("ascii")
|
| 239 |
+
uploaded.append(self._upload_image(f"data:{mime};base64,{b64}", f"image_{idx}.{extension}"))
|
| 240 |
+
parts: list[Any] = []
|
| 241 |
+
for ref in uploaded:
|
| 242 |
+
parts.append({
|
| 243 |
+
"content_type": "image_asset_pointer",
|
| 244 |
+
"asset_pointer": f"file-service://{ref['file_id']}",
|
| 245 |
+
"width": ref["width"],
|
| 246 |
+
"height": ref["height"],
|
| 247 |
+
"size_bytes": ref["file_size"],
|
| 248 |
+
})
|
| 249 |
+
text = "".join(text_parts)
|
| 250 |
+
if text:
|
| 251 |
+
parts.append(text)
|
| 252 |
conversation_messages.append({
|
| 253 |
"id": new_uuid(),
|
| 254 |
+
"author": {"role": role},
|
| 255 |
+
"content": {"content_type": "multimodal_text", "parts": parts},
|
| 256 |
+
"metadata": {
|
| 257 |
+
"attachments": [{
|
| 258 |
+
"id": ref["file_id"],
|
| 259 |
+
"mimeType": ref["mime_type"],
|
| 260 |
+
"name": ref["file_name"],
|
| 261 |
+
"size": ref["file_size"],
|
| 262 |
+
"width": ref["width"],
|
| 263 |
+
"height": ref["height"],
|
| 264 |
+
} for ref in uploaded],
|
| 265 |
+
},
|
| 266 |
})
|
| 267 |
return conversation_messages
|
| 268 |
|
services/protocol/conversation.py
CHANGED
|
@@ -14,7 +14,7 @@ import tiktoken
|
|
| 14 |
from services.account_service import account_service
|
| 15 |
from services.config import config
|
| 16 |
from services.openai_backend_api import OpenAIBackendAPI
|
| 17 |
-
from utils.helper import IMAGE_MODELS
|
| 18 |
from utils.log import logger
|
| 19 |
|
| 20 |
|
|
@@ -98,8 +98,30 @@ def normalize_messages(messages: object, system: Any = None) -> list[dict[str, A
|
|
| 98 |
normalized.append({"role": "system", "content": system_text})
|
| 99 |
if isinstance(messages, list):
|
| 100 |
for message in messages:
|
| 101 |
-
if isinstance(message, dict):
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
return normalized
|
| 104 |
|
| 105 |
|
|
|
|
| 14 |
from services.account_service import account_service
|
| 15 |
from services.config import config
|
| 16 |
from services.openai_backend_api import OpenAIBackendAPI
|
| 17 |
+
from utils.helper import IMAGE_MODELS, extract_image_from_message_content
|
| 18 |
from utils.log import logger
|
| 19 |
|
| 20 |
|
|
|
|
| 98 |
normalized.append({"role": "system", "content": system_text})
|
| 99 |
if isinstance(messages, list):
|
| 100 |
for message in messages:
|
| 101 |
+
if not isinstance(message, dict):
|
| 102 |
+
continue
|
| 103 |
+
role = message.get("role", "user")
|
| 104 |
+
content = message.get("content", "")
|
| 105 |
+
text = message_text(content)
|
| 106 |
+
images: list[tuple[bytes, str]] = []
|
| 107 |
+
if role == "user":
|
| 108 |
+
images.extend(extract_image_from_message_content(content))
|
| 109 |
+
if isinstance(content, list):
|
| 110 |
+
for part in content:
|
| 111 |
+
if not isinstance(part, dict) or part.get("type") != "image":
|
| 112 |
+
continue
|
| 113 |
+
data = part.get("data")
|
| 114 |
+
if isinstance(data, (bytes, bytearray)):
|
| 115 |
+
images.append((bytes(data), str(part.get("mime") or "image/png")))
|
| 116 |
+
if images:
|
| 117 |
+
parts: list[Any] = []
|
| 118 |
+
if text:
|
| 119 |
+
parts.append({"type": "text", "text": text})
|
| 120 |
+
for data, mime in images:
|
| 121 |
+
parts.append({"type": "image", "data": data, "mime": mime})
|
| 122 |
+
normalized.append({"role": role, "content": parts})
|
| 123 |
+
else:
|
| 124 |
+
normalized.append({"role": role, "content": text})
|
| 125 |
return normalized
|
| 126 |
|
| 127 |
|