Skip to content

Commit

Permalink
Merge pull request #999 from RockChinQ/feat/lm-studio
Browse files Browse the repository at this point in the history
feat: add supports for LM Studio
  • Loading branch information
RockChinQ authored Feb 1, 2025
2 parents e4518eb + d0eeb2b commit 9fc7af1
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 3 deletions.
23 changes: 23 additions & 0 deletions pkg/core/migrations/m022_lmstudio_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from __future__ import annotations

from .. import migration


@migration.migration_class("lmstudio-config", 22)
class LmStudioConfigMigration(migration.Migration):
"""迁移"""

async def need_migrate(self) -> bool:
"""判断当前环境是否需要运行此迁移"""

return 'lmstudio-chat-completions' not in self.ap.provider_cfg.data['requester']

async def run(self):
"""执行迁移"""
self.ap.provider_cfg.data['requester']['lmstudio-chat-completions'] = {
"base-url": "http://127.0.0.1:1234/v1",
"args": {},
"timeout": 120
}

await self.ap.provider_cfg.dump_config()
2 changes: 1 addition & 1 deletion pkg/core/stages/migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from ..migrations import m005_deepseek_cfg_completion, m006_vision_config, m007_qcg_center_url, m008_ad_fixwin_config_migrate, m009_msg_truncator_cfg
from ..migrations import m010_ollama_requester_config, m011_command_prefix_config, m012_runner_config, m013_http_api_config, m014_force_delay_config
from ..migrations import m015_gitee_ai_config, m016_dify_service_api, m017_dify_api_timeout_params, m018_xai_config, m019_zhipuai_config
from ..migrations import m020_wecom_config, m021_lark_config
from ..migrations import m020_wecom_config, m021_lark_config, m022_lmstudio_config


@stage.stage_class("MigrationStage")
Expand Down
4 changes: 2 additions & 2 deletions pkg/provider/modelmgr/modelmgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from ...core import app

from . import token
from .requesters import chatcmpl, anthropicmsgs, moonshotchatcmpl, deepseekchatcmpl, ollamachat, giteeaichatcmpl, xaichatcmpl, zhipuaichatcmpl
from .requesters import chatcmpl, anthropicmsgs, moonshotchatcmpl, deepseekchatcmpl, ollamachat, giteeaichatcmpl, xaichatcmpl, zhipuaichatcmpl, lmstudiochatcmpl

FETCH_MODEL_LIST_URL = "https://api.qchatgpt.rockchin.top/api/v2/fetch/model_list"

Expand Down Expand Up @@ -109,4 +109,4 @@ async def initialize(self):
self.model_list.append(model_info)

except Exception as e:
self.ap.logger.error(f"初始化模型 {model['name']} 失败: {e} ,请检查配置文件")
self.ap.logger.error(f"初始化模型 {model['name']} 失败: {type(e)} {e} ,请检查配置文件")
21 changes: 21 additions & 0 deletions pkg/provider/modelmgr/requesters/lmstudiochatcmpl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from __future__ import annotations

import openai

from . import chatcmpl
from .. import requester
from ....core import app


@requester.requester_class("lmstudio-chat-completions")
class LmStudioChatCompletions(chatcmpl.OpenAIChatCompletions):
"""LMStudio ChatCompletion API 请求器"""

client: openai.AsyncClient

requester_cfg: dict

def __init__(self, ap: app.Application):
self.ap = ap

self.requester_cfg = self.ap.provider_cfg.data['requester']['lmstudio-chat-completions']
5 changes: 5 additions & 0 deletions templates/provider.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@
"base-url": "https://open.bigmodel.cn/api/paas/v4",
"args": {},
"timeout": 120
},
"lmstudio-chat-completions": {
"base-url": "http://127.0.0.1:1234/v1",
"args": {},
"timeout": 120
}
},
"model": "gpt-4o",
Expand Down
20 changes: 20 additions & 0 deletions templates/schema/provider.json
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,26 @@
"default": 120
}
}
},
"lmstudio-chat-completions": {
"type": "object",
"title": "LMStudio API 请求配置",
"description": "仅可编辑 URL 和 超时时间,额外请求参数不支持可视化编辑,请到编辑器编辑",
"properties": {
"base-url": {
"type": "string",
"title": "API URL"
},
"args": {
"type": "object",
"default": {}
},
"timeout": {
"type": "number",
"title": "API 请求超时时间",
"default": 120
}
}
}
}
},
Expand Down

0 comments on commit 9fc7af1

Please sign in to comment.