34 lines
971 B
Python
34 lines
971 B
Python
from abc import ABC, abstractmethod
|
|
from typing import Dict, Any, Generator, Union, Tuple
|
|
import requests
|
|
|
|
class ModelService(ABC):
|
|
"""大模型服务基类"""
|
|
|
|
@abstractmethod
|
|
def get_api_config(self) -> Tuple[str, str]:
|
|
"""获取 API URL 和 Key"""
|
|
pass
|
|
|
|
@abstractmethod
|
|
def calculate_cost(self, usage: Dict[str, Any] = None, stream: bool = False) -> float:
|
|
"""计算费用"""
|
|
pass
|
|
|
|
@abstractmethod
|
|
def check_balance(self, balance: float) -> Tuple[bool, float, str]:
|
|
"""检查余额是否充足
|
|
Returns: (is_sufficient, estimated_cost, error_message)
|
|
"""
|
|
pass
|
|
|
|
@abstractmethod
|
|
def prepare_payload(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
|
"""准备请求载荷"""
|
|
pass
|
|
|
|
@abstractmethod
|
|
def handle_response(self, response: requests.Response, stream: bool) -> Union[Dict[str, Any], Generator]:
|
|
"""处理响应"""
|
|
pass
|