xxxxxxxxxx
110
let openai_api_proxy = "https://sordid-hexagonal-bunny.glitch.me/";
let img;
let mySound;
let textMargin = 20;
let textX = textMargin;
let textY = textMargin + 20;
function setup() {
soundFormats("mp3", "ogg");
sound_forest = loadSound("forest.mp3");
sound_beach = loadSound("beach.mp3");
sound_mountain = loadSound("mountain.mp3");
img_forest = loadImage("forest.jpg");
img_beach = loadImage("beach.jpg");
img_mountain = loadImage("mountain.jpg");
createCanvas(400, 400);
background(0);
fill(255);
// Whenever the button is clicked, call sendMessage
select("#submit").mouseClicked(sendMessage);
}
function sendMessage() {
textSize(20);
text("Loading...", 160, 200);
// Get the text from the text field
let content = select("#content").value();
// Don't send empty messages to the API
if (content == "") {
return;
}
// Start a new conversation
let messages = [
{
role: "system",
content:
"You are now entering a nature soundscape mindfulness meditation session. Please relax and describe your current state of mind or any thoughts you'd like to share.",
},
{
role: "user",
content:
'I would like you to analyze my state of mind I input later to choose the most suitable one out of the nature soundscapes from forest/beach/mountain to help me meditate and relax, make sure to output the name of the nature soundscape such as "forest.mp3" or "beach.mp3" or "mountain.mp3" in quotation mark at the end of the reply. And here is my input: ' +
content,
},
];
// Clear the text field
select("#content").value("");
// Send the request
let params = {
model: "gpt-3.5-turbo",
messages: messages,
temperature: 0.7,
};
requestOAI("POST", "/v1/chat/completions", params, (results) =>
gotResults(results, content)
);
}
function gotResults(results, content) {
let message = results.choices[0].message.content;
console.log(message);
// Display the AI-generated message as part of the mindfulness session
let a = message.search("forest");
let b = message.search("beach");
let c = message.search("mountain");
console.log(a, b, c);
// Play the corresponding nature soundscape
if (a > -1) sound_forest.play();
if (b > -1) sound_beach.play();
if (c > -1) sound_mountain.play();
if (a > -1) img = img_forest;
if (b > -1) img = img_beach;
if (c > -1) img = img_mountain;
image(img, 0, 0, 400, 400);
displayMessage(message);
}
function displayMessage(message) {
textSize(15);
// Display the AI-generated message on the canvas
message = message.match(/\w+/g);
for (let i = 0; i < message.length; i++) {
let nextChar = message[i] + " ";
//console.log(nextChar);
// print character to canvas
if (textX + textWidth(nextChar) >= width - textMargin) {
textY = textY + textAscent() + textDescent();
textX = textMargin;
}
text(nextChar, textX, textY);
textX = textX + textWidth(nextChar);
}
textMargin = 20;
textX = textMargin;
textY = textMargin + 20;
}
function draw() {
// Your draw function, if needed
}