quivr/frontend/lib/context/BrainConfigProvider/brain-config-provider.tsx
Mamadou DICKO 6ff9309082
Feat/model config (#223)
* feat(axios): add global manager

* feat: add config page

* feat(axios): add backendUrl overwrite

* feat(brainConfig): add supabase url overwrite

* feat(chat): change model config logic + add more model

* feat: add openai and anthropic api key overwrite

* feat(config): save config in local storage

* feat(config): add reset button

* feat: move vertexai to config page

* ui: add brain config icon
2023-06-02 17:01:49 +02:00

63 lines
1.5 KiB
TypeScript

"use client";
import { setEmptyStringsUndefined } from "@/lib/helpers/setEmptyStringsUndefined";
import { createContext, useEffect, useState } from "react";
import {
getBrainConfigFromLocalStorage,
saveBrainConfigInLocalStorage,
} from "./helpers/brainConfigLocalStorage";
import { BrainConfig, ConfigContext } from "./types";
export const BrainConfigContext = createContext<ConfigContext | undefined>(
undefined
);
const defaultBrainConfig: BrainConfig = {
model: "gpt-3.5-turbo",
temperature: 0,
maxTokens: 500,
keepLocal: true,
anthropicKey: undefined,
backendUrl: undefined,
openAiKey: undefined,
supabaseKey: undefined,
supabaseUrl: undefined,
};
export const BrainConfigProvider = ({
children,
}: {
children: React.ReactNode;
}) => {
const [brainConfig, setBrainConfig] =
useState<BrainConfig>(defaultBrainConfig);
const updateConfig = (newConfig: Partial<BrainConfig>) => {
setBrainConfig((config) => {
const updatedConfig: BrainConfig = {
...config,
...setEmptyStringsUndefined(newConfig),
};
saveBrainConfigInLocalStorage(updatedConfig);
return updatedConfig;
});
};
const resetConfig = () => {
updateConfig(defaultBrainConfig);
};
useEffect(() => {
setBrainConfig(getBrainConfigFromLocalStorage() ?? defaultBrainConfig);
}, []);
return (
<BrainConfigContext.Provider
value={{ config: brainConfig, updateConfig, resetConfig }}
>
{children}
</BrainConfigContext.Provider>
);
};