OpenAI-compatible client library and API proxy for K2Think AI platform. Use it as a library (like OpenAI SDK) or as HTTP API proxy - no manual authentication needed!
const K2ThinkClient = require('k2think-api-nodejs');
const client = new K2ThinkClient();
const response = await client.chat.completions.create({
model: 'MBZUAI-IFM/K2-Think-v2', // Use v2 model
messages: [{ role: 'user', content: 'Hello!' }]
});npm start
curl -X POST http://localhost:3000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{"model":"MBZUAI-IFM/K2-Think-v2","messages":[{"role":"user","content":"Hello"}]}'Automatic account rotation to avoid rate limits:
const K2ThinkMultiClient = require('./client_multi');
const client = new K2ThinkMultiClient();
// Add accounts via CLI: node accounts-cli.js add
// Rotation happens automatically!
const response = await client.chat.completions.create({
model: 'MBZUAI-IFM/K2-Think-v2',
messages: [{ role: 'user', content: 'Hello!' }]
});
console.log(`Used account: ${response._accountId}`);See docs/MULTI_ACCOUNT.md for full guide.
- Zero-configuration: Set credentials once in
.env - OpenAI-compatible: Same API as OpenAI
- Automatic authentication: Token management handled automatically
- Context preservation: Multi-turn conversations work perfectly
- Two usage modes: Library or HTTP API
- Multi-account support: Automatic rotation to avoid rate limits (NEW!)
npm installBefore using this library, you need a K2Think account:
- Sign up: https://www.k2think.ai/auth?mode=signup
- Login: https://www.k2think.ai/auth
- Save your email and password for configuration
Create .env file from .env.example:
cp .env.example .envEdit .env with your K2Think credentials:
K2THINK_EMAIL=your-email@example.com
K2THINK_PASSWORD=your-password
PORT=3000require('dotenv').config();
const K2ThinkClient = require('./client');
const client = new K2ThinkClient();
// Simple question
async function ask(question) {
const response = await client.chat.completions.create({
model: 'MBZUAI-IFM/K2-Think-v2', // Updated model name
messages: [{ role: 'user', content: question }]
});
return response.choices[0].message.content;
}
const answer = await ask('What is 2+2?');
console.log(answer);That's it! No server needed.
npm startcurl -X POST http://localhost:3000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "MBZUAI-IFM/K2-Think-v2",
"messages": [
{"role": "user", "content": "Hello! What is 2+2?"}
]
}'curl -X POST http://localhost:3000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "MBZUAI-IFM/K2-Think-v2",
"messages": [
{"role": "user", "content": "My name is Alice"},
{"role": "assistant", "content": "Nice to meet you, Alice!"},
{"role": "user", "content": "What is my name?"}
]
}'curl http://localhost:3000/v1/modelsGET /- Health checkPOST /v1/chat/completions- Chat completions (OpenAI compatible)GET /v1/models- List available models (OpenAI compatible)
Best for:
- Node.js applications and scripts
- Direct integration in your code
- When you don't need HTTP API
Example:
const client = new K2ThinkClient();
await client.chat.completions.create({...});Best for:
- Web applications (frontend)
- Multiple services/languages accessing the API
- When you need REST API
Example:
curl http://localhost:3000/v1/chat/completions ...- Automatic Authentication: Logs in to K2Think API using credentials from
.env - Token Management: Automatically refreshes expired tokens
- Request Translation: Converts OpenAI-format requests to K2Think format
- Response Formatting: Returns responses in OpenAI-compatible format
Standard OpenAI-compatible format:
{
"id": "chatcmpl-...",
"object": "chat.completion",
"created": 1762533698,
"model": "MBZUAI-IFM/K2-Think",
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": "Response here"
},
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": 431,
"completion_tokens": 69,
"total_tokens": 500
}
}Run the test suite:
# Test library mode
node test_library.js
# Test library with integration tests
node tests/library_integration.test.js
# Test API proxy mode (start server first)
npm start &
node tests/api_integration.test.jsSee docs/TEST_RESULTS.md for detailed test results.
- Authentication errors: Check credentials in
.env - Model not found: Use
MBZUAI-IFM/K2-Think-v2(notMBZUAI-IFM/K2-Think) - Port already in use: Change
PORTin.envor stop other service on port 3000 - No response: Check K2Think service availability and internet connection
- Proxy errors: The library bypasses system proxy automatically
- MBZUAI-IFM/K2-Think-v2 - Latest K2 Think model with enhanced reasoning
client.js- Library mode - K2ThinkClient class for direct useclient_multi.js- Multi-account mode - Automatic account rotationindex.js- API proxy mode - HTTP server with endpointsauth_manager.js- Authentication and token managementaccounts-cli.js- CLI utility for managing multiple accountsquick-add.js- Quick account addition script (recommended)lib/account_manager.js- Account storage and managementlib/account_rotator.js- Account rotation logicexamples/- Usage examples for library and multi-account modestests/- Integration tests for library and API modestest_library.js- Test for library modedata/accounts.json- Multi-account storage (encrypted)docs/TEST_RESULTS.md- Detailed test results and troubleshootingdocs/MULTI_ACCOUNT.md- Multi-account system guide.env- Configuration (create from.env.example)
- LIBRARY.md - Complete library documentation with examples
- USAGE.md - HTTP API usage guide
- QUICKSTART.md - Quick start guide (Russian)
- MULTI_ACCOUNT.md - Multi-account system guide
- ACCOUNT_SCHEMA.md - JSON storage schema
# Add new account (recommended)
node quick-add.js email@example.com password123 "Account Name"
# List all accounts
node accounts-cli.js list
# View statistics
node accounts-cli.js stats
# Test multi-account system
node tests/test_multi_account.js
# Run example
node examples/multi_account_example.jsIf you find this project useful, consider supporting its development:
πͺ USDT (BNB Smart Chain)
0x30C93CEB10c53db8B01ae311db83C2287C431ECd
πͺ USDT (TON Network)
UQDUm3wVVrkcdHgqAZnEIXRbtPwt9KV52M20C6vsMiheKmKV
πͺ USDT (Tron / TRC20)
TJLpsWFGkr26hbpRdHxHwMMzNjUECWKSQc
πͺ USDT (Ethereum)
0x80dCc2DA8ad2A8283F63AAaD94dD490373a48885
πͺ TON (TON Network)
UQDUm3wVVrkcdHgqAZnEIXRbtPwt9KV52M20C6vsMiheKmKV
See SUPPORT.md for more ways to help.
MIT