-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
205 lines (180 loc) · 6.56 KB
/
index.ts
File metadata and controls
205 lines (180 loc) · 6.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import type { AdapterOptions } from "./types.js";
import FormData from 'form-data';
import axios from 'axios';
import { fileTypeFromBuffer } from 'file-type';
import type { ImageGenerationAdapter } from "adminforth";
export default class ImageGenerationAdapterOpenAI implements ImageGenerationAdapter {
options: AdapterOptions;
constructor(options: AdapterOptions) {
this.options = options;
this.options.model = options.model || 'gpt-image-1';
}
validate() {
if (!this.options.openAiApiKey) {
throw new Error("API Key is required");
}
}
outputImagesMaxCountSupported(): number {
if (this.options.model === 'gpt-image-1' || this.options.model === 'dall-e-2') {
return 10;
} else if (this.options.model === 'dall-e-3') {
return 1;
}
}
outputDimensionsSupported(): string[] {
if (this.options.model === 'gpt-image-1') {
return ['1024x1024', '1536x1024', '1024x1536', 'auto'];
} else if (this.options.model === 'dall-e-2') {
return ['256x256', '512x512', '1024x1024'];
} else if (this.options.model === 'dall-e-3') {
return ['1024x1024', '1792x1024', '1024x1792'];
}
}
inputFileExtensionSupported(): string[] {
if (this.options.model === 'dall-e-2') {
return ['png'];
} else if (this.options.model === 'gpt-image-1' || this.options.model === 'dall-e-3') {
return ['png', 'jpg', 'jpeg'];
}
return [];
}
async generate(params: {
prompt: string;
inputFiles?: string[];
size?: string;
n?: number;
}): Promise<{
imageURLs?: string[];
error?: string;
}> {
const { model = this.options.model || 'dall-e-2' } = this.options;
const { prompt, inputFiles = [], size = this.outputDimensionsSupported()[0], n = 1 } = params;
if (n > this.outputImagesMaxCountSupported()) {
throw new Error(`For model "${model}", the maximum number of images is ${this.outputImagesMaxCountSupported()}`);
}
return await this.generateOrEditImage({ prompt, inputFiles, n, size });
}
stripLargeValuesInAnyObject(obj: any): any {
if (typeof obj === 'string') {
return obj.length > 100 ? obj.slice(0, 100) + '...' : obj;
}
if (Array.isArray(obj)) {
return obj.map(item => this.stripLargeValuesInAnyObject(item));
}
if (obj && typeof obj === 'object') {
return Object.fromEntries(
Object.entries(obj).map(([key, value]) => [
key,
this.stripLargeValuesInAnyObject(value),
])
);
}
return obj;
}
async guessMimeTypeByB64(b64: string): Promise<string> {
// b64 is pure base64 string without data url prefix, so split will not work, we need to guess it
const binaryData = Buffer.from(b64, 'base64');
const fileType = await fileTypeFromBuffer(binaryData);
return fileType?.mime || 'application/octet-stream'; // fallback if unknown
}
private async generateOrEditImage({
prompt,
inputFiles,
n,
size,
}: {
prompt: string;
inputFiles: string[];
n: number;
size: string;
}): Promise<{
imageURLs?: string[];
error?: string;
}> {
process.env.HEAVY_DEBUG && console.log('Generating image with prompt:',
this.stripLargeValuesInAnyObject(inputFiles), prompt, n, size);
const headers = {
Authorization: `Bearer ${this.options.openAiApiKey}`,
'Content-Type': 'application/json',
};
const model = this.options.model;
if (inputFiles.length === 0) {
let response;
try {
response = await axios.post(
'https://api.openai.com/v1/images/generations',
{ prompt, model, n, size, ...(this.options.extraParams || {}) },
{ headers }
);
} catch (error) {
return { error: error.response.data.error.message };
}
const images = response.data?.data ?? [];
const imageURLs = images.map((item: any) => {
if (item.url) {
return item.url;
}
if (item.b64_json) {
return `data:image/png;base64,${item.b64_json}`;
}
return null;
}).filter((url: string | null) => url !== null);
return { imageURLs };
} else {
const formData = new FormData();
formData.append('prompt', prompt);
formData.append('model', model);
formData.append('n', n.toString());
formData.append('size', size);
if (this.options.extraParams) {
for (const [key, value] of Object.entries(this.options.extraParams)) {
formData.append(key, value);
}
}
for (let i = 0; i < inputFiles.length; i++) {
const fileUrl = inputFiles[i];
if (fileUrl.startsWith('http')) {
let responseImage;
try {
responseImage = await axios.get(fileUrl, { responseType: 'arraybuffer' });
} catch (error) {
console.error('Error fetching input file:', error);
return { error: `Error attaching input files` };
}
const base64Data = Buffer.from(responseImage.data, 'binary').toString('base64');
const buffer = Buffer.from(base64Data, 'base64');
formData.append('image[]', buffer, { filename: `image_${i + 1}.png`, contentType: 'image/png' });
} else if (fileUrl.startsWith('data:')) {
const base64Data = fileUrl.split(',')[1];
const buffer = Buffer.from(base64Data, 'base64');
formData.append('image[]', buffer, { filename: `image_${i + 1}.png`, contentType: 'image/png' });
} else {
throw new Error(`Unsupported file URL for attachment, it should be an absolute URL strating with http or a data URL, but got: ${fileUrl}`);
}
}
const editHeaders = {
Authorization: `Bearer ${this.options.openAiApiKey}`,
...formData.getHeaders(),
};
try {
const editResponse = await axios.post(
'https://api.openai.com/v1/images/edits',
formData,
{ headers: editHeaders }
);
process.env.HEAVY_DEBUG && console.log('✏️ Edit response:', JSON.stringify( this.stripLargeValuesInAnyObject(editResponse.data)));
return {
imageURLs: await Promise.all(
editResponse.data.data.map(async (image: any) => {
const mimeTipe = await this.guessMimeTypeByB64(image.b64_json);
return `data:${mimeTipe};base64,${image.b64_json}`
}),
),
}
} catch (error) {
console.error('Error generating image:', error.response);
return { error: `Error generating image: ${error.message}` };
}
}
}
}