shgpt
v1.0.2
Published
A simple ChatGPT gateway module for Node.js with streaming support
Maintainers
Readme
ShellGPT
A simple ChatGPT gateway module for Node.js with streaming support. ShellGPT provides an easy-to-use CLI interface for interacting with OpenAI's ChatGPT API, featuring automatic API key management and conversation history.
Features
- 🤖 Interactive Chat Sessions - Start a conversation and chat naturally
- 📡 Real-time Streaming - See responses as they're generated
- 🔑 Automatic API Key Management - Secure storage and validation
- 📚 Conversation History - View and manage chat history
- ⚙️ Configurable Options - Customize model, temperature, and tokens
- 🛠️ Modular Architecture - Well-structured, maintainable code
Installation
npm installUsage
As a CLI Tool
Interactive Chat Session
# Start an interactive chat session
npm start
# Or with custom options
npm start -- --model gpt-4 --temperature 0.5Send a Single Message
# Send a single message
npm start send "What is the capital of France?"
# With custom options
npm start send "Explain quantum physics" --model gpt-4 --temperature 0.3Manage Configuration
# Show current configuration
npm start config --show
# Clear saved configuration
npm start config --clearView History
# Show conversation history
npm start history
# Clear conversation history
npm start history --clearAs a Node.js Module
const ShellGPT = require('./index.js');
async function example() {
const shellGPT = new ShellGPT();
// Send a single message
const response = await shellGPT.chat("Hello, how are you?");
console.log(response);
// Start interactive session
await shellGPT.startChat();
}CLI Commands
Global Options
-m, --model <model>- Specify the model to use (default: gpt-3.5-turbo)-t, --temperature <temperature>- Set temperature (0-2, default: 0.7)--max-tokens <tokens>- Set maximum tokens (default: 1000)
Commands
chat
Start an interactive chat session.
shgpt chat
shgpt chat --model gpt-4 --temperature 0.5send <message>
Send a single message to ChatGPT.
shgpt send "What is the weather like?"
shgpt send "Explain machine learning" --model gpt-4config
Manage configuration.
shgpt config --show # Show current configuration
shgpt config --clear # Clear saved configurationhistory
Manage conversation history.
shgpt history # Show conversation history
shgpt history --clear # Clear conversation historyInteractive Session Commands
When in an interactive chat session, you can use these commands:
clear- Clear conversation historyhistory- Show conversation historyhelp- Show available commandsquitorexit- Exit the chat session
Configuration
ShellGPT automatically manages your OpenAI API key:
- First Run: You'll be prompted to enter your OpenAI API key
- Validation: The key is validated before being saved
- Storage: The key is securely stored in
~/.shgpt/config.json - Subsequent Runs: The saved key is automatically loaded
API Key Setup
- Get your API key from OpenAI Platform
- Run ShellGPT for the first time
- Enter your API key when prompted
- The key will be validated and saved for future use
Project Structure
shellgpt/
├── src/
│ ├── auth/
│ │ └── AuthManager.js # API key management and validation
│ ├── chat/
│ │ └── ChatManager.js # Chat functionality and streaming
│ ├── cli/
│ │ └── InteractiveSession.js # Interactive CLI session
│ ├── config/
│ │ └── ConfigManager.js # Configuration file management
│ └── core/
│ └── ShellGPT.js # Main orchestrator class
├── cli.js # CLI entry point with Commander.js
├── index.js # Module entry point
├── package.json # Dependencies and metadata
└── README.md # This fileDependencies
openai- OpenAI API clientcommander- CLI argument parsingreadline- Interactive input handlingfs,path,os- File system and OS utilities
Error Handling
ShellGPT includes comprehensive error handling:
- Invalid API Key: Automatic validation and retry
- Network Errors: Graceful error messages
- Configuration Issues: Clear guidance for setup
- Graceful Shutdown: Proper cleanup on exit
Security
- API keys are stored locally in user's home directory
- Keys are validated before storage
- No keys are logged or displayed
- Configuration files use proper permissions
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
License
ISC License - see LICENSE file for details.
