|
| 1 | +const BaseService = require("../../services/BaseService"); |
| 2 | +const { TypedValue } = require("../../services/drivers/meta/Runtime"); |
| 3 | +const { Context } = require("../../util/context"); |
| 4 | + |
| 5 | +class OpenAIImageGenerationService extends BaseService { |
| 6 | + static MODULES = { |
| 7 | + openai: require('openai'), |
| 8 | + } |
| 9 | + async _init () { |
| 10 | + const sk_key = |
| 11 | + this.config?.openai?.secret_key ?? |
| 12 | + this.global_config.openai?.secret_key; |
| 13 | + |
| 14 | + this.openai = new this.modules.openai.OpenAI({ |
| 15 | + apiKey: sk_key |
| 16 | + }); |
| 17 | + } |
| 18 | + |
| 19 | + static IMPLEMENTS = { |
| 20 | + ['puter-image-generation']: { |
| 21 | + async generate ({ prompt, test_mode }) { |
| 22 | + const url = await this.generate(prompt, { |
| 23 | + ratio: this.constructor.RATIO_SQUARE, |
| 24 | + }); |
| 25 | + |
| 26 | + if ( test_mode ) { |
| 27 | + return new TypedValue({ |
| 28 | + $: 'string:url:web', |
| 29 | + content_type: 'image', |
| 30 | + }, 'https://puter-sample-data.puter.site/image_example.png'); |
| 31 | + } |
| 32 | + |
| 33 | + const image = new TypedValue({ |
| 34 | + $: 'string:url:web', |
| 35 | + content_type: 'image' |
| 36 | + }, url); |
| 37 | + |
| 38 | + return image; |
| 39 | + } |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + static RATIO_SQUARE = { w: 1024, h: 1024 }; |
| 44 | + static RATIO_PORTRAIT = { w: 1024, h: 1792 }; |
| 45 | + static RATIO_LANDSCAPE = { w: 1792, h: 1024 }; |
| 46 | + |
| 47 | + async generate (prompt, { |
| 48 | + ratio, |
| 49 | + model, |
| 50 | + }) { |
| 51 | + if ( typeof prompt !== 'string' ) { |
| 52 | + throw new Error('`prompt` must be a string'); |
| 53 | + } |
| 54 | + |
| 55 | + if ( ! ratio || ! this._validate_ratio(ratio) ) { |
| 56 | + throw new Error('`ratio` must be a valid ratio'); |
| 57 | + } |
| 58 | + |
| 59 | + model = model ?? 'dall-e-3'; |
| 60 | + |
| 61 | + const user_private_uid = Context.get('actor')?.private_uid ?? 'UNKNOWN'; |
| 62 | + if ( user_private_uid === 'UNKNOWN' ) { |
| 63 | + this.errors.report('chat-completion-service:unknown-user', { |
| 64 | + message: 'failed to get a user ID for an OpenAI request', |
| 65 | + alarm: true, |
| 66 | + trace: true, |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + const result = |
| 71 | + await this.openai.images.generate({ |
| 72 | + user: user_private_uid, |
| 73 | + prompt, |
| 74 | + size: `${ratio.w}x${ratio.h}`, |
| 75 | + }); |
| 76 | + |
| 77 | + const spending_meta = { |
| 78 | + model, |
| 79 | + size: `${ratio.w}x${ratio.h}`, |
| 80 | + }; |
| 81 | + |
| 82 | + const svc_spending = Context.get('services').get('spending'); |
| 83 | + svc_spending.record_spending('openai', 'image-generation', spending_meta); |
| 84 | + |
| 85 | + const url = result.data?.[0]?.url; |
| 86 | + return url; |
| 87 | + } |
| 88 | + |
| 89 | + _validate_ratio (ratio) { |
| 90 | + return false |
| 91 | + || ratio === this.constructor.RATIO_SQUARE |
| 92 | + || ratio === this.constructor.RATIO_PORTRAIT |
| 93 | + || ratio === this.constructor.RATIO_LANDSCAPE |
| 94 | + ; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +module.exports = { |
| 99 | + OpenAIImageGenerationService, |
| 100 | +}; |
0 commit comments