跳到主要内容
兼容性仅在 Web 环境中可用。
您可以使用 LangChain 的 WebLLM 集成直接在 Web 浏览器中运行大型语言模型 (LLM)。

设置

您需要安装 WebLLM SDK 模块才能与您的本地模型进行通信。
有关安装 LangChain 软件包的一般说明,请参阅此部分
npm
npm install -S @mlc-ai/web-llm @langchain/community @langchain/core

用法

请注意,模型首次调用时,WebLLM 将下载该模型的完整权重。这可能高达数 GB,根据应用程序最终用户的互联网连接和电脑配置,可能并非所有用户都能完成。虽然浏览器会缓存该模型未来的调用,但我们建议您使用尽可能小的模型。 我们还建议在调用和加载模型时使用单独的 Web Worker,以免阻塞执行。
// Must be run in a web environment, e.g. a web worker

import { ChatWebLLM } from "@langchain/community/chat_models/webllm";
import { HumanMessage } from "@langchain/core/messages";

// Initialize the ChatWebLLM model with the model record and chat options.
// Note that if the appConfig field is set, the list of model records
// must include the selected model record for the engine.

// You can import a list of models available by default here:
// https://github.com/mlc-ai/web-llm/blob/main/src/config.ts
//
// Or by importing it via:
// import { prebuiltAppConfig } from "@mlc-ai/web-llm";
const model = new ChatWebLLM({
  model: "Phi-3-mini-4k-instruct-q4f16_1-MLC",
  chatOptions: {
    temperature: 0.5,
  },
});

await model.initialize((progress: Record<string, unknown>) => {
  console.log(progress);
});

// Call the model with a message and await the response.
const response = await model.invoke([
  new HumanMessage({ content: "What is 1 + 1?" }),
]);

console.log(response);

/*
AIMessage {
  content: ' 2\n',
}
*/
也支持流式传输。

示例

有关完整的端到端示例,请查看此项目
以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.