ai-utils.js
Version:
Build AI applications, chatbots, and agents with JavaScript and TypeScript.
73 lines (72 loc) • 2.68 kB
JavaScript
import { z } from "zod";
import { AbstractModel } from "../../model-function/AbstractModel.js";
import { callWithRetryAndThrottle } from "../../util/api/callWithRetryAndThrottle.js";
import { createJsonResponseHandler, postJsonToApi, } from "../../util/api/postToApi.js";
import { failedAutomatic1111CallResponseHandler } from "./Automatic1111Error.js";
/**
* Create an image generation model that calls the AUTOMATIC1111 Stable Diffusion Web UI API.
*
* @see https://github.com/AUTOMATIC1111/stable-diffusion-webui
*/
export class Automatic1111ImageGenerationModel extends 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 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));
}
}
const Automatic1111ImageGenerationResponseSchema = z.object({
images: z.array(z.string()),
parameters: z.object({}),
info: z.string(),
});
async function callAutomatic1111ImageGenerationAPI({ baseUrl = "http://127.0.0.1:7860", abortSignal, height, width, prompt, negativePrompt, sampler, steps, seed, model, }) {
return 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: failedAutomatic1111CallResponseHandler,
successfulResponseHandler: createJsonResponseHandler(Automatic1111ImageGenerationResponseSchema),
abortSignal,
});
}