Skip to content

Commit

Permalink
Add a LanguageModel class that implements the Llama2 architecture via…
Browse files Browse the repository at this point in the history
… llama2.c and Emscripten

See https://github.com/gohai/llama2.c-emscripten for details.
  • Loading branch information
gohai committed Aug 2, 2023
1 parent f875305 commit 53bd1f6
Show file tree
Hide file tree
Showing 16 changed files with 671 additions and 0 deletions.
14 changes: 14 additions & 0 deletions examples/LanguageModel/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Language Model Example</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.7.0/p5.min.js"></script>
<script src="../../dist/ml5.js"></script>
</head>
<body>
<input placeholder="Prompt" id="prompt"></input> <input type="button" value="Generate" id="generate" disabled></input>
<script src="sketch.js"></script>
</body>
</html>
61 changes: 61 additions & 0 deletions examples/LanguageModel/sketch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
let lm;
let curWord = 0;
let angle = 0;
let radius = 60;

function setup() {
createCanvas(400, 400);
background(0);

lm = ml5.languageModel(onModelLoaded);
}

function draw() {
fill(0, 5);
rect(0, 0, width, height);
}

function onModelLoaded() {
console.log('Model loaded');
select('#generate').removeAttribute('disabled');
select('#generate').mouseClicked(generateText);
}

function generateText() {
let prompt = select('#prompt').value();
console.log('Prompt: ' + prompt);

let options = {
temperature: 0.9
};
lm.generate(prompt, options, onToken);

curWord = 0;
}

function onToken(lm) {
console.log(lm.tokens[lm.tokens.length-1]);

while (curWord < lm.words.length) {
push();
translate(width/2, height/2);
rotate(radians(angle));
translate(radius, 0);
rotate(radians(-angle));
angle = angle + 5;
radius += 0.5;
if (radius > width/2 || radius > height/2) {
radius = 60;
}
fill(255);
textAlign(CENTER);
textSize(10);
text(lm.words[curWord], 0, 0);
pop();
curWord++;
}

if (lm.finished) {
console.log('Generation finished');
}
}
15 changes: 15 additions & 0 deletions examples/LanguageModelAsync/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Language Model Example</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.7.0/p5.min.js"></script>
<script src="../../dist/ml5.js"></script>
</head>
<body>
<input placeholder="Prompt" id="prompt"></input> <input type="button" value="Generate" id="generate"></input>
<div id="output"></div>
<script src="sketch.js"></script>
</body>
</html>
22 changes: 22 additions & 0 deletions examples/LanguageModelAsync/sketch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
let lm;

async function setup() {
noCanvas();

lm = await ml5.languageModel();
console.log('Model loaded');

select('#generate').mouseClicked(generateText);
}

function draw() {
}

async function generateText() {
let prompt = select('#prompt').value();
console.log('Prompt: ' + prompt);

let out = await lm.generate(prompt);
console.log('Generated: ' + out);
select('#output').html(out);
}
14 changes: 14 additions & 0 deletions examples/LanguageModelEvent/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Language Model Example</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.7.0/p5.min.js"></script>
<script src="../../dist/ml5.js"></script>
</head>
<body>
<input placeholder="Prompt" id="prompt"></input> <input type="button" value="Generate" id="generate"></input>
<script src="sketch.js"></script>
</body>
</html>
62 changes: 62 additions & 0 deletions examples/LanguageModelEvent/sketch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
let lm;
let curWord = 0;
let angle = 0;
let radius = 60;

function setup() {
createCanvas(400, 400);
background(0);

lm = ml5.languageModel(onModelLoaded);
}

function draw() {
fill(0, 5);
rect(0, 0, width, height);
}

function onModelLoaded() {
console.log('Model loaded');
select('#generate').removeAttribute('disabled');
select('#generate').mouseClicked(generateText);
}

function generateText() {
let prompt = select('#prompt').value();
console.log('Prompt: ' + prompt);

let options = {
temperature: 0.9
};
lm.generate(prompt, options);
lm.on('token', onToken);
lm.on('word', onWord);
lm.on('finished', onFinshed);
}


function onToken(lm) {
console.log(lm.tokens[lm.tokens.length-1]);
}

function onWord(word, lm) {
push();
translate(width/2, height/2);
rotate(radians(angle));
translate(radius, 0);
rotate(radians(-angle));
angle = angle + 5;
radius += 0.5;
if (radius > width/2 || radius > height/2) {
radius = 60;
}
fill(255);
textAlign(CENTER);
textSize(10);
text(word, 0, 0);
pop();
}

function onFinshed(lm) {
console.log('Generation finished');
}
14 changes: 14 additions & 0 deletions examples/LanguageModelManual/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Language Model Example</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.7.0/p5.min.js"></script>
<script src="../../dist/ml5.js"></script>
</head>
<body>
<input placeholder="Prompt" id="prompt"></input> <input type="button" value="Generate" id="generate" disabled></input>
<script src="sketch.js"></script>
</body>
</html>
59 changes: 59 additions & 0 deletions examples/LanguageModelManual/sketch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
let lm;
let numOptions = 40;

async function setup() {
noCanvas();

lm = await ml5.languageModel(onModelLoaded);
}

function draw() {
}

function onModelLoaded() {
console.log('Model loaded');
select('#generate').removeAttribute('disabled');
select('#generate').mouseClicked(generateText);
}

function generateText() {
let prompt = select('#prompt').value();
console.log('Prompt: ' + prompt);

select('#prompt').remove();
select('#generate').remove();
let btn = createElement('button', prompt);
btn.attribute('disabled', 'disabled');
select('body').child(btn);

let options = {
temperature: 0.9
};

lm.manualStart(prompt, options, onTokens);
}

function onTokens(tokens, lm) {
for (let i=0; i < numOptions; i++) {
let btn = createElement('button', tokens[i].str);
btn.class('continuation');
btn.id(tokens[i].index);
btn.mousePressed(selectToken);
select('body').child(btn);
}
}

function selectToken() {
let nextToken = parseInt(this.id());

this.removeClass('continuation');
this.attribute('disabled', 'disabled');
this.mousePressed(null);

let others = selectAll('.continuation');
for (let i=0; i < others.length; i++) {
others[i].remove();
}

lm.manualNext(nextToken, onTokens);
}
14 changes: 14 additions & 0 deletions examples/LanguageModelManualAsync/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Language Model Example</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.7.0/p5.min.js"></script>
<script src="../../dist/ml5.js"></script>
</head>
<body>
<input placeholder="Prompt" id="prompt"></input> <input type="button" value="Generate" id="generate"></input>
<script src="sketch.js"></script>
</body>
</html>
62 changes: 62 additions & 0 deletions examples/LanguageModelManualAsync/sketch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
let lm;
let numOptions = 40;

async function setup() {
noCanvas();

lm = await ml5.languageModel();
console.log('Model loaded');

select('#generate').mouseClicked(generateText);
}

function draw() {
}

async function generateText() {
let prompt = select('#prompt').value();
console.log('Prompt: ' + prompt);

select('#prompt').remove();
select('#generate').remove();
let btn = createElement('button', prompt);
btn.attribute('disabled', 'disabled');
select('body').child(btn);

let options = {
temperature: 0.9
};

let tokens = await lm.manualStart(prompt, options);

for (let i=0; i < numOptions; i++) {
let btn = createElement('button', tokens[i].str);
btn.class('continuation');
btn.id(tokens[i].index);
btn.mousePressed(selectToken);
select('body').child(btn);
}
}

async function selectToken() {
let nextToken = parseInt(this.id());

this.removeClass('continuation');
this.attribute('disabled', 'disabled');
this.mousePressed(null);

let others = selectAll('.continuation');
for (let i=0; i < others.length; i++) {
others[i].remove();
}

let tokens = await lm.manualNext(nextToken);

for (let i=0; i < numOptions; i++) {
let btn = createElement('button', tokens[i].str);
btn.class('continuation');
btn.id(tokens[i].index);
btn.mousePressed(selectToken);
select('body').child(btn);
}
}
Loading

0 comments on commit 53bd1f6

Please sign in to comment.