A beginner-friendly CLI chat agent that uses OpenAI's API. Perfect for learning how to develop AI agents!
- 🤖 Interactive CLI chat interface
- 💬 Conversation history management
- 🛡️ Comprehensive error handling
- 💾 Save conversations to JSON files
- 🔧 Easy configuration with environment variables
- 📚 Educational code structure with detailed comments
- Python 3.7 or higher
- OpenAI API key
-
Clone or download this project
cd ai-agent-cli -
Install dependencies
pip install -r requirements.txt
-
Set up your OpenAI API key 🔐
Recommended: Environment variable (most secure)
export OPENAI_API_KEY="your-api-key-here"
For development: .env file
# Copy the example file cp env_example.txt .env # Edit .env and add your API key nano .env
Direct parameter (for testing only)
agent = ChatAgent(api_key="your-api-key-here")
🔒 Security Note: Never commit your API key to version control! The
.gitignorefile is configured to prevent accidental commits of.envfiles.
Run the chat agent:
python chat_agent.pyOnce the agent is running, you can use these commands:
/help- Show help information/save- Save conversation to a JSON file/clear- Clear conversation history/history- Show conversation history/security- Show API key security best practices/quitor/exit- Exit the chat agent
🤖 Welcome to the Simple CLI Chat Agent!
==================================================
✅ Chat agent initialized successfully!
💡 Type /help for available commands
💡 Type /quit to exit
--------------------------------------------------
👤 You: Hello! How are you today?
🤖 AI: Hello! I'm doing great, thank you for asking! I'm here and ready to help you with any questions or tasks you might have. How can I assist you today?
👤 You: Can you explain what an AI agent is?
🤖 AI: An AI agent is a software program that can perceive its environment, make decisions, and take actions to achieve specific goals. Think of it like a digital assistant that can:
1. **Perceive**: Take in information from various sources (text, images, data, etc.)
2. **Think**: Process that information and make decisions
3. **Act**: Execute actions based on those decisions
AI agents can be simple (like a chatbot) or complex (like autonomous robots). They're designed to operate with some level of autonomy and can learn from their experiences to improve over time.
👤 You: /save
💾 Conversation saved to conversation_20241201_143022.json
👤 You: /quit
👋 Goodbye! Thanks for chatting!
This project is designed to be educational. Here are some concepts you'll learn:
- API Integration: How to connect to external services (OpenAI)
- Error Handling: Graceful handling of API errors and edge cases
- State Management: Maintaining conversation history
- CLI Design: Building user-friendly command-line interfaces
- Environment Configuration: Managing API keys and settings securely
ChatAgentclass: Main agent logicmain()function: CLI interface and user interaction- Helper methods: History management, file operations, etc.
Try these modifications to learn more:
- Add new commands: Implement
/configto change model settings - Memory management: Add conversation summarization for long chats
- Multi-modal support: Add image processing capabilities
- Plugin system: Allow custom functions/tools
- Streaming responses: Show responses as they're generated
"OpenAI API key not found"
- Make sure you've set the
OPENAI_API_KEYenvironment variable - Or create a
.envfile with your API key
"Invalid API key"
- Check that your OpenAI API key is correct
- Ensure you have credits in your OpenAI account
"Rate limit exceeded"
- Wait a moment and try again
- Consider upgrading your OpenAI plan if this happens frequently
Import errors
- Make sure you've installed the requirements:
pip install -r requirements.txt
Feel free to fork this project and experiment! Some ideas:
- Add support for different AI models
- Implement conversation export in different formats
- Add a web interface
- Create a plugin system
This project is open source and available under the MIT License.
If you run into issues or have questions, check the troubleshooting section above or open an issue on GitHub.
Happy coding! 🚀