Skip to content

Commit

Permalink
community[minor]: Add support for Tencent Hunyuan Chat Model and Embe…
Browse files Browse the repository at this point in the history
…ddings (#5476)

* community: Add support for Tencent Hunyuan chat model and embeddings

Tencent provides its Hunyuan chat model and embeddings through Tencent cloud. This PR adds support for the [chat model](https://cloud.tencent.com/document/product/1729/105701) as well as [embedding](https://cloud.tencent.com/document/product/1729/102832).

* community[minor]: Add support for Tencent Hunyuan Chat Model and Embeddings

* refactor Chat Model streaming implementation with AsyncGenerator
* update stream example

* community[minor]: Add support for Tencent Hunyuan Chat Model and Embeddings

* make host configurable

* community[minor]: Add support for Tencent Hunyuan Chat Model and Embeddings

* support both nodejs and browser environment
* update documents and examples

* community[minor]: Add support for Tencent Hunyuan Chat Model and Embeddings

* make separate chat model and embedding entrypoints for web vs. node

* community[minor]: Add support for Tencent Hunyuan Chat Model and Embeddings

* rename docs sidebar name

* community[minor]: Add support for Tencent Hunyuan Chat Model and Embeddings

* format code

* Update build, lint, format

* Type export

---------

Co-authored-by: jacoblee93 <[email protected]>
  • Loading branch information
TeCHiScy and jacoblee93 authored May 31, 2024
1 parent b38e52c commit 153daff
Show file tree
Hide file tree
Showing 23 changed files with 1,460 additions and 0 deletions.
Binary file not shown.
1 change: 1 addition & 0 deletions docs/core_docs/docs/integrations/chat/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ The table shows, for each integration, which features have been implemented with
| ChatMistralAI | | | | | | |
| ChatOllama | | | | | | |
| ChatOpenAI | | | | | | |
| ChatTencentHunyuan | | | | | | |
| ChatTogetherAI | | | | | | |
| ChatYandexGPT | | | | | | |
| ChatZhipuAI | | | | | | |
41 changes: 41 additions & 0 deletions docs/core_docs/docs/integrations/chat/tencent_hunyuan.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
sidebar_label: Tencent Hunyuan
---

import CodeBlock from "@theme/CodeBlock";

# ChatTencentHunyuan

LangChain.js supports the Tencent Hunyuan family of models.

https://cloud.tencent.com/document/product/1729/104753

## Setup

1. Sign up for a Tencent Cloud account [here](https://cloud.tencent.com/register).
2. Create SecretID & SecretKey [here](https://console.cloud.tencent.com/cam/capi).
3. Set SecretID and SecretKey as environment variables named `TENCENT_SECRET_ID` and `TENCENT_SECRET_KEY`, respectively.

import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx";

<IntegrationInstallTooltip></IntegrationInstallTooltip>

```bash npm2yarn
npm install @langchain/community
```

If you are using LangChain.js in a browser environment, you'll also need to install the following dependencies:

```bash npm2yarn
npm install crypto-js
```

And then make sure that you import from the `web` as shown below.

## Usage

Here's an example:

import TencentHunyuan from "@examples/models/chat/integration_tencent_hunyuan.ts";

<CodeBlock language="typescript">{TencentHunyuan}</CodeBlock>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
sidebar_label: Tencent Hunyuan
---

# TencentHunyuan

The `TencentHunyuanEmbeddings` class uses the Tencent Hunyuan API to generate embeddings for a given text.

## Setup

1. Sign up for a Tencent Cloud account [here](https://cloud.tencent.com/register).
2. Create SecretID & SecretKey [here](https://console.cloud.tencent.com/cam/capi).
3. Set SecretID and SecretKey as environment variables named `TENCENT_SECRET_ID` and `TENCENT_SECRET_KEY`, respectively.

import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx";

<IntegrationInstallTooltip></IntegrationInstallTooltip>

```bash npm2yarn
npm install @langchain/community
```

If you are using LangChain.js in a browser environment, you'll also need to install the following dependencies:

```bash npm2yarn
npm install crypto-js
```

And then make sure that you import from the `web` as shown below.

## Usage

Here's an example:

import CodeBlock from "@theme/CodeBlock";
import TencentHunyuan from "@examples/models/embeddings/tencent_hunyuan.ts";

<CodeBlock language="typescript">{TencentHunyuan}</CodeBlock>
111 changes: 111 additions & 0 deletions examples/src/models/chat/integration_tencent_hunyuan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// in nodejs environment
import { ChatTencentHunyuan } from "@langchain/community/chat_models/tencent_hunyuan";

// in browser environment
// import { ChatTencentHunyuan } from "@langchain/community/chat_models/tencent_hunyuan/web";

import { HumanMessage } from "@langchain/core/messages";
import type { LLMResult } from "@langchain/core/outputs";

const messages = [new HumanMessage("Hello")];

// Default model is hunyuan-pro
const hunyuanPro = new ChatTencentHunyuan({
streaming: false,
temperature: 1,
});

let res = await hunyuanPro.invoke(messages);
console.log(res);
/*
AIMessage {
content: 'Hello! How can I help you today?Is there anything I can do for you?',
name: undefined,
additional_kwargs: {},
response_metadata: {
tokenUsage: { totalTokens: 20, promptTokens: 1, completionTokens: 19 }
},
tool_calls: [],
invalid_tool_calls: []
}
*/

// Use hunyuan-lite
const hunyuanLite = new ChatTencentHunyuan({
model: "hunyuan-lite",
streaming: false,
});

res = await hunyuanLite.invoke(messages);
console.log(res);
/*
AIMessage {
content: '你好!很高兴为你提供服务~有什么我可以帮助你的吗?',
name: undefined,
additional_kwargs: {},
response_metadata: {
tokenUsage: { totalTokens: 14, promptTokens: 1, completionTokens: 13 }
},
tool_calls: [],
invalid_tool_calls: []
}
*/

// Use hunyuan-lite with streaming
const hunyuanLiteStream = new ChatTencentHunyuan({
model: "hunyuan-lite",
streaming: true,
temperature: 1,
});

hunyuanLiteStream.invoke(messages, {
callbacks: [
{
handleLLMEnd(output: LLMResult) {
console.log(output);
/*
{
generations: [
[
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object]
]
],
llmOutput: {
tokenUsage: { totalTokens: 9, promptTokens: 1, completionTokens: 8 }
}
}
*/
},
handleLLMNewToken(token: string) {
console.log(`token: ${token}`);
/*
token: 你好
token: !
token: 很高兴
token: 能
token: 为您
token: 解答
token: 问题
token: 和建议
token: 方案
token: .
token: 如果您
token: 有其他
token: 需要帮助
token: 的地方
token: ,
token:
token: 随时
token: 告诉我
token: 哦
token: ~
token:
*/
},
},
],
});
13 changes: 13 additions & 0 deletions examples/src/models/embeddings/tencent_hunyuan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// in nodejs environment
import { TencentHunyuanEmbeddings } from "@langchain/community/embeddings/tencent_hunyuan";

// in browser environment
// import { TencentHunyuanEmbeddings } from "@langchain/community/embeddings/tencent_hunyuan/web";

/* Embed queries */
const embeddings = new TencentHunyuanEmbeddings();
const res = await embeddings.embedQuery("你好,世界!");
console.log(res);
/* Embed documents */
const documentRes = await embeddings.embedDocuments(["你好,世界!", "再见"]);
console.log({ documentRes });
16 changes: 16 additions & 0 deletions libs/langchain-community/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ embeddings/tensorflow.cjs
embeddings/tensorflow.js
embeddings/tensorflow.d.ts
embeddings/tensorflow.d.cts
embeddings/tencent_hunyuan.cjs
embeddings/tencent_hunyuan.js
embeddings/tencent_hunyuan.d.ts
embeddings/tencent_hunyuan.d.cts
embeddings/tencent_hunyuan/web.cjs
embeddings/tencent_hunyuan/web.js
embeddings/tencent_hunyuan/web.d.ts
embeddings/tencent_hunyuan/web.d.cts
embeddings/togetherai.cjs
embeddings/togetherai.js
embeddings/togetherai.d.ts
Expand Down Expand Up @@ -554,6 +562,14 @@ chat_models/premai.cjs
chat_models/premai.js
chat_models/premai.d.ts
chat_models/premai.d.cts
chat_models/tencent_hunyuan.cjs
chat_models/tencent_hunyuan.js
chat_models/tencent_hunyuan.d.ts
chat_models/tencent_hunyuan.d.cts
chat_models/tencent_hunyuan/web.cjs
chat_models/tencent_hunyuan/web.js
chat_models/tencent_hunyuan/web.d.ts
chat_models/tencent_hunyuan/web.d.cts
chat_models/togetherai.cjs
chat_models/togetherai.js
chat_models/togetherai.d.ts
Expand Down
8 changes: 8 additions & 0 deletions libs/langchain-community/langchain.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ export const config = {
"embeddings/ollama": "embeddings/ollama",
"embeddings/premai": "embeddings/premai",
"embeddings/tensorflow": "embeddings/tensorflow",
"embeddings/tencent_hunyuan": "embeddings/tencent_hunyuan/index",
"embeddings/tencent_hunyuan/web": "embeddings/tencent_hunyuan/web",
"embeddings/togetherai": "embeddings/togetherai",
"embeddings/voyage": "embeddings/voyage",
"embeddings/zhipuai": "embeddings/zhipuai",
Expand Down Expand Up @@ -175,6 +177,8 @@ export const config = {
"chat_models/ollama": "chat_models/ollama",
"chat_models/portkey": "chat_models/portkey",
"chat_models/premai": "chat_models/premai",
"chat_models/tencent_hunyuan": "chat_models/tencent_hunyuan/index",
"chat_models/tencent_hunyuan/web": "chat_models/tencent_hunyuan/web",
"chat_models/togetherai": "chat_models/togetherai",
"chat_models/webllm": "chat_models/webllm",
"chat_models/yandex": "chat_models/yandex",
Expand Down Expand Up @@ -333,6 +337,8 @@ export const config = {
"embeddings/llama_cpp",
"embeddings/gradient_ai",
"embeddings/premai",
"embeddings/tencent_hunyuan",
"embeddings/tencent_hunyuan/web",
"embeddings/zhipuai",
"llms/load",
"llms/cohere",
Expand Down Expand Up @@ -402,6 +408,8 @@ export const config = {
"chat_models/llama_cpp",
"chat_models/portkey",
"chat_models/premai",
"chat_models/tencent_hunyuan",
"chat_models/tencent_hunyuan/web",
"chat_models/iflytek_xinghuo",
"chat_models/iflytek_xinghuo/web",
"chat_models/webllm",
Expand Down
58 changes: 58 additions & 0 deletions libs/langchain-community/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
"@tensorflow/tfjs-core": "^3.6.0",
"@tsconfig/recommended": "^1.0.2",
"@types/better-sqlite3": "^7.6.9",
"@types/crypto-js": "^4.2.2",
"@types/flat": "^5.0.2",
"@types/html-to-text": "^9",
"@types/jsdom": "^21.1.1",
Expand Down Expand Up @@ -147,6 +148,7 @@
"cohere-ai": ">=6.0.0",
"convex": "^1.3.1",
"couchbase": "^4.3.0",
"crypto-js": "^4.2.0",
"d3-dsv": "^2.0.0",
"datastore-core": "^9.2.9",
"discord.js": "^14.14.1",
Expand Down Expand Up @@ -286,6 +288,7 @@
"cohere-ai": "*",
"convex": "^1.3.1",
"couchbase": "^4.3.0",
"crypto-js": "^4.2.0",
"d3-dsv": "^2.0.0",
"discord.js": "^14.14.1",
"dria": "^0.0.3",
Expand Down Expand Up @@ -549,6 +552,9 @@
"couchbase": {
"optional": true
},
"crypto-js": {
"optional": true
},
"d3-dsv": {
"optional": true
},
Expand Down Expand Up @@ -1130,6 +1136,24 @@
"import": "./embeddings/tensorflow.js",
"require": "./embeddings/tensorflow.cjs"
},
"./embeddings/tencent_hunyuan": {
"types": {
"import": "./embeddings/tencent_hunyuan.d.ts",
"require": "./embeddings/tencent_hunyuan.d.cts",
"default": "./embeddings/tencent_hunyuan.d.ts"
},
"import": "./embeddings/tencent_hunyuan.js",
"require": "./embeddings/tencent_hunyuan.cjs"
},
"./embeddings/tencent_hunyuan/web": {
"types": {
"import": "./embeddings/tencent_hunyuan/web.d.ts",
"require": "./embeddings/tencent_hunyuan/web.d.cts",
"default": "./embeddings/tencent_hunyuan/web.d.ts"
},
"import": "./embeddings/tencent_hunyuan/web.js",
"require": "./embeddings/tencent_hunyuan/web.cjs"
},
"./embeddings/togetherai": {
"types": {
"import": "./embeddings/togetherai.d.ts",
Expand Down Expand Up @@ -1949,6 +1973,24 @@
"import": "./chat_models/premai.js",
"require": "./chat_models/premai.cjs"
},
"./chat_models/tencent_hunyuan": {
"types": {
"import": "./chat_models/tencent_hunyuan.d.ts",
"require": "./chat_models/tencent_hunyuan.d.cts",
"default": "./chat_models/tencent_hunyuan.d.ts"
},
"import": "./chat_models/tencent_hunyuan.js",
"require": "./chat_models/tencent_hunyuan.cjs"
},
"./chat_models/tencent_hunyuan/web": {
"types": {
"import": "./chat_models/tencent_hunyuan/web.d.ts",
"require": "./chat_models/tencent_hunyuan/web.d.cts",
"default": "./chat_models/tencent_hunyuan/web.d.ts"
},
"import": "./chat_models/tencent_hunyuan/web.js",
"require": "./chat_models/tencent_hunyuan/web.cjs"
},
"./chat_models/togetherai": {
"types": {
"import": "./chat_models/togetherai.d.ts",
Expand Down Expand Up @@ -3153,6 +3195,14 @@
"embeddings/tensorflow.js",
"embeddings/tensorflow.d.ts",
"embeddings/tensorflow.d.cts",
"embeddings/tencent_hunyuan.cjs",
"embeddings/tencent_hunyuan.js",
"embeddings/tencent_hunyuan.d.ts",
"embeddings/tencent_hunyuan.d.cts",
"embeddings/tencent_hunyuan/web.cjs",
"embeddings/tencent_hunyuan/web.js",
"embeddings/tencent_hunyuan/web.d.ts",
"embeddings/tencent_hunyuan/web.d.cts",
"embeddings/togetherai.cjs",
"embeddings/togetherai.js",
"embeddings/togetherai.d.ts",
Expand Down Expand Up @@ -3517,6 +3567,14 @@
"chat_models/premai.js",
"chat_models/premai.d.ts",
"chat_models/premai.d.cts",
"chat_models/tencent_hunyuan.cjs",
"chat_models/tencent_hunyuan.js",
"chat_models/tencent_hunyuan.d.ts",
"chat_models/tencent_hunyuan.d.cts",
"chat_models/tencent_hunyuan/web.cjs",
"chat_models/tencent_hunyuan/web.js",
"chat_models/tencent_hunyuan/web.d.ts",
"chat_models/tencent_hunyuan/web.d.cts",
"chat_models/togetherai.cjs",
"chat_models/togetherai.js",
"chat_models/togetherai.d.ts",
Expand Down
Loading

0 comments on commit 153daff

Please sign in to comment.