From bc0b92cee8c1a8570e8d919833509515ae8e7c42 Mon Sep 17 00:00:00 2001 From: congxi Date: Mon, 8 Apr 2024 14:01:19 +0800 Subject: [PATCH] [feature] Model parameters are preferably configured using yaml files. **Phenomenon and reproduction steps** **Root cause and solution** **Impactions** **Test method** **Affected branch(es)** * main **Checklist** - [ ] Dependencies update required - [ ] Common bug (similar problem in other repo) --- deploy/templates/configmap.yaml | 38 +++++++++++++++++ df-llm-agent/config.py | 6 +++ df-llm-agent/llm_agent_app/llm_agent.py | 54 +++++++++++++++++-------- etc/df-llm-agent.yaml | 37 +++++++++++++++++ 4 files changed, 118 insertions(+), 17 deletions(-) diff --git a/deploy/templates/configmap.yaml b/deploy/templates/configmap.yaml index 694a5d7..a590fed 100644 --- a/deploy/templates/configmap.yaml +++ b/deploy/templates/configmap.yaml @@ -26,3 +26,41 @@ data: host: mysql port: 30130 database: deepflow_llm + ai: + enable: False # True,False + platforms: + - + enable: False + platform: "azure" + model: "gpt" + api_type: "azure" + api_key: "" + api_base: "" + api_version: "" + engine_name: + - "" + - + enable: False + platform: "aliyun" + model: "dashscope" + api_key: "" + engine_name: + - "qwen-turbo" + - "qwen-plus" + - + enable: False + platform: "baidu" + model: "qianfan" + api_key: "" + api_secre: "" + engine_name: + - "ERNIE-Bot" + - "ERNIE-Bot-turbo" + - + enable: False + platform: "zhipu" + model: "zhipuai" + api_key: "" + engine_name: + - "chatglm_turbo" + diff --git a/df-llm-agent/config.py b/df-llm-agent/config.py index 5ed85f6..23e47d1 100644 --- a/df-llm-agent/config.py +++ b/df-llm-agent/config.py @@ -39,6 +39,12 @@ def __init__(self, _yml=None): self.mysql_port = mysql.get('port', 20130) self.mysql_database = mysql.get('database', 'deepflow_llm') + ai = yml.get('ai', {}) + if ai: + enable = ai.get('enable', False) + if enable: + self.platforms = ai.get('platforms', []) + except Exception as e: traceback.print_exc() print("配置文件解析错误: %s" % e) diff --git a/df-llm-agent/llm_agent_app/llm_agent.py b/df-llm-agent/llm_agent_app/llm_agent.py index efed06b..d71e519 100644 --- a/df-llm-agent/llm_agent_app/llm_agent.py +++ b/df-llm-agent/llm_agent_app/llm_agent.py @@ -19,7 +19,7 @@ from utils.curl_tools import curl_tools from utils.tools import generate_uuid - +from config import config from chat_record_app.chat_record import chat_record_worker from langchain_openai import AzureChatOpenAI @@ -209,26 +209,46 @@ async def assistant_base(cls, request, user_info, platform, engine_name, prompt_ data_info = {} # data_info["user_id"] = user_id data_info["platform"] = platform - try: - res_config = await db_models.LlmConfig.filter(**data_info).all() - except Exception as e: - raise BadRequestException("SQL_ERROR", const.SQL_ERROR, f"{e}") engine_config = {} - # key = engine_name 时可能会存在多个配置,设置为当前使用值 - for v in res_config: - v_dict = dict(v) - _key = v_dict["key"] - _value = v_dict["value"] - - if _key == "engine_name": - if _value == engine_name: + + if hasattr(config, "platforms"): + res_config = config.platforms + for _info in res_config: + if _info.get('platform', '') == platform and _info.get('enable', False): + _engine_name = _info.get('engine_name', []) + if engine_name in _engine_name: + _info['engine_name'] = f"{engine_name}" + else: + _info['engine_name'] = '' + + engine_config = _info + + if not engine_config.get("enable", False): + raise BadRequestException("INVALID_PARAMETERS", f"{const.INVALID_PARAMETERS}, 平台: {platform} 未启用") + + else: + try: + res_config = await db_models.LlmConfig.filter(**data_info).all() + except Exception as e: + raise BadRequestException("SQL_ERROR", const.SQL_ERROR, f"{e}") + + # key = engine_name 时可能会存在多个配置,设置为当前使用值 + for v in res_config: + v_dict = dict(v) + _key = v_dict["key"] + _value = v_dict["value"] + + if _key == "engine_name": + if _value == engine_name: + engine_config[_key] = _value + else: engine_config[_key] = _value - else: - engine_config[_key] = _value - if engine_config.get("enable", "") != "1": - raise BadRequestException("INVALID_PARAMETERS", f"{const.INVALID_PARAMETERS}, 引用的引擎未开启: {engine_name}") + if engine_config.get("enable", "") != "1": + raise BadRequestException("INVALID_PARAMETERS", f"{const.INVALID_PARAMETERS}, 平台: {platform} 未启用") + + # print(engine_config, engine_config.get("engine_name"), engine_name) if engine_config.get("engine_name", "") != engine_name: raise BadRequestException("INVALID_PARAMETERS", f"{const.INVALID_PARAMETERS}, 引用的引擎错误: {engine_name}") diff --git a/etc/df-llm-agent.yaml b/etc/df-llm-agent.yaml index d574ae6..9124d6d 100644 --- a/etc/df-llm-agent.yaml +++ b/etc/df-llm-agent.yaml @@ -18,3 +18,40 @@ mysql: host: mysql port: 30130 database: deepflow_llm +ai: + enable: False # True,False + platforms: + - + enable: False + platform: "azure" + model: "gpt" + api_type: "azure" + api_key: "" + api_base: "" + api_version: "" + engine_name: + - "" + - + enable: False + platform: "aliyun" + model: "dashscope" + api_key: "" + engine_name: + - "qwen-turbo" + - "qwen-plus" + - + enable: False + platform: "baidu" + model: "qianfan" + api_key: "" + api_secre: "" + engine_name: + - "ERNIE-Bot" + - "ERNIE-Bot-turbo" + - + enable: False + platform: "zhipu" + model: "zhipuai" + api_key: "" + engine_name: + - "chatglm_turbo" \ No newline at end of file