easier-openai
v1.1.27
Published
A package that makes it easier to use the NPM OpenAI package.
Readme
Easier-OpenAI
A package that makes it easier to use the NPM OpenAI package.
Features
Chat Capabilities
Command Line Chat (
cmdChat) Start an interactive chat session with OpenAI models.- Parameters:
obj: Configuration object for the chat session.saveToFile: Boolean to save the conversation to a file.name: Name of the file to save the conversation.
- Usage:
Allows users to interact with OpenAI models in a loop until the user exits by typing
e-.
- Parameters:
Get Chat (
getChat) Retrieve a saved chat conversation by name.- Parameters:
chatName: Name of the chat file to retrieve.
- Usage: Returns the chat messages in JSON format.
- Parameters:
Delete Chat (
deleteChat) Delete a saved chat conversation by name.- Parameters:
chatName: Name of the chat file to delete.
- Usage: Removes the specified chat file from the system.
- Parameters:
List Chats (
listChats) List all saved chat conversations.- Usage: Returns an array of chat names.
Update System Instructions (
updateSystemInstructions) Update the system instructions for a specific chat.- Parameters:
chatName: Name of the chat file to update.newInstructions: New system instructions to apply.
- Usage: Modifies the system message in the chat history.
- Parameters:
Audio Capabilities
- Generate Audio (
generateAudio) Generate audio from text using OpenAI models.- Parameters:
tts_text: Text to convert to speech.speechFilePath: Path to save the generated audio file.systemContent: System instructions for the audio generation.
- Usage: Converts text to audio and saves it as an MP3 file.
- Parameters:
Image Capabilities
- Generate Image (
generateImage) Generate images using OpenAI models.- Parameters:
config: Configuration object for image generation (e.g., model, prompt, size).other: Additional options (e.g., save to file, file path, file name).
- Usage: Creates an image based on the provided prompt and optionally saves it to a file.
- Parameters:
Utility Functions
Log Usage (
logUsage) Log API usage details to a file.- Parameters:
endpoint: API endpoint used.model: Model name.tokens: Number of tokens used.
- Usage:
Appends usage details to a
usage.logfile.
- Parameters:
Improve Prompt (
improvePrompt) Enhance a given prompt using prompt engineering techniques.- Parameters:
prompt: The original prompt to improve.tokens: Maximum tokens for the response.reasoning: Boolean to include reasoning in the improvement process.
- Usage: Returns an improved version of the provided prompt with detailed suggestions.
- Parameters:
Persistent Data Management
- Automatically saves persistent data (e.g., chat creation count) to a
persistentData.jsonfile. - Ensures directories for conversations, audio, images, and responses are created if they do not exist.
Warning System
- Displays warnings for potentially expensive API calls unless disabled in the
.envfile.
Installation
npm install easier-openaiUsage
Importing the Library
For ES Modules:
import { chat, audio, image, utils } from 'easier-openai';For CommonJS:
const { chat, audio, image, utils } = require('easier-openai');Example: Chat Interaction
const chatConfig = {
type: 'chat',
model: 'gpt-4',
prompt: 'Hello!',
maxTokens: 100,
};
chat.cmd(chatConfig, true, 'example-chat');Example: Generate Audio
audio.generate('Hello, world!', './output.mp3', 'Generate a friendly greeting.');Example: Generate Image
const imageConfig = {
model: 'dall-e-3',
prompt: 'A futuristic cityscape at sunset',
size: '1024x1024',
};
image.generate(imageConfig, { file: true, path: './images', fileName: 'cityscape.png' });Example: Improve Prompt
const improvedPrompt = await utils.improvePrompt('Write a story about a hero.', 500, true);
console.log(improvedPrompt);Environment Variables
OPENAI_API_KEY: Your OpenAI API key.DISABLE_WARNING: Set totrueto disable warnings for expensive API calls.
Directories
./conversations: Stores saved chat conversations../gen-audio: Stores generated audio files../gen-imgs: Stores generated images../responses: Stores saved API responses.
License
MIT License
