From ebbc2fd55df1fc43e1eacceba042fa4c0bc38e77 Mon Sep 17 00:00:00 2001 From: Test Date: Fri, 20 Sep 2024 13:46:26 -0700 Subject: [PATCH 1/3] fix: use exponential backoff when checking ollama --- .../OnboardingCard/hooks/useCheckOllamaModels.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/gui/src/components/OnboardingCard/hooks/useCheckOllamaModels.ts b/gui/src/components/OnboardingCard/hooks/useCheckOllamaModels.ts index e763c0d9a..7a3205627 100644 --- a/gui/src/components/OnboardingCard/hooks/useCheckOllamaModels.ts +++ b/gui/src/components/OnboardingCard/hooks/useCheckOllamaModels.ts @@ -1,6 +1,7 @@ import { useContext, useEffect } from "react"; import { IdeMessengerContext } from "../../../context/IdeMessenger"; import { ONBOARDING_LOCAL_MODEL_TITLE } from "core/config/onboarding"; +import { withExponentialBackoff } from "core/util/withExponentialBackoff"; const CHECK_OLLAMA_CONNECTION_INTERVAL = 1000; @@ -11,9 +12,11 @@ export const useCheckOllamaModels = ( useEffect(() => { const fetchDownloadedModels = async () => { - const result = (await ideMessenger.request("llm/listModels", { - title: ONBOARDING_LOCAL_MODEL_TITLE, - })) as any; + const result = await withExponentialBackoff(() => + ideMessenger.request("llm/listModels", { + title: ONBOARDING_LOCAL_MODEL_TITLE, + }) + ); if (result.status === "success") { const models = result.content; From cd93dbf1564a86ddc336402341fda27ba7086ba0 Mon Sep 17 00:00:00 2001 From: Test Date: Sat, 21 Sep 2024 09:27:27 -0700 Subject: [PATCH 2/3] WIP: Exponential backoff for ollama --- .../customize/model-types/autocomplete.md | 2 +- .../components/OllamaStatus.tsx | 16 +++--- .../hooks/useCheckOllamaModels.ts | 52 ++++++++++++------- .../tabs/OnboardingLocalTab.tsx | 7 +-- gui/src/components/OnboardingCard/utils.ts | 2 +- gui/src/components/index.ts | 4 ++ 6 files changed, 47 insertions(+), 36 deletions(-) diff --git a/docs/docs/customize/model-types/autocomplete.md b/docs/docs/customize/model-types/autocomplete.md index a21f6be87..8ecac28e6 100644 --- a/docs/docs/customize/model-types/autocomplete.md +++ b/docs/docs/customize/model-types/autocomplete.md @@ -7,7 +7,7 @@ sidebar_position: 1 An "autocomplete model" is an LLM that is trained on a special format called fill-in-the-middle (FIM). This format is designed to be given the prefix and suffix of a code file and predict what goes between. This task is very specific, which on one hand means that the models can be smaller (even a 3B parameter model can perform well). On the other hand, this means that Chat models, though larger, will perform poorly. -In Continue, these models are used to display inline [Autocomplete](../../chat/how-to-use-it.md) suggestions as you type. +In Continue, these models are used to display inline [Autocomplete](../../autocomplete/how-to-use-it.md) suggestions as you type. ## Recommended Autocomplete models diff --git a/gui/src/components/OnboardingCard/components/OllamaStatus.tsx b/gui/src/components/OnboardingCard/components/OllamaStatus.tsx index 546ddff5d..9cc323ec6 100644 --- a/gui/src/components/OnboardingCard/components/OllamaStatus.tsx +++ b/gui/src/components/OnboardingCard/components/OllamaStatus.tsx @@ -5,31 +5,27 @@ import { import { useState, useContext } from "react"; import { IdeMessengerContext } from "../../../context/IdeMessenger"; import { providers } from "../../../pages/AddNewModel/configs/providers"; -import { useCheckOllamaModels } from "../hooks/useCheckOllamaModels"; import { StyledActionButton } from "../.."; import OllamaCompletedStep from "./OllamaCompletedStep"; import { OllamaConnectionStatuses } from "../utils"; interface OllamaStatusProps { - onConnectionVerified: () => void; + isOllamaConnected: boolean; } const { ollama: { downloadUrl }, } = providers; -export function OllamaStatus({ onConnectionVerified }: OllamaStatusProps) { +export function OllamaStatus({ isOllamaConnected }: OllamaStatusProps) { const ideMessenger = useContext(IdeMessengerContext); const [status, setStatus] = useState( - OllamaConnectionStatuses.WaitingToDownload, + isOllamaConnected + ? OllamaConnectionStatuses.Connected + : OllamaConnectionStatuses.WaitingToDownload, ); - useCheckOllamaModels((models) => { - setStatus(OllamaConnectionStatuses.Verified); - onConnectionVerified(); - }); - function onClickDownload() { ideMessenger.post("openUrl", downloadUrl); setStatus(OllamaConnectionStatuses.Downloading); @@ -52,7 +48,7 @@ export function OllamaStatus({ onConnectionVerified }: OllamaStatusProps) { ); - case OllamaConnectionStatuses.Verified: + case OllamaConnectionStatuses.Connected: return ( ); diff --git a/gui/src/components/OnboardingCard/hooks/useCheckOllamaModels.ts b/gui/src/components/OnboardingCard/hooks/useCheckOllamaModels.ts index 7a3205627..21bafe384 100644 --- a/gui/src/components/OnboardingCard/hooks/useCheckOllamaModels.ts +++ b/gui/src/components/OnboardingCard/hooks/useCheckOllamaModels.ts @@ -1,43 +1,57 @@ -import { useContext, useEffect } from "react"; +import { useContext, useEffect, useRef } from "react"; import { IdeMessengerContext } from "../../../context/IdeMessenger"; import { ONBOARDING_LOCAL_MODEL_TITLE } from "core/config/onboarding"; -import { withExponentialBackoff } from "core/util/withExponentialBackoff"; -const CHECK_OLLAMA_CONNECTION_INTERVAL = 1000; +const MAX_RETRIES = 10; +const INITIAL_DELAY = 1000; export const useCheckOllamaModels = ( onDownloadedModels: (models: string[]) => void, ) => { const ideMessenger = useContext(IdeMessengerContext); + const retryCountRef = useRef(0); + const timeoutRef = useRef(null); useEffect(() => { const fetchDownloadedModels = async () => { - const result = await withExponentialBackoff(() => - ideMessenger.request("llm/listModels", { + try { + const result = (await ideMessenger.request("llm/listModels", { title: ONBOARDING_LOCAL_MODEL_TITLE, - }) - ); + })) as any; - if (result.status === "success") { - const models = result.content; + if (result.status === "success") { + const models = result.content; - if (Array.isArray(models)) { - onDownloadedModels(models); - clearInterval(interval); + if (Array.isArray(models)) { + onDownloadedModels(models); + return; + } + } + + throw new Error("Failed to fetch models"); + } catch (error) { + if (retryCountRef.current < MAX_RETRIES) { + const delay = INITIAL_DELAY * Math.pow(2, retryCountRef.current); + console.debug( + `Failed to connect to Ollama - retrying in ${delay}ms...`, + ); + timeoutRef.current = setTimeout(() => { + retryCountRef.current += 1; + fetchDownloadedModels(); + }, delay); + } else { + console.debug( + `Max retries (${MAX_RETRIES}) reached. Unable to fetch Ollama models.`, + ); } } }; fetchDownloadedModels(); - const interval = setInterval( - fetchDownloadedModels, - CHECK_OLLAMA_CONNECTION_INTERVAL, - ); - return () => { - if (interval) { - clearInterval(interval); + if (timeoutRef.current) { + clearTimeout(timeoutRef.current); } }; }, [ideMessenger, onDownloadedModels]); diff --git a/gui/src/components/OnboardingCard/tabs/OnboardingLocalTab.tsx b/gui/src/components/OnboardingCard/tabs/OnboardingLocalTab.tsx index 03d940163..96753c59e 100644 --- a/gui/src/components/OnboardingCard/tabs/OnboardingLocalTab.tsx +++ b/gui/src/components/OnboardingCard/tabs/OnboardingLocalTab.tsx @@ -54,17 +54,14 @@ function OnboardingLocalTab() { useCheckOllamaModels((models) => { setDownloadedOllamaModels(models); - }); - - function onConnectionVerified() { setIsOllamaConnected(true); - } + }); return (

Install Ollama

- +
Date: Sun, 22 Sep 2024 20:03:51 -0700 Subject: [PATCH 3/3] check ollama status every 3s --- .../components/OnboardingCard/hooks/index.ts | 2 +- .../hooks/useCheckOllamaModels.ts | 58 ------------------- .../tabs/OnboardingLocalTab.tsx | 36 ++++++++++-- 3 files changed, 32 insertions(+), 64 deletions(-) delete mode 100644 gui/src/components/OnboardingCard/hooks/useCheckOllamaModels.ts diff --git a/gui/src/components/OnboardingCard/hooks/index.ts b/gui/src/components/OnboardingCard/hooks/index.ts index 88f66d231..f2340f5f6 100644 --- a/gui/src/components/OnboardingCard/hooks/index.ts +++ b/gui/src/components/OnboardingCard/hooks/index.ts @@ -1,2 +1,2 @@ -export * from "./useCheckOllamaModels"; export * from "./useSubmitOnboarding"; +export * from "./useOnboardingCard"; diff --git a/gui/src/components/OnboardingCard/hooks/useCheckOllamaModels.ts b/gui/src/components/OnboardingCard/hooks/useCheckOllamaModels.ts deleted file mode 100644 index 21bafe384..000000000 --- a/gui/src/components/OnboardingCard/hooks/useCheckOllamaModels.ts +++ /dev/null @@ -1,58 +0,0 @@ -import { useContext, useEffect, useRef } from "react"; -import { IdeMessengerContext } from "../../../context/IdeMessenger"; -import { ONBOARDING_LOCAL_MODEL_TITLE } from "core/config/onboarding"; - -const MAX_RETRIES = 10; -const INITIAL_DELAY = 1000; - -export const useCheckOllamaModels = ( - onDownloadedModels: (models: string[]) => void, -) => { - const ideMessenger = useContext(IdeMessengerContext); - const retryCountRef = useRef(0); - const timeoutRef = useRef(null); - - useEffect(() => { - const fetchDownloadedModels = async () => { - try { - const result = (await ideMessenger.request("llm/listModels", { - title: ONBOARDING_LOCAL_MODEL_TITLE, - })) as any; - - if (result.status === "success") { - const models = result.content; - - if (Array.isArray(models)) { - onDownloadedModels(models); - return; - } - } - - throw new Error("Failed to fetch models"); - } catch (error) { - if (retryCountRef.current < MAX_RETRIES) { - const delay = INITIAL_DELAY * Math.pow(2, retryCountRef.current); - console.debug( - `Failed to connect to Ollama - retrying in ${delay}ms...`, - ); - timeoutRef.current = setTimeout(() => { - retryCountRef.current += 1; - fetchDownloadedModels(); - }, delay); - } else { - console.debug( - `Max retries (${MAX_RETRIES}) reached. Unable to fetch Ollama models.`, - ); - } - } - }; - - fetchDownloadedModels(); - - return () => { - if (timeoutRef.current) { - clearTimeout(timeoutRef.current); - } - }; - }, [ideMessenger, onDownloadedModels]); -}; diff --git a/gui/src/components/OnboardingCard/tabs/OnboardingLocalTab.tsx b/gui/src/components/OnboardingCard/tabs/OnboardingLocalTab.tsx index 96753c59e..5dab35f7b 100644 --- a/gui/src/components/OnboardingCard/tabs/OnboardingLocalTab.tsx +++ b/gui/src/components/OnboardingCard/tabs/OnboardingLocalTab.tsx @@ -13,7 +13,9 @@ import { hasPassedFTL } from "../../../util/freeTrial"; import AddModelButtonSubtext from "../../AddModelButtonSubtext"; import OllamaModelDownload from "../components/OllamaModelDownload"; import { OllamaStatus } from "../components/OllamaStatus"; -import { useCheckOllamaModels, useSubmitOnboarding } from "../hooks"; +import { useSubmitOnboarding } from "../hooks"; + +const OLLAMA_CHECK_INTERVAL_MS = 3000; function OnboardingLocalTab() { const dispatch = useDispatch(); @@ -52,10 +54,34 @@ function OnboardingLocalTab() { } }, [downloadedOllamaModels, isOllamaConnected]); - useCheckOllamaModels((models) => { - setDownloadedOllamaModels(models); - setIsOllamaConnected(true); - }); + useEffect(() => { + const fetchDownloadedModels = async () => { + try { + const result = (await ideMessenger.request("llm/listModels", { + title: ONBOARDING_LOCAL_MODEL_TITLE, + })) as any; + + if (result.status === "success" && Array.isArray(result.content)) { + setDownloadedOllamaModels(result.content); + setIsOllamaConnected(true); + } else { + throw new Error("Failed to fetch models"); + } + } catch (error) { + console.error("Error fetching models:", error); + setIsOllamaConnected(false); + } + }; + + const intervalId = setInterval( + fetchDownloadedModels, + OLLAMA_CHECK_INTERVAL_MS, + ); + + fetchDownloadedModels(); + + return () => clearInterval(intervalId); + }, []); return (