Merge origin/main into fix/structured-retry-classification-main

Made-with: Cursor
This commit is contained in:
Xubin Ren
2026-04-06 08:28:20 +00:00
108 changed files with 7719 additions and 1495 deletions
+4 -3
View File
@@ -13,7 +13,6 @@ from collections.abc import Awaitable, Callable
from typing import Any
import json_repair
from loguru import logger
from nanobot.providers.base import LLMProvider, LLMResponse, ToolCallRequest
@@ -356,8 +355,9 @@ class AnthropicProvider(LLMProvider):
# Prompt caching
# ------------------------------------------------------------------
@staticmethod
@classmethod
def _apply_cache_control(
cls,
system: str | list[dict[str, Any]],
messages: list[dict[str, Any]],
tools: list[dict[str, Any]] | None,
@@ -384,7 +384,8 @@ class AnthropicProvider(LLMProvider):
new_tools = tools
if tools:
new_tools = list(tools)
new_tools[-1] = {**new_tools[-1], "cache_control": marker}
for idx in cls._tool_cache_marker_indices(new_tools):
new_tools[idx] = {**new_tools[idx], "cache_control": marker}
return system, new_msgs, new_tools
+35 -3
View File
@@ -61,7 +61,7 @@ class LLMResponse:
error_code: str | None = None # Provider/code semantic, e.g. rate_limit_exceeded.
error_retry_after_s: float | None = None
error_should_retry: bool | None = None
@property
def has_tool_calls(self) -> bool:
"""Check if response contains tool calls."""
@@ -201,6 +201,38 @@ class LLMProvider(ABC):
result.append(msg)
return result
@staticmethod
def _tool_name(tool: dict[str, Any]) -> str:
"""Extract tool name from either OpenAI or Anthropic-style tool schemas."""
name = tool.get("name")
if isinstance(name, str):
return name
fn = tool.get("function")
if isinstance(fn, dict):
fname = fn.get("name")
if isinstance(fname, str):
return fname
return ""
@classmethod
def _tool_cache_marker_indices(cls, tools: list[dict[str, Any]]) -> list[int]:
"""Return cache marker indices: builtin/MCP boundary and tail index."""
if not tools:
return []
tail_idx = len(tools) - 1
last_builtin_idx: int | None = None
for i in range(tail_idx, -1, -1):
if not cls._tool_name(tools[i]).startswith("mcp_"):
last_builtin_idx = i
break
ordered_unique: list[int] = []
for idx in (last_builtin_idx, tail_idx):
if idx is not None and idx not in ordered_unique:
ordered_unique.append(idx)
return ordered_unique
@staticmethod
def _sanitize_request_messages(
messages: list[dict[str, Any]],
@@ -228,7 +260,7 @@ class LLMProvider(ABC):
) -> LLMResponse:
"""
Send a chat completion request.
Args:
messages: List of message dicts with 'role' and 'content'.
tools: Optional list of tool definitions.
@@ -236,7 +268,7 @@ class LLMProvider(ABC):
max_tokens: Maximum tokens in response.
temperature: Sampling temperature.
tool_choice: Tool selection strategy ("auto", "required", or specific tool dict).
Returns:
LLMResponse with content and/or tool calls.
"""
+55 -7
View File
@@ -5,6 +5,7 @@ from __future__ import annotations
import asyncio
import email.utils
import hashlib
import importlib.util
import os
import secrets
import string
@@ -14,7 +15,17 @@ from collections.abc import Awaitable, Callable
from typing import TYPE_CHECKING, Any
import json_repair
from openai import AsyncOpenAI
if os.environ.get("LANGFUSE_SECRET_KEY") and importlib.util.find_spec("langfuse"):
from langfuse.openai import AsyncOpenAI
else:
if os.environ.get("LANGFUSE_SECRET_KEY"):
import logging
logging.getLogger(__name__).warning(
"LANGFUSE_SECRET_KEY is set but langfuse is not installed; "
"install with `pip install langfuse` to enable tracing"
)
from openai import AsyncOpenAI
from nanobot.providers.base import LLMProvider, LLMResponse, ToolCallRequest
@@ -23,6 +34,7 @@ if TYPE_CHECKING:
_ALLOWED_MSG_KEYS = frozenset({
"role", "content", "tool_calls", "tool_call_id", "name",
"reasoning_content", "extra_content",
})
_ALNUM = string.ascii_letters + string.digits
@@ -154,8 +166,9 @@ class OpenAICompatProvider(LLMProvider):
resolved = env_val.replace("{api_key}", api_key).replace("{api_base}", effective_base)
os.environ.setdefault(env_name, resolved)
@staticmethod
@classmethod
def _apply_cache_control(
cls,
messages: list[dict[str, Any]],
tools: list[dict[str, Any]] | None,
) -> tuple[list[dict[str, Any]], list[dict[str, Any]] | None]:
@@ -183,7 +196,8 @@ class OpenAICompatProvider(LLMProvider):
new_tools = tools
if tools:
new_tools = list(tools)
new_tools[-1] = {**new_tools[-1], "cache_control": cache_marker}
for idx in cls._tool_cache_marker_indices(new_tools):
new_tools[idx] = {**new_tools[idx], "cache_control": cache_marker}
return new_messages, new_tools
@staticmethod
@@ -224,6 +238,21 @@ class OpenAICompatProvider(LLMProvider):
# Build kwargs
# ------------------------------------------------------------------
@staticmethod
def _supports_temperature(
model_name: str,
reasoning_effort: str | None = None,
) -> bool:
"""Return True when the model accepts a temperature parameter.
GPT-5 family and reasoning models (o1/o3/o4) reject temperature
when reasoning_effort is set to anything other than ``"none"``.
"""
if reasoning_effort and reasoning_effort.lower() != "none":
return False
name = model_name.lower()
return not any(token in name for token in ("gpt-5", "o1", "o3", "o4"))
def _build_kwargs(
self,
messages: list[dict[str, Any]],
@@ -248,9 +277,13 @@ class OpenAICompatProvider(LLMProvider):
kwargs: dict[str, Any] = {
"model": model_name,
"messages": self._sanitize_messages(self._sanitize_empty_content(messages)),
"temperature": temperature,
}
# GPT-5 and reasoning models (o1/o3/o4) reject temperature when
# reasoning_effort is active. Only include it when safe.
if self._supports_temperature(model_name, reasoning_effort):
kwargs["temperature"] = temperature
if spec and getattr(spec, "supports_max_completion_tokens", False):
kwargs["max_completion_tokens"] = max(1, max_tokens)
else:
@@ -266,6 +299,24 @@ class OpenAICompatProvider(LLMProvider):
if reasoning_effort:
kwargs["reasoning_effort"] = reasoning_effort
# Provider-specific thinking parameters.
# Only sent when reasoning_effort is explicitly configured so that
# the provider default is preserved otherwise.
if spec and reasoning_effort is not None:
thinking_enabled = reasoning_effort.lower() != "minimal"
extra: dict[str, Any] | None = None
if spec.name == "dashscope":
extra = {"enable_thinking": thinking_enabled}
elif spec.name in (
"volcengine", "volcengine_coding_plan",
"byteplus", "byteplus_coding_plan",
):
extra = {
"thinking": {"type": "enabled" if thinking_enabled else "disabled"}
}
if extra:
kwargs.setdefault("extra_body", {}).update(extra)
if tools:
kwargs["tools"] = tools
kwargs["tool_choice"] = tool_choice or "auto"
@@ -740,9 +791,6 @@ class OpenAICompatProvider(LLMProvider):
break
chunks.append(chunk)
if on_content_delta and chunk.choices:
text = getattr(chunk.choices[0].delta, "reasoning_content", None)
if text:
await on_content_delta(text)
text = getattr(chunk.choices[0].delta, "content", None)
if text:
await on_content_delta(text)
+10
View File
@@ -200,6 +200,7 @@ PROVIDERS: tuple[ProviderSpec, ...] = (
env_key="OPENAI_API_KEY",
display_name="OpenAI",
backend="openai_compat",
supports_max_completion_tokens=True,
),
# OpenAI Codex: OAuth-based, dedicated provider
ProviderSpec(
@@ -348,6 +349,15 @@ PROVIDERS: tuple[ProviderSpec, ...] = (
backend="openai_compat",
default_api_base="https://api.groq.com/openai/v1",
),
# Qianfan (百度千帆): OpenAI-compatible API
ProviderSpec(
name="qianfan",
keywords=("qianfan", "ernie"),
env_key="QIANFAN_API_KEY",
display_name="Qianfan",
backend="openai_compat",
default_api_base="https://qianfan.baidubce.com/v2"
),
)
+31 -1
View File
@@ -1,4 +1,4 @@
"""Voice transcription provider using Groq."""
"""Voice transcription providers (Groq and OpenAI Whisper)."""
import os
from pathlib import Path
@@ -7,6 +7,36 @@ import httpx
from loguru import logger
class OpenAITranscriptionProvider:
"""Voice transcription provider using OpenAI's Whisper API."""
def __init__(self, api_key: str | None = None):
self.api_key = api_key or os.environ.get("OPENAI_API_KEY")
self.api_url = "https://api.openai.com/v1/audio/transcriptions"
async def transcribe(self, file_path: str | Path) -> str:
if not self.api_key:
logger.warning("OpenAI API key not configured for transcription")
return ""
path = Path(file_path)
if not path.exists():
logger.error("Audio file not found: {}", file_path)
return ""
try:
async with httpx.AsyncClient() as client:
with open(path, "rb") as f:
files = {"file": (path.name, f), "model": (None, "whisper-1")}
headers = {"Authorization": f"Bearer {self.api_key}"}
response = await client.post(
self.api_url, headers=headers, files=files, timeout=60.0,
)
response.raise_for_status()
return response.json().get("text", "")
except Exception as e:
logger.error("OpenAI transcription error: {}", e)
return ""
class GroqTranscriptionProvider:
"""
Voice transcription provider using Groq's Whisper API.