我正在尝试在 Azure 中设置自定义问答资源,但在新的 Azure 门户界面中找不到以下详细信息时遇到问题:
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.
我发现的大多数解决方案似乎都是基于旧版本的Azure Portal,并且从那时起布局已经发生了变化。有人可以指导我在最新的 Azure 仪表板中哪里可以找到这些值吗?
完整代码:
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.');
}
});
我发现的大多数解决方案似乎都是基于旧版本的Azure Portal,并且从那时起布局已经发生了变化。有人可以指导我在最新的 Azure 仪表板中哪里可以找到这些值吗?
使用以下代码满足您的要求。
代码:
/**
* 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.");
}
});
已登录 Firebase。