UNPKG

woolball-client

Version:

Client-side library for Woolball enabling secure browser resource sharing for distributed AI task processing

70 lines (69 loc) 2.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.webllmTextGeneration = void 0; // Dynamic import function to get the WebLLM processor async function getProcessor() { try { // Dynamically import the WebLLM processor // Using require for CommonJS compatibility const processor = require('../../providers/webllm/webllm-processor'); // Create with progress callback return processor.getWebLLMProcessor({ initProgressCallback: (progress) => { console.log(`WebLLM loading progress:`, progress); // You could also post a message to show loading progress in the UI } }); } catch (error) { console.error('Error loading WebLLM processor:', error); throw error; } } async function webllmTextGeneration(data) { const { input, model, max_new_tokens = 250, temperature = 1, stream = false, stream_options = {} } = data; try { // Parse the input as JSON - assuming it contains the full messages array const messages = JSON.parse(input); if (!Array.isArray(messages)) { throw new Error("Input must be a serialized array of messages"); } // Dynamically get the WebLLM processor const processor = await getProcessor(); // Initialize the WebLLM engine with the specified model await processor.initialize(model); // Handle streaming vs non-streaming generation if (stream) { // For streaming, we need to return a special object that the worker can handle const chunks = await processor.generateText({ messages, temperature, max_new_tokens, stream: true, stream_options }); // Return a special indicator to the worker that this is a streaming response return { streamingResponse: true, generator: chunks }; } else { // Regular non-streaming completion const response = await processor.generateText({ messages, temperature, max_new_tokens }); // Extract the generated text from the response const generatedText = response.choices[0]?.message?.content || ''; // Return the generated text return { generatedText }; } } catch (error) { console.error('WebLLM text generation error:', error); throw error; } } exports.webllmTextGeneration = webllmTextGeneration;