Welcome, Future AI Engineers! 🚀 In this project, you’ll bring artificial intelligence to life by creating a chatbot powered by the Groq API. This chatbot will respond to user messages in real time using advanced AI models! Instead of a graphical interface, we’re keeping it sleek and console-based—just like real-world API integrations.
By the end of this project, you’ll:
✅ Master API integration using JavaScript.
✅ Learn how to handle errors gracefully when making API requests.
✅ Develop a real-world chatbot with multi-turn conversation capabilities.
✅ Gain experience working with AI-powered models like llama3-8b-8192
.
No prior AI experience? No problem! This is your hands-on introduction to building with AI! 🚀
Before we start coding, you'll need access to the Groq API. Here’s how to get your key:
1️⃣ Go to Groq’s official website and create an account.
2️⃣ Navigate to your dashboard and generate an API key.
3️⃣ Copy the key (keep it secret 🧢) and paste it into the code below.
Replace 'YOUR_API_KEY_HERE'
with your actual Groq API key, then run this script:
const fetch = require("node-fetch");
const API_KEY = "YOUR_API_KEY_HERE"; // Replace with your actual API key
const API_URL = "https://api.groq.com/v1/chat/completions";
async function getAIResponse(userInput) {
try {
const response = await fetch(API_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${API_KEY}`
},
body: JSON.stringify({
model: "llama3-8b-8192",
messages: [{ role: "user", content: userInput }]
})
});
const data = await response.json();
return data.choices[0].message.content;
} catch (error) {
return `Error: ${error.message}`;
}
}
(async function main() {
const readline = require("readline").createInterface({
input: process.stdin,
output: process.stdout
});
console.log("Welcome to your AI chatbot! Type 'exit' to quit.");
while (true) {
await new Promise(resolve => {
readline.question("\nYou: ", async (userInput) => {
if (userInput.toLowerCase() === "exit") {
console.log("\nGoodbye! 👋");
readline.close();
process.exit(0);
}
const response = await getAIResponse(userInput);
console.log("AI:", response);
resolve();
});
});
}
})();
- Open your terminal or command prompt.
- Run
node chatbot.js
and start chatting with your AI assistant! - Type
"exit"
to stop the conversation.
Want to supercharge your chatbot? Try these upgrades:
✨ Chat History – Save all messages to a .txt
file.
✨ Multi-Turn Conversations – Make the AI remember context!
✨ Model Selection – Let users pick different AI models.
✨ Custom Prompts – Allow users to change the chatbot’s personality!
🔗 Groq API Documentation
💻 Node.js Documentation
Task | Points |
---|---|
Successfully connects to Groq API | ✅ 30 |
Handles errors properly | ✅ 20 |
Console-based chatbot works smoothly | ✅ 20 |
Implements additional features | ✅ 30 |
This project is an amazing opportunity to work with AI-powered APIs and create something cool. Whether you want to impress your peers, add it to your portfolio, or just have fun, this project is for you!
Happy Coding! 💻🔥