update
This commit is contained in:
114
src/dashscope/dashscope.ts
Normal file
114
src/dashscope/dashscope.ts
Normal 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; // 生成图片数量,默认为4,1-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
1
src/dashscope/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { QwenText2Image } from './dashscope.ts';
|
||||
17
src/dashscope/readme.md
Normal file
17
src/dashscope/readme.md
Normal 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
|
||||
```
|
||||
Reference in New Issue
Block a user