A lightweight, neural network-powered chatbot that recognizes user intents using a bag-of-words approach + a small feed-forward neural network built with PyTorch.
Currently recognizes the following intents:
- greeting
- goodbye
- programming (explains what coding is)
- resource (recommends learning resources)
- stocks (shows a random selection from your "portfolio")
- Intent classification using a 3-layer feed-forward neural network
- Bag-of-words (binary) input representation
- Tokenization + lemmatization with NLTK
- PyTorch model training & inference
- Supports function calling (e.g.
stocksintent → callsget_stocks()) - Model saving & loading
- Simple command-line chat interface
Enter your message: hi
Hello!
Enter your message: how can i learn coding?
Check out the NeuralNine YouTube channel and The Python Bible series (7 in 1).
Enter your message: what are my stocks?
Here are your stocks!
['NVDA', 'META', 'MSFT']
Enter your message: bye
Goodbye!
chatbot/
├── main.py # Main application + model + training loop
├── intents.json # Training patterns + responses
├── chatbot_model.pth # Trained model weights (after training)
├── dimensions.json # Input/output size info for loading
└── README.md
Python 3.8+
torch
numpy
nltk
Install dependencies:
uv add install torch numpy nltkThen download required NLTK data (only needed once):
import nltk
nltk.download('punkt')
nltk.download('wordnet')
nltk.download('omw-1.4') # optional but recommendeduv run main.pyUncomment these lines in main.py
assistant = ChatbotAssistant('intents.json', function_mappings = {'stocks': get_stocks})
assistant.parse_intents()
assistant.prepare_data()
assistant.train_model(batch_size=8, learning_rate=0.001, epochs=100)
assistant.save_model('chatbot_model.pth', 'dimensions.json')Then run:
uv run main.pyThe model will be retrained and saved.
- Open intents.json
- Add a new intent object like this:
{ "tag": "thanks", "patterns": ["Thanks", "Thank you", "Thx", "Appreciate it"], "responses": ["You're welcome!", "Anytime!", "Glad I could help"] } - (Optional) Add a function mapping in main.py:
function_mappings = { 'stocks': get_stocks, 'thanks': lambda: print("Extra action: sending virtual high-five!") }
- Retrain the model (see Option 2 above)
- Very small dataset → performance is limited on unseen phrases
- Binary bag-of-words → no word order or frequency information
- No handling of out-of-scope questions (falls back to predicted intent)
- No context/memory between messages
- Use TF-IDF instead of binary BoW
- Add word embeddings (e.g. pretrained GloVe / fastText)
- Implement a larger model (LSTM / Transformer)
- Add entity extraction (names, dates, products…)
- Connect to real APIs (weather, news, real stock data…)
- Create a web / Discord / Telegram interface
- MIT License – feel free to use, modify, and share.