Skip to content

Commit

Permalink
Merge pull request #2199 from botpress/rl_offline-lang-server
Browse files Browse the repository at this point in the history
feat(lang-server): add option to run offline
  • Loading branch information
rndlaine committed Jul 31, 2019
2 parents e65227b + 0fa4d22 commit 8a941a3
Show file tree
Hide file tree
Showing 22 changed files with 3,505 additions and 2,212 deletions.
4 changes: 4 additions & 0 deletions src/bp/index.ts
Expand Up @@ -229,6 +229,10 @@ try {
description: 'URL of metadata file which lists available languages',
default: 'http://botpress-public.nyc3.digitaloceanspaces.com/embeddings/index.json'
},
offline: {
description: 'Whether or not the language server has internet access',
default: false
},
dim: {
default: 100,
description: 'Number of language dimensions provided (25, 100 or 300 at the moment)'
Expand Down
32 changes: 30 additions & 2 deletions src/bp/lang-server/api.ts
Expand Up @@ -8,6 +8,7 @@ import ms from 'ms'

import { BadRequestError } from '../core/routers/errors'

import { getLanguageByCode } from './languages'
import { LangServerLogger } from './logger'
import { monitoringMiddleware, startMonitoring } from './monitoring'
import LanguageService from './service'
Expand All @@ -31,6 +32,8 @@ export type APIOptions = {
adminToken: string
}

const OFFLINE_ERR_MSG = 'The server is running in offline mode. This function is disabled.'

const debug = DEBUG('api')
const debugRequest = debug.sub('request')
const cachePolicy = { 'Cache-Control': `max-age=${ms('1d')}` }
Expand Down Expand Up @@ -74,7 +77,11 @@ const createExpressApp = (options: APIOptions): Application => {
return app
}

export default async function(options: APIOptions, languageService: LanguageService, downloadManager: DownloadManager) {
export default async function(
options: APIOptions,
languageService: LanguageService,
downloadManager?: DownloadManager
) {
const app = createExpressApp(options)
const logger = new LangServerLogger('API')

Expand All @@ -99,7 +106,7 @@ export default async function(options: APIOptions, languageService: LanguageServ
const language = req.language!

if (!utterances || !_.isArray(utterances) || !utterances.length) {
// For backward cpompatibility with Botpress 12.0.0 - 12.0.2
// For backward compatibility with Botpress 12.0.0 - 12.0.2
const singleInput = req.body.input
if (!singleInput || !_.isString(singleInput)) {
throw new BadRequestError('Param `utterances` is mandatory (must be an array of string)')
Expand Down Expand Up @@ -134,6 +141,19 @@ export default async function(options: APIOptions, languageService: LanguageServ
const router = express.Router({ mergeParams: true })

router.get('/', (req, res) => {
if (!downloadManager) {
const localLanguages = languageService.getModels().map(m => {
const { name } = getLanguageByCode(m.lang)
return { ...m, code: m.lang, name }
})

return res.send({
available: localLanguages,
installed: localLanguages,
downloading: []
})
}

const downloading = downloadManager.inProgress.map(x => ({
lang: x.lang,
progress: {
Expand All @@ -152,6 +172,10 @@ export default async function(options: APIOptions, languageService: LanguageServ

router.post('/:lang', adminTokenMw, async (req, res) => {
const { lang } = req.params
if (!downloadManager) {
return res.status(404).send({ success: false, error: OFFLINE_ERR_MSG })
}

try {
const downloadId = await downloadManager.download(lang)
res.json({ success: true, downloadId })
Expand All @@ -176,6 +200,10 @@ export default async function(options: APIOptions, languageService: LanguageServ

router.post('/cancel/:id', adminTokenMw, (req, res) => {
const { id } = req.params
if (!downloadManager) {
return res.send({ success: false, error: OFFLINE_ERR_MSG })
}

downloadManager.cancelAndRemove(id)
res.status(200).send({ success: true })
})
Expand Down
26 changes: 20 additions & 6 deletions src/bp/lang-server/index.ts
Expand Up @@ -24,6 +24,7 @@ export interface ArgV {
authToken?: string
adminToken?: string
metadataLocation: string
offline: boolean
dim: number
domain: string
}
Expand Down Expand Up @@ -73,12 +74,25 @@ export default async function(options: ArgV) {
logger.info(`limit: ${chalk.redBright('disabled')} (no protection - anyone can query without limitation)`)
}

if (options.offline) {
logger.info(
`mode: ${chalk.redBright(
'offline'
)} (languages need to be downloaded manually from a machine with Internet access)`
)
} else {
logger.info(`Mode: ${chalk.greenBright('online')} (languages will be downloaded from ${options.metadataLocation})`)
}

logger.info(`Serving ${options.dim} language dimensions from ${options.langDir}`)
logger.info(' ')

await Promise.all([
API(apiOptions, langService, downloadManager),
downloadManager.initialize(),
langService.initialize()
])
if (options.offline) {
await Promise.all([API(apiOptions, langService), langService.initialize()])
} else {
await Promise.all([
API(apiOptions, langService, downloadManager),
downloadManager.initialize(),
langService.initialize()
])
}
}

0 comments on commit 8a941a3

Please sign in to comment.