add support for Tencent Hunyuan (#2015)

### What problem does this PR solve?

#1853 

### Type of change


- [X] New Feature (non-breaking change which adds functionality)

Co-authored-by: Zhedong Cen <cenzhedong2@126.com>
This commit is contained in:
黄腾 2024-08-20 15:27:13 +08:00 committed by GitHub
parent 5efb3476f2
commit 6f438e0a49
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 327 additions and 9 deletions

View File

@ -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

View File

@ -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"
}
]
}
]
}

View File

@ -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
}

View File

@ -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_tokens

View File

@ -665,3 +665,55 @@ class YiCV(GptV4):
if not base_url:
base_url = "https://api.lingyiwanwu.com/v1"
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.",
},
],
}
]

View File

@ -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

View File

@ -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

View File

@ -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>

After

Width:  |  Height:  |  Size: 2.0 KiB

View File

@ -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!',

View File

@ -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: '註冊成功',

View File

@ -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: '注册成功',

View File

@ -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 = [

View File

@ -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 {

View File

@ -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;

View File

@ -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>
<BedrockModal
visible={bedrockAddingVisible}
hideModal={hideBedrockAddingModal}