Restructure, make it work if ollama is down.

This commit is contained in:
Ivor Wanders
2026-02-14 20:47:26 -05:00
parent 4f53fa3683
commit 8d78000327
2 changed files with 37 additions and 25 deletions
+11 -6
View File
@@ -8,6 +8,7 @@ import aiohttp
OLLAMA_HOST = os.environ.get("OLLAMA_HOST", "127.0.0.1:11434")
class OllamaGenerateString :
def __init__(self):
pass
@@ -42,11 +43,15 @@ class OllamaGenerateString :
@PromptServer.instance.routes.get("/iw/api/ollama/models")
async def retrieve_models(request):
print("/iw/api/ollama/models")
async with aiohttp.ClientSession() as session:
async with session.get(f'http://{OLLAMA_HOST}/api/tags') as response:
body = await response.json()
model_simple = [v["model"] for v in body["models"]]
return web.json_response({"status": "success", "models": model_simple}, status=200)
print("/iw/api/ollama/models")
try:
async with aiohttp.ClientSession() as session:
async with session.get(f'http://{OLLAMA_HOST}/api/tags') as response:
body = await response.json()
model_simple = [v["model"] for v in body["models"]]
return web.json_response({"status": "success", "models": model_simple}, status=200)
except aiohttp.client_exceptions.ClientConnectorError as e:
return web.json_response({"status": "success", "models": ["ollama is down"]}, status=200)
return web.json_response({"status": "fail"}, status=500)
+26 -19
View File
@@ -5,45 +5,52 @@ import { ComfyWidgets } from "../../scripts/widgets.js";
app.registerExtension({
name: "iw.ollama.generate",
async setup(app) {
const resp = await api.fetchApi('/iw/api/ollama/models', { });
if (resp.status !== 200) {
const err = `Error uploading temp file: ${resp.status} - ${resp.statusText}`
useToastStore().addAlert(err)
throw new Error(err)
}
const result_in_json = await resp.json();
const model_list = result_in_json.models;
app._iw_ollama_generate_string = {
"model_list": model_list
};
},
async beforeRegisterNodeDef(nodeType, nodeData, app) {
let find_widget = (node, name) => node.widgets.find((w) => w.name === name);
let find_widget = (node, name) => node.widgets.find((w) => w.name === name);
let extension = this;
if (nodeData.name === "IW_OllamaGenerateString") {
const resp = await api.fetchApi('/iw/api/ollama/models', { });
if (resp.status !== 200) {
const err = `Error uploading temp file: ${resp.status} - ${resp.statusText}`
useToastStore().addAlert(err)
throw new Error(err)
}
const result_in_json = await resp.json();
const model_list = result_in_json.models;
console.log("model list:", model_list)
console.log("Something ollama thing");
nodeType.prototype.initState = function () {
console.log("hello");
};
nodeType.prototype.initWidgets = function (app) {
this.setupWidgets(model_list);
nodeType.prototype.initWidgets = function (app) {
this.setupWidgets(app._iw_ollama_generate_string.model_list);
};
nodeType.prototype.setupWidgets = function (app, models) {
nodeType.prototype.setupWidgets = function (model_list) {
let default_model = model_list[0];
this.addWidget(
"combo",
"model",
model_list[0],
default_model,
(value) => {
this.tool = value;
},
{
values: model_list,
default: model_list[0],
default: default_model,
}
);