Skip to content

Commit

Permalink
add: Tencent Hunyuan LLM
Browse files Browse the repository at this point in the history
  • Loading branch information
yokingma authored and xqijia committed Mar 16, 2024
1 parent 1ee0ab2 commit fe01390
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 33 deletions.
14 changes: 0 additions & 14 deletions .env

This file was deleted.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"fetch-sse": "^1.0.8",
"koa": "^2.15.0",
"koa-static": "^5.0.0",
"openai": "^4.28.4"
"openai": "^4.28.4",
"tencentcloud-sdk-nodejs": "^4.0.810"
},
"devDependencies": {
"@types/koa": "^2.15.0",
Expand Down
5 changes: 5 additions & 0 deletions src/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ export const GoogleModels = {
GEMINI_PRO: 'gemini-pro'
};

export const TencentModels = {
STD: 'standard',
PRO: 'pro'
};

export const AllModels = Object.assign({}, AliyunModels, OpenAIModels, BaiduModels, GoogleModels);

export const UserAgents = [
Expand Down
10 changes: 6 additions & 4 deletions src/controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { AliyunChat, OpenAIChat, BaiduChat } from './platform';
import { EBackend, IChatInputMessage } from './interface';
import { Rag } from './rag';

const aliyun = new AliyunChat();
const openai = new OpenAIChat();
const baidu = new BaiduChat();

export const searchController = async (ctx: Context) => {
const stream = ctx.request.body.stream ?? true;
const q = ctx.request.query.q || DefaultQuery;
Expand Down Expand Up @@ -58,15 +62,13 @@ export const modelsController = async (ctx: Context) => {
aliyun: Object.values(AliyunModels),
openai: Object.values(OpenAIModels),
baidu: Object.values(BaiduModels),
google: Object.values(GoogleModels)
google: Object.values(GoogleModels),
// tencent: Object.values(TencentModels)
};
ctx.body = models;
};

function processModel(model = AliyunModels.QWEN_MAX) {
const aliyun = new AliyunChat();
const openai = new OpenAIChat();
const baidu = new BaiduChat();
if (Object.values(AliyunModels).includes(model)) {
return aliyun.chatStream.bind(aliyun);
}
Expand Down
2 changes: 1 addition & 1 deletion src/platform/base.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IChatInputMessage, IStreamHandler } from '../interface';

export abstract class BaseChat {
abstract chat(
abstract chat?(
messages: IChatInputMessage[],
system?: string,
model?: string
Expand Down
1 change: 1 addition & 0 deletions src/platform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './aliyun';
export * from './openai';
export * from './baidu';
export * from './google';
export * from './tencent';
55 changes: 55 additions & 0 deletions src/platform/tencent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import * as tencentcloud from 'tencentcloud-sdk-nodejs';
import { ClientConfig } from 'tencentcloud-sdk-nodejs/tencentcloud/common/interface';
import { Client } from 'tencentcloud-sdk-nodejs/tencentcloud/services/hunyuan/v20230901/hunyuan_client';
import { ChatStdResponse } from 'tencentcloud-sdk-nodejs/tencentcloud/services/hunyuan/v20230901/hunyuan_models';
import { BaseChat } from './base';
import { IChatInputMessage, IStreamHandler } from '../interface';
import { TencentModels } from '../constant';

export class TencentChat implements BaseChat {
private client: Client;

constructor() {
const key = process.env.TENCENT_KEY;
const secret = process.env.TENCENT_SECRET;
const config: ClientConfig = {
credential: {
secretId: key,
secretKey: secret
},
region: '',
profile: {
httpProfile: {
endpoint: 'hunyuan.tencentcloudapi.com'
}
}
};
this.client = new tencentcloud.hunyuan.v20230901.Client(config);
}

async chatStream(messages: IChatInputMessage[], onMessage?: IStreamHandler, model = TencentModels.STD): Promise<void> {
const Messages = this.transformMessage(messages);
console.log(model);
const result: any = await this.client.ChatStd({
Messages
});
return new Promise((resolve) => {
result.on('message', (res: any) => {
const data: ChatStdResponse = JSON.parse(res.data ?? '{}');
const text = data.Choices?.[0].Delta?.Content || '';
const stop = data.Choices?.[0].FinishReason === 'stop' ? true : false;
onMessage?.(text, stop);
if (stop) resolve();
});
});
}

private transformMessage(messages: IChatInputMessage[]) {
return messages.map(msg => {
return {
Role: msg.role,
Content: msg.content
};
});
}
}
10 changes: 7 additions & 3 deletions src/rag.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { EBackend, IChatInputMessage, IStreamHandler, SearchFunc } from './interface';
import { searchWithBing, searchWithGoogle, searchWithSogou } from './service';
import { MoreQuestionsPrompt, RagQueryPrompt } from './prompt';
import { AliyunChat, BaiduChat, OpenAIChat, GoogleChat } from './platform';
import { AliyunChat, BaiduChat, OpenAIChat, GoogleChat, TencentChat } from './platform';
// import { memoryCache } from './utils';
import util from 'util';
import { AliyunModels, AllModels, BaiduModels, OpenAIModels, GoogleModels } from './constant';
import { AliyunModels, AllModels, BaiduModels, OpenAIModels, GoogleModels, TencentModels } from './constant';

interface RagOptions {
backend?: EBackend
Expand All @@ -16,6 +16,7 @@ const aliyun = new AliyunChat();
const openai = new OpenAIChat();
const baidu = new BaiduChat();
const google = new GoogleChat();
const tencent = new TencentChat();

// const CACHE_NAME = 'search_with_ai';

Expand Down Expand Up @@ -108,7 +109,7 @@ export class Rag {
const messages: IChatInputMessage[] = [
{
role: 'user',
content: `${system}${query}`
content: `${system} ${query}`
}
];
return {
Expand All @@ -132,5 +133,8 @@ function processModel(model = AliyunModels.QWEN_MAX, stream = true) {
if (Object.values(GoogleModels).includes(model)) {
return stream ? google.chatStream.bind(google) : google.chat.bind(google);
}
if (Object.values(TencentModels).includes(model)) {
return tencent.chatStream.bind(tencent);
}
return stream ? aliyun.chatStream.bind(aliyun) : aliyun.chat.bind(aliyun);
}
3 changes: 2 additions & 1 deletion web/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export async function search(q: string, options: IQueryOptions) {
const query = new URLSearchParams({
q
});
await fetchEventData(`${URL}?${query.toString()}`, {
const url = `${URL}?${query.toString()}`;
await fetchEventData(url, {
method: 'POST',
signal: ctrl?.signal,
data: {
Expand Down
1 change: 0 additions & 1 deletion web/src/pages/search.vue
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ async function querySearch(val: string | null) {
engine,
onMessage: (data: IQueryResult) => {
// if (wrapperRef.value) wrapperRef.value.scrollTop = wrapperRef.value.scrollHeight
console.log(data);
if (data.contexts) {
result.value.contexts = data.contexts;
}
Expand Down
70 changes: 62 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,13 @@ acorn@^8.4.1, acorn@^8.9.0:
resolved "https://registry.npmmirror.com/acorn/-/acorn-8.11.3.tgz"
integrity sha512-Y9rRfJG5jcKOE0CLisYbojUjIrIEE7AGMzA/Sm4BslANhbS+cDMpgBdcPT91oJ7OuJ9hYJBx59RjbhxVnrF8Xg==

agent-base@6:
version "6.0.2"
resolved "https://registry.npmmirror.com/agent-base/-/agent-base-6.0.2.tgz#49fff58577cfee3f37176feab4c22e00f86d7f77"
integrity sha512-RZNwNclF7+MS/8bDg70amg32dyeZGZxiDuQmZxKLAlQjr3jGyLx+4Kkk58UO7D2QdgFIQCovuSuZESne6RG6XQ==
dependencies:
debug "4"

agentkeepalive@^4.2.1:
version "4.5.0"
resolved "https://registry.npmmirror.com/agentkeepalive/-/agentkeepalive-4.5.0.tgz#2673ad1389b3c418c5a20c5d7364f93ca04be923"
Expand Down Expand Up @@ -903,6 +910,11 @@ base64-js@^1.3.1:
resolved "https://registry.npmmirror.com/base64-js/-/base64-js-1.5.1.tgz#1b1b440160a5bf7ad40b650f095963481903930a"
integrity sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==

bignumber.js@^9.0.0:
version "9.1.2"
resolved "https://registry.npmmirror.com/bignumber.js/-/bignumber.js-9.1.2.tgz#b7c4242259c008903b13707983b5f4bbd31eda0c"
integrity sha512-2/mKyZH9K85bzOEfhXDBFZTGd1CTs+5IHpeFQo9luiBG7hghdC851Pj2WAhb6E3R6b9tZj/XKhbg4fum+Kepug==

binary-extensions@^2.0.0:
version "2.2.0"
resolved "https://registry.npmmirror.com/binary-extensions/-/binary-extensions-2.2.0.tgz"
Expand Down Expand Up @@ -1338,20 +1350,20 @@ debounce-fn@^4.0.0:
dependencies:
mimic-fn "^3.0.0"

debug@4, debug@^4, debug@^4.1.1, debug@^4.3.1, debug@^4.3.2, debug@^4.3.4:
version "4.3.4"
resolved "https://registry.npmmirror.com/debug/-/debug-4.3.4.tgz"
integrity sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==
dependencies:
ms "2.1.2"

debug@^3.1.0, debug@^3.2.7:
version "3.2.7"
resolved "https://registry.npmmirror.com/debug/-/debug-3.2.7.tgz"
integrity sha512-CFjzYYAi4ThfiQvizrFQevTTXHtnCqWfe7x1AhgEscTz6ZbLbfoLRLPugTQyBth6f8ZERVUSyWHFD/7Wu4t1XQ==
dependencies:
ms "^2.1.1"

debug@^4, debug@^4.1.1, debug@^4.3.1, debug@^4.3.2, debug@^4.3.4:
version "4.3.4"
resolved "https://registry.npmmirror.com/debug/-/debug-4.3.4.tgz"
integrity sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==
dependencies:
ms "2.1.2"

decompress-response@^3.3.0:
version "3.3.0"
resolved "https://registry.npmmirror.com/decompress-response/-/decompress-response-3.3.0.tgz#80a4dd323748384bfa248083622aedec982adff3"
Expand Down Expand Up @@ -2047,6 +2059,15 @@ [email protected]:
resolved "https://registry.npmmirror.com/form-data-encoder/-/form-data-encoder-1.7.2.tgz#1f1ae3dccf58ed4690b86d87e4f57c654fbab040"
integrity sha512-qfqtYan3rxrnCk1VYaA4H+Ms9xdpPqvLZa6xmMgFvhO32x7/3J/ExcTd6qpxM0vH2GdMI+poehyBZvqfMTto8A==

form-data@^3.0.0:
version "3.0.1"
resolved "https://registry.npmmirror.com/form-data/-/form-data-3.0.1.tgz#ebd53791b78356a99af9a300d4282c4d5eb9755f"
integrity sha512-RHkBKtLWUVwd7SqRIvCZMEvAMoGUp0XU+seQiZejj0COz3RI3hWP4sCv3gZWWLjJTd7rGwcsF5eKZGii0r/hbg==
dependencies:
asynckit "^0.4.0"
combined-stream "^1.0.8"
mime-types "^2.1.12"

form-data@^4.0.0:
version "4.0.0"
resolved "https://registry.npmmirror.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452"
Expand Down Expand Up @@ -2355,6 +2376,14 @@ http-errors@~1.6.2:
setprototypeof "1.1.0"
statuses ">= 1.4.0 < 2"

https-proxy-agent@^5.0.0:
version "5.0.1"
resolved "https://registry.npmmirror.com/https-proxy-agent/-/https-proxy-agent-5.0.1.tgz#c59ef224a04fe8b754f3db0063a25ea30d0005d6"
integrity sha512-dFcAjpTQFgoLMzC2VwU+C/CbS7uRL0lWmxDITmqm7C+7F0Odmj6s9l6alZc6AELXhrnggM2CeWSXHGOdX2YtwA==
dependencies:
agent-base "6"
debug "4"

human-signals@^2.1.0:
version "2.1.0"
resolved "https://registry.npmmirror.com/human-signals/-/human-signals-2.1.0.tgz#dc91fcba42e4d06e4abaed33b3e7a3c02f514ea0"
Expand Down Expand Up @@ -2702,6 +2731,13 @@ js-yaml@^4.1.0:
dependencies:
argparse "^2.0.1"

json-bigint@^1.0.0:
version "1.0.0"
resolved "https://registry.npmmirror.com/json-bigint/-/json-bigint-1.0.0.tgz#ae547823ac0cad8398667f8cd9ef4730f5b01ff1"
integrity sha512-SiPv/8VpZuWbvLSMtTDU8hEfrZWg/mH/nV/b4o0CYbSxu1UIQPLdwKOCIyLQX+VIPO5vrLX3i8qtqFyhdPSUSQ==
dependencies:
bignumber.js "^9.0.0"

[email protected]:
version "3.0.0"
resolved "https://registry.npmmirror.com/json-buffer/-/json-buffer-3.0.0.tgz#5b1f397afc75d677bde8bcfc0e47e1f9a3d9a898"
Expand Down Expand Up @@ -3041,7 +3077,7 @@ [email protected]:
resolved "https://registry.npmmirror.com/node-domexception/-/node-domexception-1.0.0.tgz#6888db46a1f71c0b76b3f7555016b63fe64766e5"
integrity sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==

node-fetch@^2.6.7:
node-fetch@^2.2.0, node-fetch@^2.6.7:
version "2.7.0"
resolved "https://registry.npmmirror.com/node-fetch/-/node-fetch-2.7.0.tgz#d0f0fa6e3e2dc1d27efcd8ad99d550bda94d187d"
integrity sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==
Expand Down Expand Up @@ -3863,6 +3899,19 @@ supports-preserve-symlinks-flag@^1.0.0:
resolved "https://registry.npmmirror.com/supports-preserve-symlinks-flag/-/supports-preserve-symlinks-flag-1.0.0.tgz"
integrity sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==

tencentcloud-sdk-nodejs@^4.0.810:
version "4.0.810"
resolved "https://registry.npmmirror.com/tencentcloud-sdk-nodejs/-/tencentcloud-sdk-nodejs-4.0.810.tgz#cf57fdeaf3bb51ff9f305b51b6a981e26072c98a"
integrity sha512-jZdRX4Ed9S9DW2v3PedlBDHwXKO0jvRsy7zXBCpqjw7+LccJA7/B/Mn+AIEYJyVXxenSmbMwziO1aLVxBDEhCA==
dependencies:
form-data "^3.0.0"
get-stream "^6.0.0"
https-proxy-agent "^5.0.0"
is-stream "^2.0.0"
json-bigint "^1.0.0"
node-fetch "^2.2.0"
tslib "1.13.0"

[email protected]:
version "1.0.0"
resolved "https://registry.npmmirror.com/text-hex/-/text-hex-1.0.0.tgz#69dc9c1b17446ee79a92bf5b884bb4b9127506f5"
Expand Down Expand Up @@ -3948,6 +3997,11 @@ tsconfig-paths@^3.15.0:
minimist "^1.2.6"
strip-bom "^3.0.0"

[email protected]:
version "1.13.0"
resolved "https://registry.npmmirror.com/tslib/-/tslib-1.13.0.tgz#c881e13cc7015894ed914862d276436fa9a47043"
integrity sha512-i/6DQjL8Xf3be4K/E6Wgpekn5Qasl1usyw++dAA35Ue5orEn65VIxOA+YvNNl9HV3qv70T7CNwjODHZrLwvd1Q==

[email protected]:
version "1.0.6"
resolved "https://registry.npmmirror.com/tsscmp/-/tsscmp-1.0.6.tgz"
Expand Down

0 comments on commit fe01390

Please sign in to comment.