Skip to content
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

feat/flow-builder-plugin-smart-intents-v2 #2825

Merged
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
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ All notable changes to Botonic will be documented in this file.

</details>

## [0.27.0] - tbd

### Added

### Changed

- [@botonic/plugin-flow-builder](https://www.npmjs.com/package/@botonic/plugin-flow-builder)

- [Modified Smart Intents api request to call smart intents inference v2](https://github.com/hubtype/botonic/pull/2825)

### Fixed

## [0.26.0] - 2024-04-30

### Added
Expand Down
10 changes: 8 additions & 2 deletions packages/botonic-plugin-flow-builder/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ import {
PayloadParamsBase,
} from './types'
import { getNodeByUserInput } from './user-input'
import { SmartIntentsInferenceConfig } from './user-input/smart-intent'
import { resolveGetAccessToken } from './utils'

export default class BotonicPluginFlowBuilder implements Plugin {
public cmsApi: FlowBuilderApi
private flowUrl: string
Expand All @@ -54,6 +54,7 @@ export default class BotonicPluginFlowBuilder implements Plugin {
public getKnowledgeBaseResponse?: (
request: ActionRequest
) => Promise<KnowledgeBaseResponse>
public smartIntentsConfig: SmartIntentsInferenceConfig

constructor(readonly options: BotonicPluginFlowBuilderOptions) {
const apiUrl = options.apiUrl || FLOW_BUILDER_API_URL_PROD
Expand All @@ -64,6 +65,10 @@ export default class BotonicPluginFlowBuilder implements Plugin {
this.getAccessToken = resolveGetAccessToken(options)
this.trackEvent = options.trackEvent
this.getKnowledgeBaseResponse = options.getKnowledgeBaseResponse
this.smartIntentsConfig = {
...options?.smartIntentsConfig,
useLatest: jsonVersion === FlowBuilderJSONVersion.LATEST,
}
const customFunctions = options.customFunctions || {}
this.functions = { ...DEFAULT_FUNCTIONS, ...customFunctions }
}
Expand All @@ -88,7 +93,8 @@ export default class BotonicPluginFlowBuilder implements Plugin {
const nodeByUserInput = await getNodeByUserInput(
this.cmsApi,
resolvedLocale,
request as unknown as ActionRequest
request as unknown as ActionRequest,
this.smartIntentsConfig
)
request.input.payload = this.cmsApi.getPayload(nodeByUserInput?.target)
}
Expand Down
1 change: 1 addition & 0 deletions packages/botonic-plugin-flow-builder/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface BotonicPluginFlowBuilderOptions {
getKnowledgeBaseResponse?: (
request: ActionRequest
) => Promise<KnowledgeBaseResponse>
smartIntentsConfig?: { numSmartIntentsToUse: number }
}

export interface FlowBuilderApiOptions {
Expand Down
11 changes: 8 additions & 3 deletions packages/botonic-plugin-flow-builder/src/user-input/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import {
} from '../content-fields/hubtype-fields'
import { getIntentNodeByInput } from './intent'
import { getKeywordNodeByInput } from './keyword'
import { SmartIntentsApi } from './smart-intent'
import { SmartIntentsApi, SmartIntentsInferenceConfig } from './smart-intent'

export async function getNodeByUserInput(
cmsApi: FlowBuilderApi,
locale: string,
request: ActionRequest
request: ActionRequest,
smartIntentsConfig: SmartIntentsInferenceConfig
): Promise<HtSmartIntentNode | HtIntentNode | HtKeywordNode | undefined> {
if (request.input.data) {
const keywordNode = await getKeywordNodeByInput(
Expand All @@ -24,7 +25,11 @@ export async function getNodeByUserInput(
)
if (keywordNode) return keywordNode

const smartIntentsApi = new SmartIntentsApi(cmsApi, request)
const smartIntentsApi = new SmartIntentsApi(
cmsApi,
request,
smartIntentsConfig
)
const smartIntentNode = smartIntentsApi.getNodeByInput()
if (smartIntentNode) return smartIntentNode

Expand Down
62 changes: 28 additions & 34 deletions packages/botonic-plugin-flow-builder/src/user-input/smart-intent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,60 @@ import axios from 'axios'
import { FlowBuilderApi } from '../api'
import { HtSmartIntentNode } from '../content-fields/hubtype-fields/smart-intent'

interface InferenceParam {
name: string
definition: string
export interface SmartIntentsInferenceParams {
bot_id: string
text: string
num_smart_intents_to_use?: number
use_latest: boolean
}
export class SmartIntentsApi {
public cmsApi: FlowBuilderApi
public currentRequest: ActionRequest

constructor(cmsApi: FlowBuilderApi, request: ActionRequest) {
this.currentRequest = request
this.cmsApi = cmsApi
}
export interface SmartIntentsInferenceConfig {
useLatest: boolean
numSmartIntentsToUse?: number
}

export class SmartIntentsApi {
constructor(
public cmsApi: FlowBuilderApi,
public currentRequest: ActionRequest,
public smartIntentsConfig: SmartIntentsInferenceConfig
) {}

async getNodeByInput(): Promise<HtSmartIntentNode | undefined> {
if (!this.currentRequest.input.data) return undefined
vanbasten17 marked this conversation as resolved.
Show resolved Hide resolved
const smartIntentNodes = this.cmsApi.getSmartIntentNodes()
if (!smartIntentNodes.length) return undefined

if (smartIntentNodes.length === 0) {
return undefined
const params = {
bot_id: this.currentRequest.session.bot.id,
text: this.currentRequest.input.data,
num_smart_intents_to_use: this.smartIntentsConfig.numSmartIntentsToUse,
use_latest: this.smartIntentsConfig.useLatest,
}

const params = this.getInferenceParams(smartIntentNodes)
try {
const response = await this.getInference(params)
return smartIntentNodes.find(
smartIntentNode =>
smartIntentNode.content.title === response.data.intent_name
smartIntentNode.content.title === response.data.smart_intent_title
)
} catch (e) {
console.error(e)
return undefined
}
}

private getInferenceParams(
smartIntentNodes: HtSmartIntentNode[]
): InferenceParam[] {
const intentsInferenceParams = smartIntentNodes.map(smartIntentNode => {
return {
name: smartIntentNode.content.title,
definition: smartIntentNode.content.description,
}
})
intentsInferenceParams.push({
name: 'Other',
definition: 'The text does not belong to any other intent.',
})
return intentsInferenceParams
}

private async getInference(
inferenceParams: InferenceParam[]
): Promise<{ data: { intent_name: string } }> {
inferenceParams: SmartIntentsInferenceParams
): Promise<{ data: { smart_intent_title: string } }> {
return await axios({
method: 'POST',
url: `${process.env.HUBTYPE_API_URL}/external/v1/ai/smart_intents/inference/`,
url: `${process.env.HUBTYPE_API_URL}/external/v2/ai/smart_intents/inference/`,
headers: {
Authorization: `Bearer ${this.currentRequest.session._access_token}`,
'Content-Type': 'application/json',
},
data: { text: this.currentRequest.input.data, intents: inferenceParams },
data: inferenceParams,
timeout: 10000,
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ export function mockSmartIntent(intentName?: string) {

// Change the implementation of getInference
getInferenceSpy.mockImplementation(async () => {
return intentName ? { data: { intent_name: intentName } } : undefined
return intentName ? { data: { smart_intent_title: intentName } } : undefined
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ describe('Check the contents returned by the plugin when match a smart intent',
request,
flowBuilderPlugin
)

expect((contents[0] as FlowText).text).toBe(
'Message explaining how to add a bag'
)
Expand Down
Loading