ai-utils.js
Version:
Build AI applications, chatbots, and agents with JavaScript and TypeScript.
77 lines (76 loc) • 3.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Automatic1111ImageGenerationModel = void 0;
const zod_1 = require("zod");
const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
const callWithRetryAndThrottle_js_1 = require("../../util/api/callWithRetryAndThrottle.cjs");
const postToApi_js_1 = require("../../util/api/postToApi.cjs");
const Automatic1111Error_js_1 = require("./Automatic1111Error.cjs");
/**
* Create an image generation model that calls the AUTOMATIC1111 Stable Diffusion Web UI API.
*
* @see https://github.com/AUTOMATIC1111/stable-diffusion-webui
*/
class Automatic1111ImageGenerationModel extends AbstractModel_js_1.AbstractModel {
constructor(settings) {
super({ settings });
Object.defineProperty(this, "provider", {
enumerable: true,
configurable: true,
writable: true,
value: "Automatic1111"
});
}
get modelName() {
return this.settings.model;
}
async callAPI(input, options) {
const run = options?.run;
const settings = options?.settings;
const callSettings = Object.assign(this.settings, settings, {
abortSignal: run?.abortSignal,
engineId: this.settings.model,
prompt: input.prompt,
});
return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
retry: this.settings.retry,
throttle: this.settings.throttle,
call: async () => callAutomatic1111ImageGenerationAPI(callSettings),
});
}
generateImageResponse(prompt, options) {
return this.callAPI(prompt, options);
}
extractBase64Image(response) {
return response.images[0];
}
withSettings(additionalSettings) {
return new Automatic1111ImageGenerationModel(Object.assign({}, this.settings, additionalSettings));
}
}
exports.Automatic1111ImageGenerationModel = Automatic1111ImageGenerationModel;
const Automatic1111ImageGenerationResponseSchema = zod_1.z.object({
images: zod_1.z.array(zod_1.z.string()),
parameters: zod_1.z.object({}),
info: zod_1.z.string(),
});
async function callAutomatic1111ImageGenerationAPI({ baseUrl = "http://127.0.0.1:7860", abortSignal, height, width, prompt, negativePrompt, sampler, steps, seed, model, }) {
return (0, postToApi_js_1.postJsonToApi)({
url: `${baseUrl}/sdapi/v1/txt2img`,
body: {
height,
width,
prompt,
negative_prompt: negativePrompt,
sampler_index: sampler,
steps,
seed,
override_settings: {
sd_model_checkpoint: model,
},
},
failedResponseHandler: Automatic1111Error_js_1.failedAutomatic1111CallResponseHandler,
successfulResponseHandler: (0, postToApi_js_1.createJsonResponseHandler)(Automatic1111ImageGenerationResponseSchema),
abortSignal,
});
}