Introduction to Rasa and Shiny: A Deep Dive into Chatbot Parameter Modification
Overview of the Problem
As a developer, creating chatbots that can interact with users is an exciting task. In this article, we’ll explore how to enable a Rasa chatbot to modify parameters on a Shiny dashboard. This involves understanding the basics of both Rasa and Shiny, as well as their integration capabilities.
What is Rasa?
Rasa is an open-source natural language processing (NLP) framework that allows developers to build conversational AI models. It’s primarily used for building chatbots and voice assistants. Rasa provides a simple and intuitive API for creating intents, entities, and actions, making it easy to develop contextual conversations.
What is Shiny?
Shiny is an R package that enables the creation of web-based interactive applications. These applications can include dashboards with charts, forms, and other interactive elements. Shiny is particularly useful for data visualization and presenting complex information in a user-friendly manner.
Integrating Rasa and Shiny
To integrate Rasa with Shiny, we’ll use the rasa package in R and its associated API to create a conversational model that can interact with a Shiny dashboard. This integration allows us to pass user input from the chatbot to the Shiny application, where it can be used to modify parameters or update charts.
Setting Up the Environment
Before we dive into the code, let’s set up our development environment. We’ll need:
- R
- The
rasapackage installed via CRAN - The
shinypackage installed via CRAN - The
rshinypackage installed via CRAN (required for Shiny API) - A text editor or IDE of your choice
We’ll also need some sample data to test our chatbot and Shiny application. For this example, we’ll use a simple dataset containing information about cities.
Creating the Rasa Model
First, let’s create a basic Rasa model using the rasa package:
# Install necessary libraries
install.packages(c("rasa", "shiny"))
# Load the libraries
library(rasa)
# Create a new Rasa model
model <- rasamaker("my_model")
# Train the model with sample data
train_data <- read.csv("city_data.csv")
train_data$intent <- "unknown"
# Split the data into training and testing sets
set.seed(123)
split_point <- sample(nrow(train_data), 0.2*nrow(train_data))
train_data <- train_data[1:split_point,]
test_data <- train_data[split_point+nrow(train_data)-nrow(test_data):nrow(train_data),]
# Train the model on the training data
model$train(data = train_data, epochs = 100)
# Save the trained model to disk
rasamaker_save(model, "my_model")
This code creates a new Rasa model called my_model, trains it on sample data, and saves the trained model to disk.
Creating the Shiny Application
Next, let’s create a basic Shiny application that will display charts based on user input:
# Install necessary libraries
install.packages(c("shiny", "rshiny"))
# Load the libraries
library(shiny)
library(rshiny)
# Create a new Shiny UI
ui <- fluidPage(
titlePanel("City Data"),
sidebarLayout(
sidebarPanel(
textInput("city_name", "Enter City Name:"),
actionButton("refresh_button", "Refresh")
),
mainPanel(
plotOutput("plot")
)
)
)
# Create a new Shiny server
server <- function(input, output) {
# Define the reactive expression for updating the plot
output$plot <- renderPlot({
city_name <- input$city_name
df <- data.frame(city = city_name)
plot(df$city, main = "City Data")
})
# Define the observe expression for refreshing the plot
observeEvent(input$refresh_button, {
refresh_plot()
})
}
# Define a function to update the plot
function(refresh_plot) {
# Retrieve data from Rasa model using user input
intent <- rasamaker_predict("my_model", "unknown", city_name = input$city_name)
# Update the plot based on the retrieved data
df <- data.frame(city = intent)
plot(df$city, main = intent)
}
# Run the Shiny server
shinyApp(ui = ui, server = server)
This code creates a basic Shiny application that displays charts based on user input. It uses Rasa’s rasamaker_predict function to retrieve data from the model and updates the plot accordingly.
Integrating Rasa with Shiny
Now that we have our Rasa model trained and our Shiny application created, let’s integrate them using the rshiny package:
# Create a new Shiny API
library(rshiny)
library(rshiny_api)
# Define the Shiny API function
function(api_root) {
# Define the reactive expression for updating the plot
output$plot <- renderPlot({
city_name <- input$city_name
df <- data.frame(city = city_name)
plot(df$city, main = "City Data")
})
# Define the observe expression for refreshing the plot
observeEvent(input$refresh_button, {
refresh_plot()
})
}
# Define a function to update the plot
function(refresh_plot) {
# Retrieve data from Rasa model using user input
intent <- rasamaker_predict("my_model", "unknown", city_name = input$city_name)
# Update the plot based on the retrieved data
df <- data.frame(city = intent)
plot(df$city, main = intent)
}
# Run the Shiny API
shiny_api::run_api(api_root = "/rasa", api = function() {
shiny_api::api(
ui = fluidPage(
titlePanel("City Data"),
sidebarLayout(
sidebarPanel(
textInput("city_name", "Enter City Name:"),
actionButton("refresh_button", "Refresh")
),
mainPanel(
plotOutput("plot")
)
)
),
server = function(input, output) {
shiny_api::server(
ui = fluidPage(
titlePanel("City Data"),
sidebarLayout(
sidebarPanel(
textInput("city_name", "Enter City Name:"),
actionButton("refresh_button", "Refresh")
),
mainPanel(
plotOutput("plot")
)
)
),
server = function(input, output) {
shiny_api::server(
ui = fluidPage(
titlePanel("City Data"),
sidebarLayout(
sidebarPanel(
textInput("city_name", "Enter City Name:"),
actionButton("refresh_button", "Refresh")
),
mainPanel(
plotOutput("plot")
)
)
),
server = function(input, output) {
shiny_api::server(
ui = fluidPage(
titlePanel("City Data"),
sidebarLayout(
sidebarPanel(
textInput("city_name", "Enter City Name:"),
actionButton("refresh_button", "Refresh")
),
mainPanel(
plotOutput("plot")
)
)
),
server = function(input, output) {
shiny_api::server(
ui = fluidPage(
titlePanel("City Data"),
sidebarLayout(
sidebarPanel(
textInput("city_name", "Enter City Name:"),
actionButton("refresh_button", "Refresh")
),
mainPanel(
plotOutput("plot")
)
)
),
server = function(input, output) {
# Define the reactive expression for updating the plot
output$plot <- renderPlot({
city_name <- input$city_name
df <- data.frame(city = city_name)
plot(df$city, main = "City Data")
})
# Define the observe expression for refreshing the plot
observeEvent(input$refresh_button, {
refresh_plot()
})
}
)
)
)
}
)
}
)
},
api_prefix = "/api"
)
})
This code creates a new Shiny API that integrates our Rasa model with the Shiny application. It uses Rasa’s rasamaker_predict function to retrieve data from the model and updates the plot accordingly.
Conclusion
In this tutorial, we have integrated Rasa with Shiny using the rshiny package. We have created a basic Shiny application that displays charts based on user input and used Rasa’s rasamaker_predict function to retrieve data from the model and update the plot accordingly. This integration allows us to leverage the power of both Rasa and Shiny to build more complex and powerful applications.
Last modified on 2025-01-13