|
| 1 | +import { createSlice, PayloadAction } from "@reduxjs/toolkit"; |
| 2 | +import { |
| 3 | + AI_MODELS, |
| 4 | + initOllamaOptions, |
| 5 | + IOllamaOptions, |
| 6 | +} from "../../types/common"; |
| 7 | + |
| 8 | +export interface ISettingsSlice { |
| 9 | + ollamaOptions: IOllamaOptions; |
| 10 | + model: AI_MODELS; |
| 11 | + stream: boolean; |
| 12 | +} |
| 13 | + |
| 14 | +export const initialState = { |
| 15 | + ollamaOptions: initOllamaOptions, |
| 16 | + model: AI_MODELS.LLAMA3, |
| 17 | + stream: true, |
| 18 | +} as ISettingsSlice; |
| 19 | + |
| 20 | +export const settingsSliceSlice = createSlice({ |
| 21 | + name: "settingsSlice", |
| 22 | + initialState, |
| 23 | + reducers: { |
| 24 | + settingsSetModel: ( |
| 25 | + state: ISettingsSlice, |
| 26 | + action: PayloadAction<AI_MODELS> |
| 27 | + ) => { |
| 28 | + state.model = action.payload; |
| 29 | + }, |
| 30 | + settingsSetStream: ( |
| 31 | + state: ISettingsSlice, |
| 32 | + action: PayloadAction<boolean> |
| 33 | + ) => { |
| 34 | + state.stream = action.payload; |
| 35 | + }, |
| 36 | + settingsSetOllamaOptions: ( |
| 37 | + state: ISettingsSlice, |
| 38 | + action: PayloadAction<Partial<IOllamaOptions>> |
| 39 | + ) => { |
| 40 | + const { |
| 41 | + num_ctx, |
| 42 | + seed, |
| 43 | + temperature, |
| 44 | + f16_kv, |
| 45 | + frequency_penalty, |
| 46 | + low_vram, |
| 47 | + main_gpu, |
| 48 | + mirostat, |
| 49 | + mirostat_eta, |
| 50 | + mirostat_tau, |
| 51 | + num_batch, |
| 52 | + num_gpu, |
| 53 | + num_keep, |
| 54 | + num_predict, |
| 55 | + num_thread, |
| 56 | + numa, |
| 57 | + penalize_newline, |
| 58 | + presence_penalty, |
| 59 | + repeat_last_n, |
| 60 | + repeat_penalty, |
| 61 | + stop, |
| 62 | + tfs_z, |
| 63 | + top_k, |
| 64 | + top_p, |
| 65 | + typical_p, |
| 66 | + use_mlock, |
| 67 | + use_mmap, |
| 68 | + vocab_only, |
| 69 | + } = action.payload; |
| 70 | + |
| 71 | + if (typeof num_ctx === "number") state.ollamaOptions.num_ctx = num_ctx; |
| 72 | + if (typeof seed === "number") state.ollamaOptions.seed = seed; |
| 73 | + if (typeof temperature === "number") |
| 74 | + state.ollamaOptions.temperature = temperature; |
| 75 | + if (typeof f16_kv === "boolean") state.ollamaOptions.f16_kv = f16_kv; |
| 76 | + if (typeof frequency_penalty === "number") |
| 77 | + state.ollamaOptions.frequency_penalty = frequency_penalty; |
| 78 | + if (typeof low_vram === "boolean") |
| 79 | + state.ollamaOptions.low_vram = low_vram; |
| 80 | + if (typeof main_gpu === "number") state.ollamaOptions.main_gpu = main_gpu; |
| 81 | + if (typeof mirostat === "number" && mirostat !== undefined) |
| 82 | + state.ollamaOptions.mirostat = mirostat; |
| 83 | + if (typeof mirostat_eta === "number") |
| 84 | + state.ollamaOptions.mirostat_eta = mirostat_eta; |
| 85 | + if (typeof mirostat_tau === "number") |
| 86 | + state.ollamaOptions.mirostat_tau = mirostat_tau; |
| 87 | + if (typeof num_batch === "number") |
| 88 | + state.ollamaOptions.num_batch = num_batch; |
| 89 | + if (typeof num_gpu === "number") state.ollamaOptions.num_gpu = num_gpu; |
| 90 | + if (typeof num_keep === "number" && num_keep !== undefined) |
| 91 | + state.ollamaOptions.num_keep = num_keep; |
| 92 | + if (typeof num_predict === "number") |
| 93 | + state.ollamaOptions.num_predict = num_predict; |
| 94 | + if (typeof num_thread === "number") |
| 95 | + state.ollamaOptions.num_thread = num_thread; |
| 96 | + if (typeof numa === "boolean" && numa !== undefined) |
| 97 | + state.ollamaOptions.numa = numa; |
| 98 | + if ( |
| 99 | + typeof penalize_newline === "boolean" && |
| 100 | + penalize_newline !== undefined |
| 101 | + ) |
| 102 | + state.ollamaOptions.penalize_newline = penalize_newline; |
| 103 | + if (typeof presence_penalty === "number") |
| 104 | + state.ollamaOptions.presence_penalty = presence_penalty; |
| 105 | + if (typeof repeat_last_n === "number") |
| 106 | + state.ollamaOptions.repeat_last_n = repeat_last_n; |
| 107 | + if (typeof repeat_penalty === "number") |
| 108 | + state.ollamaOptions.repeat_penalty = repeat_penalty; |
| 109 | + if (Array.isArray(stop)) state.ollamaOptions.stop = stop; |
| 110 | + if (typeof tfs_z === "number") state.ollamaOptions.tfs_z = tfs_z; |
| 111 | + if (typeof top_k === "number") state.ollamaOptions.top_k = top_k; |
| 112 | + if (typeof top_p === "number") state.ollamaOptions.top_p = top_p; |
| 113 | + if (typeof typical_p === "number") |
| 114 | + state.ollamaOptions.typical_p = typical_p; |
| 115 | + if (typeof use_mlock === "boolean" && use_mlock !== undefined) |
| 116 | + state.ollamaOptions.use_mlock = use_mlock; |
| 117 | + if (typeof use_mmap === "boolean" && use_mmap !== undefined) |
| 118 | + state.ollamaOptions.use_mmap = use_mmap; |
| 119 | + if (typeof vocab_only === "boolean" && vocab_only !== undefined) |
| 120 | + state.ollamaOptions.vocab_only = vocab_only; |
| 121 | + }, |
| 122 | + }, |
| 123 | +}); |
| 124 | + |
| 125 | +// Action creators are generated for each case reducer function |
| 126 | +export const { settingsSetModel, settingsSetOllamaOptions, settingsSetStream } = |
| 127 | + settingsSliceSlice.actions; |
| 128 | + |
| 129 | +export default settingsSliceSlice.reducer; |
0 commit comments