Skip to content

Commit 9fa8ece

Browse files
committed
react redux implemented for state management.
settings state implemented.
1 parent c6e7f65 commit 9fa8ece

File tree

7 files changed

+298
-1
lines changed

7 files changed

+298
-1
lines changed

package-lock.json

+87
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
6+
"@reduxjs/toolkit": "^2.2.6",
67
"@testing-library/jest-dom": "^5.17.0",
78
"@testing-library/react": "^13.4.0",
89
"@testing-library/user-event": "^13.5.0",
@@ -17,6 +18,7 @@
1718
"react-copy-to-clipboard": "^5.1.0",
1819
"react-dom": "^18.3.1",
1920
"react-icons": "^5.2.1",
21+
"react-redux": "^9.1.2",
2022
"react-scripts": "5.0.1",
2123
"styled-components": "^6.1.11",
2224
"typescript": "^4.9.5",

src/components/App.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { v4 as uuidv4 } from "uuid";
88
// @ts-ignore
99
import { Ollama } from "ollama/browser";
1010
import { Colors } from "../statics/Colors";
11+
import { useSelector } from "react-redux";
12+
import { RootState } from "../state/mainStore";
1113

1214
const AppEl = styled(Row)`
1315
width: 100svw;

src/index.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ import React from "react";
22
import ReactDOM from "react-dom/client";
33
import App from "./components/App";
44
import "./index.css";
5+
import { Provider } from "react-redux";
6+
import mainStore from "./state/mainStore";
57

68
const root = ReactDOM.createRoot(
79
document.getElementById("root") as HTMLElement
810
);
911
root.render(
1012
<React.StrictMode>
11-
<App />
13+
<Provider store={mainStore}>
14+
<App />
15+
</Provider>
1216
</React.StrictMode>
1317
);

src/state/mainStore.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { configureStore } from "@reduxjs/toolkit";
2+
import settingsSliceReducer from "./slices/settingsSlice";
3+
const mainStore = configureStore({
4+
reducer: {
5+
settings: settingsSliceReducer,
6+
},
7+
});
8+
9+
export default mainStore;
10+
11+
export type RootState = ReturnType<typeof mainStore.getState>;
12+
export type AppDispatch = typeof mainStore.dispatch;

src/state/slices/settingsSlice.ts

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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;

src/types/common.ts

+61
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,64 @@ export enum AI_MODELS {
1010
LLAMA3 = "llama3:8b",
1111
PHI3_MINI_4K_INSTRUCT_Q4 = "phi-3-mini-4k-instruct-q4:latest",
1212
}
13+
14+
export const initOllamaOptions: IOllamaOptions = {
15+
num_keep: 5,
16+
seed: 42,
17+
num_predict: 100,
18+
top_k: 20,
19+
top_p: 0.9,
20+
tfs_z: 0.5,
21+
typical_p: 0.7,
22+
repeat_last_n: 33,
23+
temperature: 0.8,
24+
repeat_penalty: 1.2,
25+
presence_penalty: 1.5,
26+
frequency_penalty: 1.0,
27+
mirostat: 1,
28+
mirostat_tau: 0.8,
29+
mirostat_eta: 0.6,
30+
penalize_newline: true,
31+
stop: ["\n", "user:"],
32+
numa: false,
33+
num_ctx: 1024,
34+
num_batch: 2,
35+
num_gpu: 1,
36+
main_gpu: 0,
37+
low_vram: false,
38+
f16_kv: true,
39+
vocab_only: false,
40+
use_mmap: true,
41+
use_mlock: false,
42+
num_thread: 8,
43+
};
44+
export interface IOllamaOptions {
45+
num_keep: number;
46+
seed: number;
47+
num_predict: number;
48+
top_k: number;
49+
top_p: number;
50+
tfs_z: number;
51+
typical_p: number;
52+
repeat_last_n: number;
53+
temperature: number;
54+
repeat_penalty: number;
55+
presence_penalty: number;
56+
frequency_penalty: number;
57+
mirostat: number;
58+
mirostat_tau: number;
59+
mirostat_eta: number;
60+
penalize_newline: true;
61+
stop: string[]; // ["\n", "user:"];
62+
numa: false;
63+
num_ctx: number;
64+
num_batch: number;
65+
num_gpu: number;
66+
main_gpu: number;
67+
low_vram: false;
68+
f16_kv: true;
69+
vocab_only: false;
70+
use_mmap: true;
71+
use_mlock: false;
72+
num_thread: number;
73+
}

0 commit comments

Comments
 (0)