-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
37 lines (30 loc) · 1.39 KB
/
script.js
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
import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@xenova/[email protected]';
env.allowLocalModels = false;
document.getElementById('generateBtn').addEventListener('click', async function() {
const selectedModel = document.getElementById('modelSelector').value;
const userInput = document.getElementById('textInput').value;
const outputElement = document.getElementById('output');
try {
const generator = await pipeline('text-generation', selectedModel);
const messages = [
{ "role": "system", "content": "You are a friendly assistant." },
{ "role": "user", "content": userInput },
];
const prompt = generator.tokenizer.apply_chat_template(messages, {
tokenize: false, add_generation_prompt: true,
});
const result = await generator(prompt, {
max_new_tokens: 256,
penalty_alpha: 0.45,
top_k: 3,
repetition_penalty: 1.03,
guidance_scale: 1.3,
});
console.log("Generated Text: " + result[0].generated_text)
const assistantResponses = result[0].generated_text.split('assistant');
outputElement.textContent = assistantResponses[assistantResponses.length - 1].trim();
} catch (error) {
console.error(error);
outputElement.textContent = 'Error generating response: ' + error;
}
});