RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
add support for Tencent Hunyuan (#2015)
黄腾6f438e0a4970
diff --git a/api/apps/llm_app.py b/api/apps/llm_app.pyindex f3aebb1749..e85a6679d5 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.jsonfactory = 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.jsonindex 44ebda5505..a6e4225188 100644--- a/conf/llm_factories.json+++ b/conf/llm_factories.json@@ -3156,6 +3156,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__.pyindex 142fc60de3..6bf9f96ec5 100644--- a/rag/llm/__init__.py+++ b/rag/llm/__init__.py@@ -63,7 +63,8 @@ CvModel = {"StepFun":StepFunCV,"OpenAI-API-Compatible": OpenAI_APICV,"TogetherAI": TogetherAICV,- "01.AI": YiCV+ "01.AI": YiCV,+ "Tencent Hunyuan": HunyuanCV}@@ -98,7 +99,8 @@ ChatModel = {"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.pyindex 5e338338cf..75832f7c81 100644--- a/rag/llm/chat_model.py+++ b/rag/llm/chat_model.py@@ -1088,3 +1088,83 @@ class ReplicateChat(Base):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_tokensdiff --git a/rag/llm/cv_model.py b/rag/llm/cv_model.pyindex 73d2ffbb02..65f73d00ba 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 filediff --git a/requirements.txt b/requirements.txtindex f7de9a3673..3dd67ceda5 100644--- a/requirements.txt+++ b/requirements.txt@@ -76,6 +76,7 @@ Shapely==2.0.5six==1.16.0StrEnum==0.4.15tabulate==0.9.0+tencentcloud-sdk-python==3.0.1215tika==2.6.0tiktoken==0.6.0torch==2.3.0diff --git a/requirements_arm.txt b/requirements_arm.txtindex 9b684a8a2c..b03166fcdc 100644--- a/requirements_arm.txt+++ b/requirements_arm.txt@@ -115,6 +115,7 @@ six==1.16.0sniffio==1.3.1StrEnum==0.4.15sympy==1.12+tencentcloud-sdk-python==3.0.1215threadpoolctl==3.3.0tika==2.6.0tiktoken==0.6.0diff --git a/web/src/assets/svg/llm/hunyuan.svg b/web/src/assets/svg/llm/hunyuan.svgnew file mode 100644index 0000000000..43a78d0077--- /dev/null+++ b/web/src/assets/svg/llm/hunyuan.svg@@ -0,0 +1 @@+<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1724045693347" class="icon" viewBox="0 0 1032 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4302" xmlns:xlink="http://www.w3.org/1999/xlink" width="201.5625" height="200"><path d="M3.796984 0h1018.143079v1018.143079h-1018.143079z" fill="#CCCCCC" fill-opacity="0" p-id="4303"></path><path d="M448.476277 1016.17573l-13.151015-498.465883c-26.514143-9.720085 80.597691-135.736502 124.563442-124.61647 128.917065-32.400282 269.060217-237.58793 13.204043-389.757898-774.159938 0-689.33059 951.857722-124.61647 1012.840251z" fill="#B3DDF2" p-id="4304"></path><path d="M446.021067 1015.942405c-230.673041-53.028285-456.425058-375.800853-180.29617-665.504981 63.421829-55.902418-63.262744-220.942351-167.039099-129.919299-275.725873 381.3264 86.908057 796.458332 347.335269 795.42428z" fill="#0055E9" p-id="4305"></path><path d="M408.827028 536.27505c0-2.651414-6.363394 4.576341 0 0z m0 0v39.771214c0 5.302829-7.42396 40.566638-10.605657 55.679699l39.771214 84.845257s26.514143 7.954243 29.165557 10.605657 15.643344 4.507404 21.211314 5.302829l55.6797 34.468385 92.799499-13.257071 119.313642-37.1198 111.359399-95.450914 34.468386-58.331113 29.165557-148.479199-5.302829-68.936771v-60.982529l-60.982528-111.359399-31.816971-39.771214-55.6797-50.376871-47.725457-29.165557-13.257071-7.954243-15.908486-5.302828-29.165557-10.605657-92.799499-23.862729c201.507484 66.285357 217.41597 270.444255 159.084856 368.546583s-184.713426 154.736537-307.564055 143.176371l-21.211314 18.5599z" fill="#00BCFF" p-id="4306"></path><path d="M461.828799 1016.207546c180.29617 47.698943 575.192508-124.425569 570.054068-493.163053 13.267677-164.414199-140.196181-491.471452-389.757898-503.768711 278.218202 56.660723 384.508097 567.720823 23.915757 694.670538-180.338593 45.074043-246.608041-92.799499-233.324456-190.901827-185.535365 9.375401-283.955862 407.209506 29.112529 493.163053z" fill="#0055DF" p-id="4307"></path></svg>\ No newline at end of filediff --git a/web/src/locales/en.ts b/web/src/locales/en.tsindex ec07adddf1..45ca820feb 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.tsindex 85428d85e1..237b5edeae 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.tsindex 3be6fe802f..265475cd55 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.tsindex 3b59364e6e..5036345190 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.tsindex eb34971226..96c96a5dd4 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.tsxnew file mode 100644index 0000000000..baecfdd659--- /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<IAddLlmRequestBody> & { llmFactory: string }) => {+ const [form] = Form.useForm<FieldType>();++ 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 (+ <Modal+ title={t('addLlmTitle', { name: llmFactory })}+ open={visible}+ onOk={handleOk}+ onCancel={hideModal}+ okButtonProps={{ loading }}+ confirmLoading={loading}+ >+ <Form+ name="basic"+ style={{ maxWidth: 600 }}+ autoComplete="off"+ layout={'vertical'}+ form={form}+ >+ <Form.Item<FieldType>+ label={t('addHunyuanSID')}+ name="hunyuan_sid"+ rules={[{ required: true, message: t('HunyuanSIDMessage') }]}+ >+ <Input placeholder={t('HunyuanSIDMessage')} />+ </Form.Item>+ <Form.Item<FieldType>+ label={t('addHunyuanSK')}+ name="hunyuan_sk"+ rules={[{ required: true, message: t('HunyuanSKMessage') }]}+ >+ <Input placeholder={t('HunyuanSKMessage')} />+ </Form.Item>+ </Form>+ </Modal>+ );+};++export default HunyuanModal;diff --git a/web/src/pages/user-setting/setting-model/index.tsx b/web/src/pages/user-setting/setting-model/index.tsxindex 73e6e2e409..ecdc62ad94 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) => {<Col span={12} className={styles.factoryOperationWrapper}><Space size={'middle'}><Button onClick={handleApiKeyClick}>- {isLocalLlmFactory(item.name) || item.name === 'VolcEngine'+ {isLocalLlmFactory(item.name) ||+ item.name === 'VolcEngine' ||+ item.name === 'Tencent Hunyuan'? t('addTheModel'): 'API-Key'}<SettingOutlined />@@ -162,6 +166,14 @@ const UserSettingModel = () => {volcAddingLoading,} = useSubmitVolcEngine();+ const {+ HunyuanAddingVisible,+ hideHunyuanAddingModal,+ showHunyuanAddingModal,+ onHunyuanAddingOk,+ HunyuanAddingLoading,+ } = useSubmitHunyuan();+const {bedrockAddingLoading,onBedrockAddingOk,@@ -174,8 +186,9 @@ const UserSettingModel = () => {() => ({Bedrock: showBedrockAddingModal,VolcEngine: showVolcAddingModal,+ 'Tencent Hunyuan': showHunyuanAddingModal,}),- [showBedrockAddingModal, showVolcAddingModal],+ [showBedrockAddingModal, showVolcAddingModal, showHunyuanAddingModal],);const handleAddModel = useCallback(@@ -286,6 +299,13 @@ const UserSettingModel = () => {loading={volcAddingLoading}llmFactory={'VolcEngine'}></VolcEngineModal>+ <HunyuanModal+ visible={HunyuanAddingVisible}+ hideModal={hideHunyuanAddingModal}+ onOk={onHunyuanAddingOk}+ loading={HunyuanAddingLoading}+ llmFactory={'Tencent Hunyuan'}+ ></HunyuanModal><BedrockModalvisible={bedrockAddingVisible}hideModal={hideBedrockAddingModal}