@langchain/community
Version:
Third-party integrations for LangChain.js
110 lines (109 loc) • 3.69 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const require_runtime = require("../_virtual/_rolldown/runtime.cjs");
let _langchain_core_language_models_chat_models = require("@langchain/core/language_models/chat_models");
let _langchain_core_messages = require("@langchain/core/messages");
let _langchain_core_outputs = require("@langchain/core/outputs");
let _mlc_ai_web_llm = require("@mlc-ai/web-llm");
_mlc_ai_web_llm = require_runtime.__toESM(_mlc_ai_web_llm);
//#region src/chat_models/webllm.ts
var webllm_exports = /* @__PURE__ */ require_runtime.__exportAll({ ChatWebLLM: () => ChatWebLLM });
/**
* To use this model you need to have the `@mlc-ai/web-llm` module installed.
* This can be installed using `npm install -S @mlc-ai/web-llm`.
*
* You can see a list of available model records here:
* https://github.com/mlc-ai/web-llm/blob/main/src/config.ts
* @example
* ```typescript
* // Initialize the ChatWebLLM model with the model record.
* const model = new ChatWebLLM({
* model: "Phi-3-mini-4k-instruct-q4f16_1-MLC",
* chatOptions: {
* temperature: 0.5,
* },
* });
*
* // Call the model with a message and await the response.
* const response = await model.invoke([
* new HumanMessage({ content: "My name is John." }),
* ]);
* ```
*/
var ChatWebLLM = class extends _langchain_core_language_models_chat_models.SimpleChatModel {
static inputs;
engine;
appConfig;
chatOptions;
temperature;
model;
static lc_name() {
return "ChatWebLLM";
}
constructor(inputs) {
super(inputs);
this.appConfig = inputs.appConfig;
this.chatOptions = inputs.chatOptions;
this.model = inputs.model;
this.temperature = inputs.temperature;
this.engine = new _mlc_ai_web_llm.MLCEngine({ appConfig: this.appConfig });
}
_llmType() {
return "web-llm";
}
async initialize(progressCallback) {
if (progressCallback !== void 0) this.engine.setInitProgressCallback(progressCallback);
await this.reload(this.model, this.chatOptions);
}
async reload(modelId, newChatOpts) {
await this.engine.reload(modelId, newChatOpts);
}
async *_streamResponseChunks(messages, options, runManager) {
const messagesInput = messages.map((message) => {
if (typeof message.content !== "string") throw new Error("ChatWebLLM does not support non-string message content in sessions.");
const langChainType = message._getType();
let role;
if (langChainType === "ai") role = "assistant";
else if (langChainType === "human") role = "user";
else if (langChainType === "system") role = "system";
else throw new Error("Function, tool, and generic messages are not supported.");
return {
role,
content: message.content
};
});
const stream = await this.engine.chat.completions.create({
stream: true,
messages: messagesInput,
stop: options.stop,
logprobs: true
});
for await (const chunk of stream) {
const text = chunk.choices[0].delta.content ?? "";
yield new _langchain_core_outputs.ChatGenerationChunk({
text,
message: new _langchain_core_messages.AIMessageChunk({
content: text,
additional_kwargs: {
logprobs: chunk.choices[0].logprobs,
finish_reason: chunk.choices[0].finish_reason
}
})
});
await runManager?.handleLLMNewToken(text);
}
}
async _call(messages, options, runManager) {
const chunks = [];
for await (const chunk of this._streamResponseChunks(messages, options, runManager)) chunks.push(chunk.text);
return chunks.join("");
}
};
//#endregion
exports.ChatWebLLM = ChatWebLLM;
Object.defineProperty(exports, "webllm_exports", {
enumerable: true,
get: function() {
return webllm_exports;
}
});
//# sourceMappingURL=webllm.cjs.map