Skip to content

Instantly share code, notes, and snippets.

@ff6347
Created May 15, 2024 08:54
Show Gist options
  • Save ff6347/4458a9c04a1269db6887a704af43a007 to your computer and use it in GitHub Desktop.
Save ff6347/4458a9c04a1269db6887a704af43a007 to your computer and use it in GitHub Desktop.
p5js uses ollama to generate data
function preload() {
if (ollama) {
console.log('Yes ollama is there');
} else {
console.log('No ollama is not there');
}
}
let isGenerating = false;
const data = [];
function setup() {
console.log('hello p5js');
isGenerating = true;
ollama
.chat({
model: 'llama3',
stream: false,
format: 'json',
messages: [
{
role: 'system',
content:
'You are a data generator. YOU ONLY ANSWER IN JSON. Dont give me explainations. Dont excape the JSON. Dont add new lines. I want data in the following format: `{data:[{x: <number between 0 and 100>, y:<number between 0 and 100> }]}`',
},
{
role: 'user',
content: 'Generate data for me. I need around 10 data points?',
},
],
})
.then((response) => {
isGenerating = false;
console.log(response.message.content);
const json = JSON.parse(response.message.content);
data.push(...json.data);
})
.catch((error) => {
isGenerating = false;
console.error(error);
});
}
function draw() {
background(255);
textAlign(CENTER, CENTER);
if (data.length > 0) {
data.forEach((item) => {
circle(item.x, item.y, 10);
});
} else {
if (isGenerating === true) {
text('doing busy things', width / 2, height / 2);
} else {
text('done', width / 2, height / 2);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment