From 55a453d60af8fee1199971c4ba1616eb141cb0c4 Mon Sep 17 00:00:00 2001 From: Srujan Gurram Date: Sat, 26 Oct 2024 20:21:57 +0530 Subject: [PATCH] Refactor chat settings model selection logic --- .../Settings/Sections/ChatSettings.tsx | 11 +++------- src/components/Sidebar/auth/index.tsx | 16 ++++---------- .../Sidebar/chat/ChangeChatModel.tsx | 9 +++----- src/components/Sidebar/chat/index.tsx | 2 +- src/components/Sidebar/index.tsx | 6 +++++- src/config/settings/index.ts | 19 +---------------- src/hooks/useChatCompletion.ts | 9 ++++---- src/hooks/useChatModels.ts | 21 ++++++++++--------- 8 files changed, 32 insertions(+), 61 deletions(-) diff --git a/src/components/Settings/Sections/ChatSettings.tsx b/src/components/Settings/Sections/ChatSettings.tsx index e6ac54d69..63739125e 100644 --- a/src/components/Settings/Sections/ChatSettings.tsx +++ b/src/components/Settings/Sections/ChatSettings.tsx @@ -126,18 +126,13 @@ const ChatSettings = () => { row={true} > diff --git a/src/components/Sidebar/auth/index.tsx b/src/components/Sidebar/auth/index.tsx index 68a344647..83dc1b17b 100644 --- a/src/components/Sidebar/auth/index.tsx +++ b/src/components/Sidebar/auth/index.tsx @@ -1,8 +1,7 @@ import React, { useEffect, useState } from 'react' +import { useChatModels } from '../../../hooks/useChatModels' import { useSettings } from '../../../hooks/useSettings' import { validateApiKey } from '../../../lib/validApiKey' -import { useChatModels } from '../../../hooks/useChatModels' -import { ModelInfo } from '../../../config/settings' const Auth = () => { const [, setSettings] = useSettings() @@ -13,9 +12,7 @@ const Auth = () => { useEffect(() => { if (error) { - setTimeout(() => { - setError(null) - }, 3000) + setTimeout(() => setError(null), 3000) } }, [error]) @@ -118,17 +115,12 @@ const Auth = () => { diff --git a/src/components/Sidebar/chat/ChangeChatModel.tsx b/src/components/Sidebar/chat/ChangeChatModel.tsx index e650dcac2..b5a41ebfb 100644 --- a/src/components/Sidebar/chat/ChangeChatModel.tsx +++ b/src/components/Sidebar/chat/ChangeChatModel.tsx @@ -7,18 +7,15 @@ const ChangeChatModel = () => {
diff --git a/src/components/Sidebar/chat/index.tsx b/src/components/Sidebar/chat/index.tsx index a37a8ae9c..3ba654874 100644 --- a/src/components/Sidebar/chat/index.tsx +++ b/src/components/Sidebar/chat/index.tsx @@ -58,7 +58,7 @@ const Chat = ({ settings }: ChatProps) => { clearMessages={clearMessages} cancelRequest={cancelRequest} isWebpageContextOn={settings.general.webpageContext} - isVisionModel={settings.chat.model?.capabilities.vision || false} + isVisionModel={settings.chat.model?.includes('vision') || false} /> ) diff --git a/src/components/Sidebar/index.tsx b/src/components/Sidebar/index.tsx index 618898478..08108c20a 100644 --- a/src/components/Sidebar/index.tsx +++ b/src/components/Sidebar/index.tsx @@ -10,7 +10,11 @@ function Sidebar() { return (
- {settings.chat.openAIKey ? : } + {settings.chat.openAIKey && settings.chat.model ? ( + + ) : ( + + )}
) } diff --git a/src/config/settings/index.ts b/src/config/settings/index.ts index ccbcc4dcc..0066646cc 100644 --- a/src/config/settings/index.ts +++ b/src/config/settings/index.ts @@ -13,23 +13,6 @@ export enum Mode { CREATIVE = 1.5, } -export type ModelCapabilities = { - completion_chat: boolean - function_calling: boolean - vision: boolean - fine_tuning: boolean - completion_fim: boolean -} - -export type ModelInfo = { - id: string - name: string - description: string - capabilities: ModelCapabilities - max_context_length: number - owned_by: string -} - export type Settings = { quickMenu: { enabled: boolean @@ -38,7 +21,7 @@ export type Settings = { } chat: { openAIKey: string | null - model: ModelInfo | null + model: string | null mode: Mode openAiBaseUrl: string | null } diff --git a/src/hooks/useChatCompletion.ts b/src/hooks/useChatCompletion.ts index d6afcdfd3..791ba1574 100644 --- a/src/hooks/useChatCompletion.ts +++ b/src/hooks/useChatCompletion.ts @@ -6,13 +6,13 @@ import { SystemMessage, } from '@langchain/core/messages' import { useMemo, useState } from 'react' -import type { Mode, ModelInfo } from '../config/settings' +import type { Mode } from '../config/settings' import { getMatchedContent } from '../lib/getMatchedContent' import { ChatRole, useCurrentChat } from './useCurrentChat' import type { MessageDraft } from './useMessageDraft' interface UseChatCompletionProps { - model: ModelInfo + model: string apiKey: string mode: Mode systemPrompt: string @@ -43,12 +43,11 @@ export const useChatCompletion = ({ return new ChatOpenAI({ streaming: true, openAIApiKey: apiKey, - modelName: model.id, + modelName: model, configuration: { baseURL: baseURL, }, temperature: Number(mode), - maxTokens: model.max_context_length, }) }, [apiKey, model, mode, baseURL]) @@ -99,7 +98,7 @@ export const useChatCompletion = ({ ...previousMessages, new HumanMessage({ content: - message.files.length > 0 && model.capabilities.vision + message.files.length > 0 ? [ { type: 'text', text: expandedQuery }, ...(message.files.length > 0 diff --git a/src/hooks/useChatModels.ts b/src/hooks/useChatModels.ts index c2e13b391..83ed53879 100644 --- a/src/hooks/useChatModels.ts +++ b/src/hooks/useChatModels.ts @@ -1,11 +1,17 @@ import { useCallback, useEffect, useState } from 'react' import { useSettings } from './useSettings' import axios from 'axios' -import type { ModelInfo } from '../config/settings' + +type OpenAIModel = { + id: string + object: string + created: number + owned_by: string +} export const useChatModels = () => { const [settings, setSettings] = useSettings() - const [models, setModels] = useState([]) + const [models, setModels] = useState([]) const chatSettings = settings.chat const activeChatModel = chatSettings.model @@ -20,12 +26,7 @@ export const useChatModels = () => { }, }) - // Filter for chat-capable models - const chatModels = data.data.filter( - (model: ModelInfo) => model.capabilities?.completion_chat === true, - ) - - setModels(chatModels) + setModels(data.data) } catch (error) { console.log('Failed to fetch models:', error) setModels([]) @@ -37,12 +38,12 @@ export const useChatModels = () => { fetchAvailableModels() }, [fetchAvailableModels]) - const setActiveChatModel = (model: ModelInfo) => { + const setActiveChatModel = (modelId: string) => { setSettings({ ...settings, chat: { ...chatSettings, - model, + model: modelId, }, }) }