For AI assistant or experiment with LLM interfaces.
- Chat interface styled like a terminal
- Syntax highlighting and Markdown support
- Typing animation for AI responses
- Clean and responsive UI
- Gemini API request handled securely via Cloudflare Worker
π¦ root
βββ index.html # Main HTML file
βββ script.js # Chat logic and Gemini API handler (frontend)
βββ style.css # UI stylinggit clone https://github.com/novalattasya/TerminalAI-chatbot.git
cd TerminalAI-chatbotIn script.js:
const workerUrl = "https://your-cloudflare-worker-url.workers.dev/";
const MAX_MESSAGES = 50; // Change conversation limit as neededYou may also customize the AI's personality prompt:
const prompt = [
"You are AIAssistant, a smart and friendly AI developed using Google's Gemini API..."
];- User types a message.
- The message is added to conversation history.
- A full prompt (chat history + personality) is generated.
- Prompt is sent to a Cloudflare Worker.
- Worker securely relays the prompt to the Gemini API.
- AI response is returned and displayed in the chat.
Use this template:
export default {
async fetch(request, env, ctx) {
// Allowed domains
const allowedOrigins = ["https://your-site.com"]; # Or you can use "*" for development
const origin = request.headers.get("Origin");
const isAllowedOrigin = allowedOrigins.includes(origin);
const corsHeaders = {
"Access-Control-Allow-Origin": isAllowedOrigin ? origin : "null",
"Access-Control-Allow-Methods": "POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type",
};
if (request.method === "OPTIONS") {
return new Response(null, { status: 204, headers: corsHeaders });
}
const body = await request.json();
const userPrompt = body.prompt;
const GEMINI_API_KEY = env.GEMINI_API_KEY_SECRET;
const geminiUrl = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=${GEMINI_API_KEY}`;
const response = await fetch(geminiUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ contents: [{ parts: [{ text: userPrompt }] }] })
});
const data = await response.json();
const reply = data.candidates?.[0]?.content?.parts?.[0]?.text || "No content generated.";
return new Response(JSON.stringify({ reply }), {
status: 200,
headers: { ...corsHeaders, "Content-Type": "application/json" },
});
}
};wrangler secret put GEMINI_API_KEY_SECRETwrangler publishYou can test this project by simply opening index.html in your browser after editing the workerUrl.
- Change AI tone by editing the system prompt.
- Adjust
MAX_MESSAGESto control memory length. - Modify UI colors in
style.css(e.g.,--bg-color,--bubble-bg-ai).
GPL-3.0 license. Feel free to use, modify, and share.
Developed with β€οΈ by Nopal
Built using Google Gemini API and Cloudflare Workers.
