This package has been deprecated

Author message:

The QnA Maker service is being retired on the 31st of March, 2025. A newer version of the question and answering capability is now available as part of Azure Cognitive Service for Language. For information on migrating existing QnA Maker knowledge bases to question answering, consult the migration guide: https://learn.microsoft.com/azure/cognitive-services/language-service/question-answering/how-to/migrate-qnamaker

@azure/cognitiveservices-qnamaker-runtime
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

An isomorphic javascript sdk for - QnAMakerRuntimeClient

This package contains an isomorphic SDK for interacting with the QnA Maker service such as training and asking questions. For editing and creating Knowledge Bases see @azure/cognitiveservices-qnamaker.

Currently supported environments

  • Node.js version 6.x.x or higher
  • Browser JavaScript

How to Install

npm install @azure/cognitiveservices-qnamaker-runtime

How to use

nodejs - Authentication, client creation and generateAnswer runtime as an example written in TypeScript.

Install @azure/ms-rest-azure-js
npm install @azure/ms-rest-azure-js
Sample code
const { QnAMakerRuntimeClient } = require("@azure/cognitiveservices-qnamaker-runtime");
const { CognitiveServicesCredentials } = require("@azure/ms-rest-azure-js");

async function main() {
  const QNAMAKER_KEY = process.env["QNAMAKER_KEY"] || "<QNAMAKER_KEY>";
  const QNAMAKER_ENDPOINT = process.env["QNAMAKER_ENDPOINT"] || "<QNAMAKER_ENDPOINT>";
  const kbid = process.env["QNAMAKER_KNOWLEDGE_BASE_ID"] || "<QNAMAKER_KNOWLEDGE_BASE_ID>";

  const cognitiveServicesCredentials = new CognitiveServicesCredentials(QNAMAKER_KEY);
  const client = new QnAMakerRuntimeClient(cognitiveServicesCredentials, QNAMAKER_ENDPOINT);
  const customHeaders = { Authorization: `EndpointKey ${QNAMAKER_KEY}` };

  // A question you'd like to get a response for, from the knowledge base. For example
  const question = "How are you?";

  // Maximum number of answer to retreive
  const top = 1;

  // Find only answers that contain these metadata
  const strictFilters = [{ name: "editorial", value: "chitchat" }];

  const result = await client.runtime.generateAnswer(
    kbid,
    { question, top, strictFilters },
    { customHeaders }
  );
  console.log(JSON.stringify(result));
  // Sample Result
  // {
  //   answers: [
  //     {
  //       questions: [
  //         "How are you?",
  //         "How is your tuesday?"
  //       ],
  //       answer:
  //         ""I'm doing great, thanks for asking!",
  //       score: 100,
  //       id: 90,
  //       source:
  //         "qna_chitchat_Friendly.tsv",
  //       metadata: [{ name: "editorial", value: "chitchat" }],
  //       context: { isContextOnly: false, prompts: [] }
  //     }
  //   ],
  //   debugInfo: null,
  //   activeLearningEnabled: false
  // }
}

main();

browser - Authentication, client creation and generateAnswer runtime as an example written in JavaScript.

Sample code
  • index.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>@azure/cognitiveservices-qnamaker-runtime sample</title>
    <script src="node_modules/@azure/ms-rest-js/dist/msRest.browser.js"></script>
    <script src="node_modules/@azure/cognitiveservices-qnamaker-runtime/dist/cognitiveservices-qnamaker-runtime.js"></script>
    <script type="text/javascript">
      const QNAMAKER_KEY = "<QNAMAKER_KEY>";
      const QNAMAKER_ENDPOINT = "<QNAMAKER_ENDPOINT>";
      const kbid = "<QNAMAKER_KNOWLEDGE_BASE_ID>";

      const cognitiveServiceCredentials = new msRest.ApiKeyCredentials({
        inHeader: {
          "Ocp-Apim-Subscription-Key": QNAMAKER_KEY
        }
      });

      const client = new Azure.CognitiveservicesQnamakerRuntime.QnAMakerRuntimeClient(
        cognitiveServiceCredentials,
        QNAMAKER_ENDPOINT
      );
      const customHeaders = { Authorization: `EndpointKey ${QNAMAKER_KEY}` };

      // A question you'd like to get a response for, from the knowledge base. For example
      const question = "How are you?";

      // Maximum number of answer to retreive
      const topValue = 1;

      // Find only answers that contain these metadata
      const strictFilters = [{
        name: "editorial",
        value: "chitchat"
      }];

      client.runtime
        .generateAnswer(kbid, {question, topValue, strictFilters}, {customHeaders})
        .then(result => {
          console.log(JSON.stringify(result));
          // Sample Result
          // {
          //   answers: [
          //     {
          //       questions: [
          //         "How are you?",
          //         "How is your tuesday?"
          //       ],
          //       answer:
          //         ""I'm doing great, thanks for asking!",
          //       score: 100,
          //       id: 90,
          //       source:
          //         "qna_chitchat_Friendly.tsv",
          //       metadata: [{ name: "editorial", value: "chitchat" }],
          //       context: { isContextOnly: false, prompts: [] }
          //     }
          //   ],
          //   debugInfo: null,
          //   activeLearningEnabled: false
          // }
        })
        .catch(err => {
          console.log("An error occurred:");
          console.error(err);
        });
    </script>
  </head>
  <body></body>
</html>

Related projects

Impressions

Package Sidebar

Install

npm i @azure/cognitiveservices-qnamaker-runtime

Weekly Downloads

320

Version

1.0.1

License

MIT

Unpacked Size

193 kB

Total Files

49

Last publish

Collaborators

  • microsoft1es
  • azure-sdk