This is a Ruby gem for interacting with Rasa Core.
Add this line to your application's Gemfile:
gem 'rasa_core'
And then execute:
$ bundle
Or install it yourself as:
$ gem install rasa_core
require 'rasa_core'
client = RasaCore::Client.new(server:'172.18.0.3', port:5005)
client.check_health
#=> {:success=>true, :timed_out=>false, :return_message=>"No error", :code=>200, :body=>"hello from Rasa Core: 0.11.3"}
The response is always a hash having five key/values
success
- boolean true or falsetimed_out
- boolean true or falsecode
- HTTP response codereturn_message
- a return message in could not get an http responsebody
- body response, may be in a simple string form, RubyOpenStruct
object, or a JSON format. By default body is anOpenStruct
object.
The check_health
methods return a simple 'Hello' string if Core is running.
client.version
#=> {:success=>true, :timed_out=>false, :return_message=>"No error", :code=>200, :body=>#<OpenStruct minimum_compatible_version="0.11.0", version="0.11.3">}
client.status
#=> {:success=>true, :timed_out=>false, :return_message=>"No error", :code=>200, :body=>#<OpenStruct is_ready=true, model_fingerprint="12c788db30b74a5a8eb88a640aea9ce6">}
We can change the response format
client.response_format = :json
client.version
#=> {:success=>true, :timed_out=>false, :return_message=>"No error", :code=>200, :body=>"{\"minimum_compatible_version\":\"0.11.0\",\"version\":\"0.11.3\"}\n"}
client.status
#=> {:success=>true, :timed_out=>false, :return_message=>"No error", :code=>200, :body=>"{\"is_ready\":true,\"model_fingerprint\":\"12c788db30b74a5a8eb88a640aea9ce6\"}\n"}
To send a message to Core
client.send_message(message:'hello')
#=> {:success=>true, :timed_out=>false, :return_message=>"No error", :code=>200, :body=>[#<OpenStruct recipient_id="default", text="Greetings!">]}
We can also specify a sender id when sending a message
client.send_message(message:'hello', sender_id: 'bayram')
=> {:success=>true, :timed_out=>false, :return_message=>"No error", :code=>200, :body=>[#<OpenStruct recipient_id="bayram", text="Hi there, friend!">]}
Set and access slots as following
client.append_slot(sender_id: 'bayram_123', name: "name", value: 'bayram')
client.append_slot(sender_id: 'bayram_123', name: "email", value: '[email protected]')
resp = client.conversation_tracker(sender_id: 'bayram_123')
resp[:body].slots.email
# => "[email protected]"
resp[:body].slots.name
# => "bayram"
Assume that core works on 172.18.0.3:5005
. Let's chat with our bot.
client = RasaCore::Client.new(server:'172.18.0.3', port:5005)
resp = client.send_message(sender_id: 'bayram_123', message:'Hi')
puts "Bot says: #{resp[:body].first.text}"
# => Bot says: Hi!
resp = client.send_message(sender_id: 'bayram_123', message:'Let us chat')
puts "Bot says: #{resp[:body].first.text}"
# => Bot says: Sure. I'd be happy to. What's up?
We can also interact with Rasa Core from a Ruby-On-Rails application. The following screenshots are from a simple RoR application for sending to and receiving messages from a core server.
Simple sends a GET request http://<server>:<port>/
and gets a hello
response in case the core server is running.
Receives information about the currently loaded agent.
Receives metadata about the running Core instance.
Sends a user message
Arguments: sender_id
, message
For example client.send_message(sender_id: '[email protected]', message: 'Hello')
Retrieve a conversations tracker
Arguments: sender_id
, include_events
Retrieve a conversations tracker
Arguments: sender_id
, include_events
, timestamp
, name
, value
Not all functionalities are implemented yet. I will update the gem gradually by adding other methods to interact with Rasa Core. I'd also be glad to get feedback and suggestions.
Bug reports and pull requests are welcome on GitHub at https://github.com/fade2black/rasa_core. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the RasaCore project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.