Skip to content
This repository was archived by the owner on Jan 5, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions libraries/Microsoft.Bot.Builder.AI.QnA/Models/Prompt.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;

namespace Microsoft.Bot.Builder.AI.QnA
{
/// <summary>
/// Prompt Object.
/// </summary>
public class Prompt
{
private const int DefaultDisplayOrder = 0;

/// <summary>
/// Gets or sets displayOrder - index of the prompt - used in ordering of the prompts.
/// </summary>
/// <value>Display order.</value>
[JsonProperty("displayOrder")]
public int DisplayOrder { get; set; } = DefaultDisplayOrder;

/// <summary>
/// Gets or sets qna id corresponding to the prompt - if QnaId is present, QnADTO object is ignored.
/// </summary>
/// <value>QnA Id.</value>
[JsonProperty("qnaId")]
public int QnaId { get; set; }

/// <summary>
/// Gets or sets displayText - Text displayed to represent a follow up question prompt.
/// </summary>
/// <value>Display test.</value>
[JsonProperty("displayText")]
public string DisplayText { get; set; } = string.Empty;

/// <summary>
/// Gets or sets the QnADTO returned from the API.
/// </summary>
/// <value>
/// The QnA DTO.
/// </value>
[JsonProperty("qna")]
public object Qna { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,14 @@ public class QnAMakerTraceInfo
/// </value>
[JsonProperty("metadataBoost")]
public Metadata[] MetadataBoost { get; set; }

/// <summary>
/// Gets or sets context for multi-turn responses.
/// </summary>
/// <value>
/// The context from which the QnA was extracted.
/// </value>
[JsonProperty("context")]
public QnARequestContext Context { get; set; }
}
}
32 changes: 32 additions & 0 deletions libraries/Microsoft.Bot.Builder.AI.QnA/Models/QnARequestContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using Newtonsoft.Json;

namespace Microsoft.Bot.Builder.AI.QnA
{
/// <summary>
/// The context associated with QnA. Used to mark if the current prompt is relevant with a previous question or not.
/// </summary>
public class QnARequestContext
{
/// <summary>
/// Gets or sets the previous QnA Id that was returned.
/// </summary>
/// <value>
/// The previous QnA Id.
/// </value>
[JsonProperty(PropertyName = "previousQnAId")]
public int PreviousQnAId { get; set; }

/// <summary>
/// Gets or sets the previous user query/question.
/// </summary>
/// <value>
/// The previous user query.
/// </value>
[JsonProperty(PropertyName = "previousUserQuery")]
public string PreviousUserQuery { get; set; } = string.Empty;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using Newtonsoft.Json;

namespace Microsoft.Bot.Builder.AI.QnA
{
/// <summary>
/// The context associated with QnA. Used to mark if the qna response has related prompts to display.
/// </summary>
public class QnAResponseContext
{
/// <summary>
/// Gets or sets the prompts collection of related prompts.
/// </summary>
/// <value>
/// The QnA prompts array.
/// </value>
[JsonProperty(PropertyName = "prompts")]
public Prompt[] Prompts { get; set; }
}
}
9 changes: 9 additions & 0 deletions libraries/Microsoft.Bot.Builder.AI.QnA/Models/QueryResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,14 @@ public class QueryResult
/// </value>
[JsonProperty(PropertyName = "id")]
public int Id { get; set; }

/// <summary>
/// Gets or sets context for multi-turn responses.
/// </summary>
/// <value>
/// The context from which the QnA was extracted.
/// </value>
[JsonProperty(PropertyName = "context")]
public QnAResponseContext Context { get; set; }
}
}
8 changes: 8 additions & 0 deletions libraries/Microsoft.Bot.Builder.AI.QnA/QnAMakerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ public QnAMakerOptions()
/// </value>
public int Top { get; set; }

/// <summary>
/// Gets or sets context of the previous turn.
/// </summary>
/// <value>
/// The context of previous turn.
/// </value>
public QnARequestContext Context { get; set; }

public Metadata[] StrictFilters { get; set; }

public Metadata[] MetadataBoost { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ private static void ValidateOptions(QnAMakerOptions options)
{
options.MetadataBoost = new Metadata[] { };
}

options.Context = new QnARequestContext();
}

/// <summary>
Expand Down Expand Up @@ -162,6 +164,11 @@ private QnAMakerOptions HydrateOptions(QnAMakerOptions queryOptions)
{
hydratedOptions.MetadataBoost = queryOptions.MetadataBoost;
}

if (queryOptions.Context != null)
{
hydratedOptions.Context = queryOptions.Context;
}
}

return hydratedOptions;
Expand All @@ -178,6 +185,7 @@ private async Task<QueryResult[]> QueryQnaServiceAsync(Activity messageActivity,
strictFilters = options.StrictFilters,
metadataBoost = options.MetadataBoost,
scoreThreshold = options.ScoreThreshold,
context = options.Context,
}, Formatting.None);

var httpRequestHelper = new HttpRequestUtils(httpClient);
Expand All @@ -199,6 +207,7 @@ private async Task EmitTraceInfoAsync(ITurnContext turnContext, Activity message
Top = options.Top,
StrictFilters = options.StrictFilters,
MetadataBoost = options.MetadataBoost,
Context = options.Context,
};
var traceActivity = Activity.CreateTraceActivity(QnAMaker.QnAMakerName, QnAMaker.QnAMakerTraceType, traceInfo, QnAMaker.QnAMakerTraceLabel);
await turnContext.SendActivityAsync(traceActivity).ConfigureAwait(false);
Expand Down
64 changes: 64 additions & 0 deletions tests/Microsoft.Bot.Builder.AI.QnA.Tests/QnAMakerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,70 @@ public void QnaMaker_Test_ScoreThresholdTooSmall_OutOfRange()
var qnaWithSmallThreshold = new QnAMaker(endpoint, tooSmallThreshold);
}

[TestMethod]
[TestCategory("AI")]
[TestCategory("QnAMaker")]
public async Task QnaMaker_ReturnsAnswerWithContext()
{
var mockHttp = new MockHttpMessageHandler();
mockHttp.When(HttpMethod.Post, GetRequestUrl())
.Respond("application/json", GetResponse("QnaMaker_ReturnsAnswerWithContext.json"));

var qna = GetQnAMaker(
mockHttp,
new QnAMakerEndpoint
{
KnowledgeBaseId = _knowlegeBaseId,
EndpointKey = _endpointKey,
Host = _hostname,
});

var options = new QnAMakerOptions()
{
Top = 1,
Context = new QnARequestContext()
{
PreviousQnAId = 5,
PreviousUserQuery = "how do I clean the stove?",
},
};

var results = await qna.GetAnswersAsync(GetContext("Where can I buy?"), options);
Assert.IsNotNull(results);
Assert.AreEqual(1, results.Length, "should get one result");
Assert.AreEqual(55, results[0].Id, "should get context based follow-up");
Assert.AreEqual(1, results[0].Score, "Score should be high");
}

[TestMethod]
[TestCategory("AI")]
[TestCategory("QnAMaker")]
public async Task QnaMaker_ReturnsAnswerWithoutContext()
{
var mockHttp = new MockHttpMessageHandler();
mockHttp.When(HttpMethod.Post, GetRequestUrl())
.Respond("application/json", GetResponse("QnaMaker_ReturnsAnswerWithoutContext.json"));

var qna = GetQnAMaker(
mockHttp,
new QnAMakerEndpoint
{
KnowledgeBaseId = _knowlegeBaseId,
EndpointKey = _endpointKey,
Host = _hostname,
});

var options = new QnAMakerOptions()
{
Top = 3,
};

var results = await qna.GetAnswersAsync(GetContext("Where can I buy?"), options);
Assert.IsNotNull(results);
Assert.AreEqual(2, results.Length, "should get two result");
Assert.AreNotEqual(1, results[0].Score, "Score should be low");
}

[TestMethod]
[TestCategory("AI")]
[TestCategory("QnAMaker")]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"answers": [
{
"questions": [
"Where can I buy cleaning products?"
],
"answer": "Any DIY store",
"score": 100,
"id": 55,
"source": "Editorial",
"metadata": [],
"context": {
"isContextOnly": true,
"prompts": []
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"answers": [
{
"questions": [
"Where can I buy home appliances?"
],
"answer": "Any Walmart store",
"score": 68,
"id": 56,
"source": "Editorial",
"metadata": [],
"context": {
"isContextOnly": false,
"prompts": []
}
},
{
"questions": [
"Where can I buy cleaning products?"
],
"answer": "Any DIY store",
"score": 56,
"id": 55,
"source": "Editorial",
"metadata": [],
"context": {
"isContextOnly": false,
"prompts": []
}
}
]
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
{
"answers": [
{
"questions": [
"how do I clean the stove?"
],
"answer": "BaseCamp: You can use a damp rag to clean around the Power Pack",
"score": 100,
"id": 5,
"source": "Editorial",
"metadata": []
"questions": [
"how do I clean the stove?"
],
"answer": "BaseCamp: You can use a damp rag to clean around the Power Pack",
"score": 100,
"id": 5,
"source": "Editorial",
"metadata": [],
"context": {
"isContextOnly": true,
"prompts": [
{
"displayOrder": 0,
"qnaId": 55,
"qna": null,
"displayText": "Where can I buy?"
}
]
}
}
]
}