From 65c7c0a5184286561a2c9a3727fce04df118ea93 Mon Sep 17 00:00:00 2001 From: Timothy Carambat Date: Sat, 16 Dec 2023 10:21:36 -0800 Subject: [PATCH] fix: patch api key not persisting when setting LLM/Embedder (#458) --- .../components/EmbeddingSelection/LocalAiOptions/index.jsx | 6 +++++- .../src/components/LLMSelection/LocalAiOptions/index.jsx | 6 +++++- .../src/components/LLMSelection/OpenAiOptions/index.jsx | 5 +---- server/utils/helpers/customModels.js | 6 +++++- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/frontend/src/components/EmbeddingSelection/LocalAiOptions/index.jsx b/frontend/src/components/EmbeddingSelection/LocalAiOptions/index.jsx index 5871e288..2b976e14 100644 --- a/frontend/src/components/EmbeddingSelection/LocalAiOptions/index.jsx +++ b/frontend/src/components/EmbeddingSelection/LocalAiOptions/index.jsx @@ -91,7 +91,11 @@ function LocalAIModelSelection({ settings, apiKey = null, basePath = null }) { return; } setLoading(true); - const { models } = await System.customModels("localai", apiKey, basePath); + const { models } = await System.customModels( + "localai", + typeof apiKey === "boolean" ? null : apiKey, + basePath + ); setCustomModels(models || []); setLoading(false); } diff --git a/frontend/src/components/LLMSelection/LocalAiOptions/index.jsx b/frontend/src/components/LLMSelection/LocalAiOptions/index.jsx index 846c3783..7a385278 100644 --- a/frontend/src/components/LLMSelection/LocalAiOptions/index.jsx +++ b/frontend/src/components/LLMSelection/LocalAiOptions/index.jsx @@ -108,7 +108,11 @@ function LocalAIModelSelection({ settings, basePath = null, apiKey = null }) { return; } setLoading(true); - const { models } = await System.customModels("localai", apiKey, basePath); + const { models } = await System.customModels( + "localai", + typeof apiKey === "boolean" ? null : apiKey, + basePath + ); setCustomModels(models || []); setLoading(false); } diff --git a/frontend/src/components/LLMSelection/OpenAiOptions/index.jsx b/frontend/src/components/LLMSelection/OpenAiOptions/index.jsx index d9ca3f29..fd2f8766 100644 --- a/frontend/src/components/LLMSelection/OpenAiOptions/index.jsx +++ b/frontend/src/components/LLMSelection/OpenAiOptions/index.jsx @@ -4,9 +4,6 @@ import System from "@/models/system"; export default function OpenAiOptions({ settings }) { const [inputValue, setInputValue] = useState(settings?.OpenAiKey); const [openAIKey, setOpenAIKey] = useState(settings?.OpenAiKey); - function updateOpenAiKey() { - setOpenAIKey(inputValue); - } return ( <> @@ -24,7 +21,7 @@ export default function OpenAiOptions({ settings }) { autoComplete="off" spellCheck={false} onChange={(e) => setInputValue(e.target.value)} - onBlur={updateOpenAiKey} + onBlur={() => setOpenAIKey(inputValue)} /> diff --git a/server/utils/helpers/customModels.js b/server/utils/helpers/customModels.js index 03e37377..3b4397c3 100644 --- a/server/utils/helpers/customModels.js +++ b/server/utils/helpers/customModels.js @@ -34,6 +34,8 @@ async function openAiModels(apiKey = null) { (model) => !model.owned_by.includes("openai") && model.owned_by !== "system" ); + // Api Key was successful so lets save it for future uses + if (models.length > 0 && !!apiKey) process.env.OPEN_AI_KEY = apiKey; return { models, error: null }; } @@ -41,7 +43,7 @@ async function localAIModels(basePath = null, apiKey = null) { const { Configuration, OpenAIApi } = require("openai"); const config = new Configuration({ basePath, - ...(!!apiKey ? { apiKey } : {}), + apiKey: apiKey || process.env.LOCAL_AI_API_KEY, }); const openai = new OpenAIApi(config); const models = await openai @@ -52,6 +54,8 @@ async function localAIModels(basePath = null, apiKey = null) { return []; }); + // Api Key was successful so lets save it for future uses + if (models.length > 0 && !!apiKey) process.env.LOCAL_AI_API_KEY = apiKey; return { models, error: null }; }