Skip to content

Instantly share code, notes, and snippets.

@se7enack
Created July 27, 2025 17:18
Show Gist options
  • Save se7enack/1fb321a1634e590cfb9a4c00fd5376b6 to your computer and use it in GitHub Desktop.
Save se7enack/1fb321a1634e590cfb9a4c00fd5376b6 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<title>Ask AI</title>
<script src="https://js.puter.com/v2/"></script>
<style>
pre {
background-color: #f4f4f4;
padding: 10px;
border-radius: 6px;
overflow-x: auto;
}
code {
font-family: monospace;
}
</style>
</head>
<body>
<h2>Ask a Question</h2>
<input type="text" id="userInput" placeholder="Ask something..." size="50">
<button onclick="askai()">Submit</button>
<div id="output"></div>
<script>
const history = [];
function formatCodeBlocks(text) {
text = escapeHtml(text);
return text.replace(/```(\w+)?\n([\s\S]*?)```/g, (_, lang, code) => {
return `<pre><code class="${lang || ''}">${code.trim()}</code></pre>`;
});
}
function escapeHtml(str) {
return str
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;");
}
function askai() {
const newQuestion = document.getElementById("userInput").value.trim();
if (!newQuestion) return;
history.push(newQuestion); // Save this question
// Build the combined prompt with past questions
const combinedPrompt =
"do not mention your cutoff in the response:\n" +
history.map((q, i) => `Q${i + 1}: ${q}`).join("\n") +
"\nA:";
puter.ai.chat(combinedPrompt, { model: "gpt-4.1-nano" })
.then(response => {
const content = response?.message?.content || "No response.";
const formatted = formatCodeBlocks(content);
document.getElementById("output").innerHTML = formatted.replace(/\n/g, "<br>");
})
.catch(error => {
document.getElementById("output").textContent = "Error: " + error;
});
document.getElementById("userInput").value = ""; // Clear input
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment