|
| 1 | +# Bulk AI Flow |
| 2 | +This plugin allows filling fields in records based on data from other fields using AI. |
| 3 | + |
| 4 | +## Installation |
| 5 | + |
| 6 | +To install the plugin: |
| 7 | + |
| 8 | +```bash |
| 9 | +npm install @adminforth/bulk-ai-flow --save |
| 10 | +``` |
| 11 | + |
| 12 | +You'll also need an image vision adapter: |
| 13 | + |
| 14 | +```bash |
| 15 | +npm install @adminforth/image-vision-adapter-openai --save |
| 16 | +``` |
| 17 | + |
| 18 | + |
| 19 | +## Setup |
| 20 | +Add a column for storing the URL or path to the image in the database, add this statement to the `./schema.prisma`: |
| 21 | + |
| 22 | +```ts title="./schema.prisma" |
| 23 | +model apartments { |
| 24 | + id String @id |
| 25 | + created_at DateTime? |
| 26 | + title String |
| 27 | + square_meter Float? |
| 28 | + price Decimal |
| 29 | + number_of_rooms Int? |
| 30 | + description String? |
| 31 | + country String? |
| 32 | + listed Boolean |
| 33 | + realtor_id String? |
| 34 | +//diff-add |
| 35 | + image_url String? |
| 36 | +} |
| 37 | +``` |
| 38 | +
|
| 39 | +Migrate prisma schema: |
| 40 | +
|
| 41 | +```bash |
| 42 | +npm run makemigration -- --name add-apartment-image-url ; npm run migrate:local |
| 43 | +``` |
| 44 | +
|
| 45 | +Add credentials in your `.env.local` file: |
| 46 | +```ts title=".env" |
| 47 | +... |
| 48 | + |
| 49 | +//diff-add |
| 50 | +OPENAI_API_KEY=your_secret_openai_key |
| 51 | + |
| 52 | +... |
| 53 | +``` |
| 54 | +
|
| 55 | +
|
| 56 | +Add column to `aparts` resource configuration: |
| 57 | +
|
| 58 | +```ts title="./resources/apartments.ts" |
| 59 | +//diff-add |
| 60 | +import BulkAiFlowPlugin from '@adminforth/bulk-ai-flow'; |
| 61 | +//diff-add |
| 62 | +import AdminForthImageVisionAdapterOpenAi from '@adminforth/image-vision-adapter-openai'; |
| 63 | + |
| 64 | +export const admin = new AdminForth({ |
| 65 | + ... |
| 66 | + resourceId: 'aparts', |
| 67 | + columns: [ |
| 68 | + ... |
| 69 | +//diff-add |
| 70 | + { |
| 71 | +//diff-add |
| 72 | + name: 'image_url', |
| 73 | +//diff-add |
| 74 | + label: 'Image', |
| 75 | +//diff-add |
| 76 | + showIn: { list: false, create: true, edit: true}, |
| 77 | +//diff-add |
| 78 | + } |
| 79 | + ... |
| 80 | + ], |
| 81 | + plugins: [ |
| 82 | + ... |
| 83 | + //diff-add |
| 84 | + plugins: [ |
| 85 | + //diff-add |
| 86 | + new BulkAiFlowPlugin({ |
| 87 | + //diff-add |
| 88 | + actionName: 'Analyze', |
| 89 | + //diff-add |
| 90 | + attachFiles: async ({ record }: { record: any }) => { |
| 91 | + //diff-add |
| 92 | + return [record.image_url]; |
| 93 | + //diff-add |
| 94 | + }, |
| 95 | + //diff-add |
| 96 | + adapter: new AdminForthImageVisionAdapterOpenAi( |
| 97 | + //diff-add |
| 98 | + { |
| 99 | + //diff-add |
| 100 | + openAiApiKey: process.env.OPENAI_API_KEY as string, |
| 101 | + //diff-add |
| 102 | + model: 'gpt-4.1-mini', |
| 103 | + //diff-add |
| 104 | + } |
| 105 | + //diff-add |
| 106 | + ), |
| 107 | + //diff-add |
| 108 | + outputFields: [{ |
| 109 | + //diff-add |
| 110 | + 'description': 'describe what is in the image, also include fact that price is {{price}}', |
| 111 | + //diff-add |
| 112 | + 'country': 'In which country it can be located?', |
| 113 | + //diff-add |
| 114 | + 'number_of_rooms': 'How many rooms are in the apartment? If you do not know, just guess', |
| 115 | + //diff-add |
| 116 | + 'square_meter': 'What is the square of the apartment in square meters? If you do not know, just guess', |
| 117 | + //diff-add |
| 118 | + 'listed': 'Is the apartment should be listed for sale? If you do not know, just guess, return boolean value', |
| 119 | + //diff-add |
| 120 | + }], |
| 121 | + //diff-add |
| 122 | + }), |
| 123 | + //diff-add |
| 124 | + ], |
| 125 | + |
| 126 | + |
| 127 | + ... |
| 128 | + |
| 129 | +}); |
| 130 | +``` |
| 131 | +
|
| 132 | +## Usage |
| 133 | +1. Select fields you want to fill |
| 134 | +2. Click on the three dots menu |
| 135 | +3. Click analyze |
| 136 | + |
| 137 | +4. Wait for finish analyze |
| 138 | +5. Check and edit result |
| 139 | + |
| 140 | +6. Save changhes |
| 141 | + |
0 commit comments