Skip to content

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

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

Merged
merged 10 commits into from
May 31, 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
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 @@ -186,6 +186,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 @@ -550,6 +558,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 @@ -80,6 +80,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 @@ -174,6 +176,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 @@ -332,6 +336,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 @@ -401,6 +407,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 @@ -1121,6 +1127,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 @@ -1940,6 +1964,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 @@ -3140,6 +3182,14 @@
"embeddings/tensorflow.js",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there! 👋 Just wanted to flag that the recent changes in the embeddings directory might impact the project's dependencies. It would be great if the maintainers could review this for any potential changes to peer/dev/hard dependencies. Thanks!

"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 @@ -3504,6 +3554,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
Loading