|
| 1 | +// Introduction to Machine Learning for the Arts, Fall 2024 |
| 2 | +// https://github.com/ml5js/Intro-ML-Arts-IMA-F24 |
| 3 | +// In class demonstration |
| 4 | + |
| 5 | +// Full ChatBot code here: |
| 6 | +// https://github.com/Programming-from-A-to-Z/Ollama-Examples/tree/main/p5-chatbot |
| 7 | + |
| 8 | +async function setup() { |
| 9 | + createCanvas(400, 400); |
| 10 | + console.log('Hello'); |
| 11 | + // Ollama server endpoint |
| 12 | + let url = 'http://localhost:11434/api/chat'; |
| 13 | + |
| 14 | + // Initial conversation setup, with a system message and a user message |
| 15 | + let conversationHistory = [ |
| 16 | + { role: 'system', content: 'You are a robot.' }, |
| 17 | + { role: 'user', content: 'Hi, my name is Fred!' }, |
| 18 | + ]; |
| 19 | + console.log(conversationHistory); |
| 20 | + |
| 21 | + // Options for the fetch to specify POST and data |
| 22 | + let options = { |
| 23 | + method: 'POST', |
| 24 | + headers: { 'Content-Type': 'application/json' }, |
| 25 | + body: JSON.stringify({ |
| 26 | + model: 'gemma2:2b', |
| 27 | + messages: conversationHistory, |
| 28 | + stream: false, |
| 29 | + }), |
| 30 | + }; |
| 31 | + |
| 32 | + // Send a POST request await the response |
| 33 | + let response = await fetch(url, options); |
| 34 | + |
| 35 | + // Parse the JSON response |
| 36 | + let json = await response.json(); |
| 37 | + // Full response |
| 38 | + console.log(json); |
| 39 | + // Just the text reply |
| 40 | + console.log(json.message.content); |
| 41 | + |
| 42 | + background(255); |
| 43 | + fill(0); |
| 44 | + text(json.message.content, 0, 0, width, height); |
| 45 | +} |
| 46 | + |
| 47 | +/* |
| 48 | +// CALLBACK PATTERN EXAMPLE |
| 49 | +// This is now ml5.js works |
| 50 | +faceMesh.detect(img, gotData); |
| 51 | +
|
| 52 | +function gotData(results) { |
| 53 | + console.log(results); |
| 54 | +} |
| 55 | +
|
| 56 | +// AWAIT PATTERN EXAMPLE |
| 57 | +// ml5.js does not work with way |
| 58 | +let results = await faceMesh.detect(img); |
| 59 | +*/ |
0 commit comments