Skip to content

Commit

Permalink
fix: [#4509] botbuilder-ai@4.20.0 is still installing @azure/ms-rest-…
Browse files Browse the repository at this point in the history
…js@1.11.2 for @azure/cognitiveservices-luis-runtime (#4519)

* Upgrade @azure/cognitiveservices-luis-runtime to v.4.0.0

* Remove unnecessary resolution from package.json

* Add missing docs

* Fix conflicts in yarn.lock

* Fix error message in test.
  • Loading branch information
ceciliaavila committed Aug 22, 2023
1 parent 7110e8a commit 81e2d98
Show file tree
Hide file tree
Showing 14 changed files with 774 additions and 48 deletions.
5 changes: 3 additions & 2 deletions libraries/botbuilder-ai/etc/botbuilder-ai.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { DialogTurnResult } from 'botbuilder-dialogs';
import { EnumExpression } from 'adaptive-expressions';
import { Expression } from 'adaptive-expressions';
import { IntExpression } from 'adaptive-expressions';
import { LUISRuntimeModels } from '@azure/cognitiveservices-luis-runtime';
import * as msRest from '@azure/ms-rest-js';
import { NumberExpression } from 'adaptive-expressions';
import { ObjectExpression } from 'adaptive-expressions';
import { Recognizer } from 'botbuilder-dialogs';
Expand Down Expand Up @@ -290,7 +290,7 @@ export class LuisComponentRegistration extends ComponentRegistration {
}

// @public
export interface LuisPredictionOptions extends LUISRuntimeModels.PredictionResolveOptionalParams {
export interface LuisPredictionOptions extends msRest.RequestOptionsBase {
bingSpellCheckSubscriptionKey?: string;
includeAllIntents?: boolean;
includeInstanceData?: boolean;
Expand All @@ -300,6 +300,7 @@ export interface LuisPredictionOptions extends LUISRuntimeModels.PredictionResol
staging?: boolean;
telemetryClient?: BotTelemetryClient;
timezoneOffset?: number;
verbose?: boolean;
}

// @public
Expand Down
2 changes: 1 addition & 1 deletion libraries/botbuilder-ai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
}
},
"dependencies": {
"@azure/cognitiveservices-luis-runtime": "2.0.0",
"@azure/cognitiveservices-luis-runtime": "^4.0.0",
"@azure/ms-rest-js": "^2.7.0",
"adaptive-expressions": "4.1.6",
"botbuilder-core": "4.1.6",
Expand Down
8 changes: 6 additions & 2 deletions libraries/botbuilder-ai/src/luisRecognizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import { LUISRuntimeModels as LuisModels } from '@azure/cognitiveservices-luis-runtime';
import * as msRest from '@azure/ms-rest-js';

import Url from 'url-parse';
import { BotTelemetryClient, NullTelemetryClient, RecognizerResult, TurnContext } from 'botbuilder-core';
Expand Down Expand Up @@ -41,7 +41,11 @@ export interface LuisApplication {
*
* Options per LUIS prediction.
*/
export interface LuisPredictionOptions extends LuisModels.PredictionResolveOptionalParams {
export interface LuisPredictionOptions extends msRest.RequestOptionsBase {
/**
* If true, return all intents instead of just the top scoring intent.
*/
verbose?: boolean;
/**
* (Optional) Bing Spell Check subscription key.
*/
Expand Down
55 changes: 25 additions & 30 deletions libraries/botbuilder-ai/src/luisRecognizerOptionsV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@
import * as msRest from '@azure/ms-rest-js';
import * as os from 'os';
import { LuisApplication, LuisRecognizerOptionsV2 } from './luisRecognizer';
import { CompositeChildModel, CompositeEntityModel, EntityModel, LuisResult } from './luisV2-models/luisResult';
import { LUISRuntimeClientV2 as LuisClient } from './luisV2-models/luisRuntimeClientV2';
import { LuisRecognizerInternal } from './luisRecognizerOptions';
import { NullTelemetryClient, TurnContext, RecognizerResult } from 'botbuilder-core';

import {
LUISRuntimeClient as LuisClient,
LUISRuntimeModels as LuisModels,
} from '@azure/cognitiveservices-luis-runtime';
import { DialogContext } from 'botbuilder-dialogs';

// eslint-disable-next-line @typescript-eslint/no-var-requires
Expand Down Expand Up @@ -116,7 +113,7 @@ export class LuisRecognizerV2 extends LuisRecognizerInternal {

const luisPredictionOptions = this.options;

const luisResult: LuisModels.LuisResult = await this.luisClient.prediction.resolve(
const luisResult: LuisResult = await this.luisClient.prediction.resolve(
this.application.applicationId,
utterance,
{
Expand Down Expand Up @@ -155,7 +152,7 @@ export class LuisRecognizerV2 extends LuisRecognizerInternal {
}

// Get Intents from a LuisResult object.
private getIntents(luisResult: LuisModels.LuisResult): Record<string, Record<'score', number>> {
private getIntents(luisResult: LuisResult): Record<string, Record<'score', number>> {
const intents: { [name: string]: { score: number } } = {};
if (luisResult.intents) {
luisResult.intents.reduce((prev, curr) => {
Expand All @@ -171,8 +168,8 @@ export class LuisRecognizerV2 extends LuisRecognizerInternal {
}

private getEntitiesAndMetadata(
entities: LuisModels.EntityModel[],
compositeEntities: LuisModels.CompositeEntityModel[] | undefined,
entities: EntityModel[],
compositeEntities: CompositeEntityModel[] | undefined,
verbose: boolean
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): any {
Expand All @@ -183,14 +180,14 @@ export class LuisRecognizerV2 extends LuisRecognizerInternal {
// We start by populating composite entities so that entities covered by them are removed from the entities list
if (compositeEntities) {
compositeEntityTypes = compositeEntities.map(
(compositeEntity: LuisModels.CompositeEntityModel) => compositeEntity.parentType
(compositeEntity: CompositeEntityModel) => compositeEntity.parentType
);
compositeEntities.forEach((compositeEntity: LuisModels.CompositeEntityModel) => {
compositeEntities.forEach((compositeEntity: CompositeEntityModel) => {
entities = this.populateCompositeEntity(compositeEntity, entities, entitiesAndMetadata, verbose);
});
}

entities.forEach((entity: LuisModels.EntityModel) => {
entities.forEach((entity: EntityModel) => {
// we'll address composite entities separately
if (compositeEntityTypes.indexOf(entity.type) > -1) {
return;
Expand All @@ -213,35 +210,33 @@ export class LuisRecognizerV2 extends LuisRecognizerInternal {
}

private populateCompositeEntity(
compositeEntity: LuisModels.CompositeEntityModel,
entities: LuisModels.EntityModel[],
compositeEntity: CompositeEntityModel,
entities: EntityModel[],
entitiesAndMetadata: any, // eslint-disable-line @typescript-eslint/no-explicit-any
verbose: boolean
): LuisModels.EntityModel[] {
): EntityModel[] {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const childrenEntities: any = verbose ? { $instance: {} } : {};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let childrenEntitiesMetadata: any = {};

// This is now implemented as O(n^2) search and can be reduced to O(2n) using a map as an optimization if n grows
const compositeEntityMetadata: LuisModels.EntityModel | undefined = entities.find(
(entity: LuisModels.EntityModel) => {
// For now we are matching by value, which can be ambiguous if the same composite entity shows up with the same text
// multiple times within an utterance, but this is just a stop gap solution till the indices are included in composite entities
return entity.type === compositeEntity.parentType && entity.entity === compositeEntity.value;
}
);
const compositeEntityMetadata: EntityModel | undefined = entities.find((entity: EntityModel) => {
// For now we are matching by value, which can be ambiguous if the same composite entity shows up with the same text
// multiple times within an utterance, but this is just a stop gap solution till the indices are included in composite entities
return entity.type === compositeEntity.parentType && entity.entity === compositeEntity.value;
});

const filteredEntities: LuisModels.EntityModel[] = [];
const filteredEntities: EntityModel[] = [];
if (verbose) {
childrenEntitiesMetadata = this.getEntityMetadata(compositeEntityMetadata);
}

// This is now implemented as O(n*k) search and can be reduced to O(n + k) using a map as an optimization if n or k grow
const coveredSet = new Set();
compositeEntity.children.forEach((childEntity: LuisModels.CompositeChildModel) => {
compositeEntity.children.forEach((childEntity: CompositeChildModel) => {
for (let i = 0; i < entities.length; i++) {
const entity: LuisModels.EntityModel = entities[i];
const entity: EntityModel = entities[i];
if (
!coveredSet.has(i) &&
childEntity.type === entity.type &&
Expand Down Expand Up @@ -291,7 +286,7 @@ export class LuisRecognizerV2 extends LuisRecognizerInternal {
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
private getEntityValue(entity: LuisModels.EntityModel): any {
private getEntityValue(entity: EntityModel): any {
if (entity.type.startsWith('builtin.geographyV2.')) {
return {
type: entity.type.substring(20),
Expand Down Expand Up @@ -359,7 +354,7 @@ export class LuisRecognizerV2 extends LuisRecognizerInternal {
}
}

private getEntityMetadata(entity: LuisModels.EntityModel): Record<string, string | number> {
private getEntityMetadata(entity: EntityModel): Record<string, string | number> {
const res: Record<string, string | number> = {
startIndex: entity.startIndex,
endIndex: entity.endIndex + 1,
Expand All @@ -374,7 +369,7 @@ export class LuisRecognizerV2 extends LuisRecognizerInternal {
return res;
}

private getNormalizedEntityName(entity: LuisModels.EntityModel): string {
private getNormalizedEntityName(entity: EntityModel): string {
// Type::Role -> Role
let type = entity.type.split(':').pop();
if (type.startsWith('builtin.datetimeV2.')) {
Expand Down Expand Up @@ -405,7 +400,7 @@ export class LuisRecognizerV2 extends LuisRecognizerInternal {
}
}

private getSentiment(luis: LuisModels.LuisResult): Record<'label' | 'score', unknown> | undefined {
private getSentiment(luis: LuisResult): Record<'label' | 'score', unknown> | undefined {
if (luis.sentimentAnalysis) {
return {
label: luis.sentimentAnalysis.label,
Expand All @@ -427,7 +422,7 @@ export class LuisRecognizerV2 extends LuisRecognizerInternal {

private emitTraceInfo(
context: TurnContext,
luisResult: LuisModels.LuisResult,
luisResult: LuisResult,
recognizerResult: RecognizerResult
): Promise<unknown> {
const traceInfo = {
Expand Down
4 changes: 2 additions & 2 deletions libraries/botbuilder-ai/src/luisRecognizerOptionsV3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

import fetch from 'node-fetch';
import { RequestInfo, RequestInit } from 'node-fetch';
import { LUISRuntimeModels as LuisModels } from '@azure/cognitiveservices-luis-runtime';
import { LuisApplication, LuisRecognizerOptionsV3 } from './luisRecognizer';
import { LuisResult } from './luisV2-models/luisResult';
import { LuisRecognizerInternal } from './luisRecognizerOptions';
import { NullTelemetryClient, TurnContext, RecognizerResult } from 'botbuilder-core';
import { DialogContext } from 'botbuilder-dialogs';
Expand Down Expand Up @@ -239,7 +239,7 @@ export class LuisRecognizerV3 extends LuisRecognizerInternal {

private emitTraceInfo(
context: TurnContext,
luisResult: LuisModels.LuisResult,
luisResult: LuisResult,
recognizerResult: RecognizerResult,
options: LuisRecognizerOptionsV3
): Promise<unknown> {
Expand Down
Loading

0 comments on commit 81e2d98

Please sign in to comment.