xxxxxxxxxx
71
// Step 1:
// Download Ollama: https://ollama.com/
// Step 2:
// Launch Ollama and follow its instructions.
// Step 3:
// Find a model of your choice in Ollama library: https://ollama.com/library/
// Step 4:
// Copy the code within the model page (such as llama3.1) and run it in your terminal (it may take a while to download): https://ollama.com/library/llama3.1
// ollama run llama3.1
// Step 5:
// Change model name in this sketch to match the model you just downloaded in the previous step
// mode: "llama3.1"
// Step 6:
// Download this sketch (you might need to save this sketch to your own account before you can download it) and run this sketch locally.
// Instructions on how to run your sketch locally: https://github.com/processing/p5.js/wiki/Local-server
// References:
// https://www.jsdelivr.com/package/npm/ollama-js-client
const Ollama = window.OllamaJS;
const ollama = new Ollama({
model: "llama3",
url: "http://127.0.0.1:11434/api/",
});
const prompts = [
"Answer all my questions within 20 words. What is p5.js?",
"Who is it for?",
"How can I learn more about it?",
];
let promptIndex = 0;
let responseDone = true;
let activeResponseElement;
function setup() {
noCanvas();
createElement("h1", "Ollama Predefined Prompts Example with Context");
createP(
"<i>This sketch cycles through an array of predefined user prompts.</i>"
);
createP(
"<strong>Note: this sketch must be run <a target='_blank' href='https://github.com/processing/p5.js/wiki/Local-server'>locally</a> in conjunction with <a target='_blank' href='https://ollama.com/'>Ollama</a>.</strong>"
);
createP("Click anywhere on this page to start or continue conversation!");
}
function mouseClicked() {
if (responseDone) {
responseDone = false;
const activePrompt = prompts[promptIndex];
promptIndex = (promptIndex + 1) % prompts.length;
createP("User: " + activePrompt);
activeResponseElement = createP("Assistant: ");
ollama.prompt_stream(activePrompt, (error, response) => {
if (error) {
console.error(error);
} else {
// console.log(response);
responseDone = response.done;
activeResponseElement.html(response.response, true);
}
});
}
}