/**
* @fileoverview Library for interacting with the DeepSeek API.
* Provides askAi, getCompletion, and getTokenUsage functions.
*/
const https = require('https');
/**
* Sends a chat completion request to the DeepSeek API.
*
* @param {string} model - The model name (e.g., 'deepseek-chat').
* @param {string} token - DeepSeek API key.
* @param {string} systemPrompt - System message for context.
* @param {string} message - User message.
* @returns {Promise<Object>} Resolves with the full JSON response.
*/
async function askAi(model, token, systemPrompt, message) {
const data = JSON.stringify({
model: model,
messages: [
{ role: "system", content: systemPrompt },
{ role: "user", content: message }
]
});
const options = {
hostname: 'api.deepseek.com',
path: '/v1/chat/completions',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
}
};
return new Promise((resolve, reject) => {
const req = https.request(options, (res) => {
let body = '';
res.on('data', (chunk) => { body += chunk; });
res.on('end', () => {
try {
resolve(JSON.parse(body));
} catch (err) {
reject(err);
}
});
});
req.on('error', (err) => {
reject(err);
});
req.write(data);
req.end();
});
}
/**
* Extracts the AI completion message from the API response.
*
* @param {Object} json - The full JSON response from DeepSeek.
* @returns {string} The completion text or empty string if not found.
*/
function getCompletion(json) {
if (json && json.choices && json.choices[0] && json.choices[0].message) {
return String(json.choices[0].message.content || '');
}
return '';
}
/**
* Extracts the total token usage from the API response.
*
* @param {Object} json - The full JSON response from DeepSeek.
* @returns {number} Total tokens used, or 0 if not found.
*/
function getTokenUsage(json) {
if (json && json.usage && typeof json.usage.total_tokens === 'number') {
return json.usage.total_tokens;
}
return 0;
}
module.exports = { askAi, getCompletion, getTokenUsage };
/*
@param model = String (e.g., 'deepseek-chat')
@param token = String (your DeepSeek API key)
@param systemPrompt = String (system-level prompt, e.g., 'You are an assistant.')
@param message = String (user's prompt)
*/
const { askAi, getCompletion, getTokenUsage } = require('./deepseek');
const model = 'deepseek-chat';
const token = 'your-deepseek-api-key';
const systemPrompt = 'You are an assistant.';
const message = 'What is the capital of France?';
async function main() {
const res = await askAi(model, token, systemPrompt, message);
console.log('Completion:', getCompletion(res));
console.log('Token Usage:', getTokenUsage(res));
}
main();
The deepseek.js library provides simple utilities to interact with the DeepSeek API for chat completions.
Functions:
askAi(model, token, systemPrompt, message):
getCompletion(json):
getTokenUsage(json):
Parameters:
model: Name of the DeepSeek model (e.g., 'deepseek-chat').token: Your DeepSeek API key.systemPrompt: Instruction or context for the assistant.message: The user's message.
Output:
The askAi function returns a JSON object which can be parsed with getCompletion and getTokenUsage.