Configurable ollama keep_alive Closes #1859

f305ba7ce7f7986e742f679491297a628c699472

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

2 files changed, +10 -2Ignore whitespace
default/config.yaml+7 -0
@@ -98,6 +98,13 @@ mistral:
98 # Enables prefilling of the reply with the last assistant message in the prompt98 # Enables prefilling of the reply with the last assistant message in the prompt
99 # CAUTION: The prefix is echoed into the completion. You may want to use regex to trim it out.99 # CAUTION: The prefix is echoed into the completion. You may want to use regex to trim it out.
100 enablePrefix: false100 enablePrefix: false
101# -- OLLAMA API CONFIGURATION --
102ollama:
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
101# -- SERVER PLUGIN CONFIGURATION --108# -- SERVER PLUGIN CONFIGURATION --
102enableServerPlugins: false109enableServerPlugins: false
103# User session timeout *in seconds* (defaults to 24 hours).110# 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
6const { jsonParser } = require('../../express-common');6const { jsonParser } = require('../../express-common');
7const { TEXTGEN_TYPES, TOGETHERAI_KEYS, OLLAMA_KEYS, INFERMATICAI_KEYS, OPENROUTER_KEYS, VLLM_KEYS, DREAMGEN_KEYS, FEATHERLESS_KEYS } = require('../../constants');7const { TEXTGEN_TYPES, TOGETHERAI_KEYS, OLLAMA_KEYS, INFERMATICAI_KEYS, OPENROUTER_KEYS, VLLM_KEYS, DREAMGEN_KEYS, FEATHERLESS_KEYS } = require('../../constants');
8const { forwardFetchResponse, trimV1 } = require('../../util');8const { forwardFetchResponse, trimV1, getConfigValue } = require('../../util');
9const { setAdditionalHeaders } = require('../../additional-headers');9const { setAdditionalHeaders } = require('../../additional-headers');
1010
11const router = express.Router();11const router = express.Router();
@@ -325,11 +325,12 @@ router.post('/generate', jsonParser, async function (request, response) {
325 }325 }
326326
327 if (request.body.api_type === TEXTGEN_TYPES.OLLAMA) {327 if (request.body.api_type === TEXTGEN_TYPES.OLLAMA) {
328 const keepAlive = getConfigValue('ollama.keepAlive', -1);
328 args.body = JSON.stringify({329 args.body = JSON.stringify({
329 model: request.body.model,330 model: request.body.model,
330 prompt: request.body.prompt,331 prompt: request.body.prompt,
331 stream: request.body.stream ?? false,332 stream: request.body.stream ?? false,
332 keep_alive: -1,333 keep_alive: keepAlive,
333 raw: true,334 raw: true,
334 options: _.pickBy(request.body, (_, key) => OLLAMA_KEYS.includes(key)),335 options: _.pickBy(request.body, (_, key) => OLLAMA_KEYS.includes(key)),
335 });336 });