Skip to content

Commit bbc1a61

Browse files
authored
chore(cognitive): cleanup (botpress#14288)
1 parent bf7edd3 commit bbc1a61

6 files changed

Lines changed: 37 additions & 42 deletions

File tree

packages/cognitive/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@botpress/cognitive",
3-
"version": "0.1.39",
3+
"version": "0.1.40",
44
"description": "Wrapper around the Botpress Client to call LLMs",
55
"main": "./dist/index.cjs",
66
"module": "./dist/index.mjs",

packages/cognitive/src/client.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,7 @@ export class Cognitive {
166166
return this._generateContent(input)
167167
}
168168

169-
const betaClient = new CognitiveBeta({
170-
headers: this._client.config.headers as Record<string, string>,
171-
withCredentials: this._client.config.withCredentials,
172-
baseUrl: this._client.config.apiUrl.includes('.cloud')
173-
? 'https://cognitive.botpress.cloud'
174-
: 'https://cognitive.botpress.dev',
175-
})
169+
const betaClient = new CognitiveBeta(this._client.config as any)
176170

177171
const response = await betaClient.generateText(input as any)
178172
return {

packages/cognitive/src/cognitive_beta/index.ts

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { CognitiveRequest, CognitiveResponse, CognitiveStreamChunk, Model } from
55
export { CognitiveRequest, CognitiveResponse, CognitiveStreamChunk }
66

77
type ClientProps = {
8-
baseUrl?: string
8+
apiUrl?: string
99
timeout?: number
1010
botId?: string
1111
token?: string
@@ -22,49 +22,52 @@ const isBrowser = () => typeof window !== 'undefined' && typeof window.fetch ===
2222

2323
export class CognitiveBeta {
2424
private _axiosClient: AxiosInstance
25-
private readonly _config: Required<ClientProps>
25+
private readonly _apiUrl: string
26+
private readonly _timeout: number
27+
private readonly _withCredentials: boolean
28+
private readonly _headers: Record<string, string>
2629

2730
public constructor(props: ClientProps) {
28-
this._config = {
29-
baseUrl: props.baseUrl || 'https://cognitive.botpress.cloud',
30-
timeout: props.timeout || 60_001,
31-
token: props.token || '',
32-
botId: props.botId || '',
33-
withCredentials: props.withCredentials || false,
34-
headers: props.headers || {},
31+
this._apiUrl = props.apiUrl || 'https://api.botpress.cloud'
32+
this._timeout = props.timeout || 60_001
33+
this._withCredentials = props.withCredentials || false
34+
this._headers = { ...props.headers }
35+
36+
if (props.botId) {
37+
this._headers['X-Bot-Id'] = props.botId
38+
}
39+
40+
if (props.token) {
41+
this._headers['Authorization'] = `Bearer ${props.token}`
3542
}
3643

3744
this._axiosClient = axios.create({
38-
headers: {
39-
Authorization: `Bearer ${this._config.token}`,
40-
'X-Bot-Id': this._config.botId,
41-
...this._config.headers,
42-
},
43-
withCredentials: this._config.withCredentials,
44-
baseURL: this._config.baseUrl,
45+
headers: this._headers,
46+
withCredentials: this._withCredentials,
47+
baseURL: this._apiUrl,
4548
})
4649
}
4750

4851
public async generateText(input: CognitiveRequest, options: RequestOptions = {}) {
49-
const signal = options.signal ?? AbortSignal.timeout(this._config.timeout)
52+
const signal = options.signal ?? AbortSignal.timeout(this._timeout)
5053

5154
const { data } = await this._withServerRetry(() =>
52-
this._axiosClient.post<CognitiveResponse>('/v1/generate-text', input, {
55+
this._axiosClient.post<CognitiveResponse>('/v2/cognitive/generate-text', input, {
5356
signal,
54-
timeout: options.timeout ?? this._config.timeout,
57+
timeout: options.timeout ?? this._timeout,
5558
})
5659
)
5760

5861
return data
5962
}
6063

6164
public async listModels(input: void, options: RequestOptions = {}) {
62-
const signal = options.signal ?? AbortSignal.timeout(this._config.timeout)
65+
const signal = options.signal ?? AbortSignal.timeout(this._timeout)
6366

6467
const { data } = await this._withServerRetry(() =>
65-
this._axiosClient.post<Model[]>('/v1/models', input, {
68+
this._axiosClient.post<Model[]>('/v2/cognitive/models', input, {
6669
signal,
67-
timeout: options.timeout ?? this._config.timeout,
70+
timeout: options.timeout ?? this._timeout,
6871
})
6972
)
7073

@@ -75,18 +78,16 @@ export class CognitiveBeta {
7578
request: CognitiveRequest,
7679
options: RequestOptions = {}
7780
): AsyncGenerator<CognitiveStreamChunk, void, unknown> {
78-
const signal = options.signal ?? AbortSignal.timeout(this._config.timeout)
81+
const signal = options.signal ?? AbortSignal.timeout(this._timeout)
7982

8083
if (isBrowser()) {
81-
const res = await fetch(`${this._config.baseUrl}/v1/generate-text-stream`, {
84+
const res = await fetch(`${this._apiUrl}/v2/cognitive/generate-text-stream`, {
8285
method: 'POST',
8386
headers: {
84-
Authorization: `Bearer ${this._config.token}`,
85-
'X-Bot-Id': this._config.botId,
87+
...this._headers,
8688
'Content-Type': 'application/json',
87-
...this._config.headers,
8889
},
89-
credentials: this._config.withCredentials ? 'include' : 'omit',
90+
credentials: this._withCredentials ? 'include' : 'omit',
9091
body: JSON.stringify({ ...request, stream: true }),
9192
signal,
9293
})
@@ -129,7 +130,7 @@ export class CognitiveBeta {
129130
{
130131
responseType: 'stream',
131132
signal,
132-
timeout: options.timeout ?? this._config.timeout,
133+
timeout: options.timeout ?? this._timeout,
133134
}
134135
)
135136
)

packages/llmz/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"tsx": "^4.19.2"
6868
},
6969
"peerDependencies": {
70-
"@botpress/cognitive": "0.1.39",
70+
"@botpress/cognitive": "0.1.40",
7171
"@bpinternal/thicktoken": "^1.0.5",
7272
"@bpinternal/zui": "^1.0.1"
7373
},

packages/zai/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@botpress/zai",
33
"description": "Zui AI (zai) – An LLM utility library written on top of Zui and the Botpress API",
4-
"version": "2.1.9",
4+
"version": "2.1.10",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",
77
"exports": {
@@ -29,7 +29,7 @@
2929
"author": "",
3030
"license": "ISC",
3131
"dependencies": {
32-
"@botpress/cognitive": "0.1.39",
32+
"@botpress/cognitive": "0.1.40",
3333
"json5": "^2.2.3",
3434
"jsonrepair": "^3.10.0",
3535
"lodash-es": "^4.17.21"

pnpm-lock.yaml

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)