Natural Language Processing with Python Updated EditionChapter 143

11.3 Building and Training the Chatbot

Section 3 of 6-~ 12 min read-Synced from Cuantum content

In this section, we will focus on building and training our personal assistant chatbot. This involves implementing the core functionalities, integrating the preprocessed data, and training the chatbot model to understand and respond to user inputs effectively.

11.3.1 Implementing the Core Functionality

The core functionality of our chatbot includes the NLP engine for intent recognition, task management functions, and integration with external APIs. Let's start by implementing the NLP engine, which processes user inputs and determines the corresponding intent.

NLP Engine Implementation

The NLP engine will use the model we trained in the previous section to recognize intents and extract entities from user inputs.

nlp_engine.py:

import jsonimport numpy as npimport tensorflow as tffrom tensorflow.keras.models import load_modelimport nltkfrom nltk.corpus import stopwordsfrom nltk.stem import WordNetLemmatizerimport stringimport pickle # Load pre-trained model and other resourcesmodel = load_model('models/nlp_model.h5')with open('models/vectorizer.pickle', 'rb') as file:    vectorizer = pickle.load(file)with open('models/label_encoder.pickle', 'rb') as file:    label_encoder = pickle.load(file) # Initialize lemmatizerlemmatizer = WordNetLemmatizer() # Define preprocessing functiondef preprocess_text(text):    text = text.lower()    tokens = nltk.word_tokenize(text)    tokens = [word for word in tokens if word not in string.punctuation and word not in stopwords.words('english')]    tokens = [lemmatizer.lemmatize(word) for word in tokens]    return ' '.join(tokens) # Define function to predict intentdef predict_intent(text):    preprocessed_text = preprocess_text(text)    input_vector = vectorizer.transform([preprocessed_text]).toarray()    predictions = model.predict(input_vector)    predicted_intent = label_encoder.inverse_transform([np.argmax(predictions)])    return predicted_intent[0] # Test the NLP engineuser_input = "Can you set a reminder?"predicted_intent = predict_intent(user_input)print(f"Predicted Intent: {predicted_intent}")

In this script, we load the pre-trained model, vectorizer, and label encoder. We define a preprocessing function to normalize text, tokenize it, remove stop words, and lemmatize tokens. The predict_intent function uses the preprocessed text to predict the intent using the trained model.

11.3.2 Integrating External APIs

To enhance the chatbot's functionality, we will integrate external APIs for tasks such as weather updates and general knowledge. Let's implement the integration with a weather API.

API Integration for Weather Updates

We will use the OpenWeatherMap API to fetch weather information.

api_integration.py:

import requests # Define function to get weather updatesdef get_weather(city):    api_key = "your_openweathermap_api_key"    base_url = "<http://api.openweathermap.org/data/2.5/weather?">    complete_url = base_url + "q=" + city + "&appid=" + api_key + "&units=metric"     response = requests.get(complete_url)    data = response.json()     if data["cod"] != "404":        main = data["main"]        weather = data["weather"][0]        temperature = main["temp"]        weather_description = weather["description"]        return f"The temperature in {city} is {temperature}°C with {weather_description}."    else:        return "City not found." # Test the weather API integrationcity = "New York"weather_update = get_weather(city)print(f"Weather Update: {weather_update}")

In this script, we define a function get_weather that takes a city name as input, fetches weather information from the OpenWeatherMap API, and returns a formatted weather update.

11.3.3 Implementing Task Management Functions

Next, we will implement task management functions, such as setting reminders and managing to-do lists.

Task Manager for Reminders

We will create functions to add, view, and delete reminders.

task_manager.py:

import jsonfrom datetime import datetime # Load reminders from filedef load_reminders():    try:        with open('data/reminders.json', 'r') as file:            reminders = json.load(file)    except FileNotFoundError:        reminders = []    return reminders # Save reminders to filedef save_reminders(reminders):    with open('data/reminders.json', 'w') as file:        json.dump(reminders, file, indent=4) # Add a new reminderdef add_reminder(reminder_text, reminder_time):    reminders = load_reminders()    reminder = {        "text": reminder_text,        "time": reminder_time    }    reminders.append(reminder)    save_reminders(reminders)    return "Reminder added successfully." # View all remindersdef view_reminders():    reminders = load_reminders()    if not reminders:        return "You have no reminders."    reminders_str = "\\n".join([f"{reminder['time']}: {reminder['text']}" for reminder in reminders])    return f"Your reminders:\\n{reminders_str}" # Delete a reminderdef delete_reminder(reminder_text):    reminders = load_reminders()    reminders = [reminder for reminder in reminders if reminder['text'] != reminder_text]    save_reminders(reminders)    return "Reminder deleted successfully." # Test the task manager functionsprint(add_reminder("Buy groceries", "2024-06-27 18:00"))print(view_reminders())print(delete_reminder("Buy groceries"))print(view_reminders()) 

In this script, we define functions to load and save reminders from/to a JSON file, add a new reminder, view all reminders, and delete a reminder. These functions will be used by the chatbot to manage user reminders.

11.3.4 Building the Chatbot Interface

Finally, we will build the chatbot interface to interact with users. This can be a simple command-line interface or a web-based interface using Flask.

Chatbot Interface

Let's implement a command-line interface for simplicity.

chatbot_interface.py:

from nlp_engine import predict_intentfrom api_integration import get_weatherfrom task_manager import add_reminder, view_reminders, delete_reminder def chatbot_response(user_input):    intent = predict_intent(user_input)     if intent == "greeting":        return "Hello! How can I assist you today?"    elif intent == "goodbye":        return "Goodbye! Have a great day!"    elif intent == "weather":        return "Which city would you like the weather update for?"    elif intent == "reminder":        return "What would you like to be reminded about and when?"    elif intent == "knowledge":        return "Here's a fun fact: Did you know that honey never spoils?"    else:        return "I'm sorry, I don't understand that. Can you please rephrase?" # Test the chatbot interfacewhile True:    user_input = input("You: ")    if user_input.lower() == "exit":        print("ChatBot: Goodbye! Have a great day!")        break    response = chatbot_response(user_input)    print(f"ChatBot: {response}")

In this script, the chatbot_response function determines the intent of the user input and provides an appropriate response. The chatbot can handle greetings, farewells, weather updates, reminders, and general knowledge queries.

In this section, we built and trained our personal assistant chatbot. We implemented the core functionalities, including the NLP engine for intent recognition, integration with external APIs for weather updates, and task management functions for handling reminders. We also created a simple command-line interface to interact with the chatbot. These components work together to provide a robust and interactive user experience.