https://github.com/uncle-lv/jrasa
A Java SDK for Rasa action server
https://github.com/uncle-lv/jrasa
chatbot rasa rasa-sdk
Last synced: about 1 year ago
JSON representation
A Java SDK for Rasa action server
- Host: GitHub
- URL: https://github.com/uncle-lv/jrasa
- Owner: uncle-lv
- License: apache-2.0
- Created: 2023-06-02T14:09:48.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-06-18T02:11:39.000Z (about 3 years ago)
- Last Synced: 2025-05-08T23:48:45.075Z (about 1 year ago)
- Topics: chatbot, rasa, rasa-sdk
- Language: Java
- Homepage:
- Size: 62.5 KB
- Stars: 10
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# JRasa
     
[English](https://github.com/uncle-lv/JRasa/blob/main/README.md) | [δΈζ](https://github.com/uncle-lv/JRasa/blob/main/README_zh.md)
A Java SDK for [Rasa action server](https://rasa.com/docs/rasa/action-server).
## Maven
```xml
io.github
jrasa
0.1.0-SNAPSHOT
```
## Compatibility
Rasa: >= 3.x
## Usage
You should read [the Rasa SDK documentation](https://rasa.com/docs/rasa/action-server) first to figure out the fundamental concepts.
### Running a Rasa SDK Action Server
You can run a Rasa SDK Action Server with the Java web framework you like. There is [a SpringBoot demo](https://github.com/uncle-lv/JRasa/tree/main/src/test/java/examples/springboot) for you.
### Writing Custom Actions
#### Actions
To define a custom action, create a class which implements the interface [Action](https://github.com/uncle-lv/JRasa/blob/main/src/main/java/io/github/jrasa/Action.java).
```java
import io.github.jrasa.Action;
import io.github.jrasa.CollectingDispatcher;
import io.github.jrasa.domain.Domain;
import io.github.jrasa.event.Event;
import io.github.jrasa.exception.RejectExecuteException;
import io.github.jrasa.tracker.Tracker;
import java.util.List;
public class CustomAction implements Action {
@Override
public String name() {
return "action_name";
}
@Override
public List extends Event> run(CollectingDispatcher collectingDispatcher, Tracker tracker, Domain domain) throws RejectExecuteException {
return Action.empty();
}
}
```
π‘ If no events need to be returned, you can use `Action.empty()` to return an empty list.
#### Tracker
##### getSlot
Because Java is a static programming language, you have to assign the type when you get a slot value.
There is five methods to get a slot value:
- `Object getSlot(String key)`
- ` T getSlot(String key, Class type)`
- `String getStringSlot(String key)`
- `Boolean getBoolSlot(String key)`
- `Double getDoubleSlot(String key)`
`Object getSlot(String key)` can get a slot value of any type.
With ` T getSlot(String key, Class type)`, you can assign the type of slot value.
`String getStringSlot(String key)` `Boolean getBoolSlot(String key)` `Double getDoubleSlot(String key)` can get the common type of slot value.
π‘ Decimal numbers in JSON are deserialized to double in Java, so double is used instead of float.
#### Dispatcher
There is a [Message](https://github.com/uncle-lv/JRasa/blob/main/src/main/java/io/github/jrasa/message/Message.java) class to represent responses, because methods don't support default value parameters in Java.
You can build a `Message` instance with Builder like this:
```java
Message message = Message.builder()
.text("Hello")
.image("https://i.imgur.com/nGF1K8f.jpg")
.response("utter_greet")
.attachment("")
.kwargs(new HashMap(){{
put("name", "uncle-lv");
}})
.build();
```
And then send it with `utterMessage`:
```java
dispatcher.utterMessage(message);
```
#### Events
All events are subclasses of abstract class [Event](https://github.com/uncle-lv/JRasa/blob/main/src/main/java/io/github/jrasa/event/Event.java). Their properties are the same as in the documentation. Some of them with many properties should been build with Builder.
##### SlotSet
```java
SlotSet SlotSet = new SlotSet("name", "Mary");
```
##### AllSlotsReset
```java
AllSlotsReset allSlotsReset = new AllSlotsReset();
```
##### ReminderScheduled
```java
ReminderScheduled reminderScheduled = ReminderScheduled.builder("EXTERNAL_dry_plant")
.name("remind_water_plants")
.entities(new ArrayList(){{
add(Entity.builder().entity("plant", "orchid").build());
}})
.triggerDateTime(LocalDateTime.parse("2018-09-03T11:41:10.128172", DateTimeFormatter.ISO_LOCAL_DATE_TIME))
.build();
```
##### ReminderCancelled
```java
ReminderCancelled reminderCancelled = ReminderCancelled.builder()
.name("remind_water_plants")
.build();
```
##### ConversationPaused
```java
ConversationPaused conversationPaused = new ConversationPaused();
```
##### ConversationResumed
```java
ConversationResumed conversationResumed = new ConversationResumed();
```
##### FollowupAction
```java
FollowupAction followupAction = new FollowupAction("action_say_goodbye");
```
##### UserUtteranceReverted
```java
UserUtteranceReverted userUtteranceReverted = new UserUtteranceReverted();
```
##### ActionReverted
```java
ActionReverted actionReverted = new ActionReverted();
```
##### Restarted
```java
Restarted restarted = new Restarted();
```
##### SessionStarted
```java
SessionStarted sessionStarted = new SessionStarted();
```
##### UserUttered
```java
UserUttered userUttered = UserUttered.builder()
.text("Hello bot")
.build();
```
##### BotUttered
```java
BotUttered botUttered = BotUttered.builder()
.text("Hello user")
.build();
```
##### ActionExecuted
```java
ActionExecuted actionExecuted = ActionExecuted.builder("action_greet_user")
.build();
```
#### Special Action Types
##### Knowledge Base Actions
π οΈ Not implemented yet.
##### Slot Validation Actions
There is only one difference from the official SDK.
The methods/functions are named `validate_`/`extract_`(snake case) in the official SDK. In JRasa, they should be named `validate`/`extract`(camel case), as naming convention in Java.
## Contributions
Thank you for any feedback.
## License
[Apache-2.0](https://github.com/uncle-lv/JRasa/blob/main/LICENSE)
## Thanks
- [Rasa](https://github.com/RasaHQ/rasa)