Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

api_ai_wrapper

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

api_ai_wrapper

  • 1.0.5
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

The API.AI ruby wrapper

A simple library that let's any developer automate the training process of a Natural Language Processing Engine on API.AI, and retrieve meaning from new utterances.

To install

Directly from a bash console

gem install api_ai_wrapper

Or in your Gemfile

gem "api_ai_wrapper"

The only gem dependency is http_client to manage HTTP calls to API.AI.

To start things up

The gem is centered around the ApiAiWrapper::Engine model that will be the gateway to all the gem's available endpoints. To make such calls, just instantiate an Engine with the correct developer and client tokens (for training and meaning retrieval) like so (in an initializer for instance) :

$engine = ApiAiWrapper::Engine.new({
  developer_token: "YOUR DEVELOPER TOKEN",
  client_token: "YOUR CLIENT TOKEN"
})

That's for the basic usage, if you want complete control over your API.AI engine initialization, you might do :

$engine = ApiAiWrapper::Engine.new({
  developer_token: "YOUR DEVELOPER TOKEN",
  client_token: "YOUR CLIENT TOKEN",
  client_timeout: HTTPCLIENT TIMEOUT IN SECONDS (defaults to 50000),
  locale: "YOUR ENGINE REQUIRED LOCALE" (defaults to "en"),
  version: "YOUR PREFERRED API.AI VERSION" (defaults to "20150910")
})

Because we use HTTPClient for calls, you can override the client_timeout attribute to pick your prefered timeout. As a default, it is set to 50000 seconds.

To actually use the gem

The API.AI API (that's a mouthful) offers two modes : the Training mode and the Query mode.

Training Mode

Included in this gem are all endpoints available in API.AI around entities and intents.

To use them, initializing an engine with the code above (developer_token is required) gives you access to an IntentTrainer and EntityTrainer instances that serve as proxy to call the corresponding endpoints. For example, assuming $engine has been initialized, fetching all entities on a given engine can be done as follows :

  entity_trainer = $engine.entity_trainer
  entities = entity_trainer.get_entities

Posting a new intent can be as easy as that :

  intent_trainer = $engine.intent_trainer
  intent_trainer.post_intent({
    name: "some-name",
    user_says_data: [
      {
        data: [
          {
            text: "A sample "
          },
          {
            text: "utterance",
            alias: "object",
            meta: "@object"
          },
          {
            text: " for you"
          }
        ],
        isTemplate: false,
        count: 0
      },
      {
        data: ...
      }
    ]
  })

Endpoints list

All available methods/endpoints are listed in the tables below

IntentTrainer

Use these methods on intent_trainer = $engine.intent_trainer

NameArgumentsDescription
get_intentsnoneRetrieves a list of all intents for the agent.
get_intentintent_idRetrieves the specified intent.
post_intentname, user_says_data, optionsCreates a new intent (options is a hash containing additional info about an intent specified in API.AI documentation).
put_intentintent_id, optionsUpdates the specified intent. (options is a hash containing intent fields to modify)
delete_intentintent_idDeletes the specified entity.
EntityTrainer

Use these methods on entity_trainer = $engine.entity_trainer

NameArgumentsDescription
get_entitiesnoneRetrieves a list of all entities for the agent.
get_entityentity_idRetrieves the specified entity.
post_entityname, entriesCreates a new entity.
post_entity_entriesentity_id, entriesAdds an array of entity entries to the specified entity.
put_entityentity_id, optionsCreates or updates multiple entities. (options is a hash containing entity fields to modify)
put_entity_entriesentity_id, entriesUpdates an array of entity entries in the specified entity.
delete_entityentity_idDeletes the specified entity.
delete_entity_entriesentity_id, entriesDeletes an array of entity entries from the specified entity.

Query mode

After your API.AI engine is properly trained, you can retrieve meaning (intents and entities) from a novel utterance via the query endpoint.

To use is, initializing an engine with the code above (client_token is required) gives you access to a MeaningExtractor instance that serves as proxy to call the query endpoint. Assuming $engine has been initialized, extracting the meaning from a sentence can be done as follows :

  meaning_extractor = $engine.meaning_extractor
  meaning_extractor.post_query("A sample utterance")

This call will return a JSON parsed hash with the utterance meaning returned by API.AI

NameArgumentsDescription
post_queryquery, optionsTakes natural language text and information, returns intent and entity information. (options is a hash containing additional info about an intent specified in API.AI documentation).

Errors

ApiAiWrapper::Errors::Engine::MissingTokens error is returned when an ApiAiWrapper::Engine is instantiated without any token.

ApiAiWrapper::Errors::Engine::MissingToken errors are returned when a call is made to entity_trainer, intent_trainer or meaning_extractor but the appropriate token (either developer_token or client_token) has not been defined in the Engine.

ApiAiWrapper::Errors::Engine::ApiError error is returned whenever something goes wrong during a request to API.AI. The error contains a error.message, error.code and error.status identical to the ones returned by API.AI for full transparency.

FAQs

Package last updated on 22 Aug 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc