node.jsazurebotsapi-key

How to find AZURE_ENDPOINT, AZURE_API_KEY, and KNOWLEDGE_BASE_ID in the latest Azure Portal?


I'm trying to set up a custom question-answering resource in Azure, but I'm having trouble locating the following details in the new Azure Portal interface:

AZURE_ENDPOINT: The endpoint URL for the custom question-answering resource.
AZURE_API_KEY: The API key associated with the resource.
KNOWLEDGE_BASE_ID: The ID for the knowledge base.

Most of the solutions I've found seem to be based on an older version of the Azure Portal, and the layout has changed since then. Could someone guide me on where to find these values in the latest Azure Dashboard?

Full Codes:

const functions = require('firebase-functions');
const { QnAMaker } = require('botbuilder-ai');
require('dotenv').config();

const qnaEndpoint = {
  endpoint: 'URL',
  endpointKey: process.env.AZURE_API_KEY,
  knowledgeBaseId: process.env.KNOWLEDGE_BASE_ID
};

const qnaMaker = new QnAMaker(qnaEndpoint);

exports.getAnswer = functions.https.onRequest(async (req, res) => {
  const question = req.query.question || req.body.question;

  if (!question) {
    return res.status(400).send('Please provide a question.');
  }

  try {
    const result = await qnaMaker.getAnswers({ question: question });
    if (result && result.length > 0) {
      return res.status(200).json({ answer: result[0].answer });
    } else {
      return res.status(200).json({ answer: 'No answer found.' });
    }
  } catch (error) {
    console.error('Error fetching answer:', error);
    return res.status(500).send('Error occurred while fetching answer.');
  }
});

Solution

  • Most of the solutions I've found seem to be based on an older version of the Azure Portal, and the layout has changed since then. Could someone guide me on where to find these values in the latest Azure Dashboard?

    enter image description here

    enter image description here

    enter image description here

    Use the below code for your requirement.

    Code:

    /**
     * Import function triggers from their respective submodules:
     *
     * const {onCall} = require("firebase-functions/v2/https");
     * const {onDocumentWritten} = require("firebase-functions/v2/firestore");
     *
     * See a full list of supported triggers at https://firebase.google.com/docs/functions
     */
    
    const {onRequest} = require("firebase-functions/v2/https");
    const logger = require("firebase-functions/logger");
    
    // Import necessary libraries
    require("dotenv").config();
    const {QnAMaker} = require("botbuilder-ai");
    
    // Configure the QnA endpoint with environment variables
    const qnaEndpoint = {
      endpoint: process.env.AZURE_ENDPOINT,
      endpointKey: process.env.AZURE_API_KEY,
      knowledgeBaseId: process.env.KNOWLEDGE_BASE_ID,
    };
    
    // Initialize QnAMaker with the endpoint configuration
    const qnaMaker = new QnAMaker(qnaEndpoint);
    
    // Define and export the HTTP-triggered function
    exports.getAnswer = onRequest(async (request, response) => {
      // Extract question from query parameters or request body
      const question = request.query.question || request.body.question;
    
      // Check if a question was provided
      if (!question) {
        return response.status(400).send("Please provide a question.");
      }
    
      try {
        // Call QnA Maker to get answers
        const result = await qnaMaker.getAnswers({question: question});
    
        // eslint-disable-next-line max-len
        // If answers are found, send the first one; otherwise, inform that no answer was found
        if (result && result.length > 0) {
          return response.status(200).json({answer: result[0].answer});
        } else {
          return response.status(200).json({answer: "No answer found."});
        }
      } catch (error) {
        // Log and return error message in case of failure
        logger.error("Error fetching answer:", error);
        return response.status(500).send("Error occurred while fetching answer.");
      }
    });
    

    Logged In to Firebase.

    enter image description here

    enter image description here