Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for Tencent Hunyuan #2015

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions api/apps/llm_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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
Expand All @@ -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")
Expand Down
38 changes: 38 additions & 0 deletions conf/llm_factories.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}
]
}
6 changes: 4 additions & 2 deletions rag/llm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
"StepFun":StepFunCV,
"OpenAI-API-Compatible": OpenAI_APICV,
"TogetherAI": TogetherAICV,
"01.AI": YiCV
"01.AI": YiCV,
"Tencent Hunyuan": HunyuanCV
}


Expand Down Expand Up @@ -98,7 +99,8 @@
"novita.ai": NovitaAIChat,
"SILICONFLOW": SILICONFLOWChat,
"01.AI": YiChat,
"Replicate": ReplicateChat
"Replicate": ReplicateChat,
"Tencent Hunyuan": HunyuanChat
}


Expand Down
80 changes: 80 additions & 0 deletions rag/llm/chat_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
54 changes: 53 additions & 1 deletion rag/llm/cv_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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.",
},
],
}
]
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions requirements_arm.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions web/src/assets/svg/llm/hunyuan.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions web/src/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!',
Expand Down
4 changes: 4 additions & 0 deletions web/src/locales/zh-traditional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '註冊成功',
Expand Down
4 changes: 4 additions & 0 deletions web/src/locales/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '注册成功',
Expand Down
5 changes: 3 additions & 2 deletions web/src/pages/user-setting/setting-model/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
27 changes: 27 additions & 0 deletions web/src/pages/user-setting/setting-model/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading