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 3 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
12 changes: 10 additions & 2 deletions packages/botonic-plugin-flow-builder/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ import {
PayloadParamsBase,
} from './types'
import { getNodeByUserInput } from './user-input'
import { SmartIntentsInferenceParams } from './user-input/smart-intent'
import { resolveGetAccessToken } from './utils'

export default class BotonicPluginFlowBuilder implements Plugin {
public cmsApi: FlowBuilderApi
private flowUrl: string
private flow?: HtFlowBuilderData
private functions: Record<any, any>
private currentRequest: PluginPreRequest

private getAccessToken: (session: Session) => string
public getLocale: (session: Session) => string
public trackEvent?: (
Expand All @@ -55,6 +56,8 @@ export default class BotonicPluginFlowBuilder implements Plugin {
request: ActionRequest
) => Promise<KnowledgeBaseResponse>

public smartIntentsConfig: Partial<SmartIntentsInferenceParams>

constructor(readonly options: BotonicPluginFlowBuilderOptions) {
const apiUrl = options.apiUrl || FLOW_BUILDER_API_URL_PROD
const jsonVersion = options.jsonVersion || FlowBuilderJSONVersion.LATEST
Expand All @@ -64,6 +67,10 @@ export default class BotonicPluginFlowBuilder implements Plugin {
this.getAccessToken = resolveGetAccessToken(options)
this.trackEvent = options.trackEvent
this.getKnowledgeBaseResponse = options.getKnowledgeBaseResponse
this.smartIntentsConfig = {
...(options.smartIntentsConfig || {}),
use_latest: jsonVersion === FlowBuilderJSONVersion.LATEST,
}
Iru89 marked this conversation as resolved.
Show resolved Hide resolved
const customFunctions = options.customFunctions || {}
this.functions = { ...DEFAULT_FUNCTIONS, ...customFunctions }
}
Expand All @@ -88,7 +95,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?: { num_smart_intents_to_use: 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, SmartIntentsInferenceParams } from './smart-intent'

export async function getNodeByUserInput(
cmsApi: FlowBuilderApi,
locale: string,
request: ActionRequest
request: ActionRequest,
smartIntentsConfig: Partial<SmartIntentsInferenceParams>
): 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
53 changes: 24 additions & 29 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,61 @@ 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
use_latest: boolean
num_smart_intents_to_use?: number
}
export class SmartIntentsApi {
public cmsApi: FlowBuilderApi
public currentRequest: ActionRequest
public smartIntentsConfig: Partial<SmartIntentsInferenceParams>

constructor(cmsApi: FlowBuilderApi, request: ActionRequest) {
constructor(
cmsApi: FlowBuilderApi,
request: ActionRequest,
smartIntentsConfig: Partial<SmartIntentsInferenceParams>
) {
this.currentRequest = request
this.cmsApi = cmsApi
this.smartIntentsConfig = smartIntentsConfig
}

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: SmartIntentsInferenceParams = {
bot_id: this.currentRequest.session.bot.id,
text: this.currentRequest.input.data,
...this.smartIntentsConfig,
} as SmartIntentsInferenceParams
vanbasten17 marked this conversation as resolved.
Show resolved Hide resolved

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
Loading