Code for AI
JavaScript:
// 1. Setup your Hugging Face API Token (or another provider key)
const HF_TOKEN = "Put_In_Your_Huggingface_Token";
// 2. Point to the DeepSeek model on Hugging Face's router
const MODEL_NAME = "deepseek-ai/DeepSeek-V3"; // Or your preferred DeepSeek endpoint
// Grab the HTML elements
const chatBox = document.getElementById("chat-box");
const userInput = document.getElementById("user-input");
const sendBtn = document.getElementById("send-btn");
// Function to display messages
function addMessage(text, sender) {
const messageDiv = document.createElement("div");
messageDiv.classList.add("message");
messageDiv.classList.add(sender === "user" ? "user-message" : "ai-message");
messageDiv.textContent = text;
chatBox.appendChild(messageDiv);
chatBox.scrollTop = chatBox.scrollHeight;
}
// Function to send data to the DeepSeek API
async function getAIResponse(userText) {
addMessage("Thinking...", "ai"); // DeepSeek loves to think!
try {
const response = await fetch("https://router.huggingface.co/v1/chat/completions", {
method: "POST",
headers: {
"Authorization": `Bearer ${HF_TOKEN}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
model: MODEL_NAME,
messages: [
{ role: "system", content: "You are a helpful AI assistant powered by DeepSeek." },
{ role: "user", content: userText }
],
max_tokens: 500
})
});
const data = await response.json();
chatBox.lastChild.remove(); // Remove "Thinking..."
if (data.choices && data.choices.length > 0) {
const aiText = data.choices[0].message.content;
addMessage(aiText, "ai");
} else {
addMessage("Error: DeepSeek didn't return a text response.", "ai");
}
} catch (error) {
chatBox.lastChild.remove();
addMessage("Error: Could not connect to DeepSeek.", "ai");
console.error(error);
}
}
// Event Listeners for sending messages
sendBtn.addEventListener("click", () => {
const text = userInput.value.trim();
if (text !== "") {
addMessage(text, "user");
userInput.value = "";
getAIResponse(text);
}
});
userInput.addEventListener("keypress", (event) => {
if (event.key === "Enter") {
sendBtn.click();
}
});
CSS:
body {
font-family: sans-serif;
background-color: #eceff1;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.chat-container {
width: 400px;
background: white;
border-radius: 10px;
box-shadow: 0 4px 15px rgba(0,0,0,0.1);
display: flex;
flex-direction: column;
overflow: hidden;
}
.chat-header {
background: #6366f1;
color: white;
padding: 15px;
text-align: center;
font-size: 18px;
font-weight: bold;
}
.chat-box {
height: 350px;
padding: 15px;
overflow-y: auto;
display: flex;
flex-direction: column;
gap: 10px;
background-color: #fafafa;
}
.message {
padding: 10px 15px;
border-radius: 15px;
max-width: 75%;
word-wrap: break-word;
line-height: 1.4;
}
.user-message {
background-color: #6366f1;
color: white;
align-self: flex-end;
border-bottom-right-radius: 2px;
}
.ai-message {
background-color: #e2e8f0;
color: #1e293b;
align-self: flex-start;
border-bottom-left-radius: 2px;
}
.input-area {
display: flex;
padding: 15px;
background: white;
border-top: 1px solid #e2e8f0;
}
#user-input {
flex: 1;
padding: 10px;
border: 1px solid #cbd5e1;
border-radius: 5px;
outline: none;
font-size: 15px;
}
#user-input:focus {
border-color: #6366f1;
}
#send-btn {
background: #6366f1;
color: white;
border: none;
padding: 10px 20px;
margin-left: 10px;
border-radius: 5px;
cursor: pointer;
font-weight: bold;
}
#send-btn:hover {
background: #4f46e5;
}
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Nguyen's AI Chat</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="chat-container">
<div class="chat-header">AI Chat Assistant</div>
<div class="chat-box" id="chat-box">
<!-- Chat messages will appear here -->
</div>
<div class="input-area">
<input type="text" id="user-input" placeholder="Type your message here..." autocomplete="off" />
<button id="send-btn">Enter</button>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
Nhận xét
Đăng nhận xét