From 9dff6f88591ccc91fbca0215d898a3c312db74d7 Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Sat, 9 Dec 2023 14:40:40 -0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20load=20unserialized=20config?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/commands/index.ts | 8 +- core/commands/{ => slash}/cmd.ts | 4 +- core/commands/{ => slash}/draftIssue.ts | 6 +- core/commands/slash/index.ts | 11 +++ core/commands/{ => slash}/share.ts | 2 +- core/commands/{ => slash}/stackOverflow.ts | 6 +- core/config/index.d.ts | 43 ++++++--- core/config/load.ts | 51 ++++++++++ core/context/providers/index.ts | 42 ++++---- core/llm/index.ts | 4 +- core/llm/llms/GooglePalm.ts | 2 +- core/llm/llms/LMStudio.ts | 5 +- core/llm/llms/index.ts | 46 +++++++-- gui/src/components/mainInput/ComboBox.tsx | 23 +++-- .../components/modelSelection/ModelSelect.tsx | 2 +- gui/src/hooks/CustomPostHogProvider.tsx | 7 +- gui/src/hooks/useChatHandler.ts | 96 +++++++++++++++++++ gui/src/hooks/useContextProviders.ts | 24 ++--- gui/src/hooks/useModels.ts | 71 -------------- gui/src/hooks/useSetup.ts | 2 +- gui/src/pages/gui.tsx | 29 +++--- gui/src/pages/settings.tsx | 6 +- gui/src/redux/selectors/configSelectors.ts | 21 ---- gui/src/redux/selectors/modelSelectors.ts | 10 ++ gui/src/redux/slices/serverStateReducer.ts | 14 ++- gui/src/redux/slices/stateSlice.ts | 21 +++- gui/src/redux/store.ts | 20 ++-- 27 files changed, 360 insertions(+), 216 deletions(-) rename core/commands/{ => slash}/cmd.ts (92%) rename core/commands/{ => slash}/draftIssue.ts (93%) create mode 100644 core/commands/slash/index.ts rename core/commands/{ => slash}/share.ts (95%) rename core/commands/{ => slash}/stackOverflow.ts (95%) create mode 100644 core/config/load.ts create mode 100644 gui/src/hooks/useChatHandler.ts delete mode 100644 gui/src/hooks/useModels.ts delete mode 100644 gui/src/redux/selectors/configSelectors.ts create mode 100644 gui/src/redux/selectors/modelSelectors.ts diff --git a/core/commands/index.ts b/core/commands/index.ts index 1935cc995..b0b492014 100644 --- a/core/commands/index.ts +++ b/core/commands/index.ts @@ -1,8 +1,8 @@ -import { CustomCommand } from "../config"; +import { CustomCommand, SlashCommandDescription } from "../config"; import { IDE } from "../ide/types"; import { LLM } from "../llm"; import { ChatMessage, ContextItem } from "../llm/types"; - +import SlashCommands from "./slash"; export interface ContinueSDK { ide: IDE; llm: LLM; @@ -44,3 +44,7 @@ export function slashFromCustomCommand( }, }; } + +export function slashCommandFromDescription(desc: SlashCommandDescription) { + return SlashCommands.find((cmd) => cmd.name === desc.name); +} diff --git a/core/commands/cmd.ts b/core/commands/slash/cmd.ts similarity index 92% rename from core/commands/cmd.ts rename to core/commands/slash/cmd.ts index 7a562ce10..7b620586e 100644 --- a/core/commands/cmd.ts +++ b/core/commands/slash/cmd.ts @@ -1,5 +1,5 @@ -import { SlashCommand } from "."; -import { removeQuotesAndEscapes } from "../util"; +import { SlashCommand } from ".."; +import { removeQuotesAndEscapes } from "../../util"; ` cmd = await sdk.models.default.complete( diff --git a/core/commands/draftIssue.ts b/core/commands/slash/draftIssue.ts similarity index 93% rename from core/commands/draftIssue.ts rename to core/commands/slash/draftIssue.ts index 703ba1815..e206b4d5d 100644 --- a/core/commands/draftIssue.ts +++ b/core/commands/slash/draftIssue.ts @@ -1,6 +1,6 @@ -import { ContinueSDK, SlashCommand } from "."; -import { ChatMessage } from "../llm/types"; -import { removeQuotesAndEscapes } from "../util"; +import { SlashCommand } from ".."; +import { ChatMessage } from "../../llm/types"; +import { removeQuotesAndEscapes } from "../../util"; const PROMPT = ( input: string, diff --git a/core/commands/slash/index.ts b/core/commands/slash/index.ts new file mode 100644 index 000000000..5e09ff205 --- /dev/null +++ b/core/commands/slash/index.ts @@ -0,0 +1,11 @@ +import GenerateTerminalCommand from "./cmd"; +import DraftIssueCommand from "./draftIssue"; +import ShareSlashCommand from "./share"; +import StackOverflowSlashCommand from "./stackOverflow"; + +export default [ + DraftIssueCommand, + ShareSlashCommand, + StackOverflowSlashCommand, + GenerateTerminalCommand, +]; diff --git a/core/commands/share.ts b/core/commands/slash/share.ts similarity index 95% rename from core/commands/share.ts rename to core/commands/slash/share.ts index abf38194e..ca9942810 100644 --- a/core/commands/share.ts +++ b/core/commands/slash/share.ts @@ -1,4 +1,4 @@ -import { SlashCommand } from "."; +import { SlashCommand } from ".."; const ShareSlashCommand: SlashCommand = { name: "share", diff --git a/core/commands/stackOverflow.ts b/core/commands/slash/stackOverflow.ts similarity index 95% rename from core/commands/stackOverflow.ts rename to core/commands/slash/stackOverflow.ts index 08bca8b9e..a8362b555 100644 --- a/core/commands/stackOverflow.ts +++ b/core/commands/slash/stackOverflow.ts @@ -1,6 +1,6 @@ -import { SlashCommand } from "."; -import { pruneStringFromBottom } from "../llm/countTokens"; -import { ChatMessageRole } from "../llm/types"; +import { SlashCommand } from ".."; +import { pruneStringFromBottom } from "../../llm/countTokens"; +import { ChatMessageRole } from "../../llm/types"; const SERVER_URL = "https://proxy-server-l6vsfbzhba-uw.a.run.app"; const PROMPT = ( diff --git a/core/config/index.d.ts b/core/config/index.d.ts index 7d0052969..a8bdc3aa2 100644 --- a/core/config/index.d.ts +++ b/core/config/index.d.ts @@ -4,6 +4,10 @@ These are the types for the JSON config file, config.json. **/ +import { SlashCommand } from "../commands/index"; +import { ContextProvider } from "../context"; +import { LLM } from "../llm"; + type StepName = | "AnswerQuestionChroma" | "GenerateShellCommandStep" @@ -69,7 +73,7 @@ interface ContextProviderWithParams { params: { [key: string]: any }; } -interface SlashCommand { +interface SlashCommandDescription { name: string; description: string; step: StepName | string; @@ -131,23 +135,38 @@ interface SerializedContinueConfig { modelRoles: ModelRoles; systemMessage?: string; completionOptions?: BaseCompletionOptions; - slashCommands?: SlashCommand[]; + slashCommands?: SlashCommandDescription[]; customCommands?: CustomCommand[]; contextProviders?: ContextProviderWithParams[]; + retrievalSettings?: RetrievalSettings; + disableIndexing?: boolean; } + +interface ContinueConfig { + allowAnonymousTelemetry?: boolean; + models: LLM[]; + systemMessage?: string; + completionOptions?: BaseCompletionOptions; + slashCommands?: SlashCommand[]; + contextProviders?: ContextProvider[]; + retrievalSettings?: RetrievalSettings; + disableIndexing?: boolean; +} + export { - StepName, - ContextProviderName, - TemplateType, - ModelProvider, - RequestOptions, - StepWithParams, - ContextProviderWithParams, - SlashCommand, - CustomCommand, - RetrievalSettings, BaseCompletionOptions, + ContextProviderName, + ContextProviderWithParams, + ContinueConfig, + CustomCommand, ModelDescription, + ModelProvider, ModelRoles, + RequestOptions, + RetrievalSettings, SerializedContinueConfig, + SlashCommandDescription, + StepName, + StepWithParams, + TemplateType, }; diff --git a/core/config/load.ts b/core/config/load.ts new file mode 100644 index 000000000..8b45e8f28 --- /dev/null +++ b/core/config/load.ts @@ -0,0 +1,51 @@ +import { ContinueConfig, SerializedContinueConfig } from "."; +import { + SlashCommand, + slashCommandFromDescription, + slashFromCustomCommand, +} from "../commands"; +import { ContextProvider } from "../context"; +import { contextProviderClassFromName } from "../context/providers"; +import { LLM } from "../llm"; +import { llmFromDescription } from "../llm/llms"; + +function loadSerializedConfig( + initial: SerializedContinueConfig +): ContinueConfig { + const models: LLM[] = []; + for (const desc of initial.models) { + models.push(llmFromDescription(desc, initial.completionOptions)); + } + + const slashCommands: SlashCommand[] = []; + for (const command of initial.slashCommands || []) { + const newCommand = slashCommandFromDescription(command); + if (newCommand) { + slashCommands.push(newCommand); + } + } + for (const command of initial.customCommands || []) { + slashCommands.push(slashFromCustomCommand(command)); + } + + const contextProviders: ContextProvider[] = []; + for (const provider of initial.contextProviders || []) { + const cls = contextProviderClassFromName(provider.name) as any; + contextProviders.push(new cls(provider.params)); + } + + const config: ContinueConfig = { + allowAnonymousTelemetry: initial.allowAnonymousTelemetry, + models, + systemMessage: initial.systemMessage, + completionOptions: initial.completionOptions, + slashCommands, + contextProviders, + retrievalSettings: initial.retrievalSettings, + disableIndexing: initial.disableIndexing, + }; + + return config; +} + +export { loadSerializedConfig }; diff --git a/core/context/providers/index.ts b/core/context/providers/index.ts index a48d6db70..a286e3031 100644 --- a/core/context/providers/index.ts +++ b/core/context/providers/index.ts @@ -1,19 +1,29 @@ +import { ContextProvider } from ".."; +import { ContextProviderName } from "../../config"; import DiffContextProvider from "./DiffContextProvider"; -import GitHubIssuesContextProvider from "./GitHubIssuesContextProvider"; -import TerminalContextProvider from "./TerminalContextProvider"; -// import OpenTabsContextProvider from "./OpenTabsContextProvider"; -import GoogleContextProvider from "./GoogleContextProvider"; -// import SearchContextProvider from "./SearchContextProvider"; -import URLContextProvider from "./URLContextProvider"; import FileTreeContextProvider from "./FileTreeContextProvider"; +import GitHubIssuesContextProvider from "./GitHubIssuesContextProvider"; +import GoogleContextProvider from "./GoogleContextProvider"; +import TerminalContextProvider from "./TerminalContextProvider"; +import URLContextProvider from "./URLContextProvider"; -export default { - diff: DiffContextProvider, - github: GitHubIssuesContextProvider, - terminal: TerminalContextProvider, - // open: OpenTabsContextProvider, TODO - google: GoogleContextProvider, - // search: SearchContextProvider, TODO - url: URLContextProvider, - tree: FileTreeContextProvider, -}; +const Providers: (typeof ContextProvider)[] = [ + DiffContextProvider, + FileTreeContextProvider, + GitHubIssuesContextProvider, + GoogleContextProvider, + TerminalContextProvider, + URLContextProvider, +]; + +export function contextProviderClassFromName( + name: ContextProviderName +): typeof ContextProvider { + const cls = Providers.find((cls) => cls.description.title === name); + + if (!cls) { + throw new Error(`Unknown provider ${name}`); + } + + return cls; +} diff --git a/core/llm/index.ts b/core/llm/index.ts index 1a213e32e..bb2592b1d 100644 --- a/core/llm/index.ts +++ b/core/llm/index.ts @@ -22,7 +22,7 @@ import { simplestEditPrompt, zephyrEditPrompt, } from "./templates/edit"; -import { CompletionOptions, ChatMessage } from "./types"; +import { ChatMessage, CompletionOptions } from "./types"; function autodetectTemplateType(model: string): TemplateType | undefined { const lower = model.toLowerCase(); @@ -153,7 +153,7 @@ interface LLMFullCompletionOptions { export abstract class LLM implements LLMOptions { static providerName: ModelProvider; - static defaultOptions: Partial; + static defaultOptions: Partial | undefined; get providerName(): ModelProvider { return (this.constructor as typeof LLM).providerName; diff --git a/core/llm/llms/GooglePalm.ts b/core/llm/llms/GooglePalm.ts index d07ede5b1..d576e3433 100644 --- a/core/llm/llms/GooglePalm.ts +++ b/core/llm/llms/GooglePalm.ts @@ -5,7 +5,7 @@ import { ChatMessage, CompletionOptions } from "../types"; class GooglePalm extends LLM { static providerName: ModelProvider = "google-palm"; - static defaultOptions: LLMOptions = { + static defaultOptions: Partial = { model: "chat-bison-001", }; diff --git a/core/llm/llms/LMStudio.ts b/core/llm/llms/LMStudio.ts index fad051366..083303d10 100644 --- a/core/llm/llms/LMStudio.ts +++ b/core/llm/llms/LMStudio.ts @@ -1,11 +1,10 @@ -import { ChatCompletionCreateParamsBase } from "openai/resources/chat/completions"; +import { LLMOptions } from ".."; import { ModelProvider } from "../../config"; -import { ChatMessage } from "../types"; import OpenAI from "./OpenAI"; class LMStudio extends OpenAI { static providerName: ModelProvider = "lmstudio"; - static defaultOptions = { + static defaultOptions: Partial = { apiBase: "http://localhost:1234/v1", }; } diff --git a/core/llm/llms/index.ts b/core/llm/llms/index.ts index 8517acb25..7baad6840 100644 --- a/core/llm/llms/index.ts +++ b/core/llm/llms/index.ts @@ -1,18 +1,20 @@ +import { LLM, LLMOptions } from ".."; +import { BaseCompletionOptions, ModelDescription } from "../../config"; import Anthropic from "./Anthropic"; import FreeTrial from "./FreeTrial"; import GooglePalm from "./GooglePalm"; +import HuggingFaceInferenceAPI from "./HuggingFaceInferenceAPI"; +import HuggingFaceTGI from "./HuggingFaceTGI"; +import LMStudio from "./LMStudio"; +import LlamaCpp from "./LlamaCpp"; import Llamafile from "./Llamafile"; import Ollama from "./Ollama"; +import OpenAI from "./OpenAI"; import Replicate from "./Replicate"; import TextGenWebUI from "./TextGenWebUI"; import Together from "./Together"; -import HuggingFaceTGI from "./HuggingFaceTGI"; -import HuggingFaceInferenceAPI from "./HuggingFaceInferenceAPI"; -import LlamaCpp from "./LlamaCpp"; -import OpenAI from "./OpenAI"; -import LMStudio from "./LMStudio"; -export { +const LLMs = [ Anthropic, FreeTrial, GooglePalm, @@ -26,4 +28,34 @@ export { LlamaCpp, OpenAI, LMStudio, -}; +]; + +export function llmFromDescription( + desc: ModelDescription, + completionOptions?: BaseCompletionOptions +): LLM { + const cls = LLMs.find((llm) => llm.providerName === desc.provider); + + if (!cls) { + throw new Error(`Unknown provider ${desc.provider}`); + } + + const finalCompletionOptions = { + ...completionOptions, + ...desc.completionOptions, + }; + + const options: LLMOptions = { + ...desc, + completionOptions: { + ...finalCompletionOptions, + model: desc.model || cls.defaultOptions?.model || "codellama-7b", // TODO: Fix up all the ?'s + maxTokens: + finalCompletionOptions.maxTokens || + cls.defaultOptions?.completionOptions?.maxTokens || + 1024, + }, + }; + + return new cls(options); +} diff --git a/gui/src/components/mainInput/ComboBox.tsx b/gui/src/components/mainInput/ComboBox.tsx index b68b63311..ee16a678a 100644 --- a/gui/src/components/mainInput/ComboBox.tsx +++ b/gui/src/components/mainInput/ComboBox.tsx @@ -41,7 +41,7 @@ import { } from ".."; import useContextProviders from "../../hooks/useContextProviders"; import useHistory from "../../hooks/useHistory"; -import { contextLengthSelector } from "../../redux/selectors/configSelectors"; +import { contextLengthSelector } from "../../redux/selectors/modelSelectors"; import { setTakenActionTrue } from "../../redux/slices/miscSlice"; import { addContextItems, @@ -337,7 +337,7 @@ const Li = styled.li<{ background-color: ${({ highlighted }) => highlighted ? buttonColor + "66" : "transparent"}; ${({ selected }) => selected && "font-weight: bold;"} - padding: 0.5rem 0.5rem; + padding: 0.5rem 0.25rem; display: flex; flex-direction: row; align-items: center; @@ -416,16 +416,15 @@ const ComboBox = React.forwardRef((props: ComboBoxProps, ref) => { any | undefined >(undefined); - const availableSlashCommands = []; - // useSelector( - // (state: RootStore) => - // state.serverState.slashCommands?.map((cmd) => { - // return { - // name: `/${cmd.name}`, - // description: cmd.description, - // }; - // }) || [] - // ); + const availableSlashCommands = useSelector( + (state: RootStore) => + state.state.config.slashCommands?.map((cmd) => { + return { + name: `/${cmd.name}`, + description: cmd.description, + }; + }) || [] + ); const selectedContextItems = useSelector((state: RootStore) => { if (typeof props.index !== "undefined") { return state.state.history[props.index].contextItems; diff --git a/gui/src/components/modelSelection/ModelSelect.tsx b/gui/src/components/modelSelection/ModelSelect.tsx index c24b400b4..503666d9a 100644 --- a/gui/src/components/modelSelection/ModelSelect.tsx +++ b/gui/src/components/modelSelection/ModelSelect.tsx @@ -17,7 +17,7 @@ import { vscBackground, vscForeground, } from ".."; -import { defaultModelSelector } from "../../redux/selectors/configSelectors"; +import { defaultModelSelector } from "../../redux/selectors/modelSelectors"; import { RootStore } from "../../redux/store"; import { getMetaKeyLabel } from "../../util"; import HeaderButtonWithText from "../HeaderButtonWithText"; diff --git a/gui/src/hooks/CustomPostHogProvider.tsx b/gui/src/hooks/CustomPostHogProvider.tsx index cc6de4a21..b9e974e54 100644 --- a/gui/src/hooks/CustomPostHogProvider.tsx +++ b/gui/src/hooks/CustomPostHogProvider.tsx @@ -1,13 +1,12 @@ import posthog from "posthog-js"; import { PostHogProvider } from "posthog-js/react"; -import { PropsWithChildren, useEffect } from "react"; -import { RootStore } from "../redux/store"; +import React, { PropsWithChildren, useEffect } from "react"; import { useSelector } from "react-redux"; -import React from "react"; +import { RootStore } from "../redux/store"; const CustomPostHogProvider = ({ children }: PropsWithChildren) => { const allowAnonymousTelemetry = useSelector( - (store: RootStore) => store?.serverState?.config?.allow_anonymous_telemetry + (store: RootStore) => store?.serverState?.config.allowAnonymousTelemetry ); const [client, setClient] = React.useState(undefined); diff --git a/gui/src/hooks/useChatHandler.ts b/gui/src/hooks/useChatHandler.ts new file mode 100644 index 000000000..93e40c87a --- /dev/null +++ b/gui/src/hooks/useChatHandler.ts @@ -0,0 +1,96 @@ +import { Dispatch } from "@reduxjs/toolkit"; + +import { SlashCommand } from "core/commands"; +import { ExtensionIde } from "core/ide"; +import { ChatMessage } from "core/llm/types"; +import { useSelector } from "react-redux"; +import { defaultModelSelector } from "../redux/selectors/modelSelectors"; +import { + addContextItems, + setInactive, + streamUpdate, +} from "../redux/slices/stateSlice"; +import { RootStore } from "../redux/store"; + +function useChatHandler(dispatch: Dispatch) { + const defaultModel = useSelector(defaultModelSelector); + + const slashCommands = useSelector( + (store: RootStore) => store.state.config.slashCommands || [] + ); + + const active = useSelector((store: RootStore) => store.state.active); + + function getSlashCommandForInput( + input: string + ): [SlashCommand, string] | undefined { + let slashCommand: SlashCommand | undefined; + let slashCommandName: string | undefined; + if (input.startsWith("/")) { + slashCommandName = input.split(" ")[0].substring(1); + slashCommand = slashCommands.find( + (command) => command.name === slashCommandName + ); + } + if (!slashCommand || !slashCommandName) { + return undefined; + } + + // Convert to actual slash command object with runnable function + return [slashCommand, input]; + } + + async function _streamNormalInput(messages: ChatMessage[]) { + for await (const update of defaultModel.streamChat(messages)) { + if (!active) { + break; + } + dispatch(streamUpdate(update.content)); + } + } + + async function _streamSlashCommand( + messages: ChatMessage[], + slashCommand: SlashCommand, + input: string + ) { + const sdk = { + input, + history: messages, + ide: new ExtensionIde(), + llm: defaultModel, + addContextItem: (item) => { + dispatch(addContextItems([item])); + }, + options: {}, // TODO: pass options to slash command + }; + + for await (const update of slashCommand.run(sdk)) { + if (!active) { + break; + } + dispatch(streamUpdate(update)); + } + } + + async function streamResponse(messages: ChatMessage[]) { + if (messages.length === 0) { + return; + } + + // Determine if the input is a slash command + const input = messages[-1].content; + let [slashCommand, commandInput] = getSlashCommandForInput(input); + + if (slashCommand) { + await _streamSlashCommand(messages, slashCommand, commandInput); + } else { + await _streamNormalInput(messages); + } + dispatch(setInactive()); + } + + return { streamResponse }; +} + +export default useChatHandler; diff --git a/gui/src/hooks/useContextProviders.ts b/gui/src/hooks/useContextProviders.ts index 3681860db..b3479dc08 100644 --- a/gui/src/hooks/useContextProviders.ts +++ b/gui/src/hooks/useContextProviders.ts @@ -1,22 +1,10 @@ +import { ContextProviderName } from "core/config"; +import { ContextProvider, ContextProviderDescription } from "core/context"; +import { contextProviderClassFromName } from "core/context/providers"; +import { ContextItem } from "core/llm/types"; import { useEffect, useState } from "react"; import { useSelector } from "react-redux"; import { RootStore } from "../redux/store"; -import { ContextProviderName } from "core/config"; -import { ContextProvider, ContextProviderDescription } from "core/context"; -import Providers from "core/context/providers"; -import { ContextItem } from "core/llm/types"; - -function providerNameToProvider( - name: ContextProviderName -): typeof ContextProvider { - const cls = Providers[name]; - - if (!cls) { - throw new Error(`Unknown provider ${name}`); - } - - return cls; -} function useContextProviders() { const [providers, setProviders] = useState<{ @@ -34,7 +22,9 @@ function useContextProviders() { let providersList = []; let descriptionsList = []; for (const [name, options] of Object.entries(providerConfig)) { - const provider = providerNameToProvider(name as ContextProviderName); + const provider = contextProviderClassFromName( + name as ContextProviderName + ); providersList.push((provider as any)(options)); descriptionsList.push(provider.description); } diff --git a/gui/src/hooks/useModels.ts b/gui/src/hooks/useModels.ts deleted file mode 100644 index fc16baa26..000000000 --- a/gui/src/hooks/useModels.ts +++ /dev/null @@ -1,71 +0,0 @@ -import { useEffect, useState } from "react"; -import { useSelector } from "react-redux"; -import { RootStore } from "../redux/store"; -import { - FreeTrial, - Anthropic, - GooglePalm, - LMStudio, - Llamafile, - Ollama, - Replicate, - TextGenWebUI, - Together, - HuggingFaceTGI, - HuggingFaceInferenceAPI, - LlamaCpp, - OpenAI, -} from "core/llm/llms"; -import { ModelDescription } from "core/config"; -import { LLM } from "core/llm"; - -function modelDescriptionToLLM(desc: ModelDescription): LLM { - let cls: typeof LLM; - const providerClasses = { - "openai-free-trial": FreeTrial, - anthropic: Anthropic, - "google-palm": GooglePalm, - lmstudio: LMStudio, - llamafile: Llamafile, - ollama: Ollama, - replicate: Replicate, - "text-gen-webui": TextGenWebUI, - together: Together, - "huggingface-tgi": HuggingFaceTGI, - "huggingface-inference-api": HuggingFaceInferenceAPI, - "llama.cpp": LlamaCpp, - "openai-aiohttp": OpenAI, - openai: OpenAI, - }; - - cls = providerClasses[desc.provider]; - - if (!cls) { - throw new Error(`Unknown provider ${desc.provider}`); - } - - return new (cls as any)({ ...desc, uniqueId: "None" }); -} - -function useModels() { - const [defaultModel, setDefaultModel] = useState( - new FreeTrial({ uniqueId: "None", model: "gpt-3.5-turbo" }) - ); - const models = useSelector( - (store: RootStore) => store.state.config?.models || [] - ); - const defaultTitle = useSelector( - (store: RootStore) => store.state.config.modelRoles.default - ); - - useEffect(() => { - const model = models.find((model) => model.title === defaultTitle); - if (model) { - setDefaultModel(modelDescriptionToLLM(model)); - } - }, [models, defaultTitle]); - - return { defaultModel }; -} - -export default useModels; diff --git a/gui/src/hooks/useSetup.ts b/gui/src/hooks/useSetup.ts index e9f103433..c6a0af7e2 100644 --- a/gui/src/hooks/useSetup.ts +++ b/gui/src/hooks/useSetup.ts @@ -3,8 +3,8 @@ import { useEffect } from "react"; import { setServerStatusMessage } from "../redux/slices/miscSlice"; import { postToIde } from "../util/ide"; -import { setVscMachineId } from "../redux/slices/configSlice"; import { ExtensionIde } from "core/ide/index"; +import { setVscMachineId } from "../redux/slices/configSlice"; import { setConfig } from "../redux/slices/serverStateReducer"; import { addHighlightedCode, setInactive } from "../redux/slices/stateSlice"; diff --git a/gui/src/pages/gui.tsx b/gui/src/pages/gui.tsx index d051f71f7..d65f389cd 100644 --- a/gui/src/pages/gui.tsx +++ b/gui/src/pages/gui.tsx @@ -29,14 +29,13 @@ import ErrorStepContainer from "../components/gui/ErrorStepContainer"; import StepContainer from "../components/gui/StepContainer"; import TimelineItem from "../components/gui/TimelineItem"; import ComboBox from "../components/mainInput/ComboBox"; +import useChatHandler from "../hooks/useChatHandler"; import useHistory from "../hooks/useHistory"; -import useModels from "../hooks/useModels"; -import { setTakenActionTrue } from "../redux/slices/miscSlice"; +import { defaultModelSelector } from "../redux/selectors/modelSelectors"; import { newSession, resubmitAtIndex, setInactive, - streamUpdate, submitMessage, } from "../redux/slices/stateSlice"; import { @@ -129,9 +128,8 @@ function GUI(props: GUIProps) { // #region Selectors const sessionState = useSelector((state: RootStore) => state.state); - const workspacePaths = (window as any).workspacePaths || []; - const { defaultModel } = useModels(); + const defaultModel = useSelector(defaultModelSelector); const sessionTitle = useSelector((state: RootStore) => state.state.title); const active = useSelector((state: RootStore) => state.state.active); @@ -162,7 +160,6 @@ function GUI(props: GUIProps) { const bottomMessage = useSelector( (state: RootStore) => state.uiState.bottomMessage ); - const takenAction = useSelector((state: RootStore) => state.misc.takenAction); useEffect(() => { if (!aboveComboBoxDivRef.current) return; dispatch( @@ -231,6 +228,8 @@ function GUI(props: GUIProps) { // #endregion + const { streamResponse } = useChatHandler(dispatch); + const sendInput = useCallback( (input: string) => { if ( @@ -265,13 +264,7 @@ function GUI(props: GUIProps) { const messages = constructMessages([...state.history, historyItem]); dispatch(submitMessage(message)); - - (async () => { - for await (const update of defaultModel.streamChat(messages)) { - dispatch(streamUpdate(update.content)); - } - dispatch(setInactive()); - })(); + streamResponse(messages); // Increment localstorage counter for popup const counter = localStorage.getItem("mainTextEntryCounter"); @@ -347,7 +340,13 @@ function GUI(props: GUIProps) { localStorage.setItem("mainTextEntryCounter", "1"); } }, - [sessionState.history, sessionState.contextItems, defaultModel, state] + [ + sessionState.history, + sessionState.contextItems, + defaultModel, + state, + streamResponse, + ] ); const { saveSession } = useHistory(); @@ -377,8 +376,6 @@ function GUI(props: GUIProps) { }, [sendInput]); const onMainTextInput = (event?: any) => { - console.log("onMainTextInput"); - dispatch(setTakenActionTrue(null)); if (mainTextInputRef.current) { let input = (mainTextInputRef.current as any).inputValue; diff --git a/gui/src/pages/settings.tsx b/gui/src/pages/settings.tsx index 33335b352..5920055eb 100644 --- a/gui/src/pages/settings.tsx +++ b/gui/src/pages/settings.tsx @@ -129,8 +129,8 @@ function Settings() { useEffect(() => { if (!config) return; - formMethods.setValue("system_message", config.system_message); - formMethods.setValue("temperature", config.completion_options?.temperature); + formMethods.setValue("system_message", config.systemMessage); + formMethods.setValue("temperature", config.completionOptions?.temperature); }, [config]); return ( @@ -202,7 +202,7 @@ function Settings() {

{(formMethods.watch("temperature") as number | undefined) || - config.completion_options?.temperature || + config.completionOptions?.temperature || "-"}

diff --git a/gui/src/redux/selectors/configSelectors.ts b/gui/src/redux/selectors/configSelectors.ts deleted file mode 100644 index 8d1cbf1db..000000000 --- a/gui/src/redux/selectors/configSelectors.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { RootStore } from "../store"; - -export const contextLengthSelector = (state: RootStore) => { - return ( - { - "gpt-4": 8096, - "gpt-3.5-turbo": 4096, - "gpt-3.5-turbo-16k": 16384, - "gpt-4-32k": 32768, - "claude-2": 16384, - }[state.serverState.config.model_roles?.default] || 4096 - ); -}; - -export const defaultModelSelector = (state: RootStore) => { - const title = state.serverState.config.model_roles?.default; - if (!Array.isArray(state.serverState.config.models)) return; - return state.serverState.config.models?.find( - (model) => model.title === title - ); -}; diff --git a/gui/src/redux/selectors/modelSelectors.ts b/gui/src/redux/selectors/modelSelectors.ts new file mode 100644 index 000000000..d231eb24e --- /dev/null +++ b/gui/src/redux/selectors/modelSelectors.ts @@ -0,0 +1,10 @@ +import { RootStore } from "../store"; + +export const defaultModelSelector = (state: RootStore) => { + const title = state.state.defaultModelTitle; + return state.serverState.config.models.find((model) => model.title === title); +}; + +export const contextLengthSelector = (state: RootStore) => { + return defaultModelSelector(state)?.contextLength || 4096; +}; diff --git a/gui/src/redux/slices/serverStateReducer.ts b/gui/src/redux/slices/serverStateReducer.ts index ca6de0d6a..9d43aaf2c 100644 --- a/gui/src/redux/slices/serverStateReducer.ts +++ b/gui/src/redux/slices/serverStateReducer.ts @@ -1,4 +1,7 @@ import { createSlice } from "@reduxjs/toolkit"; +import { SerializedContinueConfig } from "core/config"; +import { loadSerializedConfig } from "core/config/load"; +import FreeTrial from "core/llm/llms/FreeTrial"; import { RootStore } from "../store"; const TEST_SLASH_COMMANDS = [ @@ -21,8 +24,10 @@ const initialState: RootStore["serverState"] = { slashCommands: [], selectedContextItems: [], config: { - system_message: "", - temperature: 0.5, + models: [ + new FreeTrial({ model: "gpt-4" }), + new FreeTrial({ model: "gpt-3.5-turbo" }), + ], }, contextProviders: [], savedContextGroups: [], @@ -49,10 +54,11 @@ export const serverStateSlice = createSlice({ contextProviders: action.payload, }; }, - setConfig: (state, action) => { + setConfig: (state, { payload }: { payload: SerializedContinueConfig }) => { + const config = loadSerializedConfig(payload); return { ...state, - config: action.payload, + config, }; }, setIndexingProgress: (state, { payload }: { payload: number }) => { diff --git a/gui/src/redux/slices/stateSlice.ts b/gui/src/redux/slices/stateSlice.ts index ed4e349d9..c6cc4cbd5 100644 --- a/gui/src/redux/slices/stateSlice.ts +++ b/gui/src/redux/slices/stateSlice.ts @@ -1,9 +1,10 @@ import { createSlice } from "@reduxjs/toolkit"; -import { RootStore } from "../store"; -import { ContextItem, ContextItemId } from "core/llm/types"; import defaultConfig from "core/config/default"; -import { v4 } from "uuid"; +import { loadSerializedConfig } from "core/config/load"; +import { ContextItem, ContextItemId } from "core/llm/types"; import { PersistedSessionInfo } from "core/types"; +import { v4 } from "uuid"; +import { RootStore } from "../store"; const TEST_CONTEXT_ITEMS: ContextItem[] = [ { @@ -83,9 +84,10 @@ const initialState: RootStore["state"] = { history: [], contextItems: [], active: false, - config: defaultConfig, + config: loadSerializedConfig(defaultConfig), title: "New Session", sessionId: v4(), + defaultModelTitle: "GPT-4", }; export const stateSlice = createSlice({ @@ -324,6 +326,16 @@ export const stateSlice = createSlice({ }; } }, + setDefaultModel: (state, action) => { + const model = state.config.models.find( + (model) => model.title === action.payload + ); + if (!model) return; + return { + ...state, + defaultModelTitle: action.payload, + }; + }, }, }); @@ -339,5 +351,6 @@ export const { resubmitAtIndex, addHighlightedCode, setEditingAtIds, + setDefaultModel, } = stateSlice.actions; export default stateSlice.reducer; diff --git a/gui/src/redux/store.ts b/gui/src/redux/store.ts index 7b96935fa..c3a6cca30 100644 --- a/gui/src/redux/store.ts +++ b/gui/src/redux/store.ts @@ -1,18 +1,17 @@ import { combineReducers, configureStore } from "@reduxjs/toolkit"; -import configReducer from "./slices/configSlice"; -import miscReducer from "./slices/miscSlice"; -import uiStateReducer from "./slices/uiStateSlice"; -import serverStateReducer from "./slices/serverStateReducer"; -import stateReducer from "./slices/stateSlice"; -import { ContinueConfig } from "../schema/ContinueConfig"; import { ContextItem } from "core/llm/types"; import { ContextProviderDescription } from "../schema/ContextProviderDescription"; import { SlashCommandDescription } from "../schema/SlashCommandDescription"; +import configReducer from "./slices/configSlice"; +import miscReducer from "./slices/miscSlice"; +import serverStateReducer from "./slices/serverStateReducer"; +import stateReducer from "./slices/stateSlice"; +import uiStateReducer from "./slices/uiStateSlice"; -import storage from "redux-persist/lib/storage"; -import { persistReducer, persistStore, createTransform } from "redux-persist"; +import { ContinueConfig } from "core/config"; import { ChatHistory } from "core/llm/types"; -import { SerializedContinueConfig } from "core/config"; +import { createTransform } from "redux-persist"; +import storage from "redux-persist/lib/storage"; export interface ChatMessage { role: "system" | "user" | "assistant"; @@ -24,9 +23,10 @@ export interface RootStore { history: ChatHistory; contextItems: ContextItem[]; active: boolean; - config: SerializedContinueConfig; + config: ContinueConfig; title: string; sessionId: string; + defaultModelTitle: string; }; config: {