跳到主要内容
LangChain.js 支持腾讯混元系列模型。 https://cloud.tencent.com/document/product/1729/104753

设置

  1. 点击此处注册腾讯云账户。
  2. 点击此处创建 SecretID 和 SecretKey。
  3. 请将 SecretID 和 SecretKey 分别设置为环境变量 TENCENT_SECRET_IDTENCENT_SECRET_KEY
有关安装 LangChain 软件包的一般说明,请参阅此部分
npm
npm install @langchain/community @langchain/core
如果您在浏览器环境中使用 LangChain.js,您还需要安装以下依赖项
npm
npm install crypto-js
然后确保您从 web 导入,如下所示。

用法

以下是一个示例
// 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:
        */
      },
    },
  ],
});

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.