@untiny/qwen-ai-provider 0.0.2 → 0.0.3

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/README.md CHANGED
@@ -33,6 +33,11 @@ const { embedding } = await embed({
33
33
  model: qwen.embeddingModel('text-embedding-v4'),
34
34
  value: 'sunny day at the beach',
35
35
  });
36
+
37
+ const { image } = await generateImage({
38
+ model: qwen.imageModel('qwen-image-plus'),
39
+ prompt: '一个在海边沙滩上的小狗',
40
+ });
36
41
  ```
37
42
 
38
43
  ## Documentation
package/dist/index.d.mts CHANGED
@@ -7,6 +7,8 @@ type QwenCompletionModelId = 'qwen2.5-coder-0.5b-instruct' | 'qwen2.5-coder-1.5b
7
7
 
8
8
  type QwenEmbeddingModelId = 'text-embedding-v4' | (string & {});
9
9
 
10
+ type QwenImageModelId = 'qwen-image-plus' | (string & {});
11
+
10
12
  interface QwenProvider extends ProviderV3 {
11
13
  (modelId: QwenChatModeId): LanguageModelV3;
12
14
  /**
@@ -38,7 +40,7 @@ interface QwenProvider extends ProviderV3 {
38
40
  * @deprecated Use `embeddingModel` instead.
39
41
  */
40
42
  textEmbeddingModel(modelId: QwenEmbeddingModelId): EmbeddingModelV3;
41
- imageModel(modelId: string): ImageModelV3;
43
+ imageModel(modelId: QwenImageModelId): ImageModelV3;
42
44
  }
43
45
  interface QwenProviderSettings {
44
46
  /**
@@ -46,6 +48,11 @@ interface QwenProviderSettings {
46
48
  * 默认前缀是"https://dashscope.aliyuncs.com/compatible-mode/v1"。
47
49
  */
48
50
  baseURL?: string;
51
+ /**
52
+ * 对 API 调用使用不同的 URL 前缀,例如使用代理服务器。
53
+ * 默认前缀是"https://dashscope.aliyuncs.com/api/v1"。
54
+ */
55
+ apiUrl?: string;
49
56
  /**
50
57
  * 使用“Authorization”标头发送的 API 密钥。
51
58
  * 它默认为“DASHSCOPE_API_KEY”环境变量。
package/dist/index.d.ts CHANGED
@@ -7,6 +7,8 @@ type QwenCompletionModelId = 'qwen2.5-coder-0.5b-instruct' | 'qwen2.5-coder-1.5b
7
7
 
8
8
  type QwenEmbeddingModelId = 'text-embedding-v4' | (string & {});
9
9
 
10
+ type QwenImageModelId = 'qwen-image-plus' | (string & {});
11
+
10
12
  interface QwenProvider extends ProviderV3 {
11
13
  (modelId: QwenChatModeId): LanguageModelV3;
12
14
  /**
@@ -38,7 +40,7 @@ interface QwenProvider extends ProviderV3 {
38
40
  * @deprecated Use `embeddingModel` instead.
39
41
  */
40
42
  textEmbeddingModel(modelId: QwenEmbeddingModelId): EmbeddingModelV3;
41
- imageModel(modelId: string): ImageModelV3;
43
+ imageModel(modelId: QwenImageModelId): ImageModelV3;
42
44
  }
43
45
  interface QwenProviderSettings {
44
46
  /**
@@ -46,6 +48,11 @@ interface QwenProviderSettings {
46
48
  * 默认前缀是"https://dashscope.aliyuncs.com/compatible-mode/v1"。
47
49
  */
48
50
  baseURL?: string;
51
+ /**
52
+ * 对 API 调用使用不同的 URL 前缀,例如使用代理服务器。
53
+ * 默认前缀是"https://dashscope.aliyuncs.com/api/v1"。
54
+ */
55
+ apiUrl?: string;
49
56
  /**
50
57
  * 使用“Authorization”标头发送的 API 密钥。
51
58
  * 它默认为“DASHSCOPE_API_KEY”环境变量。
package/dist/index.js CHANGED
@@ -37,28 +37,12 @@ __export(index_exports, {
37
37
  module.exports = __toCommonJS(index_exports);
38
38
 
39
39
  // src/qwen-provider.ts
40
- var import_provider6 = require("@ai-sdk/provider");
41
- var import_provider_utils10 = require("@ai-sdk/provider-utils");
40
+ var import_provider_utils12 = require("@ai-sdk/provider-utils");
42
41
 
43
42
  // src/chat/qwen-chat-language-model.ts
44
43
  var import_provider3 = require("@ai-sdk/provider");
45
44
  var import_provider_utils4 = require("@ai-sdk/provider-utils");
46
45
 
47
- // src/qwen-error.ts
48
- var import_provider_utils = require("@ai-sdk/provider-utils");
49
- var import_zod = __toESM(require("zod"));
50
- var qwenErrorDataSchema = import_zod.default.object({
51
- object: import_zod.default.literal("error"),
52
- message: import_zod.default.string(),
53
- type: import_zod.default.string(),
54
- param: import_zod.default.string().nullable(),
55
- code: import_zod.default.string().nullable()
56
- });
57
- var qwenFailedResponseHandler = (0, import_provider_utils.createJsonErrorResponseHandler)({
58
- errorSchema: qwenErrorDataSchema,
59
- errorToMessage: (data) => data.message
60
- });
61
-
62
46
  // src/get-response-metadata.ts
63
47
  function getResponseMetadata({
64
48
  id,
@@ -89,6 +73,22 @@ function mapQwenFinishReason(finishReason) {
89
73
  }
90
74
  }
91
75
 
76
+ // src/qwen-error.ts
77
+ var import_provider_utils = require("@ai-sdk/provider-utils");
78
+ var import_zod = __toESM(require("zod"));
79
+ var qwenErrorDataSchema = import_zod.default.object({
80
+ error: import_zod.default.object({
81
+ message: import_zod.default.string(),
82
+ type: import_zod.default.string(),
83
+ param: import_zod.default.string().nullish(),
84
+ code: import_zod.default.string().nullish()
85
+ })
86
+ });
87
+ var qwenFailedResponseHandler = (0, import_provider_utils.createJsonErrorResponseHandler)({
88
+ errorSchema: qwenErrorDataSchema,
89
+ errorToMessage: (data) => data.error.message
90
+ });
91
+
92
92
  // src/chat/convert-qwen-chat-usage.ts
93
93
  function convertQwenChatUsage(usage) {
94
94
  var _a, _b, _c, _d;
@@ -580,11 +580,11 @@ var QwenChatLanguageModel = class {
580
580
  return;
581
581
  }
582
582
  const value = chunk.value;
583
- if ("object" in value) {
583
+ if ("error" in value) {
584
584
  finishReason = { unified: "error", raw: void 0 };
585
585
  controller.enqueue({
586
586
  type: "error",
587
- error: value.message
587
+ error: value.error.message
588
588
  });
589
589
  return;
590
590
  }
@@ -1074,9 +1074,9 @@ var QwenCompletionLanguageModel = class {
1074
1074
  return;
1075
1075
  }
1076
1076
  const value = chunk.value;
1077
- if ("object" in value) {
1077
+ if ("error" in value) {
1078
1078
  finishReason = { unified: "error", raw: void 0 };
1079
- controller.enqueue({ type: "error", error: value.message });
1079
+ controller.enqueue({ type: "error", error: value.error.message });
1080
1080
  return;
1081
1081
  }
1082
1082
  if (isFirstChunk) {
@@ -1217,31 +1217,153 @@ var QwenEmbeddingModel = class {
1217
1217
  }
1218
1218
  };
1219
1219
 
1220
+ // src/image/qwen-image-model.ts
1221
+ var import_provider_utils11 = require("@ai-sdk/provider-utils");
1222
+
1223
+ // src/image/qwen-image-api.ts
1224
+ var import_provider_utils10 = require("@ai-sdk/provider-utils");
1225
+ var import_zod6 = __toESM(require("zod"));
1226
+ var qwenImageErrorSchema = import_zod6.default.object({
1227
+ error: import_zod6.default.string(),
1228
+ message: import_zod6.default.string()
1229
+ });
1230
+ var qwenImageResponseSchema = (0, import_provider_utils10.lazySchema)(
1231
+ () => (0, import_provider_utils10.zodSchema)(
1232
+ import_zod6.default.object({
1233
+ output: import_zod6.default.object({
1234
+ choices: import_zod6.default.array(
1235
+ import_zod6.default.object({
1236
+ finish_reason: import_zod6.default.string(),
1237
+ message: import_zod6.default.object({
1238
+ role: import_zod6.default.literal("assistant"),
1239
+ content: import_zod6.default.array(
1240
+ import_zod6.default.object({
1241
+ image: import_zod6.default.string()
1242
+ })
1243
+ )
1244
+ })
1245
+ })
1246
+ ),
1247
+ task_metric: import_zod6.default.object({
1248
+ TOTAL: import_zod6.default.number(),
1249
+ SUCCEEDED: import_zod6.default.number(),
1250
+ FAILED: import_zod6.default.number()
1251
+ }).nullish(),
1252
+ finished: import_zod6.default.boolean().nullish()
1253
+ }),
1254
+ usage: import_zod6.default.object({
1255
+ image_count: import_zod6.default.number().nullish(),
1256
+ size: import_zod6.default.string().nullish(),
1257
+ width: import_zod6.default.number().nullish(),
1258
+ height: import_zod6.default.number().nullish()
1259
+ })
1260
+ })
1261
+ )
1262
+ );
1263
+
1264
+ // src/image/qwen-image-model.ts
1265
+ var QwenImageModel = class {
1266
+ constructor(modelId, config) {
1267
+ this.specificationVersion = "v3";
1268
+ this.maxImagesPerCall = 1;
1269
+ this.modelId = modelId;
1270
+ this.config = config;
1271
+ }
1272
+ get provider() {
1273
+ return this.config.provider;
1274
+ }
1275
+ async doGenerate(options) {
1276
+ var _a, _b;
1277
+ const warnings = [];
1278
+ if (options.aspectRatio != null) {
1279
+ warnings.push({
1280
+ type: "unsupported",
1281
+ feature: "aspectRatio",
1282
+ details: "This model does not support aspect ratio. Use `size` instead."
1283
+ });
1284
+ }
1285
+ if (options.seed != null) {
1286
+ warnings.push({ type: "unsupported", feature: "seed" });
1287
+ }
1288
+ const body = {
1289
+ model: this.modelId,
1290
+ input: {
1291
+ messages: [
1292
+ {
1293
+ role: "user",
1294
+ content: [{ text: (_a = options.prompt) != null ? _a : "" }]
1295
+ }
1296
+ ]
1297
+ },
1298
+ parameters: {
1299
+ size: (_b = options.size) == null ? void 0 : _b.replace("x", "*"),
1300
+ n: options.n,
1301
+ watermark: false,
1302
+ seed: options.seed
1303
+ }
1304
+ };
1305
+ const { value: response, responseHeaders } = await (0, import_provider_utils11.postJsonToApi)({
1306
+ url: this.config.url({
1307
+ path: "/services/aigc/multimodal-generation/generation",
1308
+ modelId: this.modelId
1309
+ }),
1310
+ headers: (0, import_provider_utils11.combineHeaders)(this.config.headers(), options.headers),
1311
+ body,
1312
+ failedResponseHandler: (0, import_provider_utils11.createJsonErrorResponseHandler)({
1313
+ errorSchema: qwenImageErrorSchema,
1314
+ errorToMessage: (data) => data.message
1315
+ }),
1316
+ successfulResponseHandler: (0, import_provider_utils11.createJsonResponseHandler)(qwenImageResponseSchema),
1317
+ abortSignal: options.abortSignal,
1318
+ fetch: this.config.fetch
1319
+ });
1320
+ const images = [];
1321
+ for (const choice of response.output.choices) {
1322
+ for (const content of choice.message.content) {
1323
+ if ("image" in content) {
1324
+ const imageResponse = await fetch(content.image);
1325
+ const imageBuffer = await imageResponse.arrayBuffer();
1326
+ images.push(new Uint8Array(imageBuffer));
1327
+ }
1328
+ }
1329
+ }
1330
+ return {
1331
+ images,
1332
+ warnings,
1333
+ response: {
1334
+ timestamp: /* @__PURE__ */ new Date(),
1335
+ modelId: this.modelId,
1336
+ headers: responseHeaders
1337
+ }
1338
+ };
1339
+ }
1340
+ };
1341
+
1220
1342
  // src/qwen-provider.ts
1221
1343
  function createQwen(options) {
1222
- var _a;
1223
- const baseURL = (_a = (0, import_provider_utils10.withoutTrailingSlash)(options == null ? void 0 : options.baseURL)) != null ? _a : "https://dashscope.aliyuncs.com/compatible-mode/v1";
1344
+ var _a, _b;
1345
+ const baseURL = (_a = (0, import_provider_utils12.withoutTrailingSlash)(options == null ? void 0 : options.baseURL)) != null ? _a : "https://dashscope.aliyuncs.com/compatible-mode/v1";
1346
+ const apiUrl = (_b = (0, import_provider_utils12.withoutTrailingSlash)(options == null ? void 0 : options.apiUrl)) != null ? _b : "https://dashscope.aliyuncs.com/api/v1";
1224
1347
  const getHeaders = () => {
1225
- const apiKey = (0, import_provider_utils10.loadApiKey)({
1348
+ const apiKey = (0, import_provider_utils12.loadApiKey)({
1226
1349
  apiKey: options == null ? void 0 : options.apiKey,
1227
1350
  environmentVariableName: "DASHSCOPE_API_KEY",
1228
1351
  description: "Qwen API key"
1229
1352
  });
1230
- return (0, import_provider_utils10.withUserAgentSuffix)(
1353
+ return (0, import_provider_utils12.withUserAgentSuffix)(
1231
1354
  {
1232
1355
  Authorization: `Bearer ${apiKey}`,
1233
1356
  ...options == null ? void 0 : options.headers
1234
1357
  },
1235
- `ai-sdk/qwen/${import_provider_utils10.VERSION}`
1358
+ `ai-sdk/qwen/${import_provider_utils12.VERSION}`
1236
1359
  );
1237
1360
  };
1238
1361
  const createChatModel = (modelId) => {
1239
1362
  return new QwenChatLanguageModel(modelId, {
1240
1363
  provider: "qwen.chat",
1241
- baseURL,
1364
+ url: ({ path }) => `${baseURL}${path}`,
1242
1365
  headers: getHeaders,
1243
- fetch: options == null ? void 0 : options.fetch,
1244
- url: ({ path }) => `${baseURL}${path}`
1366
+ fetch: options == null ? void 0 : options.fetch
1245
1367
  });
1246
1368
  };
1247
1369
  const createCompletionModel = (modelId) => {
@@ -1260,6 +1382,14 @@ function createQwen(options) {
1260
1382
  fetch: options == null ? void 0 : options.fetch
1261
1383
  });
1262
1384
  };
1385
+ const createImageModel = (modelId) => {
1386
+ return new QwenImageModel(modelId, {
1387
+ provider: `qwen.image`,
1388
+ url: ({ path }) => `${apiUrl}${path}`,
1389
+ headers: getHeaders,
1390
+ fetch: options == null ? void 0 : options.fetch
1391
+ });
1392
+ };
1263
1393
  const provider = (modelId) => {
1264
1394
  if (new.target) {
1265
1395
  throw new Error("The Qwen model function cannot be called with the new keyword.");
@@ -1274,15 +1404,13 @@ function createQwen(options) {
1274
1404
  provider.embeddingModel = createEmbeddingModel;
1275
1405
  provider.textEmbedding = createEmbeddingModel;
1276
1406
  provider.textEmbeddingModel = createEmbeddingModel;
1277
- provider.imageModel = (modelId) => {
1278
- throw new import_provider6.NoSuchModelError({ modelId, modelType: "imageModel" });
1279
- };
1407
+ provider.imageModel = createImageModel;
1280
1408
  return provider;
1281
1409
  }
1282
1410
  var qwen = createQwen();
1283
1411
 
1284
1412
  // src/version.ts
1285
- var VERSION2 = true ? "0.0.2" : "0.0.0-test";
1413
+ var VERSION2 = true ? "0.0.3" : "0.0.0-test";
1286
1414
  // Annotate the CommonJS export names for ESM import in node:
1287
1415
  0 && (module.exports = {
1288
1416
  VERSION,