Merge pull request #148 from xerrors/model-call-updata

更新模型调用的问题,并新增 openrouter 的支持
This commit is contained in:
Wenjie Zhang 2025-04-10 11:45:38 +08:00 committed by GitHub
commit 3a67a930d9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 55 additions and 32 deletions

View File

@ -118,6 +118,7 @@ def chat_post(
@chat.post("/call")
async def call(query: str = Body(...), meta: dict = Body(None)):
meta = meta or {}
model = select_model(model_provider=meta.get("model_provider"), model_name=meta.get("model_name"))
async def predict_async(query):
loop = asyncio.get_event_loop()
@ -129,7 +130,7 @@ async def call(query: str = Body(...), meta: dict = Body(None)):
return {"response": response.content}
@chat.post("/call_lite")
async def call(query: str = Body(...), meta: dict = Body(None)):
async def call_lite(query: str = Body(...), meta: dict = Body(None)):
meta = meta or {}
async def predict_async(query):
loop = asyncio.get_event_loop()

View File

@ -99,11 +99,12 @@ class Config(SimpleConfig):
except FileNotFoundError:
_models_private = {}
_models = {**_models, **_models_private}
# 修改为按照子元素合并
# _models = {**_models, **_models_private}
self.model_names = _models["MODEL_NAMES"]
self.embed_model_names = _models["EMBED_MODEL_INFO"]
self.reranker_names = _models["RERANKER_LIST"]
self.model_names = {**_models["MODEL_NAMES"], **_models_private["MODEL_NAMES"]}
self.embed_model_names = {**_models["EMBED_MODEL_INFO"], **_models_private["EMBED_MODEL_INFO"]}
self.reranker_names = {**_models["RERANKER_LIST"], **_models_private["RERANKER_LIST"]}
def _save_models_to_file(self):
_models = {

View File

@ -1,5 +1,5 @@
import os
import traceback
from src import config
from src.utils.logging_config import logger
from src.models.chat_model import OpenAIBase
@ -14,33 +14,23 @@ def select_model(model_provider=None, model_name=None):
logger.info(f"Selecting model from `{model_provider}` with `{model_name}`")
if model_provider in [
"deepseek",
"ark",
"siliconflow",
"zhipu",
"lingyiwanwu",
"together.ai",
]:
return OpenAIBase(
api_key=os.getenv(model_info["env"][0]),
base_url=model_info["base_url"],
model_name=model_name,
)
elif model_provider == "qianfan":
if model_provider is None:
raise ValueError("Model provider not specified, please modify `model_provider` in `src/config/base.yaml`")
if model_provider == "qianfan":
from src.models.chat_model import Qianfan
return Qianfan(model_name)
elif model_provider == "dashscope":
if model_provider == "dashscope":
from src.models.chat_model import DashScope
return DashScope(model_name)
elif model_provider == "openai":
if model_provider == "openai":
from src.models.chat_model import OpenModel
return OpenModel(model_name)
elif model_provider == "custom":
if model_provider == "custom":
model_info = next((x for x in config.custom_models if x["custom_id"] == model_name), None)
if model_info is None:
raise ValueError(f"Model {model_name} not found in custom models")
@ -48,7 +38,13 @@ def select_model(model_provider=None, model_name=None):
from src.models.chat_model import CustomModel
return CustomModel(model_info)
elif model_provider is None:
raise ValueError("Model provider not specified, please modify `model_provider` in `src/config/base.yaml`")
else:
raise ValueError(f"Model provider {model_provider} not supported")
# 其他模型默认使用OpenAIBase
try:
model = OpenAIBase(
api_key=os.getenv(model_info["env"][0]),
base_url=model_info["base_url"],
model_name=model_name,
)
return model
except Exception as e:
raise ValueError(f"Model provider {model_provider} load failed, {e} \n {traceback.format_exc()}")

View File

@ -99,6 +99,7 @@ MODEL_NAMES:
- doubao-1-5-pro-32k-250115
- doubao-1-5-lite-32k-250115
- deepseek-r1-250120
lingyiwanwu:
name: 零一万物
url: https://platform.lingyiwanwu.com/docs#%E6%A8%A1%E5%9E%8B%E4%B8%8E%E8%AE%A1%E8%B4%B9
@ -109,6 +110,24 @@ MODEL_NAMES:
models:
- yi-lightning
openrouter:
name: OpenRouter
url: https://openrouter.ai/models
base_url: https://openrouter.ai/api/v1
default: openai/gpt-4o
env:
- OPENROUTER_API_KEY
models:
- openai/gpt-4o
- openai/gpt-4o-mini
- google/gemini-2.5-pro-exp-03-25:free
- x-ai/grok-3-beta
- meta-llama/llama-4-maverick
- meta-llama/llama-4-maverick:free
- anthropic/claude-3.7-sonnet
- anthropic/claude-3.7-sonnet:thinking
EMBED_MODEL_INFO:
local/BAAI/bge-m3:
name: BAAI/bge-m3

Binary file not shown.

After

Width:  |  Height:  |  Size: 93 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

View File

@ -7,7 +7,8 @@ import zhipuIcon from '@/assets/providers/zhipuai.png'
import siliconflowIcon from '@/assets/providers/siliconflow.png'
import arkIcon from '@/assets/providers/ark.png'
import lingyiwanwuIcon from '@/assets/providers/lingyiwanwu.png'
import openrouterIcon from '@/assets/providers/openrouterai.png'
import defaultIcon from '@/assets/providers/default.png'
export const modelIcons = {
openai: openaiIcon,
@ -19,5 +20,7 @@ export const modelIcons = {
siliconflow: siliconflowIcon,
ark: arkIcon,
'together.ai': togetherIcon,
lingyiwanwu: lingyiwanwuIcon
lingyiwanwu: lingyiwanwuIcon,
openrouter: openrouterIcon,
default: defaultIcon // 添加默认图标
}

View File

@ -162,7 +162,7 @@
<div class="card-header" @click="toggleExpand(item)">
<!-- <div v-if="modelStatus[item]" class="success"></div> -->
<div :class="{'model-icon': true, 'available': modelStatus[item]}">
<img :src="modelIcons[item]" alt="模型图标">
<img :src="modelIcons[item] || modelIcons.default" alt="模型图标">
</div>
<div class="model-title-container">
<h3>{{ modelNames[item].name }}</h3>
@ -203,7 +203,7 @@
<div class="model-provider-card" v-for="(item, key) in notModelKeys" :key="key">
<div class="card-header">
<div class="model-icon">
<img :src="modelIcons[item]" alt="模型图标">
<img :src="modelIcons[item] || modelIcons.default" alt="模型图标">
</div>
<div class="model-title-container">
<h3 style="font-weight: 400">{{ modelNames[item].name }}</h3>
@ -266,7 +266,10 @@
</div>
<div v-if="providerConfig.allModels.length === 0" class="modal-no-models">
<a-alert v-if="!modelStatus[providerConfig.provider]" type="warning" message="请在 src/.env 中配置对应的 APIKEY并重新启动服务" />
<a-alert v-else type="warning" message="该提供商暂未适配获取模型列表的方法,如果需要添加模型,请在 src/static/models.private.yml 中添加。" />
<div v-else>
<a-alert type="warning" message="该提供商暂未适配获取模型列表的方法,如果需要添加模型,请在 src/static/models.private.yml 中添加。" />
<img src="@/assets/pics/guides/how-to-add-models.png" alt="添加模型指引" style="width: 100%; height: 100%;">
</div>
</div>
</div>
</div>