Skip to content

Commit 263eb8d

Browse files
committed
context fixes
1 parent c10121f commit 263eb8d

3 files changed

Lines changed: 37 additions & 4 deletions

File tree

lib/ollama.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ async function getModelContextWindow(baseUrl, model) {
8686
const res = await fetch(`${cleanBase}/api/show`, {
8787
method: 'POST',
8888
headers: { 'Content-Type': 'application/json' },
89-
body: JSON.stringify({ name: model })
89+
body: JSON.stringify({ name: model, model })
9090
});
9191
if (!res.ok) throw new Error(`Ollama show error ${res.status}`);
9292
const data = await res.json();

public/config.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,26 @@
195195
setStatus('Model capability detected: max tokens set to ' + cap);
196196
}
197197

198+
async function fetchAndApplyModelCapability(modelName) {
199+
const key = (modelName || '').trim();
200+
if (!key) return;
201+
const url = mainUrlEl.value.trim() || 'http://localhost:11434';
202+
try {
203+
const res = await fetch('/api/ollama/model-capability?url=' + encodeURIComponent(url) + '&model=' + encodeURIComponent(key));
204+
const data = await res.json();
205+
if (!res.ok) throw new Error(data.error || res.statusText);
206+
const cap = Number(data.contextWindow || 0);
207+
if (!cap || cap <= 0) return;
208+
const numPredictEl = document.getElementById('ollamaNumPredict');
209+
if (!numPredictEl) return;
210+
numPredictEl.max = String(cap);
211+
numPredictEl.value = String(cap);
212+
setStatus('Model capability detected: max tokens set to ' + cap);
213+
} catch (_) {
214+
applyModelCapability(key);
215+
}
216+
}
217+
198218
if (repairProjectMemoryBtn) {
199219
repairProjectMemoryBtn.addEventListener('click', async () => {
200220
repairProjectMemoryBtn.disabled = true;
@@ -254,16 +274,16 @@
254274
mainModelList.addEventListener('change', () => {
255275
if (mainModelList.value) {
256276
mainModelEl.value = mainModelList.value;
257-
applyModelCapability(mainModelList.value);
277+
fetchAndApplyModelCapability(mainModelList.value);
258278
}
259279
});
260-
applyModelCapability(mainModelEl.value);
280+
fetchAndApplyModelCapability(mainModelEl.value);
261281
setStatus('Loaded ' + (data.models?.length || 0) + ' models');
262282
} catch (e) {
263283
setStatus(e.message, true);
264284
}
265285
});
266-
mainModelEl.addEventListener('change', () => applyModelCapability(mainModelEl.value));
286+
mainModelEl.addEventListener('change', () => fetchAndApplyModelCapability(mainModelEl.value));
267287

268288
function getRagFromDom() {
269289
return {

server.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,6 +1314,19 @@ app.get('/api/ollama/models', async (req, res) => {
13141314
}
13151315
});
13161316

1317+
app.get('/api/ollama/model-capability', async (req, res) => {
1318+
const url = req.query.url || getConfig().ollama.mainUrl;
1319+
const model = (req.query.model || '').toString().trim();
1320+
if (!model) return res.status(400).json({ error: 'model is required' });
1321+
try {
1322+
const contextWindow = await getModelContextWindow(url, model);
1323+
res.json({ model, contextWindow: Number(contextWindow) || 0 });
1324+
} catch (e) {
1325+
logger.warn('GET /api/ollama/model-capability:', e.message);
1326+
res.status(502).json({ error: e.message });
1327+
}
1328+
});
1329+
13171330
// ---------------------------------------------------------------------------
13181331
// Heartbeat
13191332
// ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)