-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
community[minor]: Add support for Tencent Hunyuan Chat Model and Embe…
…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
1 parent
b38e52c
commit 153daff
Showing
23 changed files
with
1,460 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
38 changes: 38 additions & 0 deletions
38
docs/core_docs/docs/integrations/text_embedding/tencent_hunyuan.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
111
examples/src/models/chat/integration_tencent_hunyuan.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
*/ | ||
}, | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.