RecastAI 2.0.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +19 -234
- data/lib/recastai.rb +2 -0
- data/lib/recastai/action.rb +21 -0
- data/lib/recastai/client.rb +44 -8
- data/lib/recastai/conversation.rb +182 -0
- data/lib/recastai/entity.rb +2 -0
- data/lib/recastai/intent.rb +2 -0
- data/lib/recastai/response.rb +2 -0
- data/lib/recastai/utils.rb +3 -3
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c0cfad488e50151e132f8abb790be62c97652ded
|
4
|
+
data.tar.gz: 79e4585edf1ad61f8d140a536bdf1f9e8fda883e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 534d1820d0711cf6cced848521a7fe2aee894f9c4e8b9b2fc8cc07aae1fbaeb25e525f1f00c51513ac328044425d712189bba66fedaa6f6708874cd9509a6bf4
|
7
|
+
data.tar.gz: e25c33badd056b347f23a5584492773f47d69ebc7e002c1af3758d403fb727c8179c9f075b21a568bcb4f3dfe527248721e4c7d0d288ad8f4805f59a28dd34fb
|
data/README.md
CHANGED
@@ -1,21 +1,16 @@
|
|
1
|
-
# Recast.AI - Ruby
|
1
|
+
# Recast.AI - SDK Ruby
|
2
2
|
|
3
|
-
[
|
3
|
+
[logo]: https://github.com/RecastAI/SDK-Ruby/blob/master/misc/logo-inline.png "Recast.AI"
|
4
4
|
|
5
|
-
![logo]
|
6
|
-
|
7
|
-
Recast.AI official SDK in Ruby.
|
5
|
+
![alt text][logo]
|
8
6
|
|
7
|
+
Recast.AI official SDK in Ruby
|
9
8
|
|
10
9
|
## Synospis
|
11
10
|
|
12
|
-
This
|
13
|
-
|
14
|
-
|
15
|
-
## Requirements
|
16
|
-
|
17
|
-
* Ruby 2.2+
|
18
|
-
|
11
|
+
This module is a wrapper around the [Recast.AI](https://recast.ai) API, and allows you to:
|
12
|
+
* [build a bot](https://github.com/RecastAI/SDK-Ruby/wiki/Build-your-bot)
|
13
|
+
* [analyze your text](https://github.com/RecastAI/SDK-Ruby/wiki/Analyse-text)
|
19
14
|
|
20
15
|
## Installation
|
21
16
|
|
@@ -23,236 +18,26 @@ This gem is a pure Ruby interface to the [Recast.AI](https://recast.ai) API. It
|
|
23
18
|
gem install 'RecastAI'
|
24
19
|
```
|
25
20
|
|
26
|
-
##
|
27
|
-
|
28
|
-
```ruby
|
29
|
-
require 'recastai'
|
21
|
+
## Documentation
|
30
22
|
|
31
|
-
|
32
|
-
|
33
|
-
# text request
|
34
|
-
response = client.text_request(YOUR_TEXT)
|
35
|
-
if response.intent.slug == YOUR_EXPECTED_INTENT
|
36
|
-
# Do your code...
|
37
|
-
end
|
38
|
-
```
|
23
|
+
You can find the full documentation [here](https://github.com/RecastAI/SDK-Ruby/wiki).
|
39
24
|
|
40
25
|
## Specs
|
41
26
|
|
42
27
|
### Classes
|
43
28
|
|
44
|
-
This
|
45
|
-
|
46
|
-
* RecastAI::Client is the client allowing you to make requests.
|
47
|
-
* RecastAI::Response contains the response from [Recast.AI](https://recast.ai).
|
48
|
-
* RecastAI::Intent represents an intent of the response.
|
49
|
-
* RecastAI::Entity represents an entity found by Recast.AI in your user's input.
|
50
|
-
* RecastAI::RecastError is the error thrown by the gem.
|
51
|
-
|
52
|
-
Don't hesitate to dive into the code, it's commented :)
|
53
|
-
|
54
|
-
## RecastAI::Client
|
55
|
-
|
56
|
-
The Client can be instanciated with a token and a language (both optional)
|
57
|
-
|
58
|
-
```ruby
|
59
|
-
client = RecastAI::Client.new(YOUR_TOKEN, YOUR_LANGUAGE)
|
60
|
-
```
|
61
|
-
|
62
|
-
__Your tokens__
|
63
|
-
|
64
|
-
[token]: https://github.com/RecastAI/SDK-NodeJs/blob/master/misc/recast-ai-tokens.png "Tokens"
|
65
|
-
|
66
|
-
![alt text][token]
|
67
|
-
|
68
|
-
*Copy paste your request access token from your bot's settings*
|
69
|
-
|
70
|
-
__Your language__
|
71
|
-
|
72
|
-
```ruby
|
73
|
-
client = RecastAI::Client.new(YOUR_TOKEN, 'en')
|
74
|
-
```
|
75
|
-
|
76
|
-
*The language is a lowercase 639-1 isocode.*
|
77
|
-
|
78
|
-
## Text Request
|
79
|
-
|
80
|
-
text_request(text, options = {})
|
81
|
-
|
82
|
-
If you pass a token or a language in the options parameter, it will override your default client language or token
|
83
|
-
|
84
|
-
```ruby
|
85
|
-
response = client.text_request(YOUR_TEXT)
|
86
|
-
|
87
|
-
if response.intent == YOUR_EXPECTED_INTENT
|
88
|
-
# Do your code...
|
89
|
-
end
|
90
|
-
```
|
91
|
-
|
92
|
-
```ruby
|
93
|
-
# With optional parameters
|
94
|
-
response = client.text_request(YOUR_TEXT, { token: YOUR_TOKEN, language: YOUR_LANGUAGE })
|
95
|
-
```
|
96
|
-
|
97
|
-
__If a language is provided:__ the language you've given is used for processing if your bot has expressions for it, else your bot's primary language is used.
|
98
|
-
|
99
|
-
__If no language is provided:__ the language of the text is detected and is used for processing if your bot has expressions for it, else your bot's primary language is used for processing.
|
100
|
-
|
101
|
-
## File request
|
102
|
-
|
103
|
-
file_request(file, options = {})
|
104
|
-
|
105
|
-
If you pass a token or a language in the option parameter, it will override your default client language or token.
|
106
|
-
|
107
|
-
__file format: .wav__
|
108
|
-
```ruby
|
109
|
-
response = client.file_request(File.new(File.join(File.dirname(__FILE__),YOUR_FILE)))
|
110
|
-
|
111
|
-
if response.intent.slug == YOUR_EXPECTED_INTENT
|
112
|
-
# Do your code...
|
113
|
-
end
|
114
|
-
```
|
115
|
-
|
116
|
-
```ruby
|
117
|
-
# with optional parameters
|
118
|
-
response = client.file_request(File.new(File.join(File.dirname(__FILE__),YOUR_FILE)), { token: YOUR_TOKEN, language: YOUR_LANGUAGE })
|
119
|
-
```
|
120
|
-
|
121
|
-
__If a language is provided:__ the language you've given is used for processing if your bot has expressions for it, else your bot's primary language is used
|
122
|
-
|
123
|
-
__If no language is provided:__ your bot's primary language is used for processing as we do not provide language detection for speech.
|
124
|
-
|
125
|
-
|
126
|
-
## RecastAI::Response
|
127
|
-
|
128
|
-
The Response is generated after a call to either file_request or text_request.
|
129
|
-
|
130
|
-
### Get the first detected intent
|
131
|
-
|
132
|
-
| Method | Params | Return |
|
133
|
-
| ------------- |:------:| :-------------------------|
|
134
|
-
| intent() | | the first detected intent |
|
135
|
-
|
136
|
-
```ruby
|
137
|
-
response = client.text_request(YOUR_TEXT)
|
138
|
-
|
139
|
-
if response.intent.slug == YOUR_EXPECTED_INTENT
|
140
|
-
# Do your code...
|
141
|
-
end
|
142
|
-
```
|
143
|
-
|
144
|
-
### Get the first entity matching name
|
145
|
-
|
146
|
-
| Method | Params | Return |
|
147
|
-
| ---------- |:-------------:| :------------------------|
|
148
|
-
| get(name) | name: String | the first Entity matched |
|
149
|
-
|
150
|
-
```ruby
|
151
|
-
response = client.text_request(YOUR_TEXT)
|
152
|
-
|
153
|
-
location = response.get('location')
|
154
|
-
```
|
155
|
-
|
156
|
-
### Get all entities matching name
|
157
|
-
|
158
|
-
| Method | Params | Return |
|
159
|
-
| ---------- |:-------------:| :------------------------|
|
160
|
-
| all(name) | name: String | all the Entities matched |
|
161
|
-
|
162
|
-
```ruby
|
163
|
-
response = client.text_request(YOUR_TEXT)
|
164
|
-
|
165
|
-
locations = response.all('location')
|
166
|
-
```
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
### Act helpers
|
171
|
-
|
172
|
-
| Method | Params | Return |
|
173
|
-
| ------------- |:------:| :----------------------------------------------------- |
|
174
|
-
| assert? | | Bool: whether or not the sentence is an assertion |
|
175
|
-
| command? | | Bool: whether or not the sentence is a command |
|
176
|
-
| wh\_query? | | Bool: whether or not the sentence is a question |
|
177
|
-
| yn\_query? | | Bool: whether or not the sentence is a query |
|
178
|
-
|
179
|
-
### Type helpers
|
180
|
-
|
181
|
-
| Method | Params | Return |
|
182
|
-
| ------------- |:------:| :----------------------------------------------------- |
|
183
|
-
| abbreviation? | | Bool: is the answer of the sentence an abbreviation? |
|
184
|
-
| entity? | | Bool: is the answer of the sentence an entity? |
|
185
|
-
| description? | | Bool: is the answer of the sentence an description? |
|
186
|
-
| human? | | Bool: is the answer of the sentence an human? |
|
187
|
-
| location? | | Bool: is the answer of the sentence a location? |
|
188
|
-
| number? | | Bool: is the answer of the sentence an number? |
|
189
|
-
|
190
|
-
### Sentiment helpers
|
191
|
-
|
192
|
-
| Method | Params | Return |
|
193
|
-
| ------------- |:------:| :----------------------------------------------------- |
|
194
|
-
| vpositive? | | Bool: is the sentence very positive? |
|
195
|
-
| positive? | | Bool: is the sentence positive? |
|
196
|
-
| neutral? | | Bool: is the sentence neutral? |
|
197
|
-
| negative? | | Bool: is the sentence negative? |
|
198
|
-
| vnegative? | | Bool: is the sentence very negative? |
|
199
|
-
|
200
|
-
### Getters
|
201
|
-
|
202
|
-
Each of the following methods corresponds to a Response attribute
|
203
|
-
|
204
|
-
| Method | Params | Return |
|
205
|
-
| ------------- |:------:| :---------------------------------------------------|
|
206
|
-
| raw() | | String: the raw unparsed json response |
|
207
|
-
| uuid() | | String: the universal unique id of the request |
|
208
|
-
| source() | | String: the user input |
|
209
|
-
| intents() | | Array[Intent]: all the matched intents |
|
210
|
-
| act() | | String: the act of the sentence |
|
211
|
-
| type() | | String: the type of the sentence |
|
212
|
-
| sentiment() | | String: the sentiment of the sentence |
|
213
|
-
| entities() | | Array[Entity]: all the detected entities |
|
214
|
-
| language() | | String: the language of the sentence |
|
215
|
-
| version() | | String: the version of the json |
|
216
|
-
| timestamp() | | String: the timestamp at the end of the processing |
|
217
|
-
| status() | | String: the status of the response |
|
218
|
-
|
219
|
-
## RecastAI::Intent
|
220
|
-
|
221
|
-
Each of the following methods corresponds to an Intent attribute
|
222
|
-
|
223
|
-
| Attributes | Description |
|
224
|
-
| ----------- |:--------------------------------------------------------------|
|
225
|
-
| slug | String: the slug of the intent |
|
226
|
-
| confidence | Float: the unparsed json value of the intent |
|
227
|
-
|
228
|
-
## RecastAI::Entity
|
229
|
-
|
230
|
-
Each of the following methods corresponds to an Entity attribute
|
231
|
-
|
232
|
-
| Attributes | Description |
|
233
|
-
| ----------- |:--------------------------------------------------------------|
|
234
|
-
| name | String: the name of the entity |
|
235
|
-
| raw | String: the raw value extracted from the sentence |
|
236
|
-
| confidence | Float: the detection score between 0 and 1 excluded |
|
237
|
-
|
238
|
-
In addition to those methods, more attributes are generated depending of the nature of the entity.
|
239
|
-
The full list can be found there: [man.recast.ai](https://man.recast.ai/#list-of-entities)
|
240
|
-
|
241
|
-
```ruby
|
242
|
-
response = client.text_request(YOUR_TEXT)
|
243
|
-
|
244
|
-
location = response.get('location')
|
245
|
-
|
246
|
-
puts location.raw
|
247
|
-
puts location.name
|
248
|
-
```
|
249
|
-
|
250
|
-
## RecastAI::RecastError
|
251
|
-
|
252
|
-
The Recast.AI Error is thrown when receiving an non-200 response from Recast.AI.
|
29
|
+
This module contains 7 classes, as follows:
|
253
30
|
|
254
|
-
|
31
|
+
* [Client](https://github.com/RecastAI/SDK-Ruby/wiki/Class-Client) is the client allowing you to make requests.
|
32
|
+
* [Response](https://github.com/RecastAI/SDK-Ruby/wiki/Class-Response) wraps the response from a call to [Recast.AI](https://recast.ai) API with the text_request or file_request Client methods.
|
33
|
+
* [Conversation](https://github.com/RecastAI/SDK-Ruby/wiki/Class-Conversation) wraps the response from a call to [Recast.AI](https://recast.ai) API with the text_converse Client method.
|
34
|
+
* [Action](https://github.com/RecastAI/SDK-Ruby/wiki/Class-Action) represents an action to do when using the text_converse method.
|
35
|
+
* [Intent](https://github.com/RecastAI/SDK-Ruby/wiki/Class-Intent) represents an intent matched when using either the text_request, file_request or text_converse methods.
|
36
|
+
* [Entity](https://github.com/RecastAI/SDK-Ruby/wiki/Class-Entity) represents an entity extracted from an input.
|
37
|
+
* [RecastError](https://github.com/RecastAI/SDK-Ruby/wiki/Class-RecastError) is the error returned by the module.
|
255
38
|
|
39
|
+
Don't hesitate to dive into the code, it's commented ;)
|
40
|
+
`
|
256
41
|
## More
|
257
42
|
|
258
43
|
You can view the whole API reference at [man.recast.ai](https://man.recast.ai).
|
data/lib/recastai.rb
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
##
|
4
|
+
# This class builds an action used by RecastAI:::Conversation
|
5
|
+
module RecastAI
|
6
|
+
class Action
|
7
|
+
attr_reader :slug
|
8
|
+
attr_reader :done
|
9
|
+
attr_reader :reply
|
10
|
+
|
11
|
+
def initialize(action)
|
12
|
+
@slug = action['slug']
|
13
|
+
@done = action['done']
|
14
|
+
@reply = action['reply']
|
15
|
+
end
|
16
|
+
|
17
|
+
def done?
|
18
|
+
@done
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/recastai/client.rb
CHANGED
@@ -1,14 +1,50 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
+
##
|
4
|
+
# This class is the main entrypoint to Recast.AI
|
5
|
+
# When using *_converse, a RecastAI::Conversation object is returned
|
6
|
+
# When using *_request, a RecastAI::Response object is returned
|
3
7
|
module RecastAI
|
4
8
|
class Client
|
5
9
|
attr_accessor :token
|
10
|
+
attr_accessor :language
|
6
11
|
|
7
12
|
def initialize(token = nil, language = nil)
|
8
13
|
@token = token
|
9
14
|
@language = language
|
10
15
|
end
|
11
16
|
|
17
|
+
##
|
18
|
+
# Perform a text converse to Recast.AI
|
19
|
+
#
|
20
|
+
# * *Args* :
|
21
|
+
# - +text+ - String, the text to process
|
22
|
+
# - +options+ - Hash, request's options
|
23
|
+
# * *Returns* :
|
24
|
+
# - An instance of Conversation
|
25
|
+
# * *Throws* :
|
26
|
+
# - RecastError
|
27
|
+
def text_converse(text, options = {})
|
28
|
+
token = options[:token] || @token
|
29
|
+
raise(RecastError.new('Token is missing')) if token.nil?
|
30
|
+
|
31
|
+
language = options[:language] || @language
|
32
|
+
|
33
|
+
body = { text: text }
|
34
|
+
body[:language] = language unless language.nil?
|
35
|
+
body[:conversation_token] = options[:conversation_token] unless options[:conversation_token].nil?
|
36
|
+
body[:memory] = options[:memory] unless options[:memory].nil?
|
37
|
+
|
38
|
+
response = HTTParty.post(
|
39
|
+
Utils::CONVERSE_ENDPOINT,
|
40
|
+
body: body,
|
41
|
+
headers: { Authorization: "Token #{token}" }
|
42
|
+
)
|
43
|
+
raise(RecastError.new(response.message)) if response.code != 200
|
44
|
+
|
45
|
+
Conversation.new(response.body)
|
46
|
+
end
|
47
|
+
|
12
48
|
##
|
13
49
|
# Perform a text request to Recast.AI
|
14
50
|
#
|
@@ -25,12 +61,12 @@ module RecastAI
|
|
25
61
|
|
26
62
|
language = options[:language] || @language
|
27
63
|
|
28
|
-
body = {
|
29
|
-
body[
|
64
|
+
body = { text: text }
|
65
|
+
body[:language] = language unless language.nil?
|
30
66
|
response = HTTParty.post(
|
31
|
-
Utils::
|
67
|
+
Utils::REQUEST_ENDPOINT,
|
32
68
|
body: body,
|
33
|
-
headers: {
|
69
|
+
headers: { Authorization: "Token #{token}" }
|
34
70
|
)
|
35
71
|
raise(RecastError.new(response.message)) if response.code != 200
|
36
72
|
|
@@ -53,12 +89,12 @@ module RecastAI
|
|
53
89
|
|
54
90
|
language = options[:language] || @language
|
55
91
|
|
56
|
-
body = {
|
57
|
-
body[
|
92
|
+
body = { voice: File.new(file) }
|
93
|
+
body[:language] = language unless language.nil?
|
58
94
|
response = HTTMultiParty.post(
|
59
|
-
Utils::
|
95
|
+
Utils::REQUEST_ENDPOINT,
|
60
96
|
body: body,
|
61
|
-
headers: {
|
97
|
+
headers: { Authorization: "Token #{token}" }
|
62
98
|
)
|
63
99
|
raise(RecastError.new(response.message)) if response.code != 200
|
64
100
|
|
@@ -0,0 +1,182 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
##
|
4
|
+
# This class builds a conversation from *_converse
|
5
|
+
module RecastAI
|
6
|
+
class Conversation
|
7
|
+
attr_reader :raw
|
8
|
+
attr_reader :uuid
|
9
|
+
attr_reader :source
|
10
|
+
attr_reader :replies
|
11
|
+
attr_reader :action
|
12
|
+
attr_reader :next_actions
|
13
|
+
attr_reader :memory
|
14
|
+
attr_reader :entities
|
15
|
+
attr_reader :intents
|
16
|
+
attr_reader :conversation_token
|
17
|
+
attr_reader :language
|
18
|
+
attr_reader :version
|
19
|
+
attr_reader :timestamp
|
20
|
+
attr_reader :status
|
21
|
+
|
22
|
+
def initialize(response)
|
23
|
+
@raw = response
|
24
|
+
|
25
|
+
response = JSON.parse(response)
|
26
|
+
response = response['results']
|
27
|
+
|
28
|
+
@uuid = response['uuid']
|
29
|
+
@source = response['source']
|
30
|
+
@replies = response['replies']
|
31
|
+
@action = Action.new(response['action'])
|
32
|
+
@next_actions = response['next_actions'].map{ |i| Action.new(i) }
|
33
|
+
@memory = response['memory'].select{ |n, e| !e.nil? }.map{ |n, e| Entity.new(n, e) }
|
34
|
+
@entities = response['entities'].flat_map{ |n, e| e.map{ |ee| Entity.new(n, ee) } }
|
35
|
+
@intents = response['intents'].map{ |i| Intent.new(i) }
|
36
|
+
@conversation_token = response['conversation_token']
|
37
|
+
@language = response['language']
|
38
|
+
@version = response['version']
|
39
|
+
@timestamp = response['timestamp']
|
40
|
+
@status = response['status']
|
41
|
+
end
|
42
|
+
|
43
|
+
##
|
44
|
+
# Returns the first reply provided there is one
|
45
|
+
#
|
46
|
+
# * *Args* :
|
47
|
+
# * *Returns* :
|
48
|
+
# - A String or nil
|
49
|
+
def reply
|
50
|
+
@replies.any? ? @replies.first : nil
|
51
|
+
end
|
52
|
+
|
53
|
+
##
|
54
|
+
# Returns the first next action provided there is one
|
55
|
+
#
|
56
|
+
# * *Args* :
|
57
|
+
# * *Returns* :
|
58
|
+
# - An instance of Action or nil
|
59
|
+
def next_action
|
60
|
+
@next_actions.any? ? @next_actions.first : nil
|
61
|
+
end
|
62
|
+
|
63
|
+
##
|
64
|
+
# Returns the replies joined with sep
|
65
|
+
#
|
66
|
+
# * *Args* :
|
67
|
+
# - +sep+ - String, the separator (default: ' ')
|
68
|
+
# * *Returns* :
|
69
|
+
# - A String or nil
|
70
|
+
def joined_replies(sep=' ')
|
71
|
+
@replies.join(sep)
|
72
|
+
end
|
73
|
+
|
74
|
+
##
|
75
|
+
# Returns the first entity in the memory name matches the parameter
|
76
|
+
# If no name is provided, returns the full memory
|
77
|
+
#
|
78
|
+
# * *Args* :
|
79
|
+
# - +key+ - String, the memory's field name
|
80
|
+
# * *Returns* :
|
81
|
+
# - An instance of Entity or a Memory
|
82
|
+
def get_memory(key=nil)
|
83
|
+
return @memory if key.nil?
|
84
|
+
|
85
|
+
@memory.each do |entity|
|
86
|
+
return entity if entity.name.casecmp(key.to_s) == 0
|
87
|
+
end
|
88
|
+
|
89
|
+
nil
|
90
|
+
end
|
91
|
+
|
92
|
+
##
|
93
|
+
# Returns the first intent provided there is one
|
94
|
+
#
|
95
|
+
# * *Args* :
|
96
|
+
# * *Returns* :
|
97
|
+
# - A String or nil
|
98
|
+
def intent
|
99
|
+
@intents.any? ? @intents.first : nil
|
100
|
+
end
|
101
|
+
|
102
|
+
class << self
|
103
|
+
##
|
104
|
+
# Merges the conversation memory with the one given
|
105
|
+
#
|
106
|
+
# * *Args* :
|
107
|
+
# - +token+ - String, your request_token
|
108
|
+
# - +conversation_token+ String, the token of the conversation whose memory will be updated
|
109
|
+
# - +memory+ Hash, the memory you want to update with
|
110
|
+
# * *Returns* :
|
111
|
+
# - An updated Memory
|
112
|
+
# * *Throws* :
|
113
|
+
# - RecastError
|
114
|
+
def set_memory(token, conversation_token, memory)
|
115
|
+
body = { conversation_token: conversation_token, memory: memory }
|
116
|
+
response = HTTParty.put(
|
117
|
+
Utils::CONVERSE_ENDPOINT,
|
118
|
+
body: body,
|
119
|
+
headers: { Authorization: "Token #{token}" }
|
120
|
+
)
|
121
|
+
raise(RecastError.new(response.message)) if response.code != 200
|
122
|
+
|
123
|
+
response = JSON.parse(response.body)
|
124
|
+
response = response['results']
|
125
|
+
response['memory'].select{ |n, e| !e.nil? }.map{ |n, e| Entity.new(n, e) }
|
126
|
+
end
|
127
|
+
|
128
|
+
##
|
129
|
+
# Reset the conversation memory fully or partially with the one given
|
130
|
+
#
|
131
|
+
# * *Args* :
|
132
|
+
# - +token+ - String, your request_token
|
133
|
+
# - +conversation_token+ String, the token of the conversation whose memory will be updated
|
134
|
+
# - +memory+ Hash, the memory you want to update with
|
135
|
+
# * *Returns* :
|
136
|
+
# - A fully or partially reset Memory
|
137
|
+
# * *Throws* :
|
138
|
+
# - RecastError
|
139
|
+
def reset_memory(token, conversation_token, name=nil)
|
140
|
+
body = { conversation_token: conversation_token }
|
141
|
+
unless name.nil?
|
142
|
+
body[:memory] = { name => nil }
|
143
|
+
end
|
144
|
+
response = HTTParty.put(
|
145
|
+
Utils::CONVERSE_ENDPOINT,
|
146
|
+
body: body,
|
147
|
+
headers: { Authorization: "Token #{token}" }
|
148
|
+
)
|
149
|
+
raise(RecastError.new(response.message)) if response.code != 200
|
150
|
+
|
151
|
+
response = JSON.parse(response.body)
|
152
|
+
response = response['results']
|
153
|
+
response['memory'].select{ |n, e| !e.nil? }.map{ |n, e| Entity.new(n, e) }
|
154
|
+
end
|
155
|
+
|
156
|
+
##
|
157
|
+
# Resets the conversation state by setting every memory field to nil,
|
158
|
+
# all actions to not done, and the last action to nil
|
159
|
+
#
|
160
|
+
# * *Args* :
|
161
|
+
# - +token+ - String, your request_token
|
162
|
+
# - +conversation_token+ - String, the token of the conversation to reset
|
163
|
+
# * *Returns* :
|
164
|
+
# - A fully reset Memory
|
165
|
+
# * *Throws* :
|
166
|
+
# - RecastError
|
167
|
+
def reset_conversation(token, conversation_token)
|
168
|
+
body = { conversation_token: conversation_token }
|
169
|
+
response = HTTParty.delete(
|
170
|
+
Utils::CONVERSE_ENDPOINT,
|
171
|
+
body: body,
|
172
|
+
headers: { Authorization: "Token #{token}" }
|
173
|
+
)
|
174
|
+
raise(RecastError.new(response.message)) if response.code != 200
|
175
|
+
|
176
|
+
response = JSON.parse(response.body)
|
177
|
+
response = response['results']
|
178
|
+
response['memory'].select{ |n, e| !e.nil? }.map{ |n, e| Entity.new(n, e) }
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
182
|
+
end
|
data/lib/recastai/entity.rb
CHANGED
data/lib/recastai/intent.rb
CHANGED
data/lib/recastai/response.rb
CHANGED
data/lib/recastai/utils.rb
CHANGED
@@ -4,13 +4,13 @@ module RecastAI
|
|
4
4
|
class Utils
|
5
5
|
# Versioning
|
6
6
|
MAJOR = '2'.freeze
|
7
|
-
MINOR = '
|
7
|
+
MINOR = '1'.freeze
|
8
8
|
MICRO = '0'.freeze
|
9
9
|
VERSION = "#{MAJOR}.#{MINOR}.#{MICRO}".freeze
|
10
10
|
|
11
11
|
# Endpoints
|
12
|
-
|
13
|
-
|
12
|
+
REQUEST_ENDPOINT = 'https://api.recast.ai/v2/request'.freeze
|
13
|
+
CONVERSE_ENDPOINT = 'https://api.recast.ai/v2/converse'.freeze
|
14
14
|
|
15
15
|
# Act constants
|
16
16
|
ACT_ASSERT = 'assert'.freeze
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: RecastAI
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paul Renvoisé
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-10-
|
11
|
+
date: 2016-10-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|
@@ -113,7 +113,9 @@ files:
|
|
113
113
|
- bin/file
|
114
114
|
- bin/text
|
115
115
|
- lib/recastai.rb
|
116
|
+
- lib/recastai/action.rb
|
116
117
|
- lib/recastai/client.rb
|
118
|
+
- lib/recastai/conversation.rb
|
117
119
|
- lib/recastai/entity.rb
|
118
120
|
- lib/recastai/errors.rb
|
119
121
|
- lib/recastai/intent.rb
|