chatgpt-client-wrapper
v1.0.2
Published
A simple wrapper for OpenAI ChatGPT API with text and image generation capabilities
Downloads
30
Maintainers
Readme
ChatGPT API Wrapper
A simple and lightweight npm package for interacting with OpenAI's ChatGPT API, supporting both text completion and image generation.
Features
- 🤖 Text Completion: Support for all ChatGPT models (GPT-3.5, GPT-4, etc.)
- 🎨 Image Generation: DALL-E 2 and DALL-E 3 support
- 🔧 TypeScript Support: Full type definitions included
- ⚡ Simple API: Easy-to-use methods for common tasks
- 🛡️ Error Handling: Comprehensive error handling and validation
- 🔄 Flexible Configuration: Customizable timeout, base URL, and model selection
Installation
npm install chatgpt-api-wrapperQuick Start
const ChatGPT = require('chatgpt-api-wrapper');
const client = new ChatGPT({
apiKey: 'your-openai-api-key'
});
// Simple text completion
const response = await client.complete('What is artificial intelligence?');
console.log(response);
// Generate an image
const image = await client.generateImage({
prompt: 'A beautiful sunset over mountains',
model: 'dall-e-3'
});
console.log(image.data[0].url);API Reference
Constructor
const client = new ChatGPT({
apiKey: 'your-api-key', // Required
baseURL: 'custom-base-url', // Optional
timeout: 30000 // Optional (default: 30000ms)
});Text Completion
Simple Completion
const response = await client.complete(prompt, model);Advanced Chat
const response = await client.chat({
model: ChatGPT.MODELS.GPT_4,
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'Hello!' }
],
maxTokens: 150,
temperature: 0.7,
topP: 1,
frequencyPenalty: 0,
presencePenalty: 0
});Image Generation
const image = await client.generateImage({
prompt: 'A cute robot',
model: 'dall-e-3', // 'dall-e-2' or 'dall-e-3'
size: '1024x1024', // Various sizes available
quality: 'standard', // 'standard' or 'hd' (DALL-E 3 only)
style: 'vivid', // 'vivid' or 'natural' (DALL-E 3 only)
n: 1 // Number of images
});Available Models
console.log(ChatGPT.MODELS);
// Output:
// {
// GPT_4: 'gpt-4',
// GPT_4_TURBO: 'gpt-4-turbo',
// GPT_4_TURBO_PREVIEW: 'gpt-4-turbo-preview',
// GPT_3_5_TURBO: 'gpt-3.5-turbo',
// GPT_3_5_TURBO_16K: 'gpt-3.5-turbo-16k'
// }Utility Methods
// Test API connection
const isConnected = await client.testConnection();
// Get available models
const models = client.getAvailableModels();Configuration Options
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| apiKey | string | Required | Your OpenAI API key |
| baseURL | string | https://api.openai.com/v1 | API base URL |
| timeout | number | 30000 | Request timeout in milliseconds |
Image Generation Options
DALL-E 2
- Sizes:
256x256,512x512,1024x1024 - Up to 10 images per request
DALL-E 3
- Sizes:
1024x1024,1792x1024,1024x1792 - Quality:
standard,hd - Style:
vivid,natural - 1 image per request
Error Handling
The package provides detailed error messages for common issues:
try {
const response = await client.complete('Hello');
} catch (error) {
console.error('Error:', error.message);
// Detailed error information from OpenAI API
}Local Development & Testing
- Clone this repository
- Install dependencies:
npm install - Build the package:
npm run build - Set your API key:
export OPENAI_API_KEY="your-key-here" - Run tests:
npm test
TypeScript Support
This package includes full TypeScript definitions:
import ChatGPT, { ChatMessage, ChatCompletionOptions } from 'chatgpt-api-wrapper';
const client = new ChatGPT({ apiKey: 'your-key' });
const messages: ChatMessage[] = [
{ role: 'user', content: 'Hello!' }
];
const options: ChatCompletionOptions = {
model: ChatGPT.MODELS.GPT_4,
messages,
temperature: 0.7
};Examples
Chatbot with Context
const conversation = [
{ role: 'system', content: 'You are a helpful coding assistant.' },
{ role: 'user', content: 'How do I create a REST API in Node.js?' }
];
const response = await client.chat({
model: ChatGPT.MODELS.GPT_4,
messages: conversation,
maxTokens: 500
});
// Add assistant's response to conversation
conversation.push({
role: 'assistant',
content: response.choices[0].message.content
});Batch Image Generation
const prompts = [
'A cat wearing a space suit',
'A robot playing piano',
'A mountain landscape at dawn'
];
const images = await Promise.all(
prompts.map(prompt =>
client.generateImage({ prompt, model: 'dall-e-2' })
)
);
images.forEach((img, i) => {
console.log(`Image ${i + 1}: ${img.data[0].url}`);
});License
MIT
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
