@cloudbase/ai 2.22.10 → 2.23.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": "@cloudbase/ai",
3
- "version": "2.22.10",
3
+ "version": "2.23.0",
4
4
  "description": "cloudbase js sdk ai module",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -30,8 +30,8 @@
30
30
  "author": "",
31
31
  "license": "Apache-2.0",
32
32
  "dependencies": {
33
- "@cloudbase/types": "2.22.10",
34
- "@cloudbase/utilities": "2.22.10",
33
+ "@cloudbase/types": "2.23.0",
34
+ "@cloudbase/utilities": "2.23.0",
35
35
  "@mattiasbuelens/web-streams-adapter": "^0.1.0",
36
36
  "text-encoding-shim": "^1.0.5",
37
37
  "web-streams-polyfill": "^4.0.0"
@@ -39,5 +39,5 @@
39
39
  "devDependencies": {
40
40
  "@cloudbase/adapter-interface": "^0.7.1"
41
41
  },
42
- "gitHead": "5f58e0d4c6f6141d0c90e1dc48946ed55a8f81a3"
42
+ "gitHead": "59b83d8d2037675a0dd771274287087f8c623df7"
43
43
  }
package/src/AI.ts CHANGED
@@ -17,7 +17,11 @@ class AI {
17
17
  public i18n: ICloudbaseConfig['i18n']
18
18
  defaultHeaders: { [x: string]: any }
19
19
 
20
- constructor(private req: SDKRequestInterface, public baseUrl: string, i18n: ICloudbaseConfig['i18n']) {
20
+ constructor(
21
+ private req: SDKRequestInterface,
22
+ public baseUrl: string,
23
+ i18n: ICloudbaseConfig['i18n'],
24
+ ) {
21
25
  this.aiBaseUrl = `${baseUrl}/ai`
22
26
  this.aiBotBaseUrl = `${baseUrl}/aibot`
23
27
  this.bot = new Bot(this.botRequest, this.aiBotBaseUrl)
@@ -83,6 +87,10 @@ class AI {
83
87
  MODELS[name] = model
84
88
  }
85
89
 
90
+ createImageModel<const T extends 'hunyuan-exp' | (string & {})>(provider: T) {
91
+ return new models.DefaultImageModel<T>(this.modelRequest, this.aiBaseUrl, provider)
92
+ }
93
+
86
94
  modelRequest: types.ModelReq = async ({ url, data, headers, stream, timeout }) => {
87
95
  const fetchHeaders = {
88
96
  'Content-Type': 'application/json',
package/src/index.ts CHANGED
@@ -55,7 +55,7 @@ interface AIInitOption {
55
55
  baseUrl?: string
56
56
  }
57
57
 
58
- interface ICreateAi {
58
+ interface ICreateAI {
59
59
  req: SDKRequestInterface
60
60
  baseUrl?: string
61
61
  env?: string
@@ -74,7 +74,7 @@ interface ICreateAi {
74
74
  *
75
75
  * 其他参数可按需传入覆盖函数逻辑。
76
76
  */
77
- function createAi({ env, baseUrl, req, getAccessToken, handleReqInstance, i18n }: ICreateAi) {
77
+ function createAI({ env, baseUrl, req, getAccessToken, handleReqInstance, i18n }: ICreateAI) {
78
78
  const getBaseUrl = () => {
79
79
  if (baseUrl != null) {
80
80
  return baseUrl
@@ -98,10 +98,16 @@ function createAi({ env, baseUrl, req, getAccessToken, handleReqInstance, i18n }
98
98
  return new AI(getReq(), getBaseUrl(), i18n)
99
99
  }
100
100
 
101
+ // 兼容
102
+ /**
103
+ * @deprecated use `createAI`
104
+ */
105
+ const createAi = createAI
106
+
101
107
  /**
102
108
  * 创建 `AI` 实例。
103
109
  * 该函数挂载在 js sdk 的 Cloudbase 实例上。
104
- * 该函数调用了本模块暴露的 `createAi` 函数,但调用时通过传入参数做了逻辑覆盖:
110
+ * 该函数调用了本模块暴露的 `createAI` 函数,但调用时通过传入参数做了逻辑覆盖:
105
111
  *
106
112
  * - 传入从 Cloudbase 实例上的中心的 `req`
107
113
  * - 传入从 Cloudbase 实例上拿到的 `baseUrl`
@@ -126,7 +132,7 @@ function ai(
126
132
 
127
133
  const baseUrl = options?.baseUrl ?? getUrlFromCloud()
128
134
 
129
- return createAi({ req, baseUrl, handleReqInstance: ({ req }) => req, i18n: this.config.i18n })
135
+ return createAI({ req, baseUrl, handleReqInstance: ({ req }) => req, i18n: this.config.i18n })
130
136
  }
131
137
 
132
138
  const component: ICloudbaseComponent = {
@@ -142,7 +148,7 @@ function registerAi(app: ICloudbase) {
142
148
  }
143
149
  }
144
150
 
145
- export { AI, Bot, createAi, restModels as models, registerAi, utils }
151
+ export { AI, Bot, createAi, createAI, restModels as models, registerAi, utils }
146
152
  export * from './type'
147
153
 
148
154
  try {
@@ -0,0 +1,74 @@
1
+ import type {
2
+ ModelReq,
3
+ ImageGenerateInput,
4
+ ImageEditInput,
5
+ HunyuanGenerateImageInput,
6
+ HunyuanGenerateImageOutput,
7
+ HunyuanEditImageInput,
8
+ HunyuanEditImageOutput,
9
+ } from '../type'
10
+
11
+ // mock NoInfer
12
+ type NoInfer<T> = [T][T extends any ? 0 : never]
13
+
14
+ type GenerateImageInput<TProvider extends string, TInput = unknown> = unknown extends TInput
15
+ ? TProvider extends 'hunyuan-exp'
16
+ ? HunyuanGenerateImageInput
17
+ : ImageGenerateInput
18
+ : TInput
19
+
20
+ type GenerateImageOutput<TProvider extends string, TOutput = unknown> = unknown extends TOutput
21
+ ? TProvider extends 'hunyuan-exp'
22
+ ? HunyuanGenerateImageOutput
23
+ : unknown
24
+ : TOutput
25
+
26
+ type EditImageInput<T extends string, TInput = unknown> = unknown extends TInput
27
+ ? T extends 'hunyuan-exp'
28
+ ? HunyuanEditImageInput
29
+ : ImageEditInput
30
+ : TInput
31
+
32
+ type EditImageOutput<T extends string, TOutput = unknown> = unknown extends TOutput
33
+ ? T extends 'hunyuan-exp'
34
+ ? HunyuanEditImageOutput
35
+ : unknown
36
+ : TOutput
37
+
38
+ export class DefaultImageModel<const TProvider extends string> {
39
+ public generateImageSubUrl = 'images/generations'
40
+ public editImageSubUrl = 'images/edits'
41
+
42
+ constructor(
43
+ private req: ModelReq,
44
+ public baseUrl: string,
45
+ public provider: TProvider,
46
+ ) {}
47
+
48
+ public async generateImage<TInput = unknown, TOutput = unknown>(input: NoInfer<GenerateImageInput<TProvider, TInput>>,): Promise<GenerateImageOutput<TProvider, TOutput>> {
49
+ const res = await this.req({
50
+ url: this.getGenerateUrl(),
51
+ data: input,
52
+ stream: false,
53
+ })
54
+
55
+ return res as GenerateImageOutput<TProvider, TOutput>
56
+ }
57
+
58
+ public async editImage<TInput = unknown, TOutput = unknown>(input: NoInfer<EditImageInput<TProvider, TInput>>,): Promise<EditImageOutput<TProvider, TOutput>> {
59
+ const res = await this.req({
60
+ url: this.getEditUrl(),
61
+ data: input,
62
+ stream: false,
63
+ })
64
+ return res as EditImageOutput<TProvider, TOutput>
65
+ }
66
+
67
+ private getGenerateUrl() {
68
+ return `${this.baseUrl}/${this.provider}/${this.generateImageSubUrl}`
69
+ }
70
+
71
+ private getEditUrl() {
72
+ return `${this.baseUrl}/${this.provider}/${this.editImageSubUrl}`
73
+ }
74
+ }
@@ -9,6 +9,7 @@ import { HunYuanExpSimpleModel } from './HunYuanExp/index'
9
9
  import { HunYuanOpenSimpleModel } from './HunYuanOpen/index'
10
10
  import { DeepSeekSimpleModel } from './DeepSeek'
11
11
  import { DefaultSimpleModel } from './Default'
12
+ import { DefaultImageModel } from './image-model'
12
13
 
13
14
  export const MODELS = {
14
15
  hunyuan: HunYuanSimpleModel,
@@ -34,6 +35,7 @@ export {
34
35
  HunYuanExpSimpleModel,
35
36
  HunYuanOpenSimpleModel,
36
37
  DefaultSimpleModel,
38
+ DefaultImageModel,
37
39
  }
38
40
 
39
41
  export { ReactModel, toolMap } from './model'
package/src/type.ts CHANGED
@@ -20,7 +20,7 @@ export interface IModelReqInput {
20
20
  url: string
21
21
  headers?: Record<string, string>
22
22
  data?: Object
23
- stream?: boolean,
23
+ stream?: boolean
24
24
  timeout?: number
25
25
  }
26
26
 
@@ -145,3 +145,179 @@ export interface BaseDoStreamOutputChunk {
145
145
  usage?: Usage
146
146
  }
147
147
  // #endregion
148
+
149
+ // #region Image Model Types
150
+ /**
151
+ * 图片生成输入参数
152
+ */
153
+ export interface ImageGenerateInput {
154
+ model: string // 模型名称,例如 'hunyuan-image'、'dall-e-3'
155
+ prompt: string // 用于描述要生成的图片内容
156
+ size?: string // 尺寸
157
+ n?: number // 张数
158
+ [key: string]: unknown // 允许额外的厂商特定参数
159
+ }
160
+
161
+ /**
162
+ * 图片编辑输入参数
163
+ */
164
+ export interface ImageEditInput {
165
+ model: string
166
+ prompt: string
167
+ size?: string // 尺寸
168
+ n?: number // 张数
169
+ [key: string]: unknown // 允许额外的厂商特定参数
170
+ }
171
+
172
+ /**
173
+ * 混元图片生成输入参数
174
+ * 接口文档:POST /openapi/v1/images/generations
175
+ */
176
+ export interface HunyuanGenerateImageInput {
177
+ /** 模型名称,默认为 hunyuan-image */
178
+ model: 'hunyuan-image' | (string & {})
179
+ /** 用来生成图像的文本描述 */
180
+ prompt: string
181
+ /**
182
+ * 模型版本,支持 v1.8.1 和 v1.9,默认版本 v1.8.1
183
+ * - v1.8.1: 采用二阶段模型支持文生图任务,针对图像细节、稳定性等要求高的任务较为合适
184
+ * - v1.9: 采用模型矩阵能力支持文生图任务,后端由人像、游戏、通用模型,以及各类功能插件组成,灵活适配多种文生图任务
185
+ */
186
+ version?: 'v1.8.1' | 'v1.9' | (string & {})
187
+ /**
188
+ * 图片尺寸,默认 "1024x1024"
189
+ * v1.9 仅支持以下几个尺寸:
190
+ * - 1024x1024
191
+ * - 1024x768, 1152x864
192
+ * - 768x1024, 864x1152
193
+ * - 1280x768 (16:9)
194
+ * - 768x1280 (9:16)
195
+ * - 1280x720 (16:9)
196
+ * - 720x1280 (9:16)
197
+ *
198
+ * 支持用户自定义尺寸,但输入尺寸只支持 768-1280 之间且为 64 的倍数的数字
199
+ */
200
+ size?: string
201
+ /**
202
+ * 仅 v1.9 支持,负向词
203
+ * 如果不传则使用各个模型内部的默认负向词
204
+ */
205
+ negative_prompt?: string
206
+ /**
207
+ * 仅 v1.9 支持,可指定风格:
208
+ * - 古风二次元风格
209
+ * - 都市二次元风格
210
+ * - 悬疑风格
211
+ * - 校园风格
212
+ * - 都市异能风格
213
+ */
214
+ style?: '古风二次元风格' | '都市二次元风格' | '悬疑风格' | '校园风格' | '都市异能风格' | (string & {})
215
+ /**
216
+ * 为 true 时对 prompt 进行改写,实际生成的图片会使用改写后的 prompt 进行生成
217
+ * 多数场景可提升生成的图片效果,默认为 true
218
+ */
219
+ revise?: boolean
220
+ /**
221
+ * 生成图片个数,默认为 1
222
+ * 目前只支持生成一张,固定为 1,传其他的值会报错
223
+ */
224
+ n?: number
225
+ /**
226
+ * 业务自定义水印内容
227
+ * 限制 16 个字符长度(不区分中英文),生成在图片右下角
228
+ */
229
+ footnote?: string
230
+ /**
231
+ * 生成种子,仅当生成图片数为 1 时生效
232
+ * 范围 [1, 4294967295],不传时默认随机
233
+ */
234
+ seed?: number
235
+ }
236
+
237
+ /**
238
+ * 混元图片编辑输入参数
239
+ * 接口文档:POST /openapi/v1/images/edits
240
+ */
241
+ export type HunyuanEditImageInput = {
242
+ /** 模型名称,本期为 hunyuan-image,即为商品图编辑场景 */
243
+ model: 'hunyuan-image' | (string & {})
244
+ /** 编辑的文本描述 */
245
+ prompt: string
246
+ /** 模型版本 */
247
+ version?: string
248
+ /**
249
+ * 图片尺寸,默认 "800x800"
250
+ * 支持用户自定义尺寸,但输入尺寸只支持 512-1280 之间
251
+ * 目前只支持 1:1、16:9、9:16 三个尺寸
252
+ */
253
+ size?: string
254
+ /**
255
+ * 生成图片个数,默认为 1
256
+ * 目前只支持生成一张,固定为 1,传其他的值会报错
257
+ */
258
+ n?: number
259
+ /**
260
+ * beta 参数,随机种子
261
+ * 范围 [1, 4294967295],不传时默认随机
262
+ */
263
+ seed?: number
264
+ /**
265
+ * 业务自定义角标内容
266
+ * 限制 16 个字符长度(不区分中英文)
267
+ */
268
+ footnote?: string
269
+ /**
270
+ * 图 base64 编码,大小不超过 6M
271
+ * 支持 jpg/jpeg 格式,和 image_url 二选一
272
+ * 长短边比例要求 2:1 内
273
+ */
274
+ image?: string
275
+ /**
276
+ * 图片 url
277
+ * 长短边比例要求 2:1 内,和 image 二选一
278
+ */
279
+ image_url?: string
280
+ } & (
281
+ | {
282
+ mask: string
283
+ mask_url?: never
284
+ }
285
+ | {
286
+ mask?: never
287
+ mask_url: string
288
+ }
289
+ )
290
+
291
+ /**
292
+ * 混元图片生成输出
293
+ */
294
+ export interface HunyuanGenerateImageOutput {
295
+ /** 此次请求的 id */
296
+ id: string
297
+ /** unix 时间戳 */
298
+ created: number
299
+ /** 返回的图片生成内容 */
300
+ data: Array<{
301
+ /** 生成的图片 url,有效期为 24 小时 */
302
+ url: string
303
+ /** 原 prompt 改写后的文本。若 revise 为 false,则为原 prompt */
304
+ revised_prompt?: string
305
+ }>
306
+ }
307
+
308
+ /**
309
+ * 混元图片编辑输出
310
+ */
311
+ export interface HunyuanEditImageOutput {
312
+ /** 此次请求的 id */
313
+ id: string
314
+ /** unix 时间戳 */
315
+ created: number
316
+ /** 返回的图片生成内容 */
317
+ data: Array<{
318
+ /** 生成的图片 url,有效期为 24 小时 */
319
+ url: string
320
+ }>
321
+ }
322
+
323
+ // #endregion