{
"title": "Widgets",
required:[
"tg_selection",
"model"
],
"properties": {
tg_selection:{
title: "tg_selection",
type: "string",
},
model: {
ย ย ย "title": "Model",
ย ย ย "type": "string",
ย ย ย oneOf: [
ย ย ย ย {
ย ย ย ย ย "const": "dall-e-3",
ย ย ย ย ย "title": "Dall-e 3"
ย ย ย ย },
ย ย ย ย {
ย ย ย ย ย "const": "dall-e-2",
ย ย ย ย ย "title": "Dall-e 2"
ย ย ย ย },
ย ย ย ]
ย ย }
},
ย
uiSchema: {
tg_selection:{
"ui:widget": "textarea",
ย ย ย ย ย ย props: {
ย ย ย ย ย ย ย className: "w-full"
ย ย ย ย ย ย }
},
model: {
"ui:enumDisabled": [
"multiply"
]
},
},
formData: {
model: "dall-e-3"
},
}Variables used: {{tg_selection}}
{{#script}}
const OPENAI_API_KEY = plugin.getApiKeys().openAIChat;
if(!OPENAI_API_KEY?.length) throw "no apikey provided in the OpenAIChat LLM Provider"
const prompt = this.tg_selection;
try {
ย ย // Set up the request headers and body
ย ย const requestOptions = {
ย ย ย method: 'POST',
ย ย ย headers: {
ย ย ย ย 'Authorization': `Bearer ${OPENAI_API_KEY}`,
ย ย ย ย 'Content-Type': 'application/json',
ย ย ย },
ย ย ย body: JSON.stringify({
ย ย ย ย model: 'dall-e-3',
ย ย ย ย prompt,
ย ย ย ย n: 1,
ย ย ย ย size: '1024x1024'
ย ย ย })
ย ย };
ย ย // Make the request to OpenAI's API
ย ย const response = await fetch('https://api.openai.com/v1/images/generations', requestOptions);
ย ย if (!response.ok) {
ย ย ย throw new Error(`OpenAI API request failed with status ${response.status}`);
ย ย }
ย ย // Get the image data from the response
ย ย const imageData = await response.json();
ย ย // Assuming the API returns a URL or binary data for the image
ย ย const imageBinary = await requestUrl(imageData.data[0].url).then(res=>res.arrayBuffer)
ย ย // Generate a random file name
ย ย const randomFileName = `generated-image-${Math.random().toString(36).substring(2, 15)}.png`;
ย ย const filePath = `images/${randomFileName}`; // Prepend the "images" directory to the file name
ย ย // Use the Vault API to save the image file
ย ย const vault = app.vault; // Assuming this script has access to `app` from the environment
ย ย // Check if the "images" directory exists, if not, create it
ย ย let imagesDir = vault.getAbstractFileByPath('images');
ย ย if (!imagesDir) {
ย ย ย await vault.createFolder('images');
ย ย }
ย ย // Check if file exists, if so, remove it before writing new content
ย ย let existingFile = vault.getAbstractFileByPath(filePath);
ย ย if (existingFile && existingFile instanceof TFile) {
ย ย ย await vault.delete(existingFile);
ย ย }
ย ย
ย ย await vault.createBinary(filePath, new Uint8Array(imageBinary));
ย ย return filePath;
ย ย
} catch (error) {
ย ย console.error('Error generating or saving image:', error);
}
// Call the function to generate an image and save it in the vault
return await generateDalleImageAndSaveToVault(IMAGE_PROMPT);{{/script}}