UNPKG

@langchain/community

Version:
110 lines (108 loc) • 3.74 kB
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs'); const __langchain_core_language_models_chat_models = require_rolldown_runtime.__toESM(require("@langchain/core/language_models/chat_models")); const __langchain_core_messages = require_rolldown_runtime.__toESM(require("@langchain/core/messages")); const __langchain_core_outputs = require_rolldown_runtime.__toESM(require("@langchain/core/outputs")); const __mlc_ai_web_llm = require_rolldown_runtime.__toESM(require("@mlc-ai/web-llm")); //#region src/chat_models/webllm.ts var webllm_exports = {}; require_rolldown_runtime.__export(webllm_exports, { 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