mirror of
https://github.com/Alishahryar1/free-claude-code.git
synced 2026-07-03 14:05:26 +02:00
56 lines
1.9 KiB
Python
56 lines
1.9 KiB
Python
"""Request builder for Cerebras Inference (OpenAI-compatible chat completions).
|
|
|
|
Docs: https://inference-docs.cerebras.ai/resources/openai — use ``max_completion_tokens``
|
|
in API examples; non-standard fields via ``extra_body`` with the OpenAI client.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from loguru import logger
|
|
|
|
from core.anthropic import ReasoningReplayMode, build_base_request_body
|
|
from core.anthropic.conversion import OpenAIConversionError
|
|
from providers.exceptions import InvalidRequestError
|
|
|
|
|
|
def _normalize_max_completion_tokens(body: dict[str, Any]) -> None:
|
|
if "max_completion_tokens" in body:
|
|
body.pop("max_tokens", None)
|
|
return
|
|
if "max_tokens" in body and body["max_tokens"] is not None:
|
|
body["max_completion_tokens"] = body.pop("max_tokens")
|
|
|
|
|
|
def build_request_body(request_data: Any, *, thinking_enabled: bool) -> dict:
|
|
"""Build OpenAI-format request body from an Anthropic request for Cerebras."""
|
|
logger.debug(
|
|
"CEREBRAS_REQUEST: conversion start model={} msgs={}",
|
|
getattr(request_data, "model", "?"),
|
|
len(getattr(request_data, "messages", [])),
|
|
)
|
|
try:
|
|
body = build_base_request_body(
|
|
request_data,
|
|
reasoning_replay=ReasoningReplayMode.REASONING_CONTENT
|
|
if thinking_enabled
|
|
else ReasoningReplayMode.DISABLED,
|
|
)
|
|
except OpenAIConversionError as exc:
|
|
raise InvalidRequestError(str(exc)) from exc
|
|
|
|
request_extra = getattr(request_data, "extra_body", None)
|
|
if isinstance(request_extra, dict) and request_extra:
|
|
body["extra_body"] = dict(request_extra)
|
|
|
|
_normalize_max_completion_tokens(body)
|
|
|
|
logger.debug(
|
|
"CEREBRAS_REQUEST: conversion done model={} msgs={} tools={}",
|
|
body.get("model"),
|
|
len(body.get("messages", [])),
|
|
len(body.get("tools", [])),
|
|
)
|
|
return body
|