Configurable ollama keep_alive Closes #1859

f305ba7ce7f7986e742f679491297a628c699472

Cohee <18619528+Cohee1207@users.noreply.github.com>

2 files changed, +10 -2Showing whitespace changes
default/config.yaml+7 -0
@@ -98,6 +98,13 @@ mistral:
9898 # Enables prefilling of the reply with the last assistant message in the prompt
9999 # CAUTION: The prefix is echoed into the completion. You may want to use regex to trim it out.
100100 enablePrefix: false
101+# -- OLLAMA API CONFIGURATION --
102+ollama:
103+ # Controls how long the model will stay loaded into memory following the request
104+ # * -1: Keep the model loaded indefinitely
105+ # * 0: Unload the model immediately after the request
106+ # * 5m: Keep the model loaded for 5 minutes after the request. Accepts duration strings (e.g. 5h30m40s)
107+ keepAlive: -1
101108# -- SERVER PLUGIN CONFIGURATION --
102109enableServerPlugins: false
103110# User session timeout *in seconds* (defaults to 24 hours).
src/endpoints/backends/text-completions.js+3 -2
@@ -5,7 +5,7 @@ const Readable = require('stream').Readable;
55
66const { jsonParser } = require('../../express-common');
77const { TEXTGEN_TYPES, TOGETHERAI_KEYS, OLLAMA_KEYS, INFERMATICAI_KEYS, OPENROUTER_KEYS, VLLM_KEYS, DREAMGEN_KEYS, FEATHERLESS_KEYS } = require('../../constants');
88const { forwardFetchResponse, trimV1, getConfigValue } = require('../../util');
99const { setAdditionalHeaders } = require('../../additional-headers');
1010
1111const router = express.Router();
@@ -325,11 +325,12 @@ router.post('/generate', jsonParser, async function (request, response) {
325325 }
326326
327327 if (request.body.api_type === TEXTGEN_TYPES.OLLAMA) {
328+ const keepAlive = getConfigValue('ollama.keepAlive', -1);
328329 args.body = JSON.stringify({
329330 model: request.body.model,
330331 prompt: request.body.prompt,
331332 stream: request.body.stream ?? false,
332333 keep_alive: -1keepAlive,
333334 raw: true,
334335 options: _.pickBy(request.body, (_, key) => OLLAMA_KEYS.includes(key)),
335336 });