@botpress/cognitive 0.4.7 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@botpress/cognitive",
3
- "version": "0.4.7",
3
+ "version": "0.5.0",
4
4
  "description": "Wrapper around the Botpress Client to call LLMs",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.mjs",
package/refresh-models.ts CHANGED
@@ -2,7 +2,7 @@ import 'dotenv/config'
2
2
  import axios from 'axios'
3
3
  import * as fs from 'fs'
4
4
  import * as path from 'path'
5
- import { Model } from 'src/schemas.gen'
5
+ import { Model } from 'src/cognitive-v2/types'
6
6
 
7
7
  const builtInModels = ['auto', 'best', 'fast']
8
8
  const filteredLifecycles = ['deprecated', 'discontinued']
@@ -10,9 +10,7 @@ const filteredLifecycles = ['deprecated', 'discontinued']
10
10
  const modelsListPath = path.resolve(__dirname, 'src/cognitive-v2', 'models.ts')
11
11
  const typesPath = path.resolve(__dirname, 'src/cognitive-v2', 'types.ts')
12
12
 
13
- type RemoteModel = Model & { lifecycle?: string; aliases?: string[]; capabilities?: { supportsImages?: boolean } }
14
-
15
- const toRef = (m: RemoteModel | string | null | undefined): string | null => {
13
+ const toRef = (m: Model | string | null | undefined): string | null => {
16
14
  if (!m) return null
17
15
  if (typeof m === 'string') return m
18
16
 
@@ -21,22 +19,22 @@ const toRef = (m: RemoteModel | string | null | undefined): string | null => {
21
19
  }
22
20
 
23
21
  async function main(): Promise<void> {
24
- const server = process.env.COGNITIVE_SERVER || 'https://api.botpress.cloud/v2/cognitive'
25
- const key = process.env.TOKEN
26
- const botId = process.env.BOT_ID
22
+ const server = process.env.CLOUD_COGNITIVE_ENDPOINT || 'https://api.botpress.cloud/v2/cognitive'
23
+ const key = process.env.CLOUD_PAT
24
+ const botId = process.env.CLOUD_BOT_ID
27
25
 
28
26
  const {
29
27
  data: { models },
30
- } = await axios.get<{ models: RemoteModel[] }>(`${server}/models?includeDeprecated=true`, {
28
+ } = await axios.get<{ models: Model[] }>(`${server}/models?includeDeprecated=true`, {
31
29
  headers: {
32
30
  Authorization: `Bearer ${key}`,
33
31
  'X-Bot-Id': botId,
34
32
  },
35
33
  })
36
34
 
37
- const modelsObj = models.reduce((acc, m) => ((acc[m.id] = m), acc), {} as Record<string, RemoteModel>)
35
+ const modelsObj = models.reduce((acc, m) => ((acc[m.id] = m), acc), {} as Record<string, Model>)
38
36
 
39
- const defaultModel: RemoteModel = {
37
+ const defaultModel: Model = {
40
38
  id: '',
41
39
  name: '',
42
40
  description: '',
@@ -46,39 +44,62 @@ async function main(): Promise<void> {
46
44
  lifecycle: 'production',
47
45
  }
48
46
 
49
- const newFile = `import { Model } from 'src/schemas.gen'\n
50
- export type RemoteModel = Model & { aliases?: string[]; lifecycle: 'production' | 'preview' | 'deprecated' | 'discontinued'; capabilities?: { supportsImages?: boolean } }\n
51
- export const models: Record<string, RemoteModel> = ${JSON.stringify(modelsObj, null, 2)}\n
52
- export const defaultModel: RemoteModel = ${JSON.stringify(defaultModel, undefined, 2)}
47
+ const newFile = `import { Model } from './types'\n
48
+ export const models: Record<string, Model> = ${JSON.stringify(modelsObj, null, 2)}\n
49
+ export const defaultModel: Model = ${JSON.stringify(defaultModel, undefined, 2)}
53
50
  `
54
51
 
55
52
  fs.writeFileSync(modelsListPath, newFile, 'utf8')
56
53
 
57
- const withoutDeprecated = models.filter((m) => !filteredLifecycles.includes(m.lifecycle))
58
- const refs = Array.from(new Set(withoutDeprecated.map(toRef).filter(Boolean))).sort((a, b) => a.localeCompare(b))
59
- const aliases = models.flatMap((m) =>
60
- (m.aliases || []).map((a) => {
61
- const [provider] = m.id.split(':')
62
- return `${provider}:${a}`
63
- })
64
- )
54
+ const collectRefs = (list: Model[]) =>
55
+ Array.from(new Set(list.map(toRef).filter(Boolean))).sort((a, b) => a.localeCompare(b))
56
+ const collectAliases = (list: Model[]) =>
57
+ Array.from(new Set(list.flatMap((m) => (m.aliases || []).map((a) => `${m.id.split(':')[0]}:${a}`))))
58
+
59
+ const active = models.filter((m) => !filteredLifecycles.includes(m.lifecycle))
60
+ const activeLlm = active.filter((m) => !m.capabilities?.supportsTranscription)
61
+ const activeStt = active.filter((m) => m.capabilities?.supportsTranscription)
62
+
63
+ const refs = collectRefs(activeLlm)
64
+ const aliases = collectAliases(models.filter((m) => !m.capabilities?.supportsTranscription))
65
+
66
+ const sttRefs = collectRefs(activeStt)
67
+ const sttAliases = collectAliases(activeStt)
65
68
 
66
69
  const content = fs.readFileSync(typesPath, 'utf8')
67
70
 
68
- const startMarker = 'type Models ='
69
- const endMarker = 'export type CognitiveRequest'
71
+ // Update Models union
72
+ const modelsStartMarker = 'export type Models ='
73
+ const modelsEndMarker = 'export type SttModels ='
70
74
 
71
- const startIdx = content.indexOf(startMarker)
72
- const endIdx = content.indexOf(endMarker)
75
+ const modelsStartIdx = content.indexOf(modelsStartMarker)
76
+ const modelsEndIdx = content.indexOf(modelsEndMarker)
73
77
 
74
- if (startIdx === -1 || endIdx === -1 || endIdx <= startIdx) {
75
- throw new Error('Could not locate Models union block in models.ts')
78
+ if (modelsStartIdx === -1 || modelsEndIdx === -1 || modelsEndIdx <= modelsStartIdx) {
79
+ throw new Error('Could not locate Models union block in types.ts')
76
80
  }
77
81
 
78
82
  const items = [...builtInModels, ...refs, ...aliases].map((r) => ` | '${r}'`)
79
- const unionBlock = ['type Models =', ...items, ' | ({} & string)', ''].join('\n')
83
+ const modelsUnionBlock = ['export type Models =', ...items, ' | ({} & string)', '', ''].join('\n')
84
+
85
+ let nextContent = content.slice(0, modelsStartIdx) + modelsUnionBlock + content.slice(modelsEndIdx)
86
+
87
+ // Update SttModels union
88
+ const sttStartMarker = 'export type SttModels ='
89
+ const sttEndMarker = 'export type CognitiveContentPart'
90
+
91
+ const sttStartIdx = nextContent.indexOf(sttStartMarker)
92
+ const sttEndIdx = nextContent.indexOf(sttEndMarker)
93
+
94
+ if (sttStartIdx === -1 || sttEndIdx === -1 || sttEndIdx <= sttStartIdx) {
95
+ throw new Error('Could not locate SttModels union block in types.ts')
96
+ }
97
+
98
+ const sttItems = [...builtInModels, ...sttRefs, ...sttAliases].map((r) => ` | '${r}'`)
99
+ const sttUnionBlock = ['export type SttModels =', ...sttItems, ' | ({} & string)', '', ''].join('\n')
100
+
101
+ nextContent = nextContent.slice(0, sttStartIdx) + sttUnionBlock + nextContent.slice(sttEndIdx)
80
102
 
81
- const nextContent = content.slice(0, startIdx) + unionBlock + content.slice(endIdx)
82
103
  fs.writeFileSync(typesPath, nextContent, 'utf8')
83
104
  }
84
105