@cloudbase/ai 2.7.16-beta.0 → 2.7.18-beta.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/dist/cjs/bot/index.d.ts +34 -0
- package/dist/cjs/bot/index.js +238 -0
- package/dist/cjs/bot/types.d.ts +104 -0
- package/dist/cjs/bot/types.js +3 -0
- package/dist/cjs/index.d.ts +7 -2
- package/dist/cjs/index.js +47 -14
- package/dist/cjs/models/Ark/index.js +4 -2
- package/dist/cjs/models/DashScope/index.js +4 -2
- package/dist/cjs/models/HunYuan/index.js +4 -2
- package/dist/cjs/models/HunYuanBeta/index.js +4 -2
- package/dist/cjs/models/Moonshot/index.js +4 -2
- package/dist/cjs/models/Yi/index.js +4 -2
- package/dist/cjs/models/ZhiPu/index.js +4 -2
- package/dist/cjs/type.d.ts +8 -0
- package/dist/cjs/type.js +1 -1
- package/dist/cjs/utils.js +3 -2
- package/dist/esm/bot/index.d.ts +34 -0
- package/dist/esm/bot/index.js +235 -0
- package/dist/esm/bot/types.d.ts +104 -0
- package/dist/esm/bot/types.js +2 -0
- package/dist/esm/index.d.ts +7 -2
- package/dist/esm/index.js +46 -14
- package/dist/esm/models/Ark/index.js +4 -2
- package/dist/esm/models/DashScope/index.js +4 -2
- package/dist/esm/models/HunYuan/index.js +4 -2
- package/dist/esm/models/HunYuanBeta/index.js +4 -2
- package/dist/esm/models/Moonshot/index.js +4 -2
- package/dist/esm/models/Yi/index.js +4 -2
- package/dist/esm/models/ZhiPu/index.js +4 -2
- package/dist/esm/type.d.ts +8 -0
- package/dist/esm/type.js +1 -1
- package/dist/esm/utils.js +2 -1
- package/package.json +4 -3
- package/src/bot/index.ts +158 -0
- package/src/bot/types.ts +118 -0
- package/src/index.ts +43 -14
- package/src/models/Ark/index.ts +3 -1
- package/src/models/DashScope/index.ts +3 -1
- package/src/models/HunYuan/index.ts +3 -1
- package/src/models/HunYuanBeta/index.ts +3 -1
- package/src/models/Moonshot/index.ts +3 -1
- package/src/models/Yi/index.ts +3 -1
- package/src/models/ZhiPu/index.ts +3 -1
- package/src/type.ts +12 -0
- package/src/utils.ts +1 -0
package/src/index.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { ICloudbase } from '@cloudbase/types'
|
|
2
2
|
import type { ICloudbaseComponent } from '@cloudbase/types/component'
|
|
3
3
|
import type { SDKRequestInterface } from '@cloudbase/adapter-interface'
|
|
4
|
+
import { Bot } from './bot'
|
|
4
5
|
import * as models from './models'
|
|
5
6
|
import * as types from './type'
|
|
6
7
|
import * as utils from './utils'
|
|
@@ -8,9 +9,29 @@ import * as utils from './utils'
|
|
|
8
9
|
const { MODELS, ...restModels } = models
|
|
9
10
|
|
|
10
11
|
class AI {
|
|
11
|
-
|
|
12
|
+
public aiBaseUrl: string
|
|
13
|
+
public aiBotBaseUrl: string
|
|
14
|
+
public bot: Bot
|
|
12
15
|
|
|
13
|
-
|
|
16
|
+
constructor(private req: SDKRequestInterface, public baseUrl: string) {
|
|
17
|
+
this.aiBaseUrl = `${baseUrl}/ai`
|
|
18
|
+
this.aiBotBaseUrl = `${baseUrl}/aibot`
|
|
19
|
+
this.bot = new Bot(this.botRequest, this.aiBotBaseUrl)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
createModel<T extends keyof typeof MODELS>(model: T) {
|
|
23
|
+
return new MODELS[model](this.modelRequest, this.aiBaseUrl) as InstanceType<(typeof MODELS)[T]>
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
registerModel(name: string, model: types.ChatModelConstructor) {
|
|
27
|
+
if (MODELS[name] != null) {
|
|
28
|
+
console.warn(`AI model ${name} already exists!`)
|
|
29
|
+
return
|
|
30
|
+
}
|
|
31
|
+
MODELS[name] = model
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
modelRequest: types.ModelReq = async ({ url, data, headers, stream }) => (
|
|
14
35
|
await this.req.fetch({
|
|
15
36
|
method: 'post',
|
|
16
37
|
headers: {
|
|
@@ -22,16 +43,26 @@ class AI {
|
|
|
22
43
|
})
|
|
23
44
|
).data
|
|
24
45
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
46
|
+
botRequest: types.BotReq = async ({ method, url, data = {}, headers, stream }) => {
|
|
47
|
+
if (method === 'get') {
|
|
48
|
+
return (await this.req.fetch({ url: `${url}?${objectToParam(data)}`, method, headers, stream })).data
|
|
49
|
+
}
|
|
28
50
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
51
|
+
return (
|
|
52
|
+
await this.req.fetch({
|
|
53
|
+
url,
|
|
54
|
+
body: JSON.stringify(data),
|
|
55
|
+
headers: { 'Content-Type': 'application/json', ...headers },
|
|
56
|
+
stream,
|
|
57
|
+
method,
|
|
58
|
+
})
|
|
59
|
+
).data
|
|
60
|
+
|
|
61
|
+
function objectToParam(obj: Object) {
|
|
62
|
+
return Object.entries(obj)
|
|
63
|
+
.map(([key, value]) => `${key}=${value}`)
|
|
64
|
+
.join('&')
|
|
33
65
|
}
|
|
34
|
-
MODELS[name] = model
|
|
35
66
|
}
|
|
36
67
|
}
|
|
37
68
|
|
|
@@ -39,7 +70,6 @@ const COMPONENT_NAME = 'ai'
|
|
|
39
70
|
|
|
40
71
|
interface AIInitOption {
|
|
41
72
|
env?: string
|
|
42
|
-
region?: string
|
|
43
73
|
baseUrl?: string
|
|
44
74
|
}
|
|
45
75
|
|
|
@@ -55,8 +85,7 @@ async function ai(this: ICloudbase, options?: AIInitOption) {
|
|
|
55
85
|
: { accessToken: '', env: '' }
|
|
56
86
|
|
|
57
87
|
const env = options?.env ?? accessTokenFromAuth.env
|
|
58
|
-
const
|
|
59
|
-
const baseUrl = options?.baseUrl ?? `https://${env}.${region}.api.tcloudbasegateway.com/v1/ai`
|
|
88
|
+
const baseUrl = options?.baseUrl ?? `https://${env}.api.tcloudbasegateway.com/v1`
|
|
60
89
|
|
|
61
90
|
return new AI(req, baseUrl)
|
|
62
91
|
}
|
|
@@ -74,4 +103,4 @@ function registerAi(app: ICloudbase) {
|
|
|
74
103
|
}
|
|
75
104
|
}
|
|
76
105
|
|
|
77
|
-
export { types, utils, restModels as models, registerAi, AI }
|
|
106
|
+
export { types, utils, restModels as models, registerAi, AI, Bot }
|
package/src/models/Ark/index.ts
CHANGED
|
@@ -76,7 +76,9 @@ class ArkModelStreamResult implements StreamTextResult {
|
|
|
76
76
|
const data = JSON.parse(chunk.data) as ArkStreamTextOutput
|
|
77
77
|
controller.enqueue(data)
|
|
78
78
|
} catch (e) {
|
|
79
|
-
|
|
79
|
+
if (chunk.data !== '[DONE]') {
|
|
80
|
+
console.warn('Error when transforming event source data to json', e)
|
|
81
|
+
}
|
|
80
82
|
}
|
|
81
83
|
},
|
|
82
84
|
}),),)
|
|
@@ -76,7 +76,9 @@ class DashScopeModelStreamResult implements StreamTextResult {
|
|
|
76
76
|
const data = JSON.parse(chunk.data) as DashScopeStreamTextOutput
|
|
77
77
|
controller.enqueue(data)
|
|
78
78
|
} catch (e) {
|
|
79
|
-
|
|
79
|
+
if (chunk.data !== '[DONE]') {
|
|
80
|
+
console.warn('Error when transforming event source data to json', e)
|
|
81
|
+
}
|
|
80
82
|
}
|
|
81
83
|
},
|
|
82
84
|
}),),)
|
|
@@ -94,7 +94,9 @@ class HunyuanModelStreamResult implements StreamTextResult {
|
|
|
94
94
|
const data = JSON.parse(chunk.data) as HunYaunStreamTextOutput
|
|
95
95
|
controller.enqueue(data)
|
|
96
96
|
} catch (e) {
|
|
97
|
-
|
|
97
|
+
if (chunk.data !== '[DONE]') {
|
|
98
|
+
console.warn('Error when transforming event source data to json', e)
|
|
99
|
+
}
|
|
98
100
|
}
|
|
99
101
|
},
|
|
100
102
|
}),),)
|
|
@@ -94,7 +94,9 @@ class HunyuanModelStreamResult implements StreamTextResult {
|
|
|
94
94
|
const data = JSON.parse(chunk.data) as HunYaunStreamTextOutput
|
|
95
95
|
controller.enqueue(data)
|
|
96
96
|
} catch (e) {
|
|
97
|
-
|
|
97
|
+
if (chunk.data !== '[DONE]') {
|
|
98
|
+
console.warn('Error when transforming event source data to json', e)
|
|
99
|
+
}
|
|
98
100
|
}
|
|
99
101
|
},
|
|
100
102
|
}),),)
|
|
@@ -76,7 +76,9 @@ class MoonshotModelStreamResult implements StreamTextResult {
|
|
|
76
76
|
const data = JSON.parse(chunk.data) as MoonshotStreamTextOutput
|
|
77
77
|
controller.enqueue(data)
|
|
78
78
|
} catch (e) {
|
|
79
|
-
|
|
79
|
+
if (chunk.data !== '[DONE]') {
|
|
80
|
+
console.warn('Error when transforming event source data to json', e)
|
|
81
|
+
}
|
|
80
82
|
}
|
|
81
83
|
},
|
|
82
84
|
}),),)
|
package/src/models/Yi/index.ts
CHANGED
|
@@ -76,7 +76,9 @@ class YiModelStreamResult implements StreamTextResult {
|
|
|
76
76
|
const data = JSON.parse(chunk.data) as YiStreamTextOutput
|
|
77
77
|
controller.enqueue(data)
|
|
78
78
|
} catch (e) {
|
|
79
|
-
|
|
79
|
+
if (chunk.data !== '[DONE]') {
|
|
80
|
+
console.warn('Error when transforming event source data to json', e)
|
|
81
|
+
}
|
|
80
82
|
}
|
|
81
83
|
},
|
|
82
84
|
}),),)
|
|
@@ -76,7 +76,9 @@ class ZhiPuModelStreamResult implements StreamTextResult {
|
|
|
76
76
|
const data = JSON.parse(chunk.data) as ZhiPuStreamTextOutput
|
|
77
77
|
controller.enqueue(data)
|
|
78
78
|
} catch (e) {
|
|
79
|
-
|
|
79
|
+
if (chunk.data !== '[DONE]') {
|
|
80
|
+
console.warn('Error when transforming event source data to json', e)
|
|
81
|
+
}
|
|
80
82
|
}
|
|
81
83
|
},
|
|
82
84
|
}),),)
|
package/src/type.ts
CHANGED
|
@@ -29,6 +29,18 @@ export type ModelReq = <T extends IModelReqInput>(
|
|
|
29
29
|
props: T,
|
|
30
30
|
) => T['stream'] extends true ? Promise<ReadableStream<Uint8Array>> : Promise<Object>
|
|
31
31
|
|
|
32
|
+
export interface IBotReqInput {
|
|
33
|
+
url: string
|
|
34
|
+
method: string
|
|
35
|
+
headers?: Record<string, string>
|
|
36
|
+
data?: Object
|
|
37
|
+
stream?: boolean
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export type BotReq = <T extends IBotReqInput>(
|
|
41
|
+
props: T,
|
|
42
|
+
) => T['stream'] extends true ? Promise<ReadableStream<Uint8Array>> : Promise<Object>
|
|
43
|
+
|
|
32
44
|
type ChatModelMessage = {
|
|
33
45
|
role: 'user' | 'system' | 'assistant'
|
|
34
46
|
content: string
|
package/src/utils.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { TransformStream as _TransformStream, ReadableStream as _ReadableStream
|
|
|
2
2
|
import { createReadableStreamWrapper } from '@mattiasbuelens/web-streams-adapter'
|
|
3
3
|
import { createParser, type EventSourceParser, type ParsedEvent } from 'eventsource-parser'
|
|
4
4
|
import type { AsyncIterableReadableStream } from './type'
|
|
5
|
+
import { TextDecoder } from 'text-encoding-shim'
|
|
5
6
|
|
|
6
7
|
export const ReadableStream = _ReadableStream as {
|
|
7
8
|
prototype: ReadableStream
|