npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

digital-genius-javascript-sdk

v0.0.1

Published

JavaScript SDK to interact with the Digital Genius API

Downloads

1

Readme

Digital Genius Javascript SDK

Overview

Digital Genius Javascript SDK (digital-genius-javascript-sdk) is a JavaScript library utilizing Digital Genius' middleware API.

The API is fully asynchronous. All methods require an "apiKey" parameter and a callback function.

Usage

Ask

The ask method encapsulates access to the middleware API's "batch" endpoint, specifically eliminating the need for separate "createLog" and subsequent "prediction" call.

It takes a map of parameters with the following keys:

  • params['modelgroupId']
  • params['externalId']
  • params['logExternalId']
  • params['logMessageExternalId']
  • params['showTopBestAnswers']
  • params['logMessageCreatedAt']
  • params['logMessageExternalId']
  • params['id']
  • params['chatLogId']
  • params['externalId']
  • params['content']
  • params['from']
  • params['language']
  • params['channel']
  • params['answerIds']
  • params['metadata']
  • params['query']

Example

dg.ask(params, API_KEY, function (response) {
    var confidence = response.answers[0].confidence;
    var answer = response.answers[0].automationTemplate;
    console.log(answer + " with " + confidence);
});

Create Log

The createLog method allows for explicit creation of a log in the API. It accepts a JSON object that has keys for query and modelgroupid.

It takes a map of parameters with the following keys:

  • params['modelgroupId']
  • params['externalId']
  • params['logExternalId']
  • params['logMessageExternalId']
  • params['showTopBestAnswers']
  • params['logMessageCreatedAt']
  • params['logMessageExternalId']
  • params['id']
  • params['chatLogId']
  • params['externalId']
  • params['content']
  • params['from']
  • params['language']
  • params['channel']
  • params['answerIds']
  • params['metadata']
  • params['query']

Example

params = {
     "modelgroupId": "17cc6e32-ee95-4b8d-ae78-58c56e659d90",
     "query" : "Can I bring my bike on the train?"
}

dg.createLog(params, API_KEY, function(response) {
    console.log(response.id);
    console.log(response.modelgroupId);
    console.log(response.createdAt);
    console.log(response.externalId);
    console.log(response.metadata.length);
 });

Prediction

The prediction method extracts a prediction from a previously created log using its modelId.

It takes a map of parameters with the following keys:

  • params['modelgroupId']
  • params['externalId']
  • params['logExternalId']
  • params['logMessageExternalId']
  • params['showTopBestAnswers']
  • params['logMessageCreatedAt']
  • params['logMessageExternalId']
  • params['id']
  • params['chatLogId']
  • params['externalId']
  • params['content']
  • params['from']
  • params['language']
  • params['channel']
  • params['answerIds']
  • params['metadata']
  • params['query']

Example

params = {
    "modelgroupId": "17cc6e32-ee95-4b8d-ae78-58c56e659d90",
    "query" : "Can I bring my bike on the train?",
    "logExternalId": "ID_EXTERNAL_message_123abc",
    "logMessageExternalId": "ID_EXTERNAL_message_123abc"
}

sdk.prediction(params, API_KEY, function(response) {
    console.log(response.id);
    console.log(response.createdAt);
    console.log(response.logId);
    console.log(response.messageId);
    console.log(response.prediction.answers.length);
});

Installing the Module Locally

There are two ways to use the module locally.

The first way is via npm link. In the root of this project run the following:

npm link

Then in the project using digital-genius-javascript-sdk as a dependency run the following:

npm link digital-genius-javascript-sdk

The other approach, useful if deploying in an Alexa Skill project, is to install the module directly in the project. This can be done as follows.

In the root of this project run the following:

npm pack .

This will create a file named "digital-genius-javascript-sdk-1.0.0.tgz" in the current directory.

In your project's directory run the following:

npm install /path/to/digital-genius-javascript-sdk-1.0.0.tgz

Testing Setup

You will need the following dependencies:

npm install dotenv

This dependency allows for environment specific configs and uses the .env file located in the project root (https://github.com/motdotla/dotenv).

If there is a .env file then notice that we have the following setting. This is also the default value should a .env file not exist.

BASE_URL=https://demo-api.us.dgdeepai.com/api/v1/qa/

If running locally against mock service then just use BASE_URL = http://localhost:3000/. Within the index.js file located in the project root, we can use the dependency by first declaring the config and then leveraging it to get the environment property in the following manner:

require('dotenv').config()
var config = require('config');
...

uri: config.get('ask_url')
npm install -g json-server

This installs a Node module, running an Express server, that lets you specify a JSON file to act as a data source for you mock REST API (https://github.com/typicode/json-server).

Notice how this is set up in server.js file within the /mock directory. There are endpoints with specific http response codes and payloads.

From the command line navigate to the /mock directory and issue the following command:

node server.js

You should end up seem something similar to this:

$ node server.js
JSON Server is running on port 3000

If you run into issues starting up the mock server then you likely need to run npm link from within the /mock directory.

npm link json-server

There are some known issues and some work arounds documented here (https://github.com/typicode/json-server/issues/454 )

Finally, you will need to install the testing framework Mocha (https://mochajs.org/).

npm install mocha

Once thats done, navigate to the project root/test directory and run the test cases.

If the requests are making it to your mock server you will notice information be logged to console about every request.

$ node server.js
JSON Server is running on port 3000
POST /ask 200 2.800 ms - 210
POST /createLog 201 0.364 ms - 621
POST /prediction 201 0.425 ms - 1342

Generated Documentation

Documentation can be generated via jsdoc (http://usejsdoc.org/)

You can install jsdoc as follows:

$ npm install -g jsdoc

And the documentation can be generated with the following command:

$ jsdoc -d docs index.js