Spaces:
Paused
Paused
File size: 32,612 Bytes
a5784e9 | 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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 | import base64
import json
import logging
import os
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Set, Tuple, Union, cast
from urllib.parse import unquote, urlparse
from api_utils.utils_ext.files import extract_data_url_to_local, save_blob_to_local
from api_utils.utils_ext.function_calling_orchestrator import should_skip_tool_injection
from logging_utils import set_request_id
from models import Message
if TYPE_CHECKING:
from api_utils.utils_ext.function_calling_orchestrator import FunctionCallingState
def prepare_combined_prompt(
messages: List[Message],
req_id: str,
tools: Optional[List[Dict[str, Any]]] = None,
tool_choice: Optional[Union[str, Dict[str, Any]]] = None,
fc_state: Optional["FunctionCallingState"] = None,
) -> Tuple[str, List[str]]:
"""Prepare combined prompt"""
logger = logging.getLogger("AIStudioProxyServer")
set_request_id(req_id)
# Track summary stats for consolidated logging
_has_system_prompt = False
_msg_count = len(messages)
# Do not clear upload_files here; it is cleared by the upper layer at the start of each request as needed
# to avoid "file not found" errors caused by loss of historical attachments.
combined_parts: List[str] = []
system_prompt_content: Optional[str] = None
processed_system_message_indices: Set[int] = set()
files_list: List[
str
] = [] # Collect local file paths to be uploaded (images, videos, PDFs, etc.)
# If available tools are declared, inject the tool catalog before the prompt to help the model know available functions
# Skip injection when using native function calling mode (tools configured via UI)
# Pass fc_state to handle AUTO mode fallback correctly
if isinstance(tools, list) and len(tools) > 0:
if should_skip_tool_injection(tools, fc_state=fc_state):
logger.debug(
f"[{req_id}] Skipping tool catalog injection - native mode active and configured"
)
else:
try:
tool_lines: List[str] = ["Available Tools Catalog:"]
for t in tools:
name: Optional[str] = None
params_schema: Optional[Dict[str, Any]] = None
# t is Dict[str, Any] from List[Dict[str, Any]]
fn_val: Any = t.get("function") if "function" in t else t
if isinstance(fn_val, dict):
# Type narrowed: fn_val is dict
typed_fn: Dict[str, Any] = cast(Dict[str, Any], fn_val)
name_raw: Any = typed_fn.get("name") or t.get("name")
if isinstance(name_raw, str):
name = name_raw
params_raw: Any = typed_fn.get("parameters")
if isinstance(params_raw, dict):
params_schema = cast(Dict[str, Any], params_raw)
else:
# fn_val is not dict, get name directly from t
name_raw: Any = t.get("name")
if isinstance(name_raw, str):
name = name_raw
if name:
tool_lines.append(f"- Function: {name}")
if params_schema:
try:
tool_lines.append(
f" Parameter Schema: {json.dumps(params_schema, ensure_ascii=False)}"
)
except Exception:
pass
if tool_choice:
# Explicitly request or suggest callable function name
chosen_name: Optional[str] = None
if isinstance(tool_choice, dict):
# Type narrowed to dict by isinstance
typed_tool_choice: Dict[str, Any] = tool_choice
fn_val: Any = typed_tool_choice.get("function")
if isinstance(fn_val, dict):
# Standard format: {"type": "function", "function": {"name": "..."}}
typed_fn: Dict[str, Any] = cast(Dict[str, Any], fn_val)
name_raw: Any = typed_fn.get("name")
if isinstance(name_raw, str):
chosen_name = name_raw
elif "name" in typed_tool_choice:
# Flat format: {"type": "function", "name": "..."}
name_raw = typed_tool_choice.get("name")
if isinstance(name_raw, str):
chosen_name = name_raw
elif tool_choice.lower() not in (
"auto",
"none",
"no",
"off",
"required",
"any",
):
chosen_name = tool_choice
if chosen_name:
tool_lines.append(f"Recommended function to use: {chosen_name}")
combined_parts.append("\n".join(tool_lines) + "\n---\n")
except Exception:
pass
# Process system messages
for i, msg in enumerate(messages):
if msg.role == "system":
content = msg.content
if isinstance(content, str) and content.strip():
system_prompt_content = content.strip()
processed_system_message_indices.add(i)
_has_system_prompt = True
logger.debug(
f"Found system prompt at index {i}: {system_prompt_content[:80]}..."
)
system_instr_prefix = "System Instructions:\n"
combined_parts.append(f"{system_instr_prefix}{system_prompt_content}")
else:
logger.debug(f"Ignoring empty system message at index {i}")
processed_system_message_indices.add(i)
break
role_map_ui = {
"user": "User",
"assistant": "Assistant",
"system": "System",
"tool": "Tool",
}
turn_separator = "\n---\n"
# Process other messages
for i, msg in enumerate(messages):
if i in processed_system_message_indices:
continue
if msg.role == "system":
logger.debug(f"Skipping subsequent system message at index {i}")
continue
if combined_parts:
combined_parts.append(turn_separator)
role = msg.role or "unknown"
role_prefix_ui = f"{role_map_ui.get(role, role.capitalize())}:\n"
current_turn_parts: List[str] = [role_prefix_ui]
content = msg.content or ""
content_str: str = ""
if isinstance(content, str):
content_str = content.strip()
elif isinstance(content, list):
# Process multimodal content
text_parts: List[str] = []
for item in content:
# Get item type
item_type: Optional[str] = None
try:
# Guard against property exceptions when using hasattr/getattr
if hasattr(item, "type"):
item_type = item.type
except Exception:
item_type = None
if item_type is None and isinstance(item, dict):
typed_item: Dict[str, Any] = cast(Dict[str, Any], item)
item_type_raw: Any = typed_item.get("type")
if isinstance(item_type_raw, str):
item_type = item_type_raw
if item_type == "text":
# Text item
if hasattr(item, "text"):
text_parts.append(getattr(item, "text", "") or "")
elif isinstance(item, dict):
typed_item: Dict[str, Any] = cast(Dict[str, Any], item)
text_raw: Any = typed_item.get("text", "")
text_parts.append(str(text_raw))
continue
# Image/File/Media URL item
if item_type in (
"image_url",
"file_url",
"media_url",
"input_image",
) or (
isinstance(item, dict)
and (
"image_url" in item
or "input_image" in item
or "file_url" in item
or "media_url" in item
or "url" in item
)
):
try:
url_value: Optional[str] = None
# Pydantic object attributes
if hasattr(item, "image_url") and item.image_url:
url_value = item.image_url.url
try:
detail_val: Optional[str] = getattr(
item.image_url, "detail", None
)
if detail_val:
text_parts.append(
f"[Image Details: detail={detail_val}]"
)
except Exception:
pass
elif hasattr(item, "input_image") and item.input_image:
url_value = item.input_image.url
try:
detail_val: Optional[str] = getattr(
item.input_image, "detail", None
)
if detail_val:
text_parts.append(
f"[Image Details: detail={detail_val}]"
)
except Exception:
pass
elif hasattr(item, "file_url") and item.file_url:
url_value = item.file_url.url
elif hasattr(item, "media_url") and item.media_url:
url_value = item.media_url.url
elif hasattr(item, "url") and item.url:
url_value = item.url
# Dictionary structure (backwards compatibility)
if url_value is None and isinstance(item, dict):
typed_item: Dict[str, Any] = cast(Dict[str, Any], item)
image_url_raw: Any = typed_item.get("image_url")
input_image_raw: Any = typed_item.get("input_image")
if isinstance(image_url_raw, dict):
typed_img_url: Dict[str, Any] = cast(
Dict[str, Any], image_url_raw
)
url_raw: Any = typed_img_url.get("url")
if isinstance(url_raw, str):
url_value = url_raw
detail_raw: Any = typed_img_url.get("detail")
if isinstance(detail_raw, str):
text_parts.append(
f"[Image Details: detail={detail_raw}]"
)
elif isinstance(image_url_raw, str):
url_value = image_url_raw
elif isinstance(input_image_raw, dict):
typed_input_img: Dict[str, Any] = cast(
Dict[str, Any], input_image_raw
)
url_raw: Any = typed_input_img.get("url")
if isinstance(url_raw, str):
url_value = url_raw
detail_raw: Any = typed_input_img.get("detail")
if isinstance(detail_raw, str):
text_parts.append(
f"[Image Details: detail={detail_raw}]"
)
elif isinstance(input_image_raw, str):
url_value = input_image_raw
else:
# Check other URL fields
file_url_raw: Any = typed_item.get("file_url")
media_url_raw: Any = typed_item.get("media_url")
file_raw: Any = typed_item.get("file")
if isinstance(file_url_raw, dict):
typed_file_url: Dict[str, Any] = cast(
Dict[str, Any], file_url_raw
)
url_raw: Any = typed_file_url.get("url")
if isinstance(url_raw, str):
url_value = url_raw
elif isinstance(file_url_raw, str):
url_value = file_url_raw
elif isinstance(media_url_raw, dict):
typed_media_url: Dict[str, Any] = cast(
Dict[str, Any], media_url_raw
)
url_raw: Any = typed_media_url.get("url")
if isinstance(url_raw, str):
url_value = url_raw
elif isinstance(media_url_raw, str):
url_value = media_url_raw
elif "url" in typed_item:
url_raw: Any = typed_item.get("url")
if isinstance(url_raw, str):
url_value = url_raw
elif isinstance(file_raw, dict):
# Compatible with general file field
typed_file: Dict[str, Any] = cast(
Dict[str, Any], file_raw
)
url_raw: Any = typed_file.get(
"url"
) or typed_file.get("path")
if isinstance(url_raw, str):
url_value = url_raw
url_value = (url_value or "").strip()
if not url_value:
continue
# Normalize to local file list and log
if url_value.startswith("data:"):
file_path = extract_data_url_to_local(
url_value, req_id=req_id
)
if file_path:
files_list.append(file_path)
logger.debug(
f"(Prepare Prompt) Identified and added data:URL attachment: {file_path}"
)
elif url_value.startswith("file:"):
parsed = urlparse(url_value)
local_path = unquote(parsed.path)
if os.path.exists(local_path):
files_list.append(local_path)
logger.debug(
f"(Prepare Prompt) Identified and added local attachment (file://): {local_path}"
)
else:
logger.warning(
f"(Prepare Prompt) Local file pointed to by file URL does not exist: {local_path}"
)
elif os.path.isabs(url_value) and os.path.exists(url_value):
files_list.append(url_value)
logger.debug(
f"(Prepare Prompt) Identified and added local attachment (absolute path): {url_value}"
)
else:
logger.debug(
f"(Prepare Prompt) Ignoring non-local attachment URL: {url_value}"
)
except Exception as e:
logger.warning(
f"(Prepare Prompt) Error processing attachment URL: {e}"
)
continue
# Audio/Video input
if item_type in ("input_audio", "input_video"):
try:
inp: Any = None
if hasattr(item, "input_audio") and item.input_audio:
inp = item.input_audio
elif hasattr(item, "input_video") and item.input_video:
inp = item.input_video
elif isinstance(item, dict):
typed_item: Dict[str, Any] = cast(Dict[str, Any], item)
inp = typed_item.get("input_audio") or typed_item.get(
"input_video"
)
if inp:
url_value: Optional[str] = None
data_val: Optional[str] = None
mime_val: Optional[str] = None
fmt_val: Optional[str] = None
if isinstance(inp, dict):
typed_inp: Dict[str, Any] = cast(Dict[str, Any], inp)
url_raw: Any = typed_inp.get("url")
if isinstance(url_raw, str):
url_value = url_raw
data_raw: Any = typed_inp.get("data")
if isinstance(data_raw, str):
data_val = data_raw
mime_raw: Any = typed_inp.get("mime_type")
if isinstance(mime_raw, str):
mime_val = mime_raw
fmt_raw: Any = typed_inp.get("format")
if isinstance(fmt_raw, str):
fmt_val = fmt_raw
else:
# Pydantic model or object with attributes
url_attr: Any = getattr(inp, "url", None)
if isinstance(url_attr, str):
url_value = url_attr
data_attr: Any = getattr(inp, "data", None)
if isinstance(data_attr, str):
data_val = data_attr
mime_attr: Any = getattr(inp, "mime_type", None)
if isinstance(mime_attr, str):
mime_val = mime_attr
fmt_attr: Any = getattr(inp, "format", None)
if isinstance(fmt_attr, str):
fmt_val = fmt_attr
if url_value:
if url_value.startswith("data:"):
saved = extract_data_url_to_local(
url_value, req_id=req_id
)
if saved:
files_list.append(saved)
logger.debug(
f"(Prepare Prompt) Identified and added audio/video data:URL attachment: {saved}"
)
elif url_value.startswith("file:"):
parsed = urlparse(url_value)
local_path = unquote(parsed.path)
if os.path.exists(local_path):
files_list.append(local_path)
logger.debug(
f"(Prepare Prompt) Identified and added local audio/video attachment (file://): {local_path}"
)
elif os.path.isabs(url_value) and os.path.exists(
url_value
):
files_list.append(url_value)
logger.debug(
f"(Prepare Prompt) Identified and added local audio/video attachment (absolute path): {url_value}"
)
elif data_val:
if isinstance(data_val, str) and data_val.startswith(
"data:"
):
saved = extract_data_url_to_local(
data_val, req_id=req_id
)
if saved:
files_list.append(saved)
logger.debug(
f"(Prepare Prompt) Identified and added audio/video data:URL attachment: {saved}"
)
else:
# Treat as pure base64 data
try:
raw = base64.b64decode(data_val)
saved = save_blob_to_local(
raw, mime_val, fmt_val, req_id=req_id
)
if saved:
files_list.append(saved)
logger.debug(
f"(Prepare Prompt) Identified and added audio/video base64 attachment: {saved}"
)
except Exception:
pass
except Exception as e:
logger.warning(
f"(Prepare Prompt) Error processing audio/video input: {e}"
)
continue
# Other unknown items: log without affecting
logger.warning(
f"(Prepare Prompt) Warning: Ignoring non-text or unknown type content item in message at index {i}"
)
content_str = "\n".join(text_parts).strip()
elif isinstance(content, dict):
# Compatible with dictionary format content, may contain 'attachments'/'images'/'media'/'files'
typed_content: Dict[str, Any] = cast(Dict[str, Any], content)
text_parts = []
attachments_keys = ["attachments", "images", "media", "files"]
for key in attachments_keys:
items: Any = typed_content.get(key)
if isinstance(items, list):
for it in items:
url_value: Optional[str] = None
if isinstance(it, str):
url_value = it
elif isinstance(it, dict):
typed_it: Dict[str, Any] = cast(Dict[str, Any], it)
url_raw: Any = typed_it.get("url") or typed_it.get("path")
if isinstance(url_raw, str):
url_value = url_raw
if not url_value:
image_url_raw: Any = typed_it.get("image_url")
input_image_raw: Any = typed_it.get("input_image")
if isinstance(image_url_raw, dict):
typed_img_url: Dict[str, Any] = cast(
Dict[str, Any], image_url_raw
)
url_from_image: Any = typed_img_url.get("url")
if isinstance(url_from_image, str):
url_value = url_from_image
elif isinstance(input_image_raw, dict):
typed_input_img: Dict[str, Any] = cast(
Dict[str, Any], input_image_raw
)
url_from_input: Any = typed_input_img.get("url")
if isinstance(url_from_input, str):
url_value = url_from_input
if not url_value:
continue
url_value = url_value.strip()
if not url_value:
continue
if url_value.startswith("data:"):
fp = extract_data_url_to_local(url_value)
if fp:
files_list.append(fp)
logger.debug(
f"(Prepare Prompt) Identified and added dict attachment data:URL: {fp}"
)
elif url_value.startswith("file:"):
parsed = urlparse(url_value)
lp = unquote(parsed.path)
if os.path.exists(lp):
files_list.append(lp)
logger.debug(
f"(Prepare Prompt) Identified and added dict attachment file://: {lp}"
)
elif os.path.isabs(url_value) and os.path.exists(url_value):
files_list.append(url_value)
logger.debug(
f"(Prepare Prompt) Identified and added dict attachment absolute path: {url_value}"
)
else:
logger.debug(
f"(Prepare Prompt) Ignoring non-local URL for dict attachment: {url_value}"
)
# Also append potential plain text description in dictionary
text_field: Any = typed_content.get("text")
if isinstance(text_field, str):
text_parts.append(text_field)
content_str = "\n".join(text_parts).strip()
else:
logger.warning(
f"(Prepare Prompt) Warning: Unexpected content type for role {role} at index {i} ({type(content)}) or is None."
)
content_str = str(content or "").strip()
if content_str:
current_turn_parts.append(content_str)
# Handle tool calls (visualize only, do not execute actively here to avoid conflict with client execution in conversational loop)
tool_calls = msg.tool_calls
if role == "assistant" and tool_calls:
if content_str:
current_turn_parts.append("\n")
tool_call_visualizations = []
for tool_call in tool_calls:
if hasattr(tool_call, "type") and tool_call.type == "function":
function_call = tool_call.function
func_name = function_call.name if function_call else None
func_args_str = function_call.arguments if function_call else None
try:
parsed_args = json.loads(
func_args_str if func_args_str else "{}"
)
formatted_args = json.dumps(
parsed_args, indent=2, ensure_ascii=False
)
except (json.JSONDecodeError, TypeError):
formatted_args = (
func_args_str if func_args_str is not None else "{}"
)
tool_call_visualizations.append(
f"Request function call: {func_name}\nParameters:\n{formatted_args}"
)
if tool_call_visualizations:
current_turn_parts.append("\n".join(tool_call_visualizations))
# Handle tool result messages (role = 'tool'): include in prompt so model sees tool output
if role == "tool":
tool_result_lines: List[str] = []
# Standard OpenAI style: content is string, tool_call_id associates with previous call
tool_call_id = getattr(msg, "tool_call_id", None)
if tool_call_id:
tool_result_lines.append(f"Tool result (tool_call_id={tool_call_id}):")
if isinstance(msg.content, str):
tool_result_lines.append(msg.content)
elif isinstance(msg.content, list):
# Compatible with few clients putting results in a list
try:
merged_parts: List[str] = []
for it in msg.content:
if isinstance(it, dict):
if it.get("type") == "text":
text_raw = it.get("text", "")
if isinstance(text_raw, str):
merged_parts.append(text_raw)
else:
merged_parts.append(str(text_raw))
else:
merged_parts.append(str(it))
else:
merged_parts.append(str(it))
merged = "\n".join(merged_parts)
tool_result_lines.append(merged)
except Exception:
tool_result_lines.append(str(msg.content))
else:
tool_result_lines.append(str(msg.content))
if tool_result_lines:
if content_str:
current_turn_parts.append("\n")
current_turn_parts.append("\n".join(tool_result_lines))
if len(current_turn_parts) > 1 or (role == "assistant" and tool_calls):
combined_parts.append("".join(current_turn_parts))
elif not combined_parts and not current_turn_parts:
logger.debug(
f"(Prepare Prompt) Skipping empty message for role {role} at index {i} (and no tool calls)."
)
elif len(current_turn_parts) == 1 and not combined_parts:
logger.debug(
f"(Prepare Prompt) Skipping empty message for role {role} at index {i} (prefix only)."
)
final_prompt = "".join(combined_parts)
if final_prompt:
final_prompt += "\n"
# Consolidated English summary (replaces verbose Chinese logs)
sys_indicator = "Yes" if _has_system_prompt else "No"
attach_info = f", {len(files_list)} attachments" if files_list else ""
logger.debug(
f"[Prompt] Built messages: {_msg_count} (System: {sys_indicator}), "
f"Total {len(final_prompt):,} characters{attach_info}"
)
return final_prompt, files_list
|