Added block entropy endpoints for chat and image

b9857eb315553616b66aa5215eaa267f37704994

Edward Kim <edwardkim.ek@gmail.com>

12 files changed, +253 -2Ignore whitespace
public/index.html+15 -0
@@ -2427,6 +2427,7 @@
24272427 <optgroup>
24282428 <option value="01ai">01.AI (Yi)</option>
24292429 <option value="ai21">AI21</option>
2430+ <option value="blockentropy">Block Entropy</option>
24302431 <option value="claude">Claude</option>
24312432 <option value="cohere">Cohere</option>
24322433 <option value="groq">Groq</option>
@@ -2938,6 +2939,20 @@
29382939 </select>
29392940 </div>
29402941 </form>
2942+ <form id="blockentropy_form" data-source="blockentropy">
2943+ <h4 data-i18n="Block Entropy API Key">Block Entropy API Key</h4>
2944+ <div class="flex-container">
2945+ <input id="api_key_blockentropy" name="api_key_blockentropy" class="text_pole flex1" maxlength="500" value="" type="text" autocomplete="off">
2946+ <div title="Clear your API key" data-i18n="[title]Clear your API key" class="menu_button fa-solid fa-circle-xmark clear-api-key" data-key="api_key_blockentropy"></div>
2947+ </div>
2948+ <div data-for="api_key_blockentropy" class="neutral_warning" data-i18n="For privacy reasons, your API key will be hidden after you reload the page.">
2949+ For privacy reasons, your API key will be hidden after you reload the page.
2950+ </div>
2951+ <h4 data-i18n="Available Models">Select a Model</h4>
2952+ <div class="flex-container">
2953+ <select id="model_blockentropy_select" class="text_pole model_blockentropy_select"></select>
2954+ </div>
2955+ </form>
29412956 <form id="custom_form" data-source="custom">
29422957 <h4 data-i18n="Custom Endpoint (Base URL)">Custom Endpoint (Base URL)</h4>
29432958 <div class="flex-container">
public/scripts/RossAscends-mods.js+1 -0
@@ -378,6 +378,7 @@ function RA_autoconnect(PrevApi) {
378378 || (secret_state[SECRET_KEYS.PERPLEXITY] && oai_settings.chat_completion_source == chat_completion_sources.PERPLEXITY)
379379 || (secret_state[SECRET_KEYS.GROQ] && oai_settings.chat_completion_source == chat_completion_sources.GROQ)
380380 || (secret_state[SECRET_KEYS.ZEROONEAI] && oai_settings.chat_completion_source == chat_completion_sources.ZEROONEAI)
381+ || (secret_state[SECRET_KEYS.BLOCKENTROPY] && oai_settings.chat_completion_source == chat_completion_sources.BLOCKENTROPY)
381382 || (isValidUrl(oai_settings.custom_url) && oai_settings.chat_completion_source == chat_completion_sources.CUSTOM)
382383 ) {
383384 $('#api_button_openai').trigger('click');
public/scripts/extensions/stable-diffusion/index.js+64 -1
@@ -50,6 +50,7 @@ const sources = {
5050 drawthings: 'drawthings',
5151 pollinations: 'pollinations',
5252 stability: 'stability',
53+ blockentropy: 'blockentropy',
5354};
5455
5556const initiators = {
@@ -1221,7 +1222,7 @@ async function onModelChange() {
12211222 extension_settings.sd.model = $('#sd_model').find(':selected').val();
12221223 saveSettingsDebounced();
12231224
12241225 const cloudSources = [sources.horde, sources.novel, sources.openai, sources.togetherai, sources.pollinations, sources.stability, sources.blockentropy];
12251226
12261227 if (cloudSources.includes(extension_settings.sd.source)) {
12271228 return;
@@ -1433,6 +1434,9 @@ async function loadSamplers() {
14331434 case sources.stability:
14341435 samplers = ['N/A'];
14351436 break;
1437+ case sources.blockentropy:
1438+ samplers = ['N/A'];
1439+ break;
14361440 }
14371441
14381442 for (const sampler of samplers) {
@@ -1619,6 +1623,9 @@ async function loadModels() {
16191623 case sources.stability:
16201624 models = await loadStabilityModels();
16211625 break;
1626+ case sources.blockentropy:
1627+ models = await loadBlockEntropyModels();
1628+ break;
16221629 }
16231630
16241631 for (const model of models) {
@@ -1713,6 +1720,26 @@ async function loadTogetherAIModels() {
17131720 return [];
17141721}
17151722
1723+async function loadBlockEntropyModels() {
1724+ if (!secret_state[SECRET_KEYS.BLOCKENTROPY]) {
1725+ console.debug('Block Entropy API key is not set.');
1726+ return [];
1727+ }
1728+
1729+ const result = await fetch('/api/sd/blockentropy/models', {
1730+ method: 'POST',
1731+ headers: getRequestHeaders(),
1732+ });
1733+ console.log(result);
1734+ if (result.ok) {
1735+ const data = await result.json();
1736+ console.log(data);
1737+ return data;
1738+ }
1739+
1740+ return [];
1741+}
1742+
17161743async function loadHordeModels() {
17171744 const result = await fetch('/api/horde/sd-models', {
17181745 method: 'POST',
@@ -1979,6 +2006,9 @@ async function loadSchedulers() {
19792006 case sources.stability:
19802007 schedulers = ['N/A'];
19812008 break;
2009+ case sources.blockentropy:
2010+ schedulers = ['N/A'];
2011+ break;
19822012 }
19832013
19842014 for (const scheduler of schedulers) {
@@ -2055,6 +2085,9 @@ async function loadVaes() {
20552085 case sources.stability:
20562086 vaes = ['N/A'];
20572087 break;
2088+ case sources.blockentropy:
2089+ vaes = ['N/A'];
2090+ break;
20582091 }
20592092
20602093 for (const vae of vaes) {
@@ -2583,6 +2616,9 @@ async function sendGenerationRequest(generationType, prompt, additionalNegativeP
25832616 case sources.stability:
25842617 result = await generateStabilityImage(prefixedPrompt, negativePrompt, signal);
25852618 break;
2619+ case sources.blockentropy:
2620+ result = await generateBlockEntropyImage(prefixedPrompt, negativePrompt, signal);
2621+ break;
25862622 }
25872623
25882624 if (!result.data) {
@@ -2638,6 +2674,31 @@ async function generateTogetherAIImage(prompt, negativePrompt, signal) {
26382674 }
26392675}
26402676
2677+async function generateBlockEntropyImage(prompt, negativePrompt, signal) {
2678+ const result = await fetch('/api/sd/blockentropy/generate', {
2679+ method: 'POST',
2680+ headers: getRequestHeaders(),
2681+ signal: signal,
2682+ body: JSON.stringify({
2683+ prompt: prompt,
2684+ negative_prompt: negativePrompt,
2685+ model: extension_settings.sd.model,
2686+ steps: extension_settings.sd.steps,
2687+ width: extension_settings.sd.width,
2688+ height: extension_settings.sd.height,
2689+ seed: extension_settings.sd.seed >= 0 ? extension_settings.sd.seed : undefined,
2690+ }),
2691+ });
2692+
2693+ if (result.ok) {
2694+ const data = await result.json();
2695+ return { format: 'png', data: data.images[0] };
2696+ } else {
2697+ const text = await result.text();
2698+ throw new Error(text);
2699+ }
2700+}
2701+
26412702/**
26422703 * Generates an image using the Pollinations API.
26432704 * @param {string} prompt - The main instruction used to guide the image generation.
@@ -3451,6 +3512,8 @@ function isValidState() {
34513512 return true;
34523513 case sources.stability:
34533514 return secret_state[SECRET_KEYS.STABILITY];
3515+ case sources.blockentropy:
3516+ return secret_state[SECRET_KEYS.BLOCKENTROPY];
34543517 }
34553518}
34563519
public/scripts/extensions/stable-diffusion/settings.html+2 -1
@@ -48,6 +48,7 @@
4848 <option value="auto">Stable Diffusion Web UI (AUTOMATIC1111)</option>
4949 <option value="horde">Stable Horde</option>
5050 <option value="togetherai">TogetherAI</option>
51+ <option value="blockentropy">Block Entropy</option>
5152 </select>
5253 <div data-sd-source="auto">
5354 <label for="sd_auto_url">SD Web UI URL</label>
@@ -378,7 +379,7 @@
378379 </label>
379380 </div>
380381
381382 <div data-sd-source="novel,togetherai,pollinations,comfy,drawthings,vlad,auto,horde,extras,stability,blockentropy" class="marginTop5">
382383 <label for="sd_seed">
383384 <span data-i18n="Seed">Seed</span>
384385 <small data-i18n="(-1 for random)">(-1 for random)</small>
public/scripts/openai.js+54 -0
@@ -185,6 +185,7 @@ export const chat_completion_sources = {
185185 PERPLEXITY: 'perplexity',
186186 GROQ: 'groq',
187187 ZEROONEAI: '01ai',
188+ BLOCKENTROPY: 'blockentropy',
188189};
189190
190191const character_names_behavior = {
@@ -267,6 +268,7 @@ const default_settings = {
267268 perplexity_model: 'llama-3-70b-instruct',
268269 groq_model: 'llama3-70b-8192',
269270 zerooneai_model: 'yi-large',
271+ blockentropy_model: 'be-70b-base-llama3.1',
270272 custom_model: '',
271273 custom_url: '',
272274 custom_include_body: '',
@@ -347,6 +349,7 @@ const oai_settings = {
347349 perplexity_model: 'llama-3-70b-instruct',
348350 groq_model: 'llama3-70b-8192',
349351 zerooneai_model: 'yi-large',
352+ blockentropy_model: 'be-70b-base-llama3.1',
350353 custom_model: '',
351354 custom_url: '',
352355 custom_include_body: '',
@@ -1541,6 +1544,8 @@ function getChatCompletionModel() {
15411544 return oai_settings.groq_model;
15421545 case chat_completion_sources.ZEROONEAI:
15431546 return oai_settings.zerooneai_model;
1547+ case chat_completion_sources.BLOCKENTROPY:
1548+ return oai_settings.blockentropy_model;
15441549 default:
15451550 throw new Error(`Unknown chat completion source: ${oai_settings.chat_completion_source}`);
15461551 }
@@ -1654,6 +1659,23 @@ function saveModelList(data) {
16541659
16551660 $('#model_01ai_select').val(oai_settings.zerooneai_model).trigger('change');
16561661 }
1662+
1663+ if (oai_settings.chat_completion_source == chat_completion_sources.BLOCKENTROPY) {
1664+ $('.model_blockentropy_select').empty();
1665+ model_list.forEach((model) => {
1666+ $('.model_blockentropy_select').append(
1667+ $('<option>', {
1668+ value: model.id,
1669+ text: model.id,
1670+ }));
1671+ });
1672+
1673+ if (!oai_settings.blockentropy_model && model_list.length > 0) {
1674+ oai_settings.blockentropy_model = model_list[0].id;
1675+ }
1676+
1677+ $('#model_blockentropy_select').val(oai_settings.blockentropy_model).trigger('change');
1678+ }
16571679}
16581680
16591681function appendOpenRouterOptions(model_list, groupModels = false, sort = false) {
@@ -4037,6 +4059,12 @@ async function onModelChange() {
40374059 oai_settings.zerooneai_model = value;
40384060 }
40394061
4062+ if (value && $(this).is('#model_blockentropy_select')) {
4063+ console.log('Block Entropy model changed to', value);
4064+ oai_settings.blockentropy_model = value;
4065+ $('#blockentropy_model_id').val(value).trigger('input');
4066+ }
4067+
40404068 if (value && $(this).is('#model_custom_select')) {
40414069 console.log('Custom model changed to', value);
40424070 oai_settings.custom_model = value;
@@ -4302,6 +4330,19 @@ async function onModelChange() {
43024330 oai_settings.temp_openai = Math.min(oai_max_temp, oai_settings.temp_openai);
43034331 $('#temp_openai').attr('max', oai_max_temp).val(oai_settings.temp_openai).trigger('input');
43044332 }
4333+ if (oai_settings.chat_completion_source === chat_completion_sources.BLOCKENTROPY) {
4334+ if (oai_settings.max_context_unlocked) {
4335+ $('#openai_max_context').attr('max', unlocked_max);
4336+ } else {
4337+ $('#openai_max_context').attr('max', max_32k);
4338+ }
4339+
4340+ oai_settings.openai_max_context = Math.min(oai_settings.openai_max_context, Number($('#openai_max_context').attr('max')));
4341+ $('#openai_max_context').val(oai_settings.openai_max_context).trigger('input');
4342+
4343+ oai_settings.temp_openai = Math.min(oai_max_temp, oai_settings.temp_openai);
4344+ $('#temp_openai').attr('max', oai_max_temp).val(oai_settings.temp_openai).trigger('input');
4345+ }
43054346
43064347 $('#openai_max_context_counter').attr('max', Number($('#openai_max_context').attr('max')));
43074348
@@ -4509,6 +4550,18 @@ async function onConnectButtonClick(e) {
45094550 return;
45104551 }
45114552 }
4553+ if (oai_settings.chat_completion_source == chat_completion_sources.BLOCKENTROPY) {
4554+ const api_key_blockentropy = String($('#api_key_blockentropy').val()).trim();
4555+
4556+ if (api_key_blockentropy.length) {
4557+ await writeSecret(SECRET_KEYS.BLOCKENTROPY, api_key_blockentropy);
4558+ }
4559+
4560+ if (!secret_state[SECRET_KEYS.BLOCKENTROPY]) {
4561+ console.log('No secret key saved for Block Entropy');
4562+ return;
4563+ }
4564+ }
45124565
45134566 startStatusLoading();
45144567 saveSettingsDebounced();
@@ -5282,6 +5335,7 @@ $(document).ready(async function () {
52825335 $('#model_perplexity_select').on('change', onModelChange);
52835336 $('#model_groq_select').on('change', onModelChange);
52845337 $('#model_01ai_select').on('change', onModelChange);
5338+ $('#model_blockentropy_select').on('change', onModelChange);
52855339 $('#model_custom_select').on('change', onModelChange);
52865340 $('#settings_preset_openai').on('change', onSettingsPresetChange);
52875341 $('#new_oai_preset').on('click', onNewPresetClick);
public/scripts/secrets.js+2 -0
@@ -32,6 +32,7 @@ export const SECRET_KEYS = {
3232 ZEROONEAI: 'api_key_01ai',
3333 HUGGINGFACE: 'api_key_huggingface',
3434 STABILITY: 'api_key_stability',
35+ BLOCKENTROPY: 'api_key_blockentropy',
3536};
3637
3738const INPUT_MAP = {
@@ -63,6 +64,7 @@ const INPUT_MAP = {
6364 [SECRET_KEYS.FEATHERLESS]: '#api_key_featherless',
6465 [SECRET_KEYS.ZEROONEAI]: '#api_key_01ai',
6566 [SECRET_KEYS.HUGGINGFACE]: '#api_key_huggingface',
67+ [SECRET_KEYS.BLOCKENTROPY]: '#api_key_blockentropy',
6668};
6769
6870async function clearSecret() {
public/scripts/slash-commands.js+1 -0
@@ -3190,6 +3190,7 @@ function getModelOptions() {
31903190 { id: 'model_perplexity_select', api: 'openai', type: chat_completion_sources.PERPLEXITY },
31913191 { id: 'model_groq_select', api: 'openai', type: chat_completion_sources.GROQ },
31923192 { id: 'model_01ai_select', api: 'openai', type: chat_completion_sources.ZEROONEAI },
3193+ { id: 'model_blockentropy_select', api: 'openai', type: chat_completion_sources.BLOCKENTROPY },
31933194 { id: 'model_novel_select', api: 'novel', type: null },
31943195 { id: 'horde_model', api: 'koboldhorde', type: null },
31953196 ];
public/scripts/tokenizers.js+9 -0
@@ -549,6 +549,15 @@ export function getTokenizerModel() {
549549 return yiTokenizer;
550550 }
551551
552+ if (oai_settings.chat_completion_source === chat_completion_sources.BLOCKENTROPY) {
553+ if (oai_settings.blockentropy_model.includes('llama3')) {
554+ return llama3Tokenizer;
555+ }
556+ if (oai_settings.blockentropy_model.includes('miqu') || oai_settings.blockentropy_model.includes('mixtral')) {
557+ return mistralTokenizer;
558+ }
559+ }
560+
552561 // Default to Turbo 3.5
553562 return turboTokenizer;
554563}
src/constants.js+1 -0
@@ -195,6 +195,7 @@ const CHAT_COMPLETION_SOURCES = {
195195 PERPLEXITY: 'perplexity',
196196 GROQ: 'groq',
197197 ZEROONEAI: '01ai',
198+ BLOCKENTROPY: 'blockentropy',
198199};
199200
200201/**
src/endpoints/backends/chat-completions.js+10 -0
@@ -18,6 +18,7 @@ const API_PERPLEXITY = 'https://api.perplexity.ai';
1818const API_GROQ = 'https://api.groq.com/openai/v1';
1919const API_MAKERSUITE = 'https://generativelanguage.googleapis.com';
2020const API_01AI = 'https://api.01.ai/v1';
21+const API_BLOCKENTROPY = 'https://api.blockentropy.ai/v1';
2122
2223/**
2324 * Applies a post-processing step to the generated messages.
@@ -675,6 +676,10 @@ router.post('/status', jsonParser, async function (request, response_getstatus_o
675676 api_url = API_01AI;
676677 api_key_openai = readSecret(request.user.directories, SECRET_KEYS.ZEROONEAI);
677678 headers = {};
679+ } else if (request.body.chat_completion_source === CHAT_COMPLETION_SOURCES.BLOCKENTROPY) {
680+ api_url = API_BLOCKENTROPY;
681+ api_key_openai = readSecret(request.user.directories, SECRET_KEYS.BLOCKENTROPY);
682+ headers = {};
678683 } else {
679684 console.log('This chat completion source is not supported yet.');
680685 return response_getstatus_openai.status(400).send({ error: true });
@@ -941,6 +946,11 @@ router.post('/generate', jsonParser, function (request, response) {
941946 apiKey = readSecret(request.user.directories, SECRET_KEYS.ZEROONEAI);
942947 headers = {};
943948 bodyParams = {};
949+ } else if (request.body.chat_completion_source === CHAT_COMPLETION_SOURCES.BLOCKENTROPY) {
950+ apiUrl = API_BLOCKENTROPY;
951+ apiKey = readSecret(request.user.directories, SECRET_KEYS.BLOCKENTROPY);
952+ headers = {};
953+ bodyParams = {};
944954 } else {
945955 console.log('This chat completion source is not supported yet.');
946956 return response.status(400).send({ error: true });
src/endpoints/secrets.js+1 -0
@@ -44,6 +44,7 @@ const SECRET_KEYS = {
4444 ZEROONEAI: 'api_key_01ai',
4545 HUGGINGFACE: 'api_key_huggingface',
4646 STABILITY: 'api_key_stability',
47+ BLOCKENTROPY: 'api_key_blockentropy',
4748};
4849
4950// These are the keys that are safe to expose, even if allowKeysExposure is false
src/endpoints/stable-diffusion.js+93 -0
@@ -8,6 +8,7 @@ const writeFileAtomicSync = require('write-file-atomic').sync;
88const { jsonParser } = require('../express-common');
99const { readSecret, SECRET_KEYS } = require('./secrets.js');
1010const FormData = require('form-data');
11+const { scheduler } = require('timers/promises');
1112
1213/**
1314 * Sanitizes a string.
@@ -908,10 +909,102 @@ stability.post('/generate', jsonParser, async (request, response) => {
908909 }
909910});
910911
912+const blockentropy = express.Router();
913+
914+blockentropy.post('/models', jsonParser, async (request, response) => {
915+ try {
916+ const key = readSecret(request.user.directories, SECRET_KEYS.BLOCKENTROPY);
917+
918+ if (!key) {
919+ console.log('Block Entropy key not found.');
920+ return response.sendStatus(400);
921+ }
922+
923+ const modelsResponse = await fetch('https://api.blockentropy.ai/sdapi/v1/schedulers', {
924+ method: 'GET',
925+ headers: {
926+ 'Authorization': `Bearer ${key}`,
927+ },
928+ });
929+
930+ if (!modelsResponse.ok) {
931+ console.log('Block Entropy returned an error.');
932+ return response.sendStatus(500);
933+ }
934+
935+ const data = await modelsResponse.json();
936+
937+ if (!Array.isArray(data)) {
938+ console.log('Block Entropy returned invalid data.');
939+ return response.sendStatus(500);
940+ }
941+ const models = data.map(x => ({ value: x.name, text: x.name }));
942+ return response.send(models);
943+
944+ } catch (error) {
945+ console.log(error);
946+ return response.sendStatus(500);
947+ }
948+});
949+
950+blockentropy.post('/generate', jsonParser, async (request, response) => {
951+ try {
952+ const key = readSecret(request.user.directories, SECRET_KEYS.BLOCKENTROPY);
953+
954+ if (!key) {
955+ console.log('Block Entropy key not found.');
956+ return response.sendStatus(400);
957+ }
958+
959+ console.log('Block Entropy request:', request.body);
960+
961+ const result = await fetch('https://api.blockentropy.ai/sdapi/v1/txt2img', {
962+ method: 'POST',
963+ body: JSON.stringify({
964+ request_type: 'image-model-inference',
965+ prompt: request.body.prompt,
966+ negative_prompt: request.body.negative_prompt,
967+ height: request.body.height,
968+ width: request.body.width,
969+ model: request.body.model,
970+ steps: request.body.steps,
971+ scheduler: request.body.scheduler,
972+ n: 1,
973+ // Limited to 10000 on playground, works fine with more.
974+ seed: request.body.seed >= 0 ? request.body.seed : Math.floor(Math.random() * 10_000_000),
975+ }),
976+ headers: {
977+ 'Content-Type': 'application/json',
978+ 'Authorization': `Bearer ${key}`,
979+ },
980+ });
981+
982+ if (!result.ok) {
983+ console.log('Block Entropy returned an error.');
984+ return response.sendStatus(500);
985+ }
986+
987+ const data = await result.json();
988+ console.log('Block Entropy response:', data);
989+
990+ //if (data.status !== 'finished') {
991+ // console.log('Block Entropy job failed.');
992+ // return response.sendStatus(500);
993+ //}
994+
995+ return response.send(data);
996+ } catch (error) {
997+ console.log(error);
998+ return response.sendStatus(500);
999+ }
1000+});
1001+
1002+
9111003router.use('/comfy', comfy);
9121004router.use('/together', together);
9131005router.use('/drawthings', drawthings);
9141006router.use('/pollinations', pollinations);
9151007router.use('/stability', stability);
1008+router.use('/blockentropy', blockentropy);
9161009
9171010module.exports = { router };