This guide demonstrates creating a chatbot that leverages Rasa NLU for intent recognition, interacts with an external API (Amazon Product Advertising API), and utilizes PHP for backend processing.
1. Setting Up Rasa NLU:
Follow the steps from previous guides to set up a Rasa NLU project and define your training data. Here’s an example with a “search” intent for finding products on Amazon:
## Intent: search
- search for {product: PRODUCT} on Amazon
- can you find {product: PRODUCT} on Amazon?
2. Amazon Product Advertising API:
- Sign up for Amazon Associates and obtain your API credentials (Access Key ID and Secret Access Key).
- Explore the Amazon Product Advertising API documentation: https://webservices.amazon.com/paapi5/documentation/
3. Creating the PHP Backend:
- Utilize a library like
aws-sdk-php
for interacting with the Amazon Product Advertising API.
Sample PHP Code (chatbot.php):
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;
use Aws\Ecs\EcsClient; // Example using aws-sdk-php (replace with actual class)
$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 === "search") {
$product = null;
if (isset($nluData['entities']) && !empty($nluData['entities'])) {
foreach ($nluData['entities'] as $entity) {
if ($entity['entity'] === 'PRODUCT') {
$product = $entity['value'];
break;
}
}
}
if ($product) {
// Configure AWS credentials (replace placeholders with your actual credentials)
$ecsClient = new EcsClient([
'version' => 'latest',
'region' => 'us-east-1', // Replace with your desired region
'credentials' => [
'key' => 'YOUR_ACCESS_KEY_ID',
'secret' => 'YOUR_SECRET_ACCESS_KEY',
],
]);
// Replace with actual API call parameters based on Amazon Product Advertising API documentation
$params = [
'Keywords' => $product,
'SearchIndex' => 'Electronics', // Replace with appropriate category
];
try {
$response = $ecsClient->searchItems($params);
$items = $response->get('Item');
if (!empty($items)) {
$responseMessage = "Here are some products I found on Amazon for '" . $product . "':";
foreach ($items as $item) {
$responseMessage .= "\n - " . $item['ItemAttributes']['Title'] . " (Link: " . $item['ItemLinks']['ItemUrl'] . ")";
}
} else {
$responseMessage = "Sorry, I couldn't find any products matching '" . $product . "' on Amazon.";
}
} catch (Exception $e) {
$responseMessage = "An error occurred while searching for products. Please try again later.";
// Implement proper error handling and logging
}
} else {
$responseMessage = "Please specify a product to search for. For example, 'search for headphones on Amazon'";
}
}
echo $responseMessage;
?>
Explanation:
- The code retrieves the user’s question and parses it using Rasa NLU.
- If the intent is “search” and a product entity is found, it utilizes the
aws-sdk-php
library to interact with the Amazon Product Advertising API (replace with the actual API class). - The script configures your AWS credentials and performs a search based on the extracted product name.
- The retrieved product information is used to generate a response message with links.
- Error handling is included to catch potential API issues.
Important Notes:
- Remember to replace placeholders with your actual Amazon Product Advertising API credentials and configure the API call