This commit is contained in:
2025-09-15 02:25:29 +08:00
parent f6cd39be4c
commit eccea10782
6 changed files with 185 additions and 0 deletions

114
src/dashscope/dashscope.ts Normal file
View File

@@ -0,0 +1,114 @@
export class QwenText2Image {
text2imageApi = 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text2image/image-synthesis';
tasksApi = 'https://dashscope.aliyuncs.com/api/v1/tasks/{task_id}';
apiKey: string;
taskResponse: TaskResponse;
constructor(apiKey: string) {
this.apiKey = apiKey;
}
async createImage(data: CreateImageRequest): Promise<CreateImageResponse> {
const res = await fetch(this.text2imageApi, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-DashScope-Async': 'enable',
Authorization: `Bearer ${this.apiKey}`,
},
body: JSON.stringify(data),
});
return await res.json();
}
/**
* // PENDING任务排队中
// RUNNING任务处理中
// SUCCEEDED任务执行成功
// FAILED任务执行失败
// CANCELED任务取消成功
// UNKNOWN任务不存在或状态未知
* @param taskId
* @returns
*/
async getTaskStatus(taskId: string): Promise<TaskResponse> {
const url = this.tasksApi.replace('{task_id}', taskId);
const res = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${this.apiKey}`,
},
});
this.taskResponse = await res.json();
return this.taskResponse;
}
}
type TaskStatus = 'PENDING' | 'SUCCESS' | 'FAILURE' | 'CANCELED' | 'UNKNOWN';
type CreateImageResponse = {
output: {
task_status: TaskStatus;
task_id: string;
};
request_id: string;
code?: string;
message?: string;
};
type CreateImageRequest = {
model: ImageModel;
input: {
prompt: string;
negative_prompt?: string;
};
parameters?: {
size?: string; // 图片尺寸,支持 "256x256", "512x512", "1024x1024"
n: number; // 生成图片数量默认为41-4之间
seed?: number; // 随机种子seed参数取值范围是[0, 2147483647]。
prompt_extend?: boolean; // 是否启用提示词增强默认为true 是否开启prompt智能改写。开启后会使用大模型对输入prompt进行智能改写仅对正向提示词有效。对于较短的输入prompt生成效果提升明显但会增加
watermark?: boolean; // 是否添加水印默认为false 是否在生成的图片上添加水印, "AI 生成" 字样
};
};
type TaskResponse = {
output: {
task_status: TaskStatus;
task_id: string;
submit_time: string;
scheduled_time: string;
end_time: string;
results: {
orig_prompt: string;
actual_prompt: string;
url: string;
code: string;
message: string;
[key: string]: any;
}[];
task_metrics: {
TOTAL: number;
SUCCEEDED: number;
FAILED: number;
};
[key: string]: any;
};
request_id: string;
usage: {
image_count: number;
[key: string]: any;
};
code?: string;
message?: string;
};
export const models = ['wan2.2-t2i-flash', 'wan2.2-t2i-plus', 'wanx2.1-t2i-turbo', 'wanx2.1-t2i-plus', 'wanx2.0-t2i-turbo'] as const;
export type ImageModel = (typeof models)[number];
export const sizes = [
'1664x928', // 16:9
'1472x1140', // 4:3
'1328x1328', // 1:1
'1140x1472', // 3:4
'928x1664', // 9:16
] as const;
export type ImageSize = (typeof sizes)[number];

1
src/dashscope/index.ts Normal file
View File

@@ -0,0 +1 @@
export { QwenText2Image } from './dashscope.ts';

17
src/dashscope/readme.md Normal file
View File

@@ -0,0 +1,17 @@
https://help.aliyun.com/zh/model-studio/text-to-image-v2-api-reference?spm=a2c4g.11186623.help-menu-2400256.d_2_2_3.3b3256e5iOwcyt#f281932a43b03
链接仅在 24 小时内有效。
```
# OSS域名列表
dashscope-result-bj.oss-cn-beijing.aliyuncs.com
dashscope-result-hz.oss-cn-hangzhou.aliyuncs.com
dashscope-result-sh.oss-cn-shanghai.aliyuncs.com
dashscope-result-wlcb.oss-cn-wulanchabu.aliyuncs.com
dashscope-result-zjk.oss-cn-zhangjiakou.aliyuncs.com
dashscope-result-sz.oss-cn-shenzhen.aliyuncs.com
dashscope-result-hy.oss-cn-heyuan.aliyuncs.com
dashscope-result-cd.oss-cn-chengdu.aliyuncs.com
dashscope-result-gz.oss-cn-guangzhou.aliyuncs.com
dashscope-result-wlcb-acdr-1.oss-cn-wulanchabu-acdr-1.aliyuncs.com
```