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 1 commit
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
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>

<PropertyGroup>
<LocalPackageVersion>4.10.0-local</LocalPackageVersion>
<LocalPackageVersion>4.11.0-local</LocalPackageVersion>
</PropertyGroup>

<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,9 @@ public LuisAdaptiveRecognizer()
/// <inheritdoc/>
public override async Task<RecognizerResult> RecognizeAsync(DialogContext dialogContext, Activity activity, CancellationToken cancellationToken = default, Dictionary<string, string> telemetryProperties = null, Dictionary<string, double> telemetryMetrics = null)
{
var wrapper = new LuisRecognizer(RecognizerOptions(dialogContext), HttpClient);
var recognizer = new LuisRecognizer(RecognizerOptions(dialogContext), HttpClient);

// temp clone of turn context because luisrecognizer always pulls activity from turn context.
RecognizerResult result;
using (var tempContext = new TurnContext(dialogContext.Context, activity))
{
result = await wrapper.RecognizeAsync(tempContext, cancellationToken).ConfigureAwait(false);
}
RecognizerResult result = await recognizer.RecognizeAsync(dialogContext, activity, cancellationToken).ConfigureAwait(false);

TrackRecognizerResult(dialogContext, "LuisResult", FillRecognizerResultTelemetryProperties(result, telemetryProperties, dialogContext), telemetryMetrics);

Expand Down
144 changes: 143 additions & 1 deletion libraries/Microsoft.Bot.Builder.AI.LUIS/LuisRecognizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public LuisRecognizer(LuisRecognizerOptions recognizerOptions, HttpClientHandler
var currentHandler = CreateHttpHandlerPipeline(httpClientHandler, delegatingHandler);
#pragma warning restore CA2000 // Dispose objects before losing scope

HttpClient = new HttpClient(currentHandler, false)
HttpClient = new HttpClient(currentHandler, false)
{
Timeout = TimeSpan.FromMilliseconds(recognizerOptions.Timeout),
};
Expand Down Expand Up @@ -195,6 +195,16 @@ public static string TopIntent(RecognizerResult results, string defaultIntent =
public virtual async Task<RecognizerResult> RecognizeAsync(ITurnContext turnContext, CancellationToken cancellationToken)
=> await RecognizeInternalAsync(turnContext, null, null, null, cancellationToken).ConfigureAwait(false);

/// <summary>
/// Runs an utterance through a recognizer and returns a generic recognizer result.
/// </summary>
/// <param name="dialogContext">dialogcontext.</param>
/// <param name="activity">activity.</param>
/// <param name="cancellationToken">cancellationtoken.</param>
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
public virtual async Task<RecognizerResult> RecognizeAsync(DialogContext dialogContext, Activity activity, CancellationToken cancellationToken)
=> await RecognizeInternalAsync(dialogContext, activity, null, null, null, cancellationToken).ConfigureAwait(false);

/// <summary>
/// Runs an utterance through a recognizer and returns a generic recognizer result.
/// </summary>
Expand All @@ -219,6 +229,22 @@ public virtual async Task<T> RecognizeAsync<T>(ITurnContext turnContext, Cancell
return result;
}

/// <summary>
/// Runs an utterance through a recognizer and returns a strongly-typed recognizer result.
/// </summary>
/// <typeparam name="T">type of result.</typeparam>
/// <param name="dialogContext">dialogContext.</param>
/// <param name="activity">activity.</param>
/// <param name="cancellationToken">cancellationToken.</param>
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
public virtual async Task<T> RecognizeAsync<T>(DialogContext dialogContext, Activity activity, CancellationToken cancellationToken)
where T : IRecognizerConvert, new()
{
var result = new T();
result.Convert(await RecognizeInternalAsync(dialogContext, activity, null, null, null, cancellationToken).ConfigureAwait(false));
return result;
}

/// <summary>
/// Runs an utterance through a recognizer and returns a strongly-typed recognizer result.
/// </summary>
Expand Down Expand Up @@ -249,6 +275,18 @@ public virtual async Task<T> RecognizeAsync<T>(ITurnContext turnContext, LuisPre
public virtual async Task<RecognizerResult> RecognizeAsync(ITurnContext turnContext, Dictionary<string, string> telemetryProperties, Dictionary<string, double> telemetryMetrics = null, CancellationToken cancellationToken = default(CancellationToken))
=> await RecognizeInternalAsync(turnContext, null, telemetryProperties, telemetryMetrics, cancellationToken).ConfigureAwait(false);

/// <summary>
/// Return results of the analysis (Suggested actions and intents).
/// </summary>
/// <param name="dialogContext">Context object containing information for a single turn of conversation with a user.</param>
/// <param name="activity">activity to recognize.</param>
/// <param name="telemetryProperties">Additional properties to be logged to telemetry with the LuisResult event.</param>
/// <param name="telemetryMetrics">Additional metrics to be logged to telemetry with the LuisResult event.</param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <returns>The LUIS results of the analysis of the current message text in the current turn's context activity.</returns>
public virtual async Task<RecognizerResult> RecognizeAsync(DialogContext dialogContext, Activity activity, Dictionary<string, string> telemetryProperties, Dictionary<string, double> telemetryMetrics = null, CancellationToken cancellationToken = default(CancellationToken))
=> await RecognizeInternalAsync(dialogContext, activity, null, telemetryProperties, telemetryMetrics, cancellationToken).ConfigureAwait(false);

/// <summary>
/// Return results of the analysis (Suggested actions and intents).
/// </summary>
Expand Down Expand Up @@ -283,6 +321,24 @@ public virtual async Task<T> RecognizeAsync<T>(ITurnContext turnContext, LuisPre
return result;
}

/// <summary>
/// Return results of the analysis (Suggested actions and intents).
/// </summary>
/// <typeparam name="T">The recognition result type.</typeparam>
/// <param name="dialogContext">Context object containing information for a single turn of conversation with a user.</param>
/// <param name="activity">activity to recognize.</param>
/// <param name="telemetryProperties">Additional properties to be logged to telemetry with the LuisResult event.</param>
/// <param name="telemetryMetrics">Additional metrics to be logged to telemetry with the LuisResult event.</param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <returns>The LUIS results of the analysis of the current message text in the current turn's context activity.</returns>
public virtual async Task<T> RecognizeAsync<T>(DialogContext dialogContext, Activity activity, Dictionary<string, string> telemetryProperties, Dictionary<string, double> telemetryMetrics = null, CancellationToken cancellationToken = default(CancellationToken))
where T : IRecognizerConvert, new()
{
var result = new T();
result.Convert(await RecognizeInternalAsync(dialogContext, activity, null, telemetryProperties, telemetryMetrics, cancellationToken).ConfigureAwait(false));
return result;
}

/// <summary>
/// Return results of the analysis (Suggested actions and intents).
/// </summary>
Expand Down Expand Up @@ -317,6 +373,20 @@ public virtual async Task<RecognizerResult> RecognizeAsync(ITurnContext turnCont
return await RecognizeInternalAsync(turnContext, recognizerOptions, null, null, cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// Runs an utterance through a recognizer and returns a generic recognizer result.
/// </summary>
/// <param name="dialogContext">dialog context.</param>
/// <param name="activity">activity to recognize.</param>
/// <param name="recognizerOptions">A <see cref="LuisRecognizerOptions"/> instance to be used by the call.
/// This parameter overrides the default <see cref="LuisRecognizerOptions"/> passed in the constructor.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>Analysis of utterance.</returns>
public virtual async Task<RecognizerResult> RecognizeAsync(DialogContext dialogContext, Activity activity, LuisRecognizerOptions recognizerOptions, CancellationToken cancellationToken)
{
return await RecognizeInternalAsync(dialogContext, activity, recognizerOptions, null, null, cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// Runs an utterance through a recognizer and returns a strongly-typed recognizer result.
/// </summary>
Expand All @@ -334,6 +404,24 @@ public virtual async Task<T> RecognizeAsync<T>(ITurnContext turnContext, LuisRec
return result;
}

/// <summary>
/// Runs an utterance through a recognizer and returns a strongly-typed recognizer result.
/// </summary>
/// <typeparam name="T">The recognition result type.</typeparam>
/// <param name="dialogContext">dialog context.</param>
/// <param name="activity">activity to recognize.</param>
/// <param name="recognizerOptions">A <see cref="LuisRecognizerOptions"/> instance to be used by the call.
/// This parameter overrides the default <see cref="LuisRecognizerOptions"/> passed in the constructor.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>Analysis of utterance.</returns>
public virtual async Task<T> RecognizeAsync<T>(DialogContext dialogContext, Activity activity, LuisRecognizerOptions recognizerOptions, CancellationToken cancellationToken)
where T : IRecognizerConvert, new()
{
var result = new T();
result.Convert(await RecognizeInternalAsync(dialogContext, activity, recognizerOptions, null, null, cancellationToken).ConfigureAwait(false));
return result;
}

/// <summary>
/// Return results of the analysis (Suggested actions and intents).
/// </summary>
Expand All @@ -349,6 +437,22 @@ public virtual async Task<T> RecognizeAsync<T>(ITurnContext turnContext, LuisRec
return await RecognizeInternalAsync(turnContext, recognizerOptions, telemetryProperties, telemetryMetrics, cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// Return results of the analysis (Suggested actions and intents).
/// </summary>
/// <param name="dialogContext">Context object containing information for a single turn of conversation with a user.</param>
/// <param name="activity">activity to recognize.</param>
/// <param name="recognizerOptions">A <see cref="LuisRecognizerOptions"/> instance to be used by the call.
/// This parameter overrides the default <see cref="LuisRecognizerOptions"/> passed in the constructor.</param>
/// <param name="telemetryProperties">Additional properties to be logged to telemetry with the LuisResult event.</param>
/// <param name="telemetryMetrics">Additional metrics to be logged to telemetry with the LuisResult event.</param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <returns>The LUIS results of the analysis of the current message text in the current turn's context activity.</returns>
public virtual async Task<RecognizerResult> RecognizeAsync(DialogContext dialogContext, Activity activity, LuisRecognizerOptions recognizerOptions, Dictionary<string, string> telemetryProperties, Dictionary<string, double> telemetryMetrics = null, CancellationToken cancellationToken = default(CancellationToken))
{
return await RecognizeInternalAsync(dialogContext, activity, recognizerOptions, telemetryProperties, telemetryMetrics, cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// Return results of the analysis (Suggested actions and intents).
/// </summary>
Expand All @@ -368,6 +472,26 @@ public virtual async Task<T> RecognizeAsync<T>(ITurnContext turnContext, LuisRec
return result;
}

/// <summary>
/// Return results of the analysis (Suggested actions and intents).
/// </summary>
/// <typeparam name="T">The recognition result type.</typeparam>
/// <param name="dialogContext">Context object containing information for a single turn of conversation with a user.</param>
/// <param name="activity">activity to recognize.</param>
/// <param name="recognizerOptions">A <see cref="LuisRecognizerOptions"/> instance to be used by the call.
/// This parameter overrides the default <see cref="LuisRecognizerOptions"/> passed in the constructor.</param>
/// <param name="telemetryProperties">Additional properties to be logged to telemetry with the LuisResult event.</param>
/// <param name="telemetryMetrics">Additional metrics to be logged to telemetry with the LuisResult event.</param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <returns>The LUIS results of the analysis of the current message text in the current turn's context activity.</returns>
public virtual async Task<T> RecognizeAsync<T>(DialogContext dialogContext, Activity activity, LuisRecognizerOptions recognizerOptions, Dictionary<string, string> telemetryProperties, Dictionary<string, double> telemetryMetrics = null, CancellationToken cancellationToken = default(CancellationToken))
where T : IRecognizerConvert, new()
{
var result = new T();
result.Convert(await RecognizeInternalAsync(dialogContext, activity, recognizerOptions, telemetryProperties, telemetryMetrics, cancellationToken).ConfigureAwait(false));
return result;
}

/// <summary>
/// Invoked prior to a LuisResult being logged.
/// </summary>
Expand Down Expand Up @@ -495,6 +619,24 @@ private async Task<RecognizerResult> RecognizeInternalAsync(ITurnContext turnCon
return result;
}

/// <summary>
/// Returns a RecognizerResult object.
/// </summary>
/// <param name="dialogContext">Dialog turn Context.</param>
/// <param name="activity">activity to recognize.</param>
/// <param name="predictionOptions">LuisRecognizerOptions implementation to override current properties.</param>
/// <param name="telemetryProperties"> Additional properties to be logged to telemetry with the LuisResult event.</param>
/// <param name="telemetryMetrics">Additional metrics to be logged to telemetry with the LuisResult event.</param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <returns>RecognizerResult object.</returns>
private async Task<RecognizerResult> RecognizeInternalAsync(DialogContext dialogContext, Activity activity, LuisRecognizerOptions predictionOptions, Dictionary<string, string> telemetryProperties, Dictionary<string, double> telemetryMetrics, CancellationToken cancellationToken)
{
var recognizer = predictionOptions ?? _luisRecognizerOptions;
var result = await recognizer.RecognizeInternalAsync(dialogContext, activity, HttpClient, cancellationToken).ConfigureAwait(false);
await OnRecognizerResultAsync(result, dialogContext.Context, telemetryProperties, telemetryMetrics, cancellationToken).ConfigureAwait(false);
return result;
}

/// <summary>
/// Returns a LuisRecognizerOptionsV2.
/// This exists to maintain backwards compatibility with existing constructors.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Schema;

namespace Microsoft.Bot.Builder.AI.Luis
{
Expand Down Expand Up @@ -63,6 +64,6 @@ protected LuisRecognizerOptions(LuisApplication application)
internal abstract Task<RecognizerResult> RecognizeInternalAsync(ITurnContext turnContext, HttpClient httpClient, CancellationToken cancellationToken);

// Support DialogContext
internal abstract Task<RecognizerResult> RecognizeInternalAsync(DialogContext context, HttpClient httpClient, CancellationToken cancellationToken);
internal abstract Task<RecognizerResult> RecognizeInternalAsync(DialogContext context, Activity activity, HttpClient httpClient, CancellationToken cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class LuisRecognizerOptionsV2 : LuisRecognizerOptions
/// </summary>
/// <param name="application">The LUIS application to use to recognize text.</param>
public LuisRecognizerOptionsV2(LuisApplication application)
: base(application)
: base(application)
{
}

Expand All @@ -43,7 +43,7 @@ public LuisRecognizerOptionsV2(LuisApplication application)
/// <value> This settings will be used to call Luis.</value>
public LuisPredictionOptions PredictionOptions { get; set; } = new LuisPredictionOptions();

internal override async Task<RecognizerResult> RecognizeInternalAsync(DialogContext context, HttpClient httpClient, CancellationToken cancellationToken)
internal override async Task<RecognizerResult> RecognizeInternalAsync(DialogContext context, Activity actiivty, HttpClient httpClient, CancellationToken cancellationToken)
=> await RecognizeInternalAsync(context.Context, httpClient, cancellationToken).ConfigureAwait(false);

internal override async Task<RecognizerResult> RecognizeInternalAsync(ITurnContext turnContext, HttpClient httpClient, CancellationToken cancellationToken)
Expand Down
Loading