文本翻译 API 3.0 版的连接问题 - Microsoft Azure QnA 聊天机器人

Connection issues with Translator Text API version 3.0 - Microsoft Azure QnA chat bot

我想将我的 Azure QnA 聊天机器人与翻译层认知系统连接起来。我使用此页面作为参考:https://docs.microsoft.com/en-us/azure/cognitive-services/translator/quickstart-csharp-translate

我正在 C# 和 Microsoft Azure 的 在线代码编辑器上进行。

不幸的是,我无法连接到翻译层(至少看起来是这样)。

当我尝试调试它时,我可以看到它停在了这个特定的部分:

var response = await client.SendAsync(request);

var responseBody = await response.Content.ReadAsStringAsync();

我检查了网络超时错误,有很多(20)。都说"There was an error sending this message to your bot: HTTP status code GatewayTimeout".

我可以 "build.cmd" 正常,没有任何错误,当我尝试做 Debug.WriteLine 或 Console.WriteLine 时,什么也没有打印出来(我什至在 VS 和模拟器中试过)

与上面的 link 相比,我唯一不同的是,我在私有方法之外定义了 "host" 和 "key":

private static async Task<string> TranslateQuestionToEnglish (...)

所以,我想把任何一个词翻译成英文。 当我取出这两行代码并使用静态值测试方法时,它显然有效(与 QnA 和其他所有内容一起)。

稍后,我将在 "Task MessageReceivedAsync" 中调用此方法。

我创建了一个翻译认知服务,我从那里得到的唯一东西是 "Keys" 的第一个密钥,并在此方法中使用了它。 这是我创建的认知服务唯一需要的东西吗??

另一件我不确定的事情,如果那件事造成问题,那就是当我访问所有资源时,我可以看到我的 qnatestbot(web app bot) 和 translator_test(cognitive services) 的类型为 "global" location,而我的 qnatestbot(app service) 的类型为 "west europe" location。他们在不同地区的事情会不会出问题? 我应该把它们都放在西欧吗(因为我在德国)?

不过,现在我查看 translator_test(认知服务)端点,我可以看到它是 ...api.congitivemicrosft.com/.../v1。 0.

但是,当我创建一个资源时,它是这样自动创建的,没有从我这边指定它? 我该如何改变它?

我希望有人成功地遇到了这样的问题并且可以帮助我。提前谢谢你

I want to connect my Azure QnA Chat Bot with the translation layer cognitive system. I am using this page as a reference: https://docs.microsoft.com/en-us/azure/cognitive-services/translator/quickstart-csharp-translate

我尝试创建一个示例来实现您的要求:将用户输入翻译成英文并将翻译文本传递给 QnAMaker 对话框,该示例在本地和 Azure 上都可以正常工作,您可以参考。

在 MessagesController 中:

[BotAuthentication]
public class MessagesController : ApiController
{

    static string uri = "https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&to=en";
    static string key = "{the_key}";

    /// <summary>
    /// POST: api/Messages
    /// receive a message from a user and send replies
    /// </summary>
    /// <param name="activity"></param>
    [ResponseType(typeof(void))]
    public virtual async Task<HttpResponseMessage> Post([FromBody] Activity activity)
    {
        // check if activity is of type message
        if (activity.GetActivityType() == ActivityTypes.Message)
        {
            if (activity.Text != null)
            {
                var textinEN = await TranslateQuestionToEnglish(activity.Text);
                activity.Text = textinEN;
            }

            await Conversation.SendAsync(activity, () => new RootDialog());
        }
        else
        {
            HandleSystemMessage(activity);
        }
        return new HttpResponseMessage(System.Net.HttpStatusCode.Accepted);
    }

    private static async Task<string> TranslateQuestionToEnglish(string text)
    {
        System.Object[] body = new System.Object[] { new { Text = text } };
        var requestBody = JsonConvert.SerializeObject(body);

        using (var client = new HttpClient())
        using (var request = new HttpRequestMessage())
        {
            request.Method = HttpMethod.Post;
            request.RequestUri = new Uri(uri);
            request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
            request.Headers.Add("Ocp-Apim-Subscription-Key", key);

            var response = await client.SendAsync(request);
            var responseBody = await response.Content.ReadAsStringAsync();

            dynamic jsonResponse = JsonConvert.DeserializeObject(responseBody);
            var textinen = jsonResponse[0]["translations"][0]["text"].Value;

            return textinen;
        }
    }

    private Activity HandleSystemMessage(Activity message)
    {
        if (message.Type == ActivityTypes.DeleteUserData)
        {
            // Implement user deletion here
            // If we handle user deletion, return a real message
        }
        else if (message.Type == ActivityTypes.ConversationUpdate)
        {
            // Handle conversation state changes, like members being added and removed
            // Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
            // Not available in all channels
        }
        else if (message.Type == ActivityTypes.ContactRelationUpdate)
        {
            // Handle add/remove from contact lists
            // Activity.From + Activity.Action represent what happened
        }
        else if (message.Type == ActivityTypes.Typing)
        {
            // Handle knowing tha the user is typing
        }
        else if (message.Type == ActivityTypes.Ping)
        {
        }

        return null;
    }
}

在对话框中:

[Serializable]
public class RootDialog : IDialog<object>
{
    public async Task StartAsync(IDialogContext context)
    {
        /* Wait until the first message is received from the conversation and call MessageReceviedAsync 
        *  to process that message. */
        context.Wait(this.MessageReceivedAsync);
    }

    private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
    {
        /* When MessageReceivedAsync is called, it's passed an IAwaitable<IMessageActivity>. To get the message,
            *  await the result. */
        var message = await result;

        var qnaAuthKey = GetSetting("QnAAuthKey"); 
        var qnaKBId = Utils.GetAppSetting("QnAKnowledgebaseId");
        var endpointHostName = Utils.GetAppSetting("QnAEndpointHostName");

        // QnA Subscription Key and KnowledgeBase Id null verification
        if (!string.IsNullOrEmpty(qnaAuthKey) && !string.IsNullOrEmpty(qnaKBId))
        {
            // Forward to the appropriate Dialog based on whether the endpoint hostname is present
            if (string.IsNullOrEmpty(endpointHostName))
                await context.Forward(new BasicQnAMakerPreviewDialog(), AfterAnswerAsync, message, CancellationToken.None);
            else
                await context.Forward(new BasicQnAMakerDialog(), AfterAnswerAsync, message, CancellationToken.None);
        }
        else
        {
            await context.PostAsync("Please set QnAKnowledgebaseId, QnAAuthKey and QnAEndpointHostName (if applicable) in App Settings. Learn how to get them at https://aka.ms/qnaabssetup.");
        }

    }

    private async Task AfterAnswerAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
    {
        // wait for the next user message
        context.Wait(MessageReceivedAsync);
    }

    public static string GetSetting(string key)
    {
        var value = Utils.GetAppSetting(key);
        if (String.IsNullOrEmpty(value) && key == "QnAAuthKey")
        {
            value = Utils.GetAppSetting("QnASubscriptionKey"); // QnASubscriptionKey for backward compatibility with QnAMaker (Preview)
        }
        return value;
    }
}

// Dialog for QnAMaker Preview service
[Serializable]
public class BasicQnAMakerPreviewDialog : QnAMakerDialog
{
    // Go to https://qnamaker.ai and feed data, train & publish your QnA Knowledgebase.
    // Parameters to QnAMakerService are:
    // Required: subscriptionKey, knowledgebaseId, 
    // Optional: defaultMessage, scoreThreshold[Range 0.0 – 1.0]
    public BasicQnAMakerPreviewDialog() : base(new QnAMakerService(new QnAMakerAttribute(RootDialog.GetSetting("QnAAuthKey"), Utils.GetAppSetting("QnAKnowledgebaseId"), "No good match in FAQ.", 0.5)))
    { }
}

// Dialog for QnAMaker GA service
[Serializable]
public class BasicQnAMakerDialog : QnAMakerDialog
{
    // Go to https://qnamaker.ai and feed data, train & publish your QnA Knowledgebase.
    // Parameters to QnAMakerService are:
    // Required: qnaAuthKey, knowledgebaseId, endpointHostName
    // Optional: defaultMessage, scoreThreshold[Range 0.0 – 1.0]
    public BasicQnAMakerDialog() : base(new QnAMakerService(new QnAMakerAttribute(RootDialog.GetSetting("QnAAuthKey"), Utils.GetAppSetting("QnAKnowledgebaseId"), "No good match in FAQ.", 0.5, 1, Utils.GetAppSetting("QnAEndpointHostName"))))
    { }

}

测试结果:

注意: 我们可以使用 ConfigurationManager.AppSettings["QnAKnowledgebaseId"]; 从 web.config 访问 QnAKnowledgebaseId 等设置if 运行 bot 应用程序在本地。更多信息请参考.