rasa

package module
v0.0.0-...-fe28d98 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 4, 2021 License: MPL-2.0 Imports: 7 Imported by: 0

README

Rasa SDK - Golang (Work In Progress)

GoDoc License

This package provides an SDK for Rasa chatbots, written in Go.

Rasa SDK Features

This package implements an SDK based on the specifications for Rasa 2.*.

Features:

  • Custom action handlers at /webhook.
  • Custom NLG endpoint at /nlg. (TODO)
  • Supports additional /, /actions, and /health endpoints.
  • Exposes an API similar to the python SDK.
  • Configurable logging and server settings.
  • Code generation utility rasagen for boilerplate and constants, based on Rasa's domain.yaml. (TODO - current version is outdated)
  • Clients for the Rest and Callback webhooks.
  • Callback output channel support.

Notes:

  • Support for kwargs is experimental.

Import

go get go.scarlet.dev/rasa

Godoc

https://pkg.go.dev/go.scarlet.dev/rasa.

License

Unless otherwise specified, code present in this library is licensed under the Mozilla Public License Version v2.0.

Authors

  • Eddy (@scarlet.dev)

Documentation

Index

Constants

View Source
const (
	// DefaultServerPort is the default port for the Rasa Action Server.
	//
	// This port is used for both the `/webhook` and `/nlg` endpoints.
	DefaultServerPort = "5055"

	// DefaultRasaAPIPort is the default port for the Rasa API.
	//
	// Used by the webhooks package.
	DefaultRasaAPIPort = "5005"

	// DefaultRasaEndpoint is the default endpoint used by webhook clients.
	DefaultRasaEndpoint = "http://localhost:5005"
)
View Source
const (
	EventTypeActionExecuted          = EventType("action")
	EventTypeActionExecutionRejected = EventType("action_execution_rejected")
	EventTypeActionReverted          = EventType("undo")
	EventTypeActiveLoop              = EventType("active_loop")
	EventTypeAgentUttered            = EventType("agent")
	EventTypeAllSlotsReset           = EventType("reset_slots")
	EventTypeBotUttered              = EventType("bot")
	EventTypeConversationPaused      = EventType("pause")
	EventTypeConversationResumed     = EventType("resume")
	EventTypeFollowupAction          = EventType("followup")
	EventTypeLoopInterrupted         = EventType("loop_interrupted")
	EventTypeReminderCancelled       = EventType("cancel_reminder")
	EventTypeReminderScheduled       = EventType("reminder")
	EventTypeRestarted               = EventType("restart")
	EventTypeSessionStarted          = EventType("session_started")
	EventTypeSlotSet                 = EventType("slot")
	EventTypeStoryExported           = EventType("export")
	EventTypeUserFeaturization       = EventType("user_featurization")
	EventTypeUserUtteranceReverted   = EventType("rewind")
	EventTypeUserUttered             = EventType("user")
)

TODO

Variables

This section is empty.

Functions

This section is empty.

Types

type ActionExecuted

type ActionExecuted struct {
	Timestamp  Time    `json:"timestamp,omitempty"`
	ActionName string  `json:"name"`
	Policy     string  `json:"policy,omitempty"`
	Confidence float32 `json:"confidence,omitempty"`
}

ActionExecuted TODO

func (*ActionExecuted) MarshalJSON

func (e *ActionExecuted) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (*ActionExecuted) Time

func (e *ActionExecuted) Time() time.Time

Time implements Event.

func (ActionExecuted) Type

func (ActionExecuted) Type() EventType

Type implements Event.

type ActionExecutionRejected

type ActionExecutionRejected struct {
	Timestamp  Time    `json:"timestamp,omitempty"`
	ActionName string  `json:"name"`
	Policy     string  `json:"policy,omitempty"`
	Confidence float32 `json:"confidence,omitempty"`
}

ActionExecutionRejected TODO

func (*ActionExecutionRejected) MarshalJSON

func (e *ActionExecutionRejected) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (ActionExecutionRejected) Time

Time implements Event.

func (ActionExecutionRejected) Type

Type implements Event.

type ActionName

type ActionName string

ActionName TODO

type ActionReverted

type ActionReverted struct {
	Timestamp Time `json:"timestamp,omitempty"`
}

ActionReverted TODO

func (*ActionReverted) MarshalJSON

func (e *ActionReverted) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (ActionReverted) Time

func (e ActionReverted) Time() time.Time

Time implements Event.

func (ActionReverted) Type

func (ActionReverted) Type() EventType

Type implements Event.

type ActiveLoop

type ActiveLoop struct {
	Timestamp Time   `json:"timestamp,omitempty"`
	Name      string `json:"name,omitempty"`
}

ActiveLoop TODO

func (*ActiveLoop) MarshalJSON

func (e *ActiveLoop) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (ActiveLoop) Time

func (e ActiveLoop) Time() time.Time

Time implements Event.

func (ActiveLoop) Type

func (ActiveLoop) Type() EventType

Type implements Event.

type AgentUttered

type AgentUttered struct {
	Timestamp Time    `json:"timestamp,omitempty"`
	Text      string  `json:"test,omitempty"`
	Data      JSONMap `json:"data,omitempty"`
}

AgentUttered TODO

func (*AgentUttered) MarshalJSON

func (e *AgentUttered) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (AgentUttered) Time

func (e AgentUttered) Time() time.Time

Time implements Event.

func (AgentUttered) Type

func (AgentUttered) Type() EventType

Type implements Event.

type AllSlotsReset

type AllSlotsReset struct {
	Timestamp Time `json:"timestamp,omitempty"`
}

AllSlotsReset TODO

func (*AllSlotsReset) MarshalJSON

func (e *AllSlotsReset) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (AllSlotsReset) Time

func (e AllSlotsReset) Time() time.Time

Time implements Event.

func (AllSlotsReset) Type

func (AllSlotsReset) Type() EventType

Type implements Event.

type BotUttered

type BotUttered struct {
	Timestamp Time    `json:"timestamp,omitempty"`
	Text      string  `json:"text,omitempty"`
	Data      JSONMap `json:"data,omitempty"`
	Metadata  JSONMap `json:"metadata,omitempty"`
}

BotUttered TODO

func (*BotUttered) MarshalJSON

func (e *BotUttered) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (BotUttered) Time

func (e BotUttered) Time() time.Time

Time implements Event.

func (BotUttered) Type

func (BotUttered) Type() EventType

Type implements Event.

type Button

type Button struct {
	// Title holds teh text on the button.
	Title string `json:"title"`
	// Payload holds the payload being sent if the button is pressed.
	Payload string `json:"payload"`
}

Button defines the structure of a Button response.

A button can be clicked by the user in a conversation.

type ConversationPaused

type ConversationPaused struct {
	Timestamp Time `json:"timestamp,omitempty"`
}

ConversationPaused TODO

func (*ConversationPaused) MarshalJSON

func (e *ConversationPaused) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (ConversationPaused) Time

func (e ConversationPaused) Time() time.Time

Time implements Event.

func (ConversationPaused) Type

Type implements Event.

type ConversationResumed

type ConversationResumed struct {
	Timestamp Time `json:"timestamp,omitempty"`
}

ConversationResumed TODO

func (*ConversationResumed) MarshalJSON

func (e *ConversationResumed) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (ConversationResumed) Time

func (e ConversationResumed) Time() time.Time

Time implements Event.

func (ConversationResumed) Type

Type implements Event.

type Domain

type Domain struct {
	Config    DomainConfig                     `json:"config"`
	Intents   []IntentDescription              `json:"intents"`
	Entities  []string                         `json:"entities"`
	Slots     map[string]SlotDescription       `json:"slots"`
	Responses map[string][]TemplateDescription `json:"responses"`
	Actions   []string                         `json:"actions"`
}

Domain contains the configuration of the AI's domain as sent to the action server by Rasa's engine.

type DomainConfig

type DomainConfig struct {
	StoreEntitiesAsSlots bool `json:"store_entities_as_slots"`
}

DomainConfig contains domain settings.

type Entity

type Entity struct {
	Start      int         `json:"start"`
	End        int         `json:"end"`
	Value      interface{} `json:"value"`
	Entity     string      `json:"entity"`
	Confidence float64     `json:"confidence"`
	Group      string      `json:"group,omitempty"`
	Role       string      `json:"role,omitempty"`
	Extractor  string      `json:"extractor"`
}

Entity describes an entity and its detected location, value, and confidence.

type EntityName

type EntityName string

EntityName TODO

type Event

type Event interface {
	// Type returns the constant representing the event's type in a marshalled
	// JSON object.
	Type() EventType

	// Timestamp returns the timestamp associated with the event as an instance
	// of time.Time.
	Time() time.Time
}

Event represents a serializable event object.

type EventType

type EventType string

EventType TODO

type Events

type Events []Event

Events implements JSON Unmarshaling of Rasa's Event types.

func (*Events) UnmarshalJSON

func (l *Events) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON implements json.Unmarshaler.

type FollowupAction

type FollowupAction struct {
	Timestamp  Time   `json:"timestamp,omitempty"`
	ActionName string `json:"name"`
}

FollowupAction TODO

func (*FollowupAction) MarshalJSON

func (e *FollowupAction) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (FollowupAction) Time

func (e FollowupAction) Time() time.Time

Time implements Event.

func (FollowupAction) Type

func (FollowupAction) Type() EventType

Type implements Event.

type Intent

type Intent struct {
	Confidence float64 `json:"confidence,omitempty"`
	Name       string  `json:"name,omitempty"`
}

Intent describes an intent and its detected confidence.

type IntentDescription

type IntentDescription struct {
	UseEntities bool `json:"use_entities"`
}

IntentDescription contains a domain intent description.

type IntentName

type IntentName string

IntentName TODO

type JSONMap

type JSONMap = map[string]interface{}

JSONMap is a descriptive type alias for a free-form JSON Object.

type LoopInterrupted

type LoopInterrupted struct {
	Timestamp     Time `json:"timestamp,omitempty"`
	IsInterrupted bool `json:"is_interrupted,omitempty"`
}

LoopInterrupted TODO

func (*LoopInterrupted) MarshalJSON

func (e *LoopInterrupted) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (LoopInterrupted) Time

func (e LoopInterrupted) Time() time.Time

Time implements Event.

func (LoopInterrupted) Type

func (LoopInterrupted) Type() EventType

Type implements Event.

type Message

type Message struct {
	// RecipientID is used by Rasa for messages sent over an output channel.
	RecipientID string `json:"recipient_id,omitempty"`

	// Text contains a text payload.
	Text string `json:"text,omitempty"`

	// Image contains an image payload in the form of an URL. For direct image
	// transfer, use a data-url.
	Image string `json:"image,omitempty"`

	// JSONMessage allows custom data to be included in the payload.
	JSONMessage JSONMap `json:"json_message,omitempty"`

	// Template holds a template name to be used.
	//
	// A client should never consult this field. It is used by NLG and
	// Rasa-internal endpoints to generate responses based on templates when the
	// template identifiers are returned by a custom action server.
	Template string `json:"template,omitempty"`

	// Buttons contains a list of clickable buttons that should be rendered by
	// the UI.
	Buttons []Button `json:"buttons,omitempty"`

	// Attachment.
	Attachment string `json:"attachment,omitempty"`

	// Elements.
	Elements []JSONMap `json:"elements,omitempty"`

	// Kwargs holds additional fields at the root level of the object that are
	// not otherwise provided in the default Message struct.
	//
	// Kwargs are used internally by the action server. Prefer using the
	// JSONMessage field if custom payloads are required to avoid conflicts with
	// the library.
	Kwargs JSONMap `json:"-"` // FIXME implement custom ser/de
}

Message TODO

func (*Message) MarshalJSON

func (m *Message) MarshalJSON() (data []byte, err error)

MarshalJSON implements json.Marshaler.

func (*Message) WithKwargs

func (m *Message) WithKwargs(kwargs JSONMap) *Message

WithKwargs adds the free-form kwargs to the m.Kwargs.

type ParseResult

type ParseResult struct {
	Intent        Intent   `json:"intent"`
	IntentRanking []Intent `json:"intent_ranking,omitempty"`
	Entities      []Entity `json:"entities,omitempty"`
	Text          string   `json:"text,omitempty"`
}

ParseResult holds a processed (parsed) message description.

type ReminderCancelled

type ReminderCancelled struct {
	Timestamp  Time      `json:"timestamp,omitempty"`
	Name       string    `json:"name,omitempty"`
	IntentName string    `json:"intent,omitempty"`
	Entities   []JSONMap `json:"entities,omitempty"`
}

ReminderCancelled TODO

func (*ReminderCancelled) MarshalJSON

func (e *ReminderCancelled) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (ReminderCancelled) Time

func (e ReminderCancelled) Time() time.Time

Time implements Event.

func (ReminderCancelled) Type

Type implements Event.

type ReminderScheduled

type ReminderScheduled struct {
	Timestamp         Time      `json:"timestamp,omitempty"`
	Name              string    `json:"name,omitempty"`
	IntentName        string    `json:"intent,omitempty"`
	Entities          []JSONMap `json:"entities,omitempty"`
	DateTime          time.Time `json:"date_time"`
	KillOnUserMessage bool      `json:"kill_on_user_msg"`
}

ReminderScheduled TODO

func (*ReminderScheduled) MarshalJSON

func (e *ReminderScheduled) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (ReminderScheduled) Time

func (e ReminderScheduled) Time() time.Time

Time implements Event.

func (ReminderScheduled) Type

Type implements Event.

type Restarted

type Restarted struct {
	Timestamp Time `json:"timestamp,omitempty"`
}

Restarted TODO

func (*Restarted) MarshalJSON

func (e *Restarted) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (Restarted) Time

func (e Restarted) Time() time.Time

Time implements Event.

func (Restarted) Type

func (Restarted) Type() EventType

Type implements Event.

type SessionStarted

type SessionStarted struct {
	Timestamp Time `json:"timestamp,omitempty"`
}

SessionStarted TODO

func (*SessionStarted) MarshalJSON

func (e *SessionStarted) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (SessionStarted) Time

func (e SessionStarted) Time() time.Time

Time implements Event.

func (SessionStarted) Type

func (SessionStarted) Type() EventType

Type implements Event.

type SlotDescription

type SlotDescription struct {
	AutoFill     bool     `json:"auto_fill"`
	Type         string   `json:"type"`
	InitialValue string   `json:"initial_value,omitempty"`
	Values       []string `json:"values,omitempty"`
}

SlotDescription contains a domain slot description.

type SlotName

type SlotName string

SlotName TODO

type SlotSet

type SlotSet struct {
	Timestamp Time        `json:"timestamp,omitempty"`
	Key       string      `json:"name"`
	Value     interface{} `json:"value,omitempty"`
}

SlotSet TODO

func (*SlotSet) MarshalJSON

func (e *SlotSet) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (SlotSet) Time

func (e SlotSet) Time() time.Time

Time implements Event.

func (SlotSet) Type

func (SlotSet) Type() EventType

Type implements Event.

type Slots

type Slots map[string]interface{}

Slots is a wrapper type around slots.

func (Slots) Has

func (m Slots) Has(slot string) bool

Has returns true if the Slots contains the requested Slot.

func (Slots) String

func (m Slots) String() string

String implements fmt.Stringer.

String returns a simple json-like representation of the map's values.

func (Slots) Update

func (m Slots) Update(s Slots)

Update will copy the values present in s into m.

type StoryExported

type StoryExported struct {
	Timestamp Time `json:"timestamp,omitempty"`
}

StoryExported TODO

func (*StoryExported) MarshalJSON

func (e *StoryExported) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (StoryExported) Time

func (e StoryExported) Time() time.Time

Time implements Event.

func (StoryExported) Type

func (StoryExported) Type() EventType

Type implements Event.

type TActiveLoop

type TActiveLoop struct {
	Name           string       `json:"name"`
	Validate       *bool        `json:"validate,omitempty"`
	Rejected       bool         `json:"rejected,omitempty"`
	TriggerMessage *ParseResult `json:"trigger_message"`
}

TActiveLoop holds a ActiveLoop description in the Tracker.

func (*TActiveLoop) Is

func (f *TActiveLoop) Is(name string) bool

Is returns whether f represents a Form with the provided name.

func (*TActiveLoop) IsActive

func (f *TActiveLoop) IsActive() bool

IsActive returns whether f represents an active Form.

func (*TActiveLoop) ShouldValidate

func (f *TActiveLoop) ShouldValidate() bool

ShouldValidate returns whether the form should validate itself. It will return true unless validation has been explicitely disabled.

type TemplateDescription

type TemplateDescription struct {
	// the template text
	Text string
}

TemplateDescription contains a domain template description.

type Time

type Time time.Time

Time provides a type alias around time.Time which serializes as an int64 for proper interaction with Rasa.

During both serializing and deserializing, all sub-second precision is dropped.

func (Time) AsTime

func (t Time) AsTime() time.Time

AsTime returns t as an instance of time.Time from the standard library.

func (Time) MarshalJSON

func (t Time) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (*Time) UnmarshalJSON

func (t *Time) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON implements json.Unmarshaler.

type Tracker

type Tracker struct {
	SenderID         string       `json:"sender_id"`
	Slots            Slots        `json:"slots,omitempty"`
	LatestMessage    *ParseResult `json:"latest_message,omitempty"`
	LatestActionName string       `json:"latest_action_name,omitempty"`
	Events           Events       `json:"events"`
	Paused           bool         `json:"paused"`
	FollowupAction   string       `json:"followup_action,omitempty"`
	ActiveLoop       *TActiveLoop `json:"active_loop,omitempty"`
}

Tracker contains the state of the Tracker sent to the action server by the Rasa engine.

func (*Tracker) Entity

func (t *Tracker) Entity(
	entity, role, group string,
) (value interface{})

Entity returns the current value of the requested entity, or nil if the entity has no value.

If the entity has more than one possible value, the first is returned.

func (*Tracker) EntityValues

func (t *Tracker) EntityValues(
	entity, role, group string,
) (values []interface{})

EntityValues returns the current value of the requested entity as a slice. The slice may be empty, and may contain 0 or more entries.

func (*Tracker) HasActiveForm

func (t *Tracker) HasActiveForm() bool

HasActiveForm returns whether the Tracker state represents an active Form.

func (*Tracker) HasActiveLoop

func (t *Tracker) HasActiveLoop() bool

HasActiveLoop returns whether the Tracker state represents an active Loop.

func (*Tracker) HasSlots

func (t *Tracker) HasSlots() bool

HasSlots returns whether there are any Slots present in the Tracker.

func (*Tracker) LatestEntityValues

func (t *Tracker) LatestEntityValues(entity, role, group string) (values []interface{})

LatestEntityValues returns the entity values found for the passed entity name in the latest message.

func (*Tracker) Slot

func (t *Tracker) Slot(name string) (val interface{}, ok bool)

Slot returns the value of the slot as an interface. The `ok` flag indicates whether the slot was present.

func (*Tracker) SlotAs

func (t *Tracker) SlotAs(name string, dst interface{}) (ok bool)

SlotAs attempts to assign the value of the slot to the `dst` pointer. The `ok` flag indicates whether the slot was present, and successfully assigned to `dst`.

Passing a non-assignable type to `dst` leads to undefined behaviour.

func (*Tracker) SlotsToValidate

func (t *Tracker) SlotsToValidate() (slots Slots)

SlotsToValidate returns the slots which were recently set.

This can be used to validate form slots after they were extracted.

type UserFeaturization

type UserFeaturization struct {
	Timestamp               Time `json:"timestamp,omitempty"`
	UseTextForFeaturization bool `json:"use_text_for_featurization"`
}

UserFeaturization TODO

func (*UserFeaturization) MarshalJSON

func (e *UserFeaturization) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (UserFeaturization) Time

func (e UserFeaturization) Time() time.Time

Time implements Event.

func (UserFeaturization) Type

Type implements Event.

type UserUtteranceReverted

type UserUtteranceReverted struct {
	Timestamp Time `json:"timestamp,omitempty"`
}

UserUtteranceReverted TODO

func (*UserUtteranceReverted) MarshalJSON

func (e *UserUtteranceReverted) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (UserUtteranceReverted) Time

func (e UserUtteranceReverted) Time() time.Time

Time implements Event.

func (UserUtteranceReverted) Type

Type implements Event.

type UserUttered

type UserUttered struct {
	Timestamp    Time         `json:"timestamp,omitempty"`
	Text         string       `json:"text,omitempty"`
	ParseData    *ParseResult `json:"parse_data,omitempty"`
	InputChannel string       `json:"input_channel,omitempty"`
	Metadata     JSONMap      `json:"metadata,omitempty"` // experimental
}

UserUttered TODO

func (*UserUttered) MarshalJSON

func (e *UserUttered) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (UserUttered) Time

func (e UserUttered) Time() time.Time

Time implements Event.

func (UserUttered) Type

func (UserUttered) Type() EventType

Type implements Event.

Directories

Path Synopsis
form
Package form implements Form Actions for Rasa.
Package form implements Form Actions for Rasa.
knowledge
Package knowledge implements the Knowledge Base component for Rasa.
Package knowledge implements the Knowledge Base component for Rasa.
cmd
rasagen
rasagen CLI contains code generators that deal with the boilerplate of implementing an ActionServer for Rasa in Go.
rasagen CLI contains code generators that deal with the boilerplate of implementing an ActionServer for Rasa in Go.
Package events provides types for consumers of events passed to event brokers.
Package events provides types for consumers of events passed to event brokers.
examples
helloworld
Package main contains an implementation of a "hello world" example to demo the usage of the Rasa SDK.
Package main contains an implementation of a "hello world" example to demo the usage of the Rasa SDK.
Package nlg implements a framework for Natural Language Generation for Rasa's custom NLG endpoint.
Package nlg implements a framework for Natural Language Generation for Rasa's custom NLG endpoint.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL