chore: format
This commit is contained in:
@@ -54,7 +54,6 @@ from open_webui.utils.misc import (
|
||||
from open_webui.utils.auth import get_admin_user, get_verified_user
|
||||
from open_webui.utils.headers import include_user_info_headers
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -805,67 +804,77 @@ def convert_to_azure_payload(url, payload: dict, api_version: str):
|
||||
def convert_to_responses_payload(payload: dict) -> dict:
|
||||
"""
|
||||
Convert Chat Completions payload to Responses API format.
|
||||
|
||||
|
||||
Chat Completions: { messages: [{role, content}], ... }
|
||||
Responses API: { input: [{type: "message", role, content: [...]}], instructions: "system" }
|
||||
"""
|
||||
messages = payload.pop("messages", [])
|
||||
|
||||
|
||||
system_content = ""
|
||||
input_items = []
|
||||
|
||||
|
||||
for msg in messages:
|
||||
role = msg.get("role", "user")
|
||||
content = msg.get("content", "")
|
||||
|
||||
|
||||
# Check for stored output items (from previous Responses API turn)
|
||||
stored_output = msg.get("output")
|
||||
if stored_output and isinstance(stored_output, list):
|
||||
input_items.extend(stored_output)
|
||||
continue
|
||||
|
||||
|
||||
if role == "system":
|
||||
if isinstance(content, str):
|
||||
system_content = content
|
||||
elif isinstance(content, list):
|
||||
system_content = "\n".join(p.get("text", "") for p in content if p.get("type") == "text")
|
||||
system_content = "\n".join(
|
||||
p.get("text", "") for p in content if p.get("type") == "text"
|
||||
)
|
||||
continue
|
||||
|
||||
|
||||
# Convert content format
|
||||
text_type = "output_text" if role == "assistant" else "input_text"
|
||||
|
||||
|
||||
if isinstance(content, str):
|
||||
content_parts = [{"type": text_type, "text": content}]
|
||||
elif isinstance(content, list):
|
||||
content_parts = []
|
||||
for part in content:
|
||||
if part.get("type") == "text":
|
||||
content_parts.append({"type": text_type, "text": part.get("text", "")})
|
||||
content_parts.append(
|
||||
{"type": text_type, "text": part.get("text", "")}
|
||||
)
|
||||
elif part.get("type") == "image_url":
|
||||
url_data = part.get("image_url", {})
|
||||
url = url_data.get("url", "") if isinstance(url_data, dict) else url_data
|
||||
url = (
|
||||
url_data.get("url", "")
|
||||
if isinstance(url_data, dict)
|
||||
else url_data
|
||||
)
|
||||
content_parts.append({"type": "input_image", "image_url": url})
|
||||
else:
|
||||
content_parts = [{"type": text_type, "text": str(content)}]
|
||||
|
||||
input_items.append({
|
||||
"type": "message",
|
||||
"role": role,
|
||||
"content": content_parts
|
||||
})
|
||||
|
||||
|
||||
input_items.append({"type": "message", "role": role, "content": content_parts})
|
||||
|
||||
responses_payload = {**payload, "input": input_items}
|
||||
|
||||
|
||||
if system_content:
|
||||
responses_payload["instructions"] = system_content
|
||||
|
||||
|
||||
if "max_tokens" in responses_payload:
|
||||
responses_payload["max_output_tokens"] = responses_payload.pop("max_tokens")
|
||||
|
||||
|
||||
# Remove Chat Completions-only parameters not supported by the Responses API
|
||||
for unsupported_key in ("stream_options", "logit_bias", "frequency_penalty", "presence_penalty", "stop"):
|
||||
for unsupported_key in (
|
||||
"stream_options",
|
||||
"logit_bias",
|
||||
"frequency_penalty",
|
||||
"presence_penalty",
|
||||
"stop",
|
||||
):
|
||||
responses_payload.pop(unsupported_key, None)
|
||||
|
||||
|
||||
# Convert Chat Completions tools format to Responses API format
|
||||
# Chat Completions: {"type": "function", "function": {"name": ..., "description": ..., "parameters": ...}}
|
||||
# Responses API: {"type": "function", "name": ..., "description": ..., "parameters": ...}
|
||||
@@ -888,9 +897,8 @@ def convert_to_responses_payload(payload: dict) -> dict:
|
||||
# Already in correct format or unknown format, pass through
|
||||
converted_tools.append(tool)
|
||||
responses_payload["tools"] = converted_tools
|
||||
|
||||
return responses_payload
|
||||
|
||||
return responses_payload
|
||||
|
||||
|
||||
def convert_responses_result(response: dict) -> dict:
|
||||
@@ -1036,7 +1044,7 @@ async def generate_chat_completion(
|
||||
headers["api-key"] = key
|
||||
|
||||
headers["api-version"] = api_version
|
||||
|
||||
|
||||
if is_responses:
|
||||
payload = convert_to_responses_payload(payload)
|
||||
request_url = f"{request_url}/responses?api-version={api_version}"
|
||||
|
||||
Reference in New Issue
Block a user