👋 Welcome, future AI dev!
Today we’re creating a simple web app that uses AI magic to generate text responses — kind of like a mini ChatGPT!
🧩 Step 1: Setup Your HTML File
Create a file named index.html and add this code:
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
body { font-family: Arial; padding: 20px; background: #f5f5f5; text-align: center; }
#output { margin-top: 20px; background: #fff; padding: 15px; border-radius: 8px; }
input { padding: 10px; width: 70%; }
<input id="userInput" placeholder="Ask me anything..." />
<button onclick="getAIResponse()">Send</button>
<script src="app.js"></script>
⚙️ Step 2: Add Some AI Power with JavaScript
Now, create a file called app.js in the same folder.
You’ll use OpenAI’s free API (you’ll need an API key from platform.openai.com).
async function getAIResponse() {
const input = document.getElementById("userInput").value;
const output = document.getElementById("output");
output.innerHTML = "Thinking... 🧠";
const response = await fetch("https://api.openai.com/v1/chat/completions", {
"Content-Type": "application/json",
"Authorization": "Bearer YOUR_API_KEY_HERE"
messages: [{ role: "user", content: input }]
const data = await response.json();
🪄 Replace YOUR_API_KEY_HERE with your real key.
💡 Step 3: Run It
-
Open
index.htmlin your browser. -
Type something like “Tell me a joke.”
-
Watch your AI app reply — tada! 🎉
🧠 Bonus Idea: Make It Cooler
-
Add a dark mode toggle 🌙
-
Save chat history in
localStorage💾 -
Let users change AI personalities (fun mode, teacher mode, etc.) 🧑🏫
.png)
Comments
Post a Comment