2024-07-07 01:58:23 +08:00
|
|
|
|
import os
|
2025-09-18 20:34:10 +08:00
|
|
|
|
import traceback
|
2025-09-01 22:37:03 +08:00
|
|
|
|
|
2026-01-28 11:30:55 +08:00
|
|
|
|
from openai import AsyncOpenAI
|
2025-09-20 22:53:37 +08:00
|
|
|
|
from tenacity import before_sleep_log, retry, retry_if_exception_type, stop_after_attempt, wait_exponential
|
2025-09-01 22:37:03 +08:00
|
|
|
|
|
2025-09-18 20:34:10 +08:00
|
|
|
|
from src import config
|
2025-10-11 02:34:42 +08:00
|
|
|
|
from src.utils import logger
|
2025-09-01 22:37:03 +08:00
|
|
|
|
|
2024-07-07 01:58:23 +08:00
|
|
|
|
|
2025-10-14 02:27:15 +08:00
|
|
|
|
def split_model_spec(model_spec, sep="/"):
|
|
|
|
|
|
"""
|
|
|
|
|
|
将 provider/model 形式的字符串拆分为 (provider, model)
|
|
|
|
|
|
"""
|
|
|
|
|
|
if not model_spec or not isinstance(model_spec, str):
|
|
|
|
|
|
return "", ""
|
|
|
|
|
|
if not sep:
|
|
|
|
|
|
return model_spec, ""
|
|
|
|
|
|
try:
|
|
|
|
|
|
provider, model_name = model_spec.split(sep, 1)
|
|
|
|
|
|
return provider, model_name
|
|
|
|
|
|
except ValueError:
|
|
|
|
|
|
return model_spec, ""
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-05-24 11:29:45 +08:00
|
|
|
|
class OpenAIBase:
|
2025-07-02 02:38:36 +08:00
|
|
|
|
def __init__(self, api_key, base_url, model_name, **kwargs):
|
2025-03-10 16:33:40 +08:00
|
|
|
|
self.api_key = api_key
|
|
|
|
|
|
self.base_url = base_url
|
2026-01-28 11:30:55 +08:00
|
|
|
|
self.client = AsyncOpenAI(api_key=api_key, base_url=base_url)
|
2024-07-07 01:58:23 +08:00
|
|
|
|
self.model_name = model_name
|
2025-04-09 11:47:08 +08:00
|
|
|
|
self.info = kwargs
|
2024-07-07 01:58:23 +08:00
|
|
|
|
|
2025-09-19 01:10:20 +08:00
|
|
|
|
@retry(
|
|
|
|
|
|
stop=stop_after_attempt(3),
|
|
|
|
|
|
wait=wait_exponential(multiplier=1, min=1, max=10),
|
|
|
|
|
|
retry=retry_if_exception_type((Exception,)),
|
|
|
|
|
|
before_sleep=before_sleep_log(logger, log_level="WARNING"),
|
2025-09-19 01:32:07 +08:00
|
|
|
|
reraise=True,
|
2025-09-19 01:10:20 +08:00
|
|
|
|
)
|
2026-01-28 11:30:55 +08:00
|
|
|
|
async def call(self, message, stream=False):
|
2024-07-07 01:58:23 +08:00
|
|
|
|
if isinstance(message, str):
|
2025-09-01 22:37:03 +08:00
|
|
|
|
messages = [{"role": "user", "content": message}]
|
2024-07-07 01:58:23 +08:00
|
|
|
|
else:
|
|
|
|
|
|
messages = message
|
|
|
|
|
|
|
2025-05-09 14:48:32 +08:00
|
|
|
|
try:
|
2025-09-19 01:10:20 +08:00
|
|
|
|
if stream:
|
|
|
|
|
|
response = self._stream_response(messages)
|
|
|
|
|
|
else:
|
2026-01-28 11:30:55 +08:00
|
|
|
|
response = await self._get_response(messages)
|
2025-05-09 14:48:32 +08:00
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
2025-09-02 01:08:42 +08:00
|
|
|
|
err = (
|
|
|
|
|
|
f"Error streaming response: {e}, URL: {self.base_url}, "
|
|
|
|
|
|
f"API Key: {self.api_key[:5]}***, Model: {self.model_name}"
|
|
|
|
|
|
)
|
2025-05-09 14:48:32 +08:00
|
|
|
|
logger.error(err)
|
|
|
|
|
|
raise Exception(err)
|
2024-07-07 01:58:23 +08:00
|
|
|
|
|
2025-09-19 01:10:20 +08:00
|
|
|
|
return response
|
|
|
|
|
|
|
2026-01-28 11:30:55 +08:00
|
|
|
|
async def _stream_response(self, messages):
|
|
|
|
|
|
response = await self.client.chat.completions.create(
|
2025-09-19 01:10:20 +08:00
|
|
|
|
model=self.model_name,
|
|
|
|
|
|
messages=messages,
|
|
|
|
|
|
stream=True,
|
|
|
|
|
|
)
|
2026-01-28 11:30:55 +08:00
|
|
|
|
async for chunk in response:
|
2025-09-19 01:10:20 +08:00
|
|
|
|
if len(chunk.choices) > 0:
|
|
|
|
|
|
yield chunk.choices[0].delta
|
|
|
|
|
|
|
2026-01-28 11:30:55 +08:00
|
|
|
|
async def _get_response(self, messages):
|
|
|
|
|
|
response = await self.client.chat.completions.create(
|
2024-07-07 01:58:23 +08:00
|
|
|
|
model=self.model_name,
|
|
|
|
|
|
messages=messages,
|
|
|
|
|
|
stream=False,
|
|
|
|
|
|
)
|
|
|
|
|
|
return response.choices[0].message
|
2025-03-24 19:07:51 +08:00
|
|
|
|
|
2026-01-28 11:30:55 +08:00
|
|
|
|
async def get_models(self):
|
2025-03-10 16:33:40 +08:00
|
|
|
|
try:
|
2026-01-28 11:30:55 +08:00
|
|
|
|
return await self.client.models.list(extra_query={"type": "text"})
|
2025-03-10 16:33:40 +08:00
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.error(f"Error getting models: {e}")
|
|
|
|
|
|
return []
|
2024-07-07 01:58:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
2024-09-09 17:07:03 +08:00
|
|
|
|
class OpenModel(OpenAIBase):
|
|
|
|
|
|
def __init__(self, model_name=None):
|
|
|
|
|
|
model_name = model_name or "gpt-4o-mini"
|
|
|
|
|
|
api_key = os.getenv("OPENAI_API_KEY")
|
|
|
|
|
|
base_url = os.getenv("OPENAI_API_BASE")
|
|
|
|
|
|
super().__init__(api_key=api_key, base_url=base_url, model_name=model_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-07-31 20:22:05 +08:00
|
|
|
|
class GeneralResponse:
|
2024-07-07 17:21:07 +08:00
|
|
|
|
def __init__(self, content):
|
|
|
|
|
|
self.content = content
|
2024-07-31 20:22:05 +08:00
|
|
|
|
self.is_full = False
|
2024-07-07 17:21:07 +08:00
|
|
|
|
|
2025-09-01 22:37:03 +08:00
|
|
|
|
|
2025-10-14 02:27:15 +08:00
|
|
|
|
def select_model(model_provider=None, model_name=None, model_spec=None):
|
2025-09-18 20:34:10 +08:00
|
|
|
|
"""根据模型提供者选择模型"""
|
2025-10-14 02:27:15 +08:00
|
|
|
|
if model_spec:
|
|
|
|
|
|
spec_provider, spec_model_name = split_model_spec(model_spec)
|
|
|
|
|
|
model_provider = model_provider or spec_provider
|
|
|
|
|
|
model_name = model_name or spec_model_name
|
|
|
|
|
|
|
|
|
|
|
|
if model_provider is None or not model_name:
|
|
|
|
|
|
default_provider, default_model = split_model_spec(getattr(config, "default_model", ""))
|
|
|
|
|
|
model_provider = model_provider or default_provider
|
|
|
|
|
|
model_name = model_name or default_model
|
|
|
|
|
|
|
|
|
|
|
|
assert model_provider, "Model provider not specified"
|
|
|
|
|
|
|
2025-10-22 11:51:32 +08:00
|
|
|
|
model_info = config.model_names.get(model_provider)
|
|
|
|
|
|
if not model_info:
|
|
|
|
|
|
raise ValueError(f"Unknown model provider: {model_provider}")
|
|
|
|
|
|
|
|
|
|
|
|
model_name = model_name or model_info.default
|
2025-09-18 20:34:10 +08:00
|
|
|
|
|
2025-10-14 02:27:15 +08:00
|
|
|
|
if not model_name:
|
|
|
|
|
|
raise ValueError(f"Model name not specified for provider {model_provider}")
|
|
|
|
|
|
|
2025-09-18 20:34:10 +08:00
|
|
|
|
logger.info(f"Selecting model from `{model_provider}` with `{model_name}`")
|
|
|
|
|
|
|
|
|
|
|
|
if model_provider == "openai":
|
|
|
|
|
|
return OpenModel(model_name)
|
|
|
|
|
|
|
|
|
|
|
|
# 其他模型,默认使用OpenAIBase
|
|
|
|
|
|
try:
|
|
|
|
|
|
model = OpenAIBase(
|
2025-11-16 21:24:26 +08:00
|
|
|
|
api_key=os.environ.get(model_info.env, model_info.env),
|
2025-10-22 11:51:32 +08:00
|
|
|
|
base_url=model_info.base_url,
|
2025-09-18 20:34:10 +08:00
|
|
|
|
model_name=model_name,
|
|
|
|
|
|
)
|
|
|
|
|
|
return model
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
raise ValueError(f"Model provider {model_provider} load failed, {e} \n {traceback.format_exc()}")
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-09-22 22:02:57 +08:00
|
|
|
|
async def test_chat_model_status(provider: str, model_name: str) -> dict:
|
|
|
|
|
|
"""
|
|
|
|
|
|
测试指定聊天模型的状态
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
|
provider: 模型提供商
|
|
|
|
|
|
model_name: 模型名称
|
|
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
dict: 包含状态信息的字典
|
|
|
|
|
|
"""
|
|
|
|
|
|
try:
|
|
|
|
|
|
# 加载模型
|
|
|
|
|
|
logger.debug(f"Selecting chat model {provider}/{model_name}")
|
|
|
|
|
|
model = select_model(provider, model_name)
|
|
|
|
|
|
|
|
|
|
|
|
# 使用简单的测试消息
|
|
|
|
|
|
test_messages = [{"role": "user", "content": "Say 1"}]
|
|
|
|
|
|
|
|
|
|
|
|
# 发送测试请求
|
2026-01-28 11:30:55 +08:00
|
|
|
|
response = await model.call(test_messages, stream=False)
|
2025-09-22 22:02:57 +08:00
|
|
|
|
logger.debug(f"Test chat model status response: {response}")
|
|
|
|
|
|
|
|
|
|
|
|
# 检查响应是否有效
|
|
|
|
|
|
if response and response.content:
|
|
|
|
|
|
return {"provider": provider, "model_name": model_name, "status": "available", "message": "连接正常"}
|
|
|
|
|
|
else:
|
|
|
|
|
|
return {"provider": provider, "model_name": model_name, "status": "unavailable", "message": "响应无效"}
|
|
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.error(f"测试聊天模型状态失败 {provider}/{model_name}: {e}")
|
|
|
|
|
|
return {"provider": provider, "model_name": model_name, "status": "error", "message": str(e)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def test_all_chat_models_status() -> dict:
|
|
|
|
|
|
"""
|
|
|
|
|
|
测试所有支持的聊天模型状态
|
|
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
dict: 包含所有模型状态的字典
|
|
|
|
|
|
"""
|
|
|
|
|
|
from src import config
|
|
|
|
|
|
|
|
|
|
|
|
results = {}
|
|
|
|
|
|
|
|
|
|
|
|
# 获取所有可用的模型
|
|
|
|
|
|
for provider, provider_info in config.model_names.items():
|
2025-10-10 14:59:12 +08:00
|
|
|
|
# 处理普通模型
|
|
|
|
|
|
for model_name in provider_info.models:
|
|
|
|
|
|
model_id = f"{provider}/{model_name}"
|
|
|
|
|
|
status = await test_chat_model_status(provider, model_name)
|
|
|
|
|
|
results[model_id] = status
|
2025-09-22 22:02:57 +08:00
|
|
|
|
|
|
|
|
|
|
available_count = len([m for m in results.values() if m["status"] == "available"])
|
|
|
|
|
|
|
|
|
|
|
|
return {"models": results, "total": len(results), "available": available_count}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-07-31 20:22:05 +08:00
|
|
|
|
if __name__ == "__main__":
|
2025-05-24 11:29:45 +08:00
|
|
|
|
pass
|