
Company News
Socket Joins the OpenJS Foundation
Socket is proud to join the OpenJS Foundation as a Silver Member, deepening our commitment to the long-term health and security of the JavaScript ecosystem.
A lightweight and easy-to-use wrapper for OpenAI's Chat API. DecaChat provides a simple interface for creating chat-based applications with OpenAI's GPT models.
npm install deca-chat
import { DecaChat } from 'deca-chat';
// Initialize the chat client
const chat = new DecaChat({
apiKey: 'your-openai-api-key'
});
// Send a message and get a response
async function example() {
const response = await chat.chat('Hello, how are you?');
console.log(response);
}
The DecaChat constructor accepts a configuration object with the following options:
interface DecaChatConfig {
apiKey: string; // Required: Your OpenAI API key
model?: string; // Optional: Default 'gpt-4o-mini'
baseUrl?: string; // Optional: Default 'https://api.openai.com/v1'
maxTokens?: number; // Optional: Default 1000
temperature?: number; // Optional: Default 0.7
}
const chat = new DecaChat(config: DecaChatConfig);
setSystemMessage(message: string): voidSets the system message for the conversation. This resets the conversation history and starts with the new system message.
chat.setSystemMessage('You are a helpful assistant specialized in JavaScript.');
async chat(message: string): Promise<string>Sends a message and returns the assistant's response. The message and response are automatically added to the conversation history.
const response = await chat.chat('What is a closure in JavaScript?');
clearConversation(): voidClears the entire conversation history, including the system message.
chat.clearConversation();
getConversation(): ChatMessage[]Returns the current conversation history, including system messages, user messages, and assistant responses.
const history = chat.getConversation();
import { DecaChat } from 'deca-chat';
async function example() {
// Initialize with custom configuration
const chat = new DecaChat({
apiKey: 'your-openai-api-key',
model: 'gpt-4',
maxTokens: 2000,
temperature: 0.8
});
// Set a system message
chat.setSystemMessage('You are a helpful coding assistant.');
try {
// Start a conversation
const response1 = await chat.chat('How do I create a React component?');
console.log('Assistant:', response1);
// Continue the conversation
const response2 = await chat.chat('How do I add props to it?');
console.log('Assistant:', response2);
// Get conversation history
const history = chat.getConversation();
console.log('Conversation History:', history);
} catch (error) {
console.error('Error:', error);
}
}
const chat = new DecaChat({
apiKey: 'your-api-key',
baseUrl: 'https://your-custom-endpoint.com/v1',
model: 'gpt-4o-mini'
});
// Start with a system message
chat.setSystemMessage('You are a helpful assistant.');
// Have a conversation
await chat.chat('What is TypeScript?');
await chat.chat('How does it differ from JavaScript?');
// Get the conversation history
const history = chat.getConversation();
console.log(history);
/* Output:
[
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'What is TypeScript?' },
{ role: 'assistant', content: '...' },
{ role: 'user', content: 'How does it differ from JavaScript?' },
{ role: 'assistant', content: '...' }
]
*/
// Clear the conversation and start fresh
chat.clearConversation();
The chat method throws errors when:
Always wrap API calls in try-catch blocks for proper error handling:
try {
const response = await chat.chat('Hello');
console.log(response);
} catch (error) {
console.error('Chat error:', error);
}
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
MIT License - see the LICENSE file for details.
FAQs
A simple wrapper for OpenAI's chat API - that includes ability to use custom baseURL's
The npm package deca-chat receives a total of 17 weekly downloads. As such, deca-chat popularity was classified as not popular.
We found that deca-chat demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
Socket is proud to join the OpenJS Foundation as a Silver Member, deepening our commitment to the long-term health and security of the JavaScript ecosystem.

Security News
npm now links to Socket's security analysis on every package page. Here's what you'll find when you click through.

Security News
A compromised npm publish token was used to push a malicious postinstall script in cline@2.3.0, affecting the popular AI coding agent CLI with 90k weekly downloads.