This guide takes you through creating a more advanced chatbot that leverages Rasa NLU for natural language understanding and integrates it with a PHP backend for additional functionalities.
1. Setting Up Rasa NLU:
Follow step 1 from the previous guide to set up a new Rasa project and define your training data in data/nlu.md
. Here’s an extended example with additional intents and entities:
## Intent: greet
- hello
- hi
- good morning
## Intent: goodbye
- bye
- see you later
- have a good day
## Intent: weather
- what's the weather like?
- is it going to rain today?
- what's the temperature in {city: CITY}? # Entity example
## Entity: CITY
- (London|New York|Paris)
2. Training the Rasa NLU Model:
Follow step 2 from the previous guide to train the Rasa NLU model based on your training data (rasa train nlu
).
3. Creating the PHP Backend:
This enhanced backend will utilize Guzzle for API communication and introduce additional features:
- Fetching Weather Data: Utilize an external API like OpenWeatherMap to retrieve weather information based on the extracted city entity.
Sample PHP Code (chatbot.php):
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;
$userQuestion = $_POST['user_question'];
$client = new Client(['base_uri' => 'http://localhost:5000/api']);
$nluResponse = $client->request('POST', '/parse', [
'json' => ['text' => $userQuestion],
]);
$nluData = json_decode($nluResponse->getBody(), true);
$intent = $nluData['intent']['name'];
$confidence = $nluData['intent']['confidence'];
$responseMessage = "Sorry, I don't understand.";
if ($intent === "greet") {
$responseMessage = "Hello! How can I help you today?";
} else if ($intent === "goodbye") {
$responseMessage = "Have a good day!";
} else if ($intent === "weather") {
$city = null;
if (isset($nluData['entities']) && !empty($nluData['entities'])) {
foreach ($nluData['entities'] as $entity) {
if ($entity['entity'] === 'CITY') {
$city = $entity['value'];
break;
}
}
}
if ($city) {
// Call OpenWeatherMap API (example using a placeholder API key)
$weatherClient = new Client(['base_uri' => 'https://api.openweathermap.org/data/2.5']);
$apiKey = 'YOUR_OPENWEATHERMAP_API_KEY'; // Replace with your actual API key
$weatherResponse = $weatherClient->request('GET', '/weather', [
'query' => ['q' => $city, 'appid' => $apiKey],
]);
$weatherData = json_decode($weatherResponse->getBody(), true);
if (isset($weatherData['main'])) {
$temp = kelvinToCelsius($weatherData['main']['temp']); // Helper function to convert Kelvin
$responseMessage = "The weather in " . $city . " is currently " . $weatherData['weather'][0]['description'] . ". The temperature is " . $temp . "°C.";
} else {
$responseMessage = "Sorry, I couldn't find weather information for " . $city . ".";
}
} else {
$responseMessage = "To check the weather, please specify a city. For example, 'What's the weather like in London?'";
}
}
echo $responseMessage;
function kelvinToCelsius($kelvin) {
return $kelvin - 273.15; // Conversion formula
}
?>
Explanation:
- The code retrieves the user’s question, sends it for NLU parsing, and extracts the intent and confidence.
- For the “weather” intent, it checks if a city entity is present.
- If a city is found, it utilizes another Guzzle client to call the OpenWeatherMap API (replace with your actual API key).
- The retrieved weather data is used to construct a meaningful response message.
- The
kelvinToCelsius
function is a helper function for temperature conversion (replace with error handling if needed).