38 lines
1.3 KiB
Python
38 lines
1.3 KiB
Python
from __future__ import annotations
|
|
from typing import Any
|
|
import dashscope
|
|
from dashscope.common.error import (
|
|
AuthenticationError,
|
|
InvalidParameter,
|
|
RequestFailure,
|
|
ServiceUnavailableError,
|
|
TimeoutException,
|
|
)
|
|
from src.ai_client import ProviderError
|
|
|
|
def build_dashscope_tools(openai_tools: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
|
out: list[dict[str, Any]] = []
|
|
for t in openai_tools:
|
|
if t.get("type") != "function":
|
|
continue
|
|
fn = t.get("function", {})
|
|
out.append({
|
|
"name": fn.get("name", ""),
|
|
"description": fn.get("description", ""),
|
|
"parameters": fn.get("parameters", {"type": "object", "properties": {}}),
|
|
})
|
|
return out
|
|
|
|
def classify_dashscope_error(exc: Exception) -> ProviderError:
|
|
if isinstance(exc, AuthenticationError):
|
|
return ProviderError(kind="auth", provider="qwen", original=exc)
|
|
if isinstance(exc, TimeoutException):
|
|
return ProviderError(kind="network", provider="qwen", original=exc)
|
|
if isinstance(exc, ServiceUnavailableError):
|
|
return ProviderError(kind="network", provider="qwen", original=exc)
|
|
if isinstance(exc, InvalidParameter):
|
|
return ProviderError(kind="quota", provider="qwen", original=exc)
|
|
if isinstance(exc, RequestFailure):
|
|
return ProviderError(kind="network", provider="qwen", original=exc)
|
|
return ProviderError(kind="unknown", provider="qwen", original=exc)
|