OpenAI: Generate Image

OpenAI: Generate Image

OpenAI: 画像生成

This item generates an image using OpenAI’s model.

Auto Step icon
Basic Configs
Step Name
Note
Configs for this Auto Step
conf_Auth
C1: Authorization Setting in which Project API Key is set *
conf_OrganizationId
C-deprecated: Organization ID (Default organization if blank)
conf_Model
C2: Model *
conf_Prompt
C3: Prompt *#{EL}
conf_Size
C4: Image Size (auto if not selected)
conf_Format
C5: Image Format (Only supported for gpt-image-1)
conf_Style
C6: Image Style (Only supported for dall-e-3)
conf_Quality
C7: Image Quality (auto if not selected)
conf_File
C8: Data item to add the generated file *
conf_FileName
C9: File name to save as *#{EL}

Notes

  • Project API Keys can be created here
  • Some options are only supported on certain models

Capture

See Also

Script (click to open)
  • An XML file that contains the code below is available to download
    • openai-dalle-image-generate.xml (C) Questetra, Inc. (MIT License)
    • If you are using Professional, you can modify the contents of this file and use it as your own add-on auto step

function main() {
    ////// == 工程コンフィグ・ワークフローデータの参照 / Config & Data Retrieving ==
    const auth = configs.getObject('conf_Auth');   /// REQUIRED
    const organizationId = configs.get('conf_OrganizationId');
    const model = configs.get('conf_Model');
    const prompt = configs.get('conf_Prompt');
    if (prompt === '') {
        throw new Error('Prompt is empty.');
    }

    const size = retriveSelectItem('conf_Size');
    const style = retriveSelectItem('conf_Style');
    const format = retriveSelectItem('conf_Format');
    const quality = retriveSelectItem('conf_Quality');
    const fileName = configs.get('conf_FileName');
    if (fileName === '') {
        throw new Error('File name is empty.');
    }

    ////// == 演算 / Calculating ==
    const newFile = generateImage(auth, organizationId, model, prompt, size, style, format, quality, fileName);

    saveFileData('conf_File', newFile);
}

/**
 * form-type="SELECT_ITEM" の設定値を取得する
 * 空の場合、undefined を返す
 * @param configName
 * @returns {undefined|String}
 */
const retriveSelectItem = (configName) => {
    const value = configs.get(configName);
    if (value === '') {
        return undefined;
    }
    return value;
};

/**
 * 画像生成
 * @param auth
 * @param organizationId
 * @param model
 * @param prompt
 * @param size
 * @param output_format
 * @param style
 * @param quality
 * @returns {String} answer1
 */
const generateImage = (auth, organizationId, model, prompt, size, style, format, quality, fileName) => {
    // https://platform.openai.com/docs/guides/safety-best-practices
    // Sending end-user IDs in your requests can be a useful tool to help OpenAI monitor and detect abuse.
    const user = `m${processInstance.getProcessModelInfoId().toString()}`;

    /// prepare json
    const requestJson = {
        model,
        prompt,
        size,
        style,
        output_format: format,
        quality,
        user,
        n: 1,
    };

    if (model !== "gpt-image-1") {
        Object.assign(requestJson, {
            response_format: 'b64_json'
        });
    }

    let request = httpClient.begin().authSetting(auth)
        .body(JSON.stringify(requestJson), 'application/json');
    if (organizationId !== null && organizationId !== '') {
        request = request.header('OpenAI-Organization', organizationId);
    }
    const response = request.post('https://api.openai.com/v1/images/generations');

    const responseCode = response.getStatusCode();
    const responseBody = response.getResponseAsString();
    if (responseCode !== 200) {
        engine.log(responseBody);
        throw new Error(`Failed to request. status: ${responseCode}`);
    }
    const responseJson = JSON.parse(responseBody);
    const image = base64.decodeFromStringToByteArray(responseJson.data[0].b64_json);

    let contentType = "image/png";
    if (model === "gpt-image-1" && format !== undefined) {
        contentType = `image/${format}`;
    }

    return new com.questetra.bpms.core.event.scripttask.NewQfile(
        fileName,
        contentType,
        image
    );
};

/**
 * データ項目への保存
 * @param configName
 * @param newFile
 */
const saveFileData = (configName, newFile) => {
    const def = configs.getObject(configName);
    if (def === null) {
        return;
    }
    let files = engine.findData(def);
    if (files === null) {
        files = new java.util.ArrayList();
    }
    files.add(newFile);
    engine.setData(def, files);
};
Scroll to Top

Discover more from Questetra Support

Subscribe now to keep reading and get access to the full archive.

Continue reading