Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7993215
commit 9f4fc0f
Showing
7 changed files
with
496 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
# wit.ai Matcher | ||
|
||
[Recast.AI](https://recast.ai/) is an NLP API for matching strings to [intents](https://recast.ai/docs/intent). Intents are created on the Recast.AI website. | ||
|
||
## [Example 1](#example1) | ||
|
||
```python | ||
from opsdroid.matchers import match_recastai | ||
|
||
@match_recastai('greetings') | ||
async def hello(opsdroid, config, message): | ||
"""Replies to user when any 'greetings' | ||
intent is returned by Recast.AI | ||
""" | ||
await message.respond("Hello there!") | ||
``` | ||
|
||
The above skill would be called on any intent which has a name of `'greetings'`. | ||
|
||
## Example 2 | ||
|
||
```python | ||
from opsdroid.matchers import match_recastai | ||
|
||
@match_recastai('ask-joke') | ||
async def my_skill(opsdroid, config, message): | ||
"""Returns a joke if asked by the user""" | ||
await message.respond('What do you call a bear with no teeth? -- A gummy bear!') | ||
``` | ||
|
||
The above skill would be called on any intent which has a name of `'ask-joke'`. | ||
|
||
|
||
## Creating a Recast.AI bot | ||
You need to [register](https://recast.ai/signup) on Recast.AI and create a bot in order to use Recast.AI with opsdroid. | ||
|
||
You can find a quick getting started with the Recast.AI guide [here](https://recast.ai/docs/create-your-bot). | ||
|
||
## Configuring opsdroid | ||
|
||
In order to enable Recast.AI skills, you must specify an `access-token` for your bot in the parsers section of the opsdroid configuration file. | ||
You can find this `access-token` in the settings of your bot under the name: `'Request access token'`. | ||
|
||
You can also set a `min-score` option to tell opsdroid to ignore any matches which score less than a given number between 0 and 1. The default for this is 0 which will match all messages. | ||
|
||
```yaml | ||
|
||
parsers: | ||
- name: recastai | ||
access-token: 85769fjoso084jd | ||
min-score: 0.8 | ||
``` | ||
|
||
## Message object additional parameters | ||
|
||
### `message.recastai` | ||
|
||
An http response object which has been returned by the Recast.AI API. This allows you to access any information from the matched intent including other entities, intents, values, etc. | ||
|
||
|
||
## Example Skill | ||
|
||
```python | ||
from opsdroid.matchers import match_recastai | ||
|
||
import json | ||
|
||
|
||
@match_recastai('ask-feeling') | ||
async def dumpResponse(opsdroid, config, message): | ||
print(json.dumps(message.witai)) | ||
``` | ||
|
||
### Return Value on "How are you?" | ||
|
||
The example skill will print the following on the message "how are you?". | ||
|
||
```json | ||
{ | ||
"results": | ||
{ | ||
"uuid": "cab86e23-caaf-4131-9b83-a564887203da", | ||
"source": "how are you?", | ||
"intents": [ | ||
{ | ||
"slug": "ask-feeling", | ||
"confidence": 0.99 | ||
} | ||
], | ||
"act": "wh-query", | ||
"type": "desc:manner", | ||
"sentiment": "neutral", | ||
"entities": | ||
{ | ||
"pronoun": [ | ||
{ | ||
"person": 2, | ||
"number": "singular", | ||
"gender": "unknown", | ||
"raw": "you", | ||
"confidence": 0.99 | ||
} | ||
] | ||
}, | ||
"language": "en", | ||
"processing_language": "en", | ||
"version": "2.10.1", | ||
"timestamp": "2017-11-15T11:50:51.478057+00:00", | ||
"status": 200}, | ||
"message": "Requests rendered with success" | ||
} | ||
} | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
"""A helper function for parsing and executing Recast.AI skills.""" | ||
import logging | ||
import json | ||
|
||
import aiohttp | ||
|
||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
async def call_recastai(message, config): | ||
"""Call the recastai api and return the response.""" | ||
async with aiohttp.ClientSession() as session: | ||
payload = { | ||
"language": "en", | ||
"text": message.text | ||
} | ||
headers = { | ||
"Authorization": "Token " + config['access-token'], | ||
"Content-Type": "application/json" | ||
} | ||
resp = await session.post("https://api.recast.ai/v2/request", | ||
data=json.dumps(payload), | ||
headers=headers) | ||
result = await resp.json() | ||
_LOGGER.info("Recastai response - %s", json.dumps(result)) | ||
|
||
return result | ||
|
||
|
||
async def parse_recastai(opsdroid, message, config): | ||
"""Parse a message against all recastai intents.""" | ||
matched_skills = [] | ||
if 'access-token' in config: | ||
try: | ||
result = await call_recastai(message, config) | ||
except aiohttp.ClientOSError: | ||
_LOGGER.error("No response from Recast.AI, check your network.") | ||
return matched_skills | ||
|
||
if result['results'] is None: | ||
_LOGGER.error("Recast.AI error - %s", result["message"]) | ||
return matched_skills | ||
elif not result["results"]["intents"]: | ||
_LOGGER.error("Recast.AI error - No intent found " | ||
"for the message %s", str(message.text)) | ||
return matched_skills | ||
|
||
# try: | ||
# confidence = result["results"]["intents"][0]["confidence"] | ||
# except (KeyError, IndexError): | ||
# confidence = 0.0 | ||
if "min-score" in config and \ | ||
result["results"]["intents"][0]["confidence"] < \ | ||
config["min-score"]: | ||
_LOGGER.debug("Recast.AI score lower than min-score") | ||
return matched_skills | ||
|
||
if result: | ||
for skill in opsdroid.skills: | ||
if "recastai_intent" in skill: | ||
if (skill["recastai_intent"] in | ||
result["results"]["intents"][0]["slug"]): | ||
message.recastai = result | ||
_LOGGER.debug("Matched against skill %s", | ||
skill["config"]["name"]) | ||
|
||
matched_skills.append({ | ||
"score": | ||
result["results"]["intents"][0]["confidence"], | ||
"skill": skill["skill"], | ||
"config": skill["config"], | ||
"message": message | ||
}) | ||
return matched_skills |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.