Skip to content

Commit 7828be7

Browse files
committed
fix: update uploader docs
1 parent 8da7810 commit 7828be7

File tree

4 files changed

+123
-50
lines changed

4 files changed

+123
-50
lines changed

adminforth/documentation/blog/2024-10-01-ai-blog/index.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -440,11 +440,10 @@ export default {
440440
{ originalFilename, originalExtension }: {originalFilename: string, originalExtension: string }
441441
) => `user-avatars/${new Date().getFullYear()}/${randomUUID()}/${originalFilename}.${originalExtension}`,
442442
generation: {
443-
provider: 'openai-dall-e',
443+
provider: 'openai',
444444
countToGenerate: 2,
445445
openAiOptions: {
446-
model: 'dall-e-3',
447-
size: '1024x1024',
446+
model: 'gpt-4o',
448447
apiKey: process.env.OPENAI_API_KEY,
449448
},
450449
},
@@ -562,11 +561,10 @@ export default {
562561
{ originalFilename, originalExtension }: {originalFilename: string, originalExtension: string }
563562
) => `post-previews/${new Date().getFullYear()}/${randomUUID()}/${originalFilename}.${originalExtension}`,
564563
generation: {
565-
provider: 'openai-dall-e',
564+
provider: 'openai',
566565
countToGenerate: 2,
567566
openAiOptions: {
568-
model: 'dall-e-3',
569-
size: '1792x1024',
567+
model: 'gpt-4o',
570568
apiKey: process.env.OPENAI_API_KEY,
571569
},
572570
fieldsForContext: ['title'],

adminforth/documentation/docs/tutorial/05-Plugins/05-upload.md

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -260,20 +260,16 @@ new UploadPlugin({
260260
...
261261
//diff-add
262262
generation: {
263-
//diff-add
264-
provider: 'openai-dall-e',
265263
//diff-add
266264
countToGenerate: 2, // how much images generate in one shot
267265
//diff-add
268-
openAiOptions: {
269-
//diff-add
270-
model: 'dall-e-3', // one of models from OpenAI docs https://platform.openai.com/docs/api-reference/images/create
271-
//diff-add
272-
size: '1792x1024', // make sure that size is supported by model using OpenAI docs
273-
//diff-add
274-
apiKey: process.env.OPENAI_API_KEY as string,
275-
//diff-add
276-
},
266+
adapter: new ImageGenerationAdapterOpenAI({
267+
//diff-add
268+
openAiApiKey: process.env.OPENAI_API_KEY as string,
269+
//diff-add
270+
model: 'gpt-image-1',
271+
//diff-add
272+
}),
277273
//diff-add
278274
fieldsForContext: ['title'],
279275

@@ -284,6 +280,53 @@ Here is how it works:
284280
285281
![alt text](demoImgGen-1.gif)
286282
283+
You can also pass additional parameters to OpenAI API call
284+
285+
286+
```ts title="./apartments.ts"
287+
new ImageGenerationAdapterOpenAI({
288+
//diff-add
289+
openAiApiKey: process.env.OPENAI_API_KEY as string,
290+
//diff-add
291+
model: 'gpt-image-1',
292+
//diff-add
293+
extraParams: {
294+
//diff-add
295+
moderation: 'low',
296+
//diff-add
297+
quality: 'high',
298+
//diff-add
299+
}),
300+
```
301+
302+
303+
304+
305+
## Generation from existing image(s)
306+
307+
You can not only generate images from text, but also edit, post-process or improve existing images. E.g. remove texts, add objects, change colors, etc.
308+
309+
Create a new column `apartment_source` in `apartments` table, and put another instance of `UploadPlugin` in the same resource configuration.
310+
311+
312+
313+
Now tweak the current UploadPlugin configuration in a next way:
314+
315+
316+
```ts title="./apartments.ts"
317+
generation: {
318+
adapter: new ImageGenerationAdapterOpenAI({
319+
openAiApiKey: process.env.OPENAI_API_KEY as string,
320+
}),
321+
322+
attachFiles: ({ record, adminUser }: { record: any; adminUser: AdminUser }) => {
323+
// attach apartment source image to generation, image should be public
324+
return [`https://tmpbucket-adminforth.s3.eu-central-1.amazonaws.com/${record.apartment_source}`];
325+
},
326+
generationPrompt: "Remove text from the image",
327+
countToGenerate: 3,
328+
}
329+
```
287330
288331
### Rate limits
289332
@@ -294,11 +337,11 @@ new UploadPlugin({
294337
...
295338
generation: {
296339
...
297-
//diff-add
298-
rateLimit: {
299-
limit: '5/12h', // up to 5 times per 12 hour
300-
errorMessage: 'You exhausted your image generation limit 5 times per 12 hours, please try again later',
301-
}
340+
//diff-add
341+
rateLimit: {
342+
limit: '5/12h', // up to 5 times per 12 hour
343+
errorMessage: 'You exhausted your image generation limit 5 times per 12 hours, please try again later',
344+
}
302345
...
303346
});
304347
```

dev-demo/resources/apartments.ts

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { admin } from '../index.js';
1313
import RichEditorPlugin from "../../plugins/adminforth-rich-editor";
1414
import { AdminForthResourceInput } from "../../adminforth";
1515
import CompletionAdapterOpenAIChatGPT from "../../adapters/adminforth-completion-adapter-open-ai-chat-gpt/index.js";
16+
import ImageGenerationAdapterOpenAI from "../../adapters/adminforth-image-generation-adapter-openai/index.js";
1617

1718
const demoChecker = async ({ record, adminUser, resource }) => {
1819
if (adminUser.dbUser.role !== "superadmin") {
@@ -186,6 +187,16 @@ export default {
186187
required: false,
187188
editingNote: "Upload image of apartment",
188189
},
190+
{
191+
name: "apartment_source",
192+
showIn: {
193+
all: true,
194+
show: () => true,
195+
filter: false,
196+
},
197+
required: false,
198+
editingNote: "Upload image of apartment",
199+
},
189200
{
190201
name: "price",
191202
showIn: {create: true, edit: true, filter: true, show: true},
@@ -282,24 +293,52 @@ export default {
282293
return `aparts/${new Date().getFullYear()}/${uuid()}/${originalFilename}.${originalExtension}`
283294
},
284295
generation: {
285-
provider: "openai-dall-e",
286-
countToGenerate: 2,
287-
openAiOptions: {
288-
model: "dall-e-3",
289-
size: "1792x1024",
290-
apiKey: process.env.OPENAI_API_KEY as string,
291-
},
292-
fieldsForContext: ["title"],
293-
rateLimit: {
294-
limit: "2/1m",
295-
errorMessage:
296-
"For demo purposes, you can generate only 2 images per minute",
297-
},
296+
adapter: new ImageGenerationAdapterOpenAI({
297+
openAiApiKey: process.env.OPENAI_API_KEY as string,
298+
}),
299+
300+
// attachFiles: ({ record, adminUser }: { record: any; adminUser: AdminUser }) => {
301+
// // attach apartment source image to generation, image should be public
302+
// return [`https://tmpbucket-adminforth.s3.eu-central-1.amazonaws.com/${record.apartment_source}`];
303+
// },
304+
generationPrompt: "Add a 10 kittyies to the appartment look, it should be foto-realistic, they should be different colors, sitting all around the appartment",
305+
countToGenerate: 3,
306+
// rateLimit: {
307+
// limit: "2/1m",
308+
// errorMessage:
309+
// "For demo purposes, you can generate only 2 images per minute",
310+
// },
311+
},
312+
preview: {
313+
// Used to display preview (if it is image) in list and show views
314+
// previewUrl: ({s3Path}) => `https://tmpbucket-adminforth.s3.eu-central-1.amazonaws.com/${s3Path}`,
315+
maxWidth: "200px",
316+
},
317+
}),
318+
new UploadPlugin({
319+
pathColumnName: "apartment_source",
320+
s3Bucket: "tmpbucket-adminforth",
321+
s3Region: "eu-central-1",
322+
allowedFileExtensions: [
323+
"jpg",
324+
"jpeg",
325+
"png",
326+
"gif",
327+
"webm",
328+
"exe",
329+
"webp",
330+
],
331+
maxFileSize: 1024 * 1024 * 20, // 5MB
332+
s3AccessKeyId: process.env.AWS_ACCESS_KEY_ID as string,
333+
s3SecretAccessKey: process.env.AWS_SECRET_ACCESS_KEY as string,
334+
s3ACL: 'public-read', // ACL which will be set to uploaded file
335+
s3Path: ({ originalFilename, originalExtension, contentType, record }) => {
336+
console.log("🔥", JSON.stringify(record));
337+
return `aparts2/${new Date().getFullYear()}/${uuid()}/${originalFilename}.${originalExtension}`
298338
},
299339
preview: {
300340
// Used to display preview (if it is image) in list and show views
301341
// previewUrl: ({s3Path}) => `https://tmpbucket-adminforth.s3.eu-central-1.amazonaws.com/${s3Path}`,
302-
showInList: true,
303342
maxWidth: "200px",
304343
},
305344
}),
@@ -315,13 +354,6 @@ export default {
315354
openAiApiKey: process.env.OPENAI_API_KEY as string,
316355
}),
317356
}),
318-
// new TextCompletePlugin({
319-
// openAiApiKey: process.env.OPENAI_API_KEY as string,
320-
// fieldName: 'description',
321-
// expert: {
322-
// debounceTime: 250,
323-
// }
324-
// }),
325357
new RichEditorPlugin({
326358
htmlFieldName: "description",
327359
completion: {

dev-demo/resources/users.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,13 @@ export default {
133133
clientSecret: process.env.MICROSOFT_CLIENT_SECRET,
134134
useOpenID: true,
135135
}),
136-
new AdminForthAdapterKeycloakOauth2({
137-
name: "Keycloak",
138-
clientID: process.env.KEYCLOAK_CLIENT_ID,
139-
clientSecret: process.env.KEYCLOAK_CLIENT_SECRET,
140-
keycloakUrl: process.env.KEYCLOAK_URL,
141-
realm: process.env.KEYCLOAK_REALM,
142-
}),
136+
// new AdminForthAdapterKeycloakOauth2({
137+
// name: "Keycloak",
138+
// clientID: process.env.KEYCLOAK_CLIENT_ID,
139+
// clientSecret: process.env.KEYCLOAK_CLIENT_SECRET,
140+
// keycloakUrl: process.env.KEYCLOAK_URL,
141+
// realm: process.env.KEYCLOAK_REALM,
142+
// }),
143143
],
144144
emailField: 'email',
145145
emailConfirmedField: 'email_confirmed'

0 commit comments

Comments
 (0)