nodejs 是否有 Azure Custom Question Answering SDK?
结论
有。Node.js 使用的 npm 包是:
npm install @azure/ai-language-questionanswering
以下两个包名是错误的:
@azure/ai-language-question-answering
@azure/ai-language-qna
@azure/ai-language-questionanswering 用于调用 Azure AI Language 的 Custom Question Answering。项目需要在 Language Studio 中创建、管理和部署,Node.js 应用通过 SDK 查询已部署的知识库。
安装与配置
先检查 npm 当前使用的 registry,并确认它可以访问公共包:
npm config get registry
npm view @azure/ai-language-questionanswering
npm install @azure/ai-language-questionanswering
如果 npm view 无法返回包信息,通常需要排查代理、私有 registry、网络连接或 npm 配置。确认组织策略允许后,可以将 registry 设置为:
npm config set registry https://registry.npmjs.org/
然后准备以下配置:
export AZURE_LANGUAGE_ENDPOINT="https://<resource-name>.cognitiveservices.azure.com/"
export AZURE_LANGUAGE_KEY="<resource-key>"
export AZURE_LANGUAGE_PROJECT="<project-name>"
export AZURE_LANGUAGE_DEPLOYMENT="<deployment-name>"
project-name 和 deployment-name 必须与 Language Studio 中的实际名称一致。项目也必须完成部署,只保存知识库内容还不能用于运行时查询。
Node.js 调用示例
CommonJS 示例:
const {
QuestionAnsweringClient,
AzureKeyCredential,
} = require("@azure/ai-language-questionanswering");
const endpoint = process.env.AZURE_LANGUAGE_ENDPOINT;
const key = process.env.AZURE_LANGUAGE_KEY;
const projectName = process.env.AZURE_LANGUAGE_PROJECT;
const deploymentName = process.env.AZURE_LANGUAGE_DEPLOYMENT;
if (!endpoint || !key || !projectName || !deploymentName) {
throw new Error("缺少 Azure Language 环境变量");
}
const client = new QuestionAnsweringClient(
endpoint,
new AzureKeyCredential(key)
);
async function askQuestion(question) {
const result = await client.getAnswers(question, {
projectName,
deploymentName,
});
if (!result.answers || result.answers.length === 0) {
return null;
}
return result.answers[0];
}
askQuestion("如何重置密码?")
.then((answer) => {
if (!answer) {
console.log("没有找到答案");
return;
}
console.log("答案:", answer.answer);
console.log("置信度:", answer.confidence);
})
.catch((error) => {
console.error("查询失败:", error);
});
如果项目使用 ESM,可以改为:
import {
QuestionAnsweringClient,
AzureKeyCredential,
} from "@azure/ai-language-questionanswering";
具体的方法签名可能随 SDK 版本变化。使用时请查看当前安装版本附带的类型定义和官方 API 文档,避免直接套用其他版本的示例。
在 Azure Bot 中使用
在 Bot 的消息处理逻辑中调用 getAnswers(),再将结果发送给用户:
this.onMessage(async (context, next) => {
const result = await client.getAnswers(context.activity.text, {
projectName,
deploymentName,
});
const answer = result.answers?.[0];
await context.sendActivity(
answer?.answer || "没有找到合适的答案。"
);
await next();
});
用于生产环境时,还需要处理超时、限流、服务异常和低置信度结果。不要把密钥直接写入源码,也不要将其提交到 Git 仓库。
常见失败原因
如果包已成功安装,但调用仍然失败,可以检查以下项目:
- Azure AI Language 项目是否已经部署。
projectName和deploymentName是否与实际名称完全一致,包括大小写和空格。- endpoint 是否属于对应的 Azure AI Language 资源。
- key 是否属于同一个资源。
- 当前 Node.js 版本是否满足已安装 SDK 声明的运行环境要求。
- 项目使用 CommonJS 还是 ESM,导入方式是否与之匹配。
- 企业网络是否使用私有 npm registry,以及该 registry 是否同步了
@azurescope。 - Bot 的运行环境是否可以访问 Azure endpoint。
其他语言的 SDK
Custom Question Answering 也提供其他语言的 Azure SDK,常见包名如下:
- .NET:
Azure.AI.Language.QuestionAnswering - Python:
azure-ai-language-questionanswering - Java:
com.azure:azure-ai-language-questionanswering - JavaScript/TypeScript:
@azure/ai-language-questionanswering
如果目标语言没有合适的 SDK,也可以直接调用 Azure AI Language 的 REST API。使用 REST API 时,需要自行处理认证、请求参数、API 版本和错误响应。Node.js 项目通常优先使用官方 SDK。
备注:内容仅供参考。
