From 00c6c722701bbe62dec0cf98f5b068632b426e77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20L=C3=89VEIL?= Date: Wed, 29 Mar 2023 05:10:47 +0200 Subject: [PATCH] :sparkles: feat: add DEFAULT_MODEL environment variable (#280) * :sparkles: feat: add DEFAULT_MODEL environment variable * set the model maxLength setting in the models definition * set the model tokenLimit setting in the models definition --- .env.local.example | 3 ++- README.md | 12 ++++++++++ components/Chat/Chat.tsx | 15 ++++++++---- components/Chat/ChatInput.tsx | 27 +++++++++++++--------- components/Chat/ModelSelect.tsx | 17 ++++++++++---- components/Chat/SystemPrompt.tsx | 3 +-- pages/api/chat.ts | 5 +--- pages/index.tsx | 39 ++++++++++++++++++++++++-------- types/openai.ts | 11 ++++++++- 9 files changed, 94 insertions(+), 38 deletions(-) diff --git a/.env.local.example b/.env.local.example index e6c4374..992bfab 100644 --- a/.env.local.example +++ b/.env.local.example @@ -1 +1,2 @@ -OPENAI_API_KEY=YOUR_KEY \ No newline at end of file +OPENAI_API_KEY=YOUR_KEY +DEFAULT_MODEL=gpt-3.5-turbo \ No newline at end of file diff --git a/README.md b/README.md index 3b97ab1..7c2ed41 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,18 @@ npm run dev You should be able to start chatting. +## Configuration + +When deploying the application, the following environment variables can be set: + +| Environment Variable | Default value | Description | +|----------------------|------------------|---------------------------------------------------------| +| OPENAI_API_KEY | | The default API key used for authentication with OpenAI | +| DEFAULT_MODEL | `gpt-3.5-turbo` | The default model to use on new conversations | + +If you do not provide an OpenAI API key with `OPENAI_API_KEY`, users will have to provide their own key. +If you don't have an OpenAI API key, you can get one [here](https://platform.openai.com/account/api-keys). + ## Contact If you have any questions, feel free to reach out to me on [Twitter](https://twitter.com/mckaywrigley). diff --git a/components/Chat/Chat.tsx b/components/Chat/Chat.tsx index 78b3d1f..01e43e8 100644 --- a/components/Chat/Chat.tsx +++ b/components/Chat/Chat.tsx @@ -2,7 +2,7 @@ import { Conversation, Message } from '@/types/chat'; import { IconArrowDown } from '@tabler/icons-react'; import { KeyValuePair } from '@/types/data'; import { ErrorMessage } from '@/types/error'; -import { OpenAIModel } from '@/types/openai'; +import { OpenAIModel, OpenAIModelID } from '@/types/openai'; import { Prompt } from '@/types/prompt'; import { throttle } from '@/utils'; import { IconClearAll, IconKey, IconSettings } from '@tabler/icons-react'; @@ -29,6 +29,7 @@ interface Props { models: OpenAIModel[]; apiKey: string; serverSideApiKeyIsSet: boolean; + defaultModelId: OpenAIModelID; messageIsStreaming: boolean; modelError: ErrorMessage | null; loading: boolean; @@ -48,6 +49,7 @@ export const Chat: FC = memo( models, apiKey, serverSideApiKeyIsSet, + defaultModelId, messageIsStreaming, modelError, loading, @@ -206,6 +208,7 @@ export const Chat: FC = memo( onUpdateConversation(conversation, { key: 'model', @@ -236,12 +239,13 @@ export const Chat: FC = memo( className="ml-2 cursor-pointer hover:opacity-50" onClick={handleSettings} > - + {showSettings && ( @@ -250,6 +254,7 @@ export const Chat: FC = memo( onUpdateConversation(conversation, { key: 'model', @@ -306,7 +311,7 @@ export const Chat: FC = memo( className="flex h-7 w-7 items-center justify-center rounded-full bg-white shadow-md hover:shadow-lg focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-[#515152d7]" onClick={handleScrollDown} > - + )} diff --git a/components/Chat/ChatInput.tsx b/components/Chat/ChatInput.tsx index 0995548..d8ab132 100644 --- a/components/Chat/ChatInput.tsx +++ b/components/Chat/ChatInput.tsx @@ -1,5 +1,5 @@ import { Message } from '@/types/chat'; -import { OpenAIModel, OpenAIModelID } from '@/types/openai'; +import { OpenAIModel } from '@/types/openai'; import { Prompt } from '@/types/prompt'; import { IconPlayerStop, IconRepeat, IconSend } from '@tabler/icons-react'; import { useTranslation } from 'next-i18next'; @@ -56,7 +56,7 @@ export const ChatInput: FC = ({ const handleChange = (e: React.ChangeEvent) => { const value = e.target.value; - const maxLength = model.id === OpenAIModelID.GPT_3_5 ? 12000 : 24000; + const maxLength = model.maxLength; if (value.length > maxLength) { alert( @@ -109,7 +109,10 @@ export const ChatInput: FC = ({ const selectedPrompt = filteredPrompts[activePromptIndex]; if (selectedPrompt) { setContent((prevContent) => { - const newContent = prevContent?.replace(/\/\w*$/, selectedPrompt.content); + const newContent = prevContent?.replace( + /\/\w*$/, + selectedPrompt.content, + ); return newContent; }); handlePromptSelect(selectedPrompt); @@ -211,8 +214,9 @@ export const ChatInput: FC = ({ if (textareaRef && textareaRef.current) { textareaRef.current.style.height = 'inherit'; textareaRef.current.style.height = `${textareaRef.current?.scrollHeight}px`; - textareaRef.current.style.overflow = `${textareaRef?.current?.scrollHeight > 400 ? 'auto' : 'hidden' - }`; + textareaRef.current.style.overflow = `${ + textareaRef?.current?.scrollHeight > 400 ? 'auto' : 'hidden' + }`; } }, [content]); @@ -257,15 +261,16 @@ export const ChatInput: FC = ({