Spaces:
Runtime error
Runtime error
File size: 24,204 Bytes
5eeb2b8 828be99 5eeb2b8 828be99 5eeb2b8 828be99 5eeb2b8 828be99 5eeb2b8 828be99 5eeb2b8 828be99 5eeb2b8 828be99 5eeb2b8 828be99 5eeb2b8 828be99 5eeb2b8 828be99 5eeb2b8 828be99 5eeb2b8 828be99 5eeb2b8 828be99 5eeb2b8 828be99 5eeb2b8 828be99 5eeb2b8 828be99 5eeb2b8 828be99 5eeb2b8 828be99 5eeb2b8 828be99 5eeb2b8 828be99 5eeb2b8 828be99 5eeb2b8 828be99 5eeb2b8 ea6fec7 5eeb2b8 ea6fec7 5eeb2b8 ea6fec7 5eeb2b8 ea6fec7 5eeb2b8 ea6fec7 5eeb2b8 ea6fec7 5eeb2b8 ea6fec7 5eeb2b8 ea6fec7 5eeb2b8 ea6fec7 5eeb2b8 ea6fec7 5eeb2b8 ea6fec7 5eeb2b8 | 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 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 | from __future__ import annotations
import reprlib
import uuid
from dataclasses import dataclass
from datetime import datetime, timezone
from typing import Any
import orjson
from gemini_webapi.constants import Model
from loguru import logger
from ..models import (
ContentItem,
Message,
ModelData,
ResponseCreateRequest,
ResponseCreateResponse,
ResponseImageGenerationCall,
ResponseInputContent,
ResponseInputItem,
ResponseOutputContent,
ResponseOutputMessage,
ResponseReasoning,
ResponseSummaryPart,
ResponseToolCall,
ResponseToolChoice,
ResponseUsage,
Tool,
ToolChoiceFunction,
)
from ..utils import g_config
from ..utils.helper import (
TOOL_HINT_STRIPPED,
TOOL_WRAP_HINT,
estimate_tokens,
extract_tool_calls,
remove_tool_call_blocks,
)
@dataclass
class StructuredOutputRequirement:
"""Represents a structured response requirement derived from response_format."""
schema_name: str
schema: dict[str, Any]
instruction: str
raw_format: dict[str, Any]
def calculate_usage(
messages: list[Message],
assistant_text: str | None,
tool_calls: list[Any] | None,
thoughts: str | None = None,
) -> tuple[int, int, int, int]:
"""Calculate prompt, completion, total and reasoning tokens consistently."""
prompt_tokens = 0
for msg in messages:
if isinstance(msg.content, str):
prompt_tokens += estimate_tokens(msg.content)
elif isinstance(msg.content, list):
text_parts: list[str] = []
for item in msg.content:
if isinstance(item, dict):
if item.get("type") == "text":
text_val = item.get("text")
if isinstance(text_val, str) and text_val:
text_parts.append(text_val)
continue
if getattr(item, "type", "") == "text":
text_val = getattr(item, "text", None)
if isinstance(text_val, str) and text_val:
text_parts.append(text_val)
prompt_tokens += estimate_tokens("\n".join(text_parts))
tool_args_text = ""
if tool_calls:
for call in tool_calls:
if hasattr(call, "function"):
tool_args_text += call.function.arguments or ""
elif isinstance(call, dict):
tool_args_text += call.get("function", {}).get("arguments", "")
completion_basis = assistant_text or ""
if tool_args_text:
completion_basis = (
f"{completion_basis}\n{tool_args_text}" if completion_basis else tool_args_text
)
completion_tokens = estimate_tokens(completion_basis)
reasoning_tokens = estimate_tokens(thoughts) if thoughts else 0
total_completion_tokens = completion_tokens + reasoning_tokens
return (
prompt_tokens,
total_completion_tokens,
prompt_tokens + total_completion_tokens,
reasoning_tokens,
)
def create_responses_standard_payload(
response_id: str,
created_time: int,
model_name: str,
detected_tool_calls: list[Any] | None,
image_call_items: list[ResponseImageGenerationCall],
response_contents: list[ResponseOutputContent],
usage: ResponseUsage,
request: ResponseCreateRequest,
normalized_input: Any,
full_thoughts: str | None = None,
) -> ResponseCreateResponse:
"""Build a standard Responses API payload."""
message_id = f"msg_{uuid.uuid4().hex[:24]}"
reason_id = f"rs_{uuid.uuid4().hex[:24]}"
output_items: list[Any] = []
if full_thoughts:
output_items.append(
ResponseReasoning(
id=reason_id,
type="reasoning",
status="completed",
summary=[ResponseSummaryPart(type="summary_text", text=full_thoughts)],
)
)
output_items.append(
ResponseOutputMessage(
id=message_id,
type="message",
status="completed",
role="assistant",
content=response_contents,
)
)
if detected_tool_calls:
output_items.extend(
[
ResponseToolCall(
id=call.id if hasattr(call, "id") else call["id"],
type="tool_call",
status="completed",
function=call.function if hasattr(call, "function") else call["function"],
)
for call in detected_tool_calls
]
)
output_items.extend(image_call_items)
return ResponseCreateResponse(
id=response_id,
object="response",
created_at=created_time,
model=model_name,
output=output_items,
status="completed",
usage=usage,
input=normalized_input or None,
metadata=request.metadata or {},
tools=request.tools or [],
tool_choice=request.tool_choice or "auto",
)
def create_chat_completion_standard_payload(
completion_id: str,
created_time: int,
model_name: str,
visible_output: str | None,
tool_calls_payload: list[dict[str, Any]] | None,
finish_reason: str,
usage: dict[str, Any],
reasoning_content: str | None = None,
) -> dict[str, Any]:
"""Build a standard OpenAI chat completion payload."""
return {
"id": completion_id,
"object": "chat.completion",
"created": created_time,
"model": model_name,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": visible_output or None,
"tool_calls": tool_calls_payload or None,
"reasoning_content": reasoning_content or None,
},
"finish_reason": finish_reason,
}
],
"usage": usage,
}
def process_llm_output(
thoughts: str | None,
raw_text: str,
structured_requirement: StructuredOutputRequirement | None,
) -> tuple[str | None, str, str, list[Any]]:
"""
Post-process Gemini output to extract tool calls and prepare clean text.
Returns: (thoughts, visible_text, storage_output, tool_calls)
"""
if thoughts:
thoughts = thoughts.strip()
visible_output, tool_calls = extract_tool_calls(raw_text)
if tool_calls:
logger.debug(f"Detected {len(tool_calls)} tool call(s) in model output.")
visible_output = visible_output.strip()
storage_output = remove_tool_call_blocks(raw_text)
storage_output = storage_output.strip()
if structured_requirement and visible_output:
try:
structured_payload = orjson.loads(visible_output)
canonical_output = orjson.dumps(structured_payload).decode("utf-8")
visible_output = canonical_output
storage_output = canonical_output
logger.debug(
f"Structured response fulfilled (schema={structured_requirement.schema_name})."
)
except orjson.JSONDecodeError:
logger.warning(
f"Failed to decode JSON for structured response (schema={structured_requirement.schema_name})."
)
return thoughts, visible_output, storage_output, tool_calls
def build_structured_requirement(
response_format: dict[str, Any] | None,
) -> StructuredOutputRequirement | None:
"""Translate OpenAI-style response_format into internal instructions."""
if not response_format or not isinstance(response_format, dict):
return None
if response_format.get("type") != "json_schema":
logger.warning(
f"Unsupported response_format type requested: {reprlib.repr(response_format)}"
)
return None
json_schema = response_format.get("json_schema")
if not isinstance(json_schema, dict):
logger.warning(
f"Invalid json_schema payload in response_format: {reprlib.repr(response_format)}"
)
return None
schema = json_schema.get("schema")
if not isinstance(schema, dict):
logger.warning(
f"Missing `schema` object in response_format payload: {reprlib.repr(response_format)}"
)
return None
schema_name = json_schema.get("name") or "response"
strict = json_schema.get("strict", True)
pretty_schema = orjson.dumps(schema, option=orjson.OPT_SORT_KEYS).decode("utf-8")
instruction_parts = [
"You must respond with a single valid JSON document that conforms to the schema shown below.",
"Do not include explanations, comments, or any text before or after the JSON.",
f'Schema name: "{schema_name}"',
"JSON Schema:",
pretty_schema,
]
if not strict:
instruction_parts.insert(
1,
"The schema allows unspecified fields, but include only what is necessary "
"to satisfy the user's request.",
)
instruction = "\n\n".join(instruction_parts)
return StructuredOutputRequirement(
schema_name=schema_name,
schema=schema,
instruction=instruction,
raw_format=response_format,
)
def build_tool_prompt(
tools: list[Tool],
tool_choice: str | ToolChoiceFunction | None,
) -> str:
"""Generate a system prompt describing available tools."""
if not tools:
return ""
lines: list[str] = [
"You can invoke the following developer tools. "
"Call a tool only when it is required and follow the JSON schema exactly "
"when providing arguments."
]
for tool in tools:
function = tool.function
description = function.description or "No description provided."
lines.append(f"Tool `{function.name}`: {description}")
if function.parameters:
schema_text = orjson.dumps(
function.parameters,
option=orjson.OPT_SORT_KEYS,
).decode("utf-8")
lines.append("Arguments JSON schema:")
lines.append(schema_text)
else:
lines.append("Arguments JSON schema: {}")
if tool_choice == "none":
lines.append(
"For this request you must not call any tool. "
"Provide the best possible natural language answer."
)
elif tool_choice == "required":
lines.append(
"You must call at least one tool before responding to the user. "
"Do not provide a final user-facing answer until a tool call has been issued."
)
elif isinstance(tool_choice, ToolChoiceFunction):
target = tool_choice.function.name
lines.append(
f"You are required to call the tool named `{target}`. Do not call any other tool."
)
lines.append(
"When you decide to call a tool you MUST respond with nothing except a single "
"[function_calls] block exactly like the template below."
)
lines.append("Do not add text before or after the block.")
lines.append("[function_calls]")
lines.append('[call:tool_name]{"argument": "value"}[/call]')
lines.append("[/function_calls]")
lines.append(
"Use double quotes for JSON keys and values. CRITICAL: The content inside "
"[call:...]...[/call] MUST be a raw JSON object. Do not wrap it in ```json "
"blocks or add any conversational text inside the tag."
)
lines.append(
"To call multiple tools, list each [call:tool_name]...[/call] entry sequentially "
"within a single [function_calls] block."
)
lines.append(
"If no tool call is needed, provide a normal response and DO NOT use the "
"[function_calls] tag."
)
return "\n".join(lines)
def build_image_generation_instruction(
tools: list[Any] | None,
tool_choice: ResponseToolChoice | None,
) -> str | None:
"""Construct explicit guidance so Gemini emits images when requested."""
has_forced_choice = tool_choice is not None and tool_choice.type == "image_generation"
primary = tools[0] if tools else None
if not has_forced_choice and primary is None:
return None
instructions: list[str] = [
"Image generation is enabled. When the user requests an image, you must return "
"an actual generated image, not a text description.",
"For new image requests, generate at least one new image matching the description.",
"If the user provides an image and asks for edits or variations, return a newly "
"generated image with the requested changes.",
"Avoid all text replies unless a short caption is explicitly requested. "
"Do not explain, apologize, or describe image creation steps.",
"Never send placeholder text like 'Here is your image' or any other response "
"without an actual image attachment.",
]
if primary:
primary_model = getattr(primary, "model", None)
primary_format = getattr(primary, "output_format", None)
if primary_model:
instructions.append(
f"Where styles differ, favor the `{primary_model}` image model when rendering the scene."
)
if primary_format:
instructions.append(
f"Encode the image using the `{primary_format}` format whenever possible."
)
if has_forced_choice:
instructions.append(
"Image generation was explicitly requested. You must return at least one "
"generated image. Any response without an image will be treated as a failure."
)
return "\n\n".join(instructions)
def append_tool_hint_to_last_user_message(messages: list[Message]) -> None:
"""Ensure the last user message carries the tool-call formatting hint."""
for msg in reversed(messages):
if msg.role != "user" or msg.content is None:
continue
if isinstance(msg.content, str):
if TOOL_HINT_STRIPPED not in msg.content:
msg.content = f"{msg.content}\n{TOOL_WRAP_HINT}"
return
if isinstance(msg.content, list):
for part in reversed(msg.content):
if isinstance(part, dict):
if part.get("type") != "text":
continue
text_value = part.get("text") or ""
if TOOL_HINT_STRIPPED in str(text_value):
return
part["text"] = f"{text_value}\n{TOOL_WRAP_HINT}"
return
if getattr(part, "type", None) != "text":
continue
text_value = getattr(part, "text", "") or ""
if TOOL_HINT_STRIPPED in text_value:
return
setattr(part, "text", f"{text_value}\n{TOOL_WRAP_HINT}")
return
msg.content.append(ContentItem(type="text", text=TOOL_WRAP_HINT.strip())) # type: ignore[arg-type]
return
def prepare_messages_for_model(
source_messages: list[Message],
tools: list[Tool] | None,
tool_choice: str | ToolChoiceFunction | None,
extra_instructions: list[str] | None = None,
inject_system_defaults: bool = True,
) -> list[Message]:
"""Return a deep-copied message list enriched with internal instructions."""
prepared = [msg.model_copy(deep=True) for msg in source_messages]
tool_id_to_name: dict[str, str] = {}
for msg in prepared:
if msg.role == "assistant" and msg.tool_calls:
for tc in msg.tool_calls:
tool_id_to_name[tc.id] = tc.function.name
for msg in prepared:
if msg.role == "tool" and not msg.name and msg.tool_call_id:
msg.name = tool_id_to_name.get(msg.tool_call_id)
instructions: list[str] = []
if inject_system_defaults:
if tools:
tool_prompt = build_tool_prompt(tools, tool_choice)
if tool_prompt:
instructions.append(tool_prompt)
if extra_instructions:
instructions.extend(instr for instr in extra_instructions if instr)
logger.debug(
f"Applied {len(extra_instructions)} extra instructions for tool/structured output."
)
if not instructions:
if tools and tool_choice != "none":
append_tool_hint_to_last_user_message(prepared)
return prepared
combined_instructions = "\n\n".join(instructions)
if prepared and prepared[0].role == "system" and isinstance(prepared[0].content, str):
existing = prepared[0].content or ""
if combined_instructions not in existing:
separator = "\n\n" if existing else ""
prepared[0].content = f"{existing}{separator}{combined_instructions}"
else:
prepared.insert(0, Message(role="system", content=combined_instructions))
if tools and tool_choice != "none":
append_tool_hint_to_last_user_message(prepared)
return prepared
def response_items_to_messages(
items: str | list[ResponseInputItem],
) -> tuple[list[Message], str | list[ResponseInputItem]]:
"""Convert Responses API input items into internal Message objects."""
messages: list[Message] = []
if isinstance(items, str):
messages.append(Message(role="user", content=items))
logger.debug("Normalized Responses input: single string message.")
return messages, items
normalized_input: list[ResponseInputItem] = []
for item in items:
role = item.role
content = item.content
normalized_contents: list[ResponseInputContent] = []
if isinstance(content, str):
normalized_contents.append(ResponseInputContent(type="input_text", text=content))
messages.append(Message(role=role, content=content))
else:
converted: list[ContentItem] = []
for part in content:
if part.type == "input_text":
text_value = part.text or ""
normalized_contents.append(
ResponseInputContent(type="input_text", text=text_value)
)
if text_value:
converted.append(ContentItem(type="text", text=text_value))
elif part.type == "input_image":
image_url = part.image_url
if image_url:
normalized_contents.append(
ResponseInputContent(
type="input_image",
image_url=image_url,
detail=part.detail if part.detail else "auto",
)
)
converted.append(
ContentItem(
type="image_url",
image_url={
"url": image_url,
"detail": part.detail if part.detail else "auto",
},
)
) # type: ignore[arg-type]
elif part.type == "input_file":
if part.file_url or part.file_data:
normalized_contents.append(part)
file_info: dict[str, str | None] = {}
if part.file_data:
file_info["file_data"] = part.file_data
file_info["filename"] = part.filename
if part.file_url:
file_info["url"] = part.file_url
converted.append(ContentItem(type="file", file=file_info)) # type: ignore[arg-type]
messages.append(Message(role=role, content=converted or None)) # type: ignore[arg-type, assignment]
normalized_input.append(
ResponseInputItem(
type="message",
role=item.role,
content=normalized_contents or [],
)
)
logger.debug(f"Normalized Responses input: {len(normalized_input)} message items.")
return messages, normalized_input
def instructions_to_messages(
instructions: str | list[ResponseInputItem] | None,
) -> list[Message]:
"""Normalize a Responses API instructions payload into Message objects."""
if not instructions:
return []
if isinstance(instructions, str):
return [Message(role="system", content=instructions)]
instruction_messages: list[Message] = []
for item in instructions:
if item.type and item.type != "message":
continue
role = item.role
content = item.content
if isinstance(content, str):
instruction_messages.append(Message(role=role, content=content))
continue
converted: list[ContentItem] = []
for part in content:
if part.type == "input_text":
text_value = part.text or ""
if text_value:
converted.append(ContentItem(type="text", text=text_value))
elif part.type == "input_image":
image_url = part.image_url
if image_url:
converted.append(
ContentItem(
type="image_url",
image_url={
"url": image_url,
"detail": part.detail if part.detail else "auto",
},
)
) # type: ignore[arg-type]
elif part.type == "input_file":
file_info: dict[str, str | None] = {}
if part.file_data:
file_info["file_data"] = part.file_data
file_info["filename"] = part.filename
if part.file_url:
file_info["url"] = part.file_url
if file_info:
converted.append(ContentItem(type="file", file=file_info)) # type: ignore[arg-type]
instruction_messages.append(Message(role=role, content=converted or None)) # type: ignore[arg-type, assignment]
return instruction_messages
def get_default_model_name() -> str:
"""Return the configured default model used by the server."""
return g_config.gemini.default_model.strip() or "gemini-3.0-pro"
def get_model_by_name(name: str) -> Model:
"""Resolve a configured or built-in model by name."""
strategy = g_config.gemini.model_strategy
custom_models = {m.model_name: m for m in g_config.gemini.models if m.model_name}
if name in custom_models:
return Model.from_dict(custom_models[name].model_dump())
if strategy == "overwrite":
raise ValueError(f"Model '{name}' not found in custom models (strategy='overwrite').")
return Model.from_name(name)
def get_available_models() -> list[ModelData]:
"""Return visible models according to the configured model strategy."""
now = int(datetime.now(tz=timezone.utc).timestamp())
strategy = g_config.gemini.model_strategy
models_data: list[ModelData] = []
custom_models = [m for m in g_config.gemini.models if m.model_name]
for model_cfg in custom_models:
models_data.append(
ModelData(
id=str(model_cfg.model_name),
created=now,
owned_by="custom",
)
)
if strategy == "append":
custom_ids = {m.model_name for m in custom_models}
for model in Model:
model_name = model.model_name
if not model_name or model_name == "unspecified":
continue
if model_name in custom_ids:
continue
models_data.append(
ModelData(
id=model_name,
created=now,
owned_by="gemini-web",
)
)
return models_data
|