-
Notifications
You must be signed in to change notification settings - Fork 487
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
OnReceive is only run if all relevant Middleware Complete #146
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
1775422
DO NOT MERGE - Mostly done.
cleemullins fce5c60
Merge pull request #1 from Microsoft/master
c6096c3
Potential test for the exception handling.
971a58d
Improved test case.
bc7f4a0
DO NOT MERGE - Mostly done.
cleemullins 5fe0db6
Bot runs OnReceive only if all relevant Middlware runs
cleemullins 251ad1b
Merge pull request #1 from Microsoft/master
3d1f971
Merge remote-tracking branch 'origin/master' into CLM/PassExceptionsO…
ab244ba
Merged.
cleemullins a7c7047
Merge branch 'CLM/PassExceptionsOnTaskFaults'
800cb11
Changed to DataTestMethod for consistency due to MSTest V2
2a41914
Merge pull request #147 from RyanDawkins/master
cleemullins 8095fec
Extracted QnAMaker logic from corresponding middleware
562f029
Merge pull request #148 from cod7alex/qnamaker-extract
cleemullins c0b6539
Updated QnAMaker to use v3 API
garypretty 6aabdc1
Remove checks for undefined token types
dandriscoll 0c46cab
Fix comments and move claim name constants into AuthenticationConstants
dandriscoll 2e3f4d1
Remove trailing whitespace
dandriscoll cd1a296
Merge pull request #154 from dandriscoll/auth
cleemullins 8d4aecc
Merge pull request #151 from garypretty/master
cleemullins 756859b
DO NOT MERGE - Mostly done.
cleemullins 208ed30
Bot runs OnReceive only if all relevant Middlware runs
cleemullins ea965aa
Merge branch 'CLM/RunOnReceiveAtTheEnd' of https://github.com/Microso…
cleemullins File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using Newtonsoft.Json; | ||
using System; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Bot.Builder.Ai | ||
{ | ||
public class QnAMaker | ||
{ | ||
public const string qnaMakerServiceEndpoint = "https://westus.api.cognitive.microsoft.com/qnamaker/v3.0/knowledgebases/"; | ||
public const string APIManagementHeader = "Ocp-Apim-Subscription-Key"; | ||
public const string JsonMimeType = "application/json"; | ||
|
||
private readonly HttpClient _httpClient; | ||
private readonly QnAMakerOptions _options; | ||
private readonly string _answerUrl; | ||
|
||
public QnAMaker(QnAMakerOptions options, HttpClient httpClient) | ||
{ | ||
_httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient)); | ||
_options = options ?? throw new ArgumentNullException(nameof(options)); | ||
if (string.IsNullOrEmpty(options.KnowledgeBaseId)) | ||
{ | ||
throw new ArgumentException(nameof(options.KnowledgeBaseId)); | ||
} | ||
|
||
_answerUrl = $"{qnaMakerServiceEndpoint}{options.KnowledgeBaseId}/generateanswer"; | ||
|
||
if (_options.ScoreThreshold == 0) | ||
{ | ||
_options.ScoreThreshold = 0.3F; | ||
} | ||
|
||
if (_options.Top == 0) | ||
{ | ||
_options.Top = 1; | ||
} | ||
|
||
if (_options.StrictFilters == null) | ||
{ | ||
_options.StrictFilters = new Metadata[] {}; | ||
} | ||
|
||
if (_options.MetadataBoost == null) | ||
{ | ||
_options.MetadataBoost = new Metadata[] { }; | ||
} | ||
} | ||
|
||
public async Task<QueryResult[]> GetAnswers(string question) | ||
{ | ||
var request = new HttpRequestMessage(HttpMethod.Post, _answerUrl); | ||
|
||
string jsonRequest = JsonConvert.SerializeObject(new | ||
{ | ||
question, | ||
top = _options.Top, | ||
strictFilters = _options.StrictFilters, | ||
metadataBoost = _options.MetadataBoost | ||
}, Formatting.None); | ||
|
||
var content = new StringContent(jsonRequest, System.Text.Encoding.UTF8, JsonMimeType); | ||
content.Headers.Add(APIManagementHeader, _options.SubscriptionKey); | ||
|
||
var response = await _httpClient.PostAsync(_answerUrl, content).ConfigureAwait(false); | ||
if (response.IsSuccessStatusCode) | ||
{ | ||
var jsonResponse = await response.Content.ReadAsStringAsync().ConfigureAwait(false); | ||
var results = JsonConvert.DeserializeObject<QueryResults>(jsonResponse); | ||
foreach (var answer in results.Answers) | ||
{ | ||
answer.Score = answer.Score / 100; | ||
} | ||
|
||
return results.Answers.Where(answer => answer.Score > _options.ScoreThreshold).ToArray(); | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
public class QnAMakerOptions | ||
{ | ||
public string SubscriptionKey { get; set; } | ||
public string KnowledgeBaseId { get; set; } | ||
public float ScoreThreshold { get; set; } | ||
public int Top { get; set; } | ||
public Metadata[] StrictFilters { get; set; } | ||
public Metadata[] MetadataBoost { get; set; } | ||
} | ||
|
||
[Serializable] | ||
public class Metadata | ||
{ | ||
[JsonProperty(PropertyName = "name")] | ||
public string Name { get; set; } | ||
|
||
[JsonProperty(PropertyName = "value")] | ||
public string Value { get; set; } | ||
} | ||
|
||
public class QueryResult | ||
{ | ||
[JsonProperty("questions")] | ||
public string[] Questions { get; set; } | ||
|
||
[JsonProperty("answer")] | ||
public string Answer { get; set; } | ||
|
||
[JsonProperty("score")] | ||
public float Score { get; set; } | ||
|
||
[JsonProperty(PropertyName = "metadata")] | ||
public Metadata[] Metadata { get; set; } | ||
|
||
[JsonProperty(PropertyName = "source")] | ||
public string Source { get; set; } | ||
|
||
[JsonProperty(PropertyName = "qnaId")] | ||
public int QnaId { get; set; } | ||
} | ||
|
||
public class QueryResults | ||
{ | ||
[JsonProperty("answers")] | ||
public QueryResult[] Answers { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps a trace would be useful here - QnA service returning a failure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@johnataylor
Agreed. That's part of a different PR (someone else did it) that's only showing up here because I rebased my PR against master to make merging cleaner.