quivr/frontend/lib/context/BrainConfigProvider/brain-config-provider.tsx
Cezar Vasconcelos 3b7390dd61
Feat/add new openai llm models (#334)
* feat(frontend): update OpenAi models and maxToken select's

* fix: update openai model to use the most recent

* fix: adjust max token for gpt-3 turbo

* fix: duplicating models

* fix: openai model type list
2023-06-14 21:30:52 +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-0613",
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>
);
};