File size: 2,178 Bytes
bf8ef86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { FilesetResolver, LlmInference } from "https://cdn.jsdelivr.net/npm/@mediapipe/tasks-genai";

const input = document.getElementById("input");
const output = document.getElementById("output");
const submit = document.getElementById("submit");

const modelFileName = "gemma-2b-it-gpu-int4.bin";

/**

 * Display newly generated partial results to the output text box.

 */
function displayPartialResults(partialResults, complete) {
    output.textContent += partialResults;

    if (complete) {
        if (!output.textContent) {
            output.textContent = "Result is empty";
        }
        submit.disabled = false;
    }
}

/**

 * Main function to run LLM Inference.

 */
async function runDemo() {
    const genaiFileset = await FilesetResolver.forGenAiTasks("https://cdn.jsdelivr.net/npm/@mediapipe/tasks-genai/wasm");
    let llmInference;

    submit.onclick = () => {
        output.textContent = "";
        submit.disabled = true;
        llmInference.generateResponse(input.value, displayPartialResults);
    };

    submit.value = "Loading the model...";
    LlmInference.createFromOptions(genaiFileset, {
        baseOptions: { modelAssetPath: modelFileName },
        // maxTokens: 512,  // The maximum number of tokens (input tokens + output
        //                  // tokens) the model handles.
        // randomSeed: 1,   // The random seed used during text generation.
        // topK: 1,  // The number of tokens the model considers at each step of
        //           // generation. Limits predictions to the top k most-probable
        //           // tokens. Setting randomSeed is required for this to make
        //           // effects.
        // temperature:
        //     1.0,  // The amount of randomness introduced during generation.
        //           // Setting randomSeed is required for this to make effects.
    })
        .then((llm) => {
            llmInference = llm;
            submit.disabled = false;
            submit.value = "Get Response";
        })
        .catch(() => {
            alert("Failed to initialize the task.");
        });
}

runDemo();