Skip to content

Latest commit

 

History

8 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Contextual Chatbot

A Python-based contextual chatbot that uses Natural Language Processing (NLP) and a neural network to understand and respond to user queries. The chatbot is trained on custom intents and can provide contextual responses based on user input.

Features

  • Natural Language Understanding using NLTK
  • Neural Network-based response generation
  • Customizable intents and responses
  • Context-aware conversation handling
  • Probability-based response selection
  • GPU support for faster training (if available)

Prerequisites

  • Python 3.x
  • PyTorch
  • NLTK
  • NumPy

Installation

  1. Clone the repository:

    git clone https://github.com/remicku/contextual-chatbot.git
    cd contextual-chatbot
  2. (Optional) Create the conda environment if it doesn't exist:

    conda create -n pytorch python=3.9
  3. Activate the environment:

    conda activate pytorch
  4. Install the required packages:

    pip install torch nltk numpy
  5. Download required NLTK data:

    import nltk
    nltk.download('punkt')

Project Structure

  • chat.py - Main chatbot interface (interactive CLI)
  • train.py - Script for training the neural network model
  • model.py - Neural network model architecture
  • nltk_utils.py - NLP utility functions (tokenization, BoW)
  • intents.json - Training data containing patterns and responses
  • data.pth - Saved model state and metadata

Usage

  1. Train the model (creates data.pth):

    python  train.py
  2. Start chatting:

    python  chat.py
  3. Deactivate the environment when you're done:

    conda deactivate

Type 'quit' to exit the chat.

Customization

You can customize the chatbot's responses by modifying the intents.json file. The file structure should be:

{
  "intents": [
	{
	  "tag": "greeting",
	  "patterns": ["Hi", "Hello", "Hey"],
	  "responses": ["Hello!", "Hi there!", "Hey!"]
	}
    // Add more intents as needed
  ]
}

Changes to this file require retraining the model (python train.py).

How It Works

  1. Input Preprocessing: User input is tokenized and stemmed using NLTK
  2. Vectorization: The text is converted to a bag-of-words vector
  3. Prediction: A neural network processes the input and predicts the most likely intent (responses are selected based on the predicted intent and confidence threshold)
  4. Confidence Check: If the model's confidence > 75%, a matching response is chosen
  5. Fallback: Otherwise, a default fallback message is returned

Model Architecture

The neural network consists of:

  • Input layer: Size = vocabulary length
  • Two hidden layers: 8 neurons each, ReLU activation
  • Output layer: Size = number of unique intents
  • Loss function: CrossEntropyLoss
  • Optimizer: Adam

Contributing

Contributions are welcome! Feel free to submit issues or open pull requests to improve this chatbot.

About

Contextual chatbot that uses NLP

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages