Tutorials
Free, Unlimited Kimi K2 API
This tutorial will show you how to use Puter.js to access Kimi K2 capabilities for free, without needing API keys, backend, or server-side setup. Puter.js is completely free for apps, allowing you to provide your users with powerful multilingual AI capabilities without any server-side setup.
Puter is the pioneer of the "User Pays" model, which allows developers to incorporate AI capabilities into their applications while each user covers their own usage costs. This model enables developers to offer advanced AI capabilities to users at no cost to themselves, without any API keys or server-side setup.
Getting Started
You can use Puter.js without any API keys or sign-ups. To start using Puter.js, include the following script tag in your HTML file, either in the <head>
or <body>
section:
<script src="https://js.puter.com/v2/"></script>
Nothing else is required to start using Puter.js for free access to Kimi K2 capabilities.
Example 1Basic text generation with Kimi K2
To generate text using Kimi K2, use the puter.ai.chat()
function:
puter.ai.chat("Explain artificial intelligence in simple terms", { model: "moonshotai/kimi-k2" })
.then(response => {
puter.print(response);
});
Full code example:
<html>
<body>
<script src="https://js.puter.com/v2/"></script>
<script>
puter.ai.chat("Explain artificial intelligence in simple terms", { model: "moonshotai/kimi-k2" })
.then(response => {
puter.print(response);
});
</script>
</body>
</html>
Example 2Multilingual conversation with Chinese support
Kimi K2 excels at multilingual interactions, particularly with Chinese language processing:
<html>
<body>
<script src="https://js.puter.com/v2/"></script>
<script>
puter.ai.chat("请用中文解释机器学习的基本概念,并举一个实际应用的例子。", {
model: "moonshotai/kimi-k2"
}).then(response => {
puter.print(response);
});
</script>
</body>
</html>
This example demonstrates Kimi K2's ability to understand and respond in Chinese, making it ideal for applications targeting Chinese-speaking users or requiring multilingual support.
Example 3Streaming responses for better user experience
For longer responses, you can use streaming to get results in real-time:
<html>
<body>
<div id="response"></div>
<script src="https://js.puter.com/v2/"></script>
<script>
async function streamResponse() {
const outputDiv = document.getElementById('response');
const response = await puter.ai.chat(
"Write a detailed comparison between traditional programming and AI-assisted development",
{model: 'moonshotai/kimi-k2', stream: true}
);
for await (const part of response) {
if (part?.text) {
outputDiv.innerHTML += part.text;
}
}
}
streamResponse();
</script>
</body>
</html>
This example demonstrates streaming responses, which provides a better user experience by showing the AI's response as it's generated rather than waiting for the complete response.
Example 4Interactive chat application
Here's a complete interactive chat interface using Kimi K2:
<html>
<body>
<div style="max-width: 800px; margin: 20px auto; font-family: Arial, sans-serif;">
<h1>Chat with Kimi K2</h1>
<div id="chat-history" style="height: 400px; border: 1px solid #ddd; padding: 10px; overflow-y: scroll; margin-bottom: 10px;"></div>
<input type="text" id="user-input" style="width: 70%; padding: 10px;" placeholder="Type your message here..." />
<button id="send-button" style="width: 25%; padding: 10px;">Send</button>
</div>
<script src="https://js.puter.com/v2/"></script>
<script>
const chatHistory = document.getElementById('chat-history');
const userInput = document.getElementById('user-input');
const sendButton = document.getElementById('send-button');
function addMessage(sender, message, isStreaming = false) {
const messageDiv = document.createElement('div');
messageDiv.style.marginBottom = '10px';
messageDiv.innerHTML = `<strong>${sender}:</strong> <span class="message-content">${message}</span>`;
if (sender === 'You') {
messageDiv.style.color = '#0066cc';
} else {
messageDiv.style.color = '#006600';
}
chatHistory.appendChild(messageDiv);
chatHistory.scrollTop = chatHistory.scrollHeight;
if (isStreaming) {
return messageDiv.querySelector('.message-content');
}
}
async function sendMessage() {
const message = userInput.value.trim();
if (!message) return;
addMessage('You', message);
userInput.value = '';
sendButton.disabled = true;
const responseSpan = addMessage('Kimi K2', '', true);
try {
const response = await puter.ai.chat(message, {
model: 'moonshotai/kimi-k2',
stream: true
});
for await (const part of response) {
if (part?.text) {
responseSpan.innerHTML += part.text;
chatHistory.scrollTop = chatHistory.scrollHeight;
}
}
} catch (error) {
responseSpan.innerHTML = `Error: ${error.message}`;
}
sendButton.disabled = false;
}
sendButton.addEventListener('click', sendMessage);
userInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
sendMessage();
}
});
</script>
</body>
</html>
This comprehensive example creates a fully functional chat interface that demonstrates Kimi K2's conversational capabilities with real-time streaming responses.
That's it! You now have free access to Kimi K2's powerful AI capabilities using Puter.js. This allows you to build sophisticated multilingual applications with advanced AI features without needing API keys, backend infrastructure, or complex billing management.
Related
Free, Serverless AI and Cloud
Start creating powerful web applications with Puter.js in seconds!
Get Started Now