diff --git a/api/apps/llm_app.py b/api/apps/llm_app.py index f3aebb1749f..e85a6679d58 100644 --- a/api/apps/llm_app.py +++ b/api/apps/llm_app.py @@ -106,7 +106,7 @@ def set_api_key(): @manager.route('/add_llm', methods=['POST']) @login_required -@validate_request("llm_factory", "llm_name", "model_type") +@validate_request("llm_factory") def add_llm(): req = request.json factory = req["llm_factory"] @@ -120,6 +120,11 @@ def add_llm(): api_key = '{' + f'"volc_ak": "{req.get("volc_ak", "")}", ' \ f'"volc_sk": "{req.get("volc_sk", "")}", ' \ f'"ep_id": "{endpoint_id}", ' + '}' + elif factory == "Tencent Hunyuan": + api_key = '{' + f'"hunyuan_sid": "{req.get("hunyuan_sid", "")}", ' \ + f'"hunyuan_sk": "{req.get("hunyuan_sk", "")}"' + '}' + req["api_key"] = api_key + return set_api_key() elif factory == "Bedrock": # For Bedrock, due to its special authentication method # Assemble bedrock_ak, bedrock_sk, bedrock_region @@ -132,7 +137,7 @@ def add_llm(): api_key = "xxxxxxxxxxxxxxx" elif factory == "OpenAI-API-Compatible": llm_name = req["llm_name"]+"___OpenAI-API" - api_key = req.get("api_key","xxxxxxxxxxxxxxx") + api_key = req.get("api_key","xxxxxxxxxxxxxxx") else: llm_name = req["llm_name"] api_key = req.get("api_key","xxxxxxxxxxxxxxx") diff --git a/conf/llm_factories.json b/conf/llm_factories.json index be1065c8e1c..53bedab60bd 100644 --- a/conf/llm_factories.json +++ b/conf/llm_factories.json @@ -3120,6 +3120,44 @@ "tags": "LLM,TEXT EMBEDDING", "status": "1", "llm": [] + }, + { + "name": "Tencent Hunyuan", + "logo": "", + "tags": "LLM,IMAGE2TEXT", + "status": "1", + "llm": [ + { + "llm_name": "hunyuan-pro", + "tags": "LLM,CHAT,32k", + "max_tokens": 32768, + "model_type": "chat" + }, + { + "llm_name": "hunyuan-standard", + "tags": "LLM,CHAT,32k", + "max_tokens": 32768, + "model_type": "chat" + }, + { + "llm_name": "hunyuan-standard-256K", + "tags": "LLM,CHAT,256k", + "max_tokens": 262144, + "model_type": "chat" + }, + { + "llm_name": "hunyuan-lite", + "tags": "LLM,CHAT,256k", + "max_tokens": 262144, + "model_type": "chat" + }, + { + "llm_name": "hunyuan-vision", + "tags": "LLM,IMAGE2TEXT,8k", + "max_tokens": 8192, + "model_type": "image2text" + } + ] } ] } diff --git a/rag/llm/__init__.py b/rag/llm/__init__.py index 142fc60de32..6bf9f96ec50 100644 --- a/rag/llm/__init__.py +++ b/rag/llm/__init__.py @@ -63,7 +63,8 @@ "StepFun":StepFunCV, "OpenAI-API-Compatible": OpenAI_APICV, "TogetherAI": TogetherAICV, - "01.AI": YiCV + "01.AI": YiCV, + "Tencent Hunyuan": HunyuanCV } @@ -98,7 +99,8 @@ "novita.ai": NovitaAIChat, "SILICONFLOW": SILICONFLOWChat, "01.AI": YiChat, - "Replicate": ReplicateChat + "Replicate": ReplicateChat, + "Tencent Hunyuan": HunyuanChat } diff --git a/rag/llm/chat_model.py b/rag/llm/chat_model.py index 5e338338cf4..75832f7c81c 100644 --- a/rag/llm/chat_model.py +++ b/rag/llm/chat_model.py @@ -1088,3 +1088,83 @@ def chat_streamly(self, system, history, gen_conf): yield ans + "\n**ERROR**: " + str(e) yield num_tokens_from_string(ans) + + +class HunyuanChat(Base): + def __init__(self, key, model_name, base_url=None): + from tencentcloud.common import credential + from tencentcloud.hunyuan.v20230901 import hunyuan_client + + key = json.loads(key) + sid = key.get("hunyuan_sid", "") + sk = key.get("hunyuan_sk", "") + cred = credential.Credential(sid, sk) + self.model_name = model_name + self.client = hunyuan_client.HunyuanClient(cred, "") + + def chat(self, system, history, gen_conf): + from tencentcloud.hunyuan.v20230901 import models + from tencentcloud.common.exception.tencent_cloud_sdk_exception import ( + TencentCloudSDKException, + ) + + _gen_conf = {} + _history = [{k.capitalize(): v for k, v in item.items() } for item in history] + if system: + _history.insert(0, {"Role": "system", "Content": system}) + if "temperature" in gen_conf: + _gen_conf["Temperature"] = gen_conf["temperature"] + if "top_p" in gen_conf: + _gen_conf["TopP"] = gen_conf["top_p"] + + req = models.ChatCompletionsRequest() + params = {"Model": self.model_name, "Messages": _history, **_gen_conf} + req.from_json_string(json.dumps(params)) + ans = "" + try: + response = self.client.ChatCompletions(req) + ans = response.Choices[0].Message.Content + return ans, response.Usage.TotalTokens + except TencentCloudSDKException as e: + return ans + "\n**ERROR**: " + str(e), 0 + + def chat_streamly(self, system, history, gen_conf): + from tencentcloud.hunyuan.v20230901 import models + from tencentcloud.common.exception.tencent_cloud_sdk_exception import ( + TencentCloudSDKException, + ) + + _gen_conf = {} + _history = [{k.capitalize(): v for k, v in item.items() } for item in history] + if system: + _history.insert(0, {"Role": "system", "Content": system}) + + if "temperature" in gen_conf: + _gen_conf["Temperature"] = gen_conf["temperature"] + if "top_p" in gen_conf: + _gen_conf["TopP"] = gen_conf["top_p"] + req = models.ChatCompletionsRequest() + params = { + "Model": self.model_name, + "Messages": _history, + "Stream": True, + **_gen_conf, + } + req.from_json_string(json.dumps(params)) + ans = "" + total_tokens = 0 + try: + response = self.client.ChatCompletions(req) + for resp in response: + resp = json.loads(resp["data"]) + if not resp["Choices"] or not resp["Choices"][0]["Delta"]["Content"]: + continue + ans += resp["Choices"][0]["Delta"]["Content"] + total_tokens += 1 + + yield ans + + except TencentCloudSDKException as e: + yield ans + "\n**ERROR**: " + str(e) + + yield total_tokens diff --git a/rag/llm/cv_model.py b/rag/llm/cv_model.py index 73d2ffbb027..65f73d00ba4 100644 --- a/rag/llm/cv_model.py +++ b/rag/llm/cv_model.py @@ -664,4 +664,56 @@ class YiCV(GptV4): def __init__(self, key, model_name, lang="Chinese",base_url="https://api.lingyiwanwu.com/v1",): if not base_url: base_url = "https://api.lingyiwanwu.com/v1" - super().__init__(key, model_name,lang,base_url) \ No newline at end of file + super().__init__(key, model_name,lang,base_url) + + +class HunyuanCV(Base): + def __init__(self, key, model_name, lang="Chinese",base_url=None): + from tencentcloud.common import credential + from tencentcloud.hunyuan.v20230901 import hunyuan_client + + key = json.loads(key) + sid = key.get("hunyuan_sid", "") + sk = key.get("hunyuan_sk", "") + cred = credential.Credential(sid, sk) + self.model_name = model_name + self.client = hunyuan_client.HunyuanClient(cred, "") + self.lang = lang + + def describe(self, image, max_tokens=4096): + from tencentcloud.hunyuan.v20230901 import models + from tencentcloud.common.exception.tencent_cloud_sdk_exception import ( + TencentCloudSDKException, + ) + + b64 = self.image2base64(image) + req = models.ChatCompletionsRequest() + params = {"Model": self.model_name, "Messages": self.prompt(b64)} + req.from_json_string(json.dumps(params)) + ans = "" + try: + response = self.client.ChatCompletions(req) + ans = response.Choices[0].Message.Content + return ans, response.Usage.TotalTokens + except TencentCloudSDKException as e: + return ans + "\n**ERROR**: " + str(e), 0 + + def prompt(self, b64): + return [ + { + "Role": "user", + "Contents": [ + { + "Type": "image_url", + "ImageUrl": { + "Url": f"data:image/jpeg;base64,{b64}" + }, + }, + { + "Type": "text", + "Text": "请用中文详细描述一下图中的内容,比如时间,地点,人物,事情,人物心情等,如果有数据请提取出数据。" if self.lang.lower() == "chinese" else + "Please describe the content of this picture, like where, when, who, what happen. If it has number data, please extract them out.", + }, + ], + } + ] \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index f7de9a36734..3dd67ceda5a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -76,6 +76,7 @@ Shapely==2.0.5 six==1.16.0 StrEnum==0.4.15 tabulate==0.9.0 +tencentcloud-sdk-python==3.0.1215 tika==2.6.0 tiktoken==0.6.0 torch==2.3.0 diff --git a/requirements_arm.txt b/requirements_arm.txt index 9b684a8a2c9..b03166fcdc7 100644 --- a/requirements_arm.txt +++ b/requirements_arm.txt @@ -115,6 +115,7 @@ six==1.16.0 sniffio==1.3.1 StrEnum==0.4.15 sympy==1.12 +tencentcloud-sdk-python==3.0.1215 threadpoolctl==3.3.0 tika==2.6.0 tiktoken==0.6.0 diff --git a/web/src/assets/svg/llm/hunyuan.svg b/web/src/assets/svg/llm/hunyuan.svg new file mode 100644 index 00000000000..43a78d0077a --- /dev/null +++ b/web/src/assets/svg/llm/hunyuan.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/web/src/locales/en.ts b/web/src/locales/en.ts index 0bd4aa914fd..1ede459ef18 100644 --- a/web/src/locales/en.ts +++ b/web/src/locales/en.ts @@ -521,6 +521,10 @@ The above is the content you need to summarize.`, 'eu-central-1': 'Europe (Frankfurt)', 'us-gov-west-1': 'AWS GovCloud (US-West)', 'ap-southeast-2': 'Asia Pacific (Sydney)', + addHunyuanSID: 'Hunyuan Secret ID', + HunyuanSIDMessage: 'Please input your Secret ID', + addHunyuanSK: 'Hunyuan Secret Key', + HunyuanSKMessage: 'Please input your Secret Key', }, message: { registered: 'Registered!', diff --git a/web/src/locales/zh-traditional.ts b/web/src/locales/zh-traditional.ts index 8df77f654a0..85eb7e934b0 100644 --- a/web/src/locales/zh-traditional.ts +++ b/web/src/locales/zh-traditional.ts @@ -484,6 +484,10 @@ export default { 'eu-central-1': '歐洲 (法蘭克福)', 'us-gov-west-1': 'AWS GovCloud (US-West)', 'ap-southeast-2': '亞太地區 (雪梨)', + addHunyuanSID: '混元 Secret ID', + HunyuanSIDMessage: '請輸入 Secret ID', + addHunyuanSK: '混元 Secret Key', + HunyuanSKMessage: '請輸入 Secret Key', }, message: { registered: '註冊成功', diff --git a/web/src/locales/zh.ts b/web/src/locales/zh.ts index 698ef1d8ae0..f9fd7c9f837 100644 --- a/web/src/locales/zh.ts +++ b/web/src/locales/zh.ts @@ -501,6 +501,10 @@ export default { 'eu-central-1': '欧洲 (法兰克福)', 'us-gov-west-1': 'AWS GovCloud (US-West)', 'ap-southeast-2': '亚太地区 (悉尼)', + addHunyuanSID: '混元 Secret ID', + HunyuanSIDMessage: '请输入 Secret ID', + addHunyuanSK: '混元 Secret Key', + HunyuanSKMessage: '请输入 Secret Key', }, message: { registered: '注册成功', diff --git a/web/src/pages/user-setting/setting-model/constant.ts b/web/src/pages/user-setting/setting-model/constant.ts index 3b59364e6ed..5036345190e 100644 --- a/web/src/pages/user-setting/setting-model/constant.ts +++ b/web/src/pages/user-setting/setting-model/constant.ts @@ -30,8 +30,9 @@ export const IconMap = { Upstage: 'upstage', 'novita.ai': 'novita-ai', SILICONFLOW: 'siliconflow', - "01.AI": 'yi', - "Replicate": 'replicate' + '01.AI': 'yi', + Replicate: 'replicate', + 'Tencent Hunyuan': 'hunyuan', }; export const BedrockRegionList = [ diff --git a/web/src/pages/user-setting/setting-model/hooks.ts b/web/src/pages/user-setting/setting-model/hooks.ts index eb349712264..96c96a5dd48 100644 --- a/web/src/pages/user-setting/setting-model/hooks.ts +++ b/web/src/pages/user-setting/setting-model/hooks.ts @@ -163,6 +163,33 @@ export const useSubmitVolcEngine = () => { }; }; +export const useSubmitHunyuan = () => { + const { addLlm, loading } = useAddLlm(); + const { + visible: HunyuanAddingVisible, + hideModal: hideHunyuanAddingModal, + showModal: showHunyuanAddingModal, + } = useSetModalState(); + + const onHunyuanAddingOk = useCallback( + async (payload: IAddLlmRequestBody) => { + const ret = await addLlm(payload); + if (ret === 0) { + hideHunyuanAddingModal(); + } + }, + [hideHunyuanAddingModal, addLlm], + ); + + return { + HunyuanAddingLoading: loading, + onHunyuanAddingOk, + HunyuanAddingVisible, + hideHunyuanAddingModal, + showHunyuanAddingModal, + }; +}; + export const useSubmitBedrock = () => { const { addLlm, loading } = useAddLlm(); const { diff --git a/web/src/pages/user-setting/setting-model/hunyuan-modal/index.tsx b/web/src/pages/user-setting/setting-model/hunyuan-modal/index.tsx new file mode 100644 index 00000000000..baecfdd6596 --- /dev/null +++ b/web/src/pages/user-setting/setting-model/hunyuan-modal/index.tsx @@ -0,0 +1,78 @@ +import { useTranslate } from '@/hooks/common-hooks'; +import { IModalProps } from '@/interfaces/common'; +import { IAddLlmRequestBody } from '@/interfaces/request/llm'; +import { Form, Input, Modal, Select } from 'antd'; +import omit from 'lodash/omit'; + +type FieldType = IAddLlmRequestBody & { + vision: boolean; + hunyuan_sid: string; + hunyuan_sk: string; +}; + +const { Option } = Select; + +const HunyuanModal = ({ + visible, + hideModal, + onOk, + loading, + llmFactory, +}: IModalProps & { llmFactory: string }) => { + const [form] = Form.useForm(); + + const { t } = useTranslate('setting'); + + const handleOk = async () => { + const values = await form.validateFields(); + const modelType = + values.model_type === 'chat' && values.vision + ? 'image2text' + : values.model_type; + + const data = { + ...omit(values, ['vision']), + model_type: modelType, + llm_factory: llmFactory, + }; + console.info(data); + + onOk?.(data); + }; + + return ( + +
+ + label={t('addHunyuanSID')} + name="hunyuan_sid" + rules={[{ required: true, message: t('HunyuanSIDMessage') }]} + > + + + + label={t('addHunyuanSK')} + name="hunyuan_sk" + rules={[{ required: true, message: t('HunyuanSKMessage') }]} + > + + + +
+ ); +}; + +export default HunyuanModal; diff --git a/web/src/pages/user-setting/setting-model/index.tsx b/web/src/pages/user-setting/setting-model/index.tsx index 73e6e2e4094..ecdc62ad945 100644 --- a/web/src/pages/user-setting/setting-model/index.tsx +++ b/web/src/pages/user-setting/setting-model/index.tsx @@ -34,10 +34,12 @@ import { useHandleDeleteLlm, useSubmitApiKey, useSubmitBedrock, + useSubmitHunyuan, useSubmitOllama, useSubmitSystemModelSetting, useSubmitVolcEngine, } from './hooks'; +import HunyuanModal from './hunyuan-modal'; import styles from './index.less'; import OllamaModal from './ollama-modal'; import SystemModelSettingModal from './system-model-setting-modal'; @@ -88,7 +90,9 @@ const ModelCard = ({ item, clickApiKey }: IModelCardProps) => {