quivr/frontend/lib/context/BrainConfigProvider/brain-config-provider.tsx
Stan Girard b76262c4f0
Fix a bunch of issues (#442)
* fix(tokens): overflow

* fix(chat): fixed deletion error of the chat

#441

* fix(default-brain): autoset default brain when on upload page

* fix(promptengineering): changed prompt a bit

* feat(growthbook): removed

* docs(readme): updated
2023-07-01 17:22:28 +02:00

66 lines
1.5 KiB
TypeScript

/* eslint-disable */
"use client";
import { createContext, useEffect, useState } from "react";
import { setEmptyStringsUndefined } from "@/lib/helpers/setEmptyStringsUndefined";
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-0613",
temperature: 0,
maxTokens: 256,
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>
);
};