feat(ai): Add Qwen adapter for DashScope provider
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
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
|
||||
|
||||
dashscope.common.error.InvalidApiKey = dashscope.common.error.AuthenticationError
|
||||
|
||||
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="rate_limit", provider="qwen", original=exc)
|
||||
if isinstance(exc, ServiceUnavailableError):
|
||||
return ProviderError(kind="quota", 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)
|
||||
Reference in New Issue
Block a user