Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add keep alive strategy for ollama api #391

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion sample.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ ANTHROPIC = "" # Anthropic API key - sk-ant-1234567890abcdef1234567890abcdef

[API_ENDPOINTS]
SEARXNG = "http://localhost:32768" # SearxNG API URL
OLLAMA = "" # Ollama API URL - http://host.docker.internal:11434
OLLAMA = "" # Ollama API URL - http://host.docker.internal:11434

[OLLAMA]
KEEP_ALIVE = "5m"
5 changes: 5 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ interface Config {
GROQ: string;
ANTHROPIC: string;
};
OLLAMA: {
KEEP_ALIVE: string;
};
API_ENDPOINTS: {
SEARXNG: string;
OLLAMA: string;
Expand Down Expand Up @@ -45,6 +48,8 @@ export const getSearxngApiEndpoint = () =>

export const getOllamaApiEndpoint = () => loadConfig().API_ENDPOINTS.OLLAMA;

export const getOllamaKeepAliveStrategy = () => loadConfig().OLLAMA.KEEP_ALIVE;

export const updateConfig = (config: RecursivePartial<Config>) => {
const currentConfig = loadConfig();

Expand Down
4 changes: 3 additions & 1 deletion src/lib/providers/ollama.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OllamaEmbeddings } from '@langchain/community/embeddings/ollama';
import { getOllamaApiEndpoint } from '../../config';
import { getOllamaApiEndpoint, getOllamaKeepAliveStrategy } from '../../config';
import logger from '../../utils/logger';
import { ChatOllama } from '@langchain/community/chat_models/ollama';

Expand All @@ -24,6 +24,7 @@ export const loadOllamaChatModels = async () => {
baseUrl: ollamaEndpoint,
model: model.model,
temperature: 0.7,
keepAlive: getOllamaKeepAliveStrategy(),
}),
};

Expand Down Expand Up @@ -57,6 +58,7 @@ export const loadOllamaEmbeddingsModels = async () => {
model: new OllamaEmbeddings({
baseUrl: ollamaEndpoint,
model: model.model,
keepAlive: getOllamaKeepAliveStrategy(),
}),
};

Expand Down
5 changes: 5 additions & 0 deletions src/routes/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getAnthropicApiKey,
getOpenaiApiKey,
updateConfig,
getOllamaKeepAliveStrategy,
} from '../config';
import logger from '../utils/logger';

Expand Down Expand Up @@ -50,6 +51,7 @@ router.get('/', async (_, res) => {

config['openaiApiKey'] = getOpenaiApiKey();
config['ollamaApiUrl'] = getOllamaApiEndpoint();
config['ollamaKeepAliveStrategy'] = getOllamaKeepAliveStrategy();
config['anthropicApiKey'] = getAnthropicApiKey();
config['groqApiKey'] = getGroqApiKey();

Expand All @@ -72,6 +74,9 @@ router.post('/', async (req, res) => {
API_ENDPOINTS: {
OLLAMA: config.ollamaApiUrl,
},
OLLAMA: {
KEEP_ALIVE: config.ollamaKeepAliveStrategy,
},
};

updateConfig(updatedConfig);
Expand Down
50 changes: 50 additions & 0 deletions ui/components/SettingsDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ interface SettingsType {
embeddingModelProviders: {
[key: string]: [Record<string, any>];
};
ollamaKeepAliveStrategy: string;
openaiApiKey: string;
groqApiKey: string;
anthropicApiKey: string;
Expand All @@ -78,6 +79,10 @@ const SettingsDialog = ({
const [selectedChatModel, setSelectedChatModel] = useState<string | null>(
null,
);

const [selectedOllamaKeepAliveStrategy, setSelectedOllamaKeepAliveStrategy] =
useState<string | null>(null);

const [selectedEmbeddingModelProvider, setSelectedEmbeddingModelProvider] =
useState<string | null>(null);
const [selectedEmbeddingModel, setSelectedEmbeddingModel] = useState<
Expand Down Expand Up @@ -124,6 +129,10 @@ const SettingsDialog = ({
(data.chatModelProviders &&
data.chatModelProviders[chatModelProvider]?.[0].name) ||
'';
const ollamaKeepAliveStrategy =
localStorage.getItem('ollamaKeepAliveStrategy') ||
data.ollamaKeepAliveStrategy ||
'';
const embeddingModelProvider =
localStorage.getItem('embeddingModelProvider') ||
defaultEmbeddingModelProvider ||
Expand All @@ -136,6 +145,7 @@ const SettingsDialog = ({

setSelectedChatModelProvider(chatModelProvider);
setSelectedChatModel(chatModel);
setSelectedOllamaKeepAliveStrategy(ollamaKeepAliveStrategy);
setSelectedEmbeddingModelProvider(embeddingModelProvider);
setSelectedEmbeddingModel(embeddingModel);
setCustomOpenAIApiKey(localStorage.getItem('openAIApiKey') || '');
Expand Down Expand Up @@ -164,6 +174,10 @@ const SettingsDialog = ({

localStorage.setItem('chatModelProvider', selectedChatModelProvider!);
localStorage.setItem('chatModel', selectedChatModel!);
localStorage.setItem(
'ollamaKeepAliveStrategy',
selectedOllamaKeepAliveStrategy!,
);
localStorage.setItem(
'embeddingModelProvider',
selectedEmbeddingModelProvider!,
Expand Down Expand Up @@ -293,6 +307,42 @@ const SettingsDialog = ({
/>
</div>
)}

{selectedChatModelProvider &&
selectedChatModelProvider === 'ollama' && (
<div className="flex flex-col space-y-1">
<p className="text-black/70 dark:text-white/70 text-sm">
KeepAlive Strategy
</p>
<Select
value={selectedOllamaKeepAliveStrategy ?? undefined}
onChange={(e) => {
setSelectedOllamaKeepAliveStrategy(
e.target.value,
);
setConfig({
...config,
ollamaKeepAliveStrategy: e.target.value,
});
}}
options={[
{
value: '5m',
label: '5 Minutes',
},
{
value: '60m',
label: '1 Hour',
},
{
value: '-1m',
label: 'Forever',
},
]}
/>
</div>
)}

{selectedChatModelProvider &&
selectedChatModelProvider === 'custom_openai' && (
<>
Expand Down