From dedb95c343f36cbcef7aa48e216bfd33a7c54141 Mon Sep 17 00:00:00 2001 From: Niek van der Maas Date: Fri, 17 Mar 2023 18:03:07 +0100 Subject: [PATCH] Improve settings, allow model selection, fixes Suggestions: model choices, horizontal sliders with min/max value in settings #44 --- src/lib/Chat.svelte | 81 ++++++++++++++++++++++++++++++++++++++------ src/lib/Types.svelte | 36 ++++++++++++++++++-- 2 files changed, 104 insertions(+), 13 deletions(-) diff --git a/src/lib/Chat.svelte b/src/lib/Chat.svelte index 5b6c278..5611b19 100644 --- a/src/lib/Chat.svelte +++ b/src/lib/Chat.svelte @@ -2,7 +2,15 @@ //import { fetchEventSource } from "@microsoft/fetch-event-source"; import { apiKeyStorage, chatsStorage, addMessage, clearMessages } from "./Storage.svelte"; - import type { Request, Response, Message, Settings } from "./Types.svelte"; + import { + type Request, + type Response, + type Message, + type Settings, + supportedModels, + type ResponseModels, + type SettingsSelect, + } from "./Types.svelte"; import Code from "./Code.svelte"; import { afterUpdate, onMount } from "svelte"; @@ -19,40 +27,65 @@ let recording = false; const settingsMap: Settings[] = [ + { + key: "model", + name: "Model", + default: "gpt-3.5-turbo", + options: supportedModels, + type: "select", + }, { key: "temperature", name: "Sampling Temperature", default: 1, + min: 0, + max: 2, + step: 0.1, type: "number", }, { key: "top_p", name: "Nucleus Sampling", default: 1, + min: 0, + max: 1, + step: 0.1, type: "number", }, { key: "n", name: "Number of Messages", default: 1, + min: 1, + max: 10, + step: 1, type: "number", }, { key: "max_tokens", name: "Max Tokens", default: 0, + min: 0, + max: 32768, + step: 1024, type: "number", }, { key: "presence_penalty", name: "Presence Penalty", default: 0, + min: -2, + max: 2, + step: 0.2, type: "number", }, { key: "frequency_penalty", name: "Frequency Penalty", default: 0, + min: -2, + max: 2, + step: 0.2, type: "number", }, ]; @@ -61,7 +94,7 @@ const token_price = 0.000002; // $0.002 per 1000 tokens // Focus the input on mount - onMount(() => { + onMount(async () => { input.focus(); // Try to detect speech recognition support @@ -237,8 +270,23 @@ } }; - const showSettings = () => { + const showSettings = async () => { settings.classList.add("is-active"); + + // Load available models from OpenAI + const allModels = (await ( + await fetch("https://api.openai.com/v1/models", { + method: "GET", + headers: { + Authorization: `Bearer ${$apiKeyStorage}`, + "Content-Type": "application/json", + }, + }) + ).json()) as ResponseModels; + const filteredModels = supportedModels.filter((model) => allModels.data.find((m) => m.id === model)); + + // Update the models in the settings + (settingsMap[0] as SettingsSelect).options = filteredModels; }; const closeSettings = () => { @@ -431,16 +479,29 @@ {#each settingsMap as setting}
- +
- + {#if setting.type === "number"} + + {:else if setting.type === "select"} +
+ +
+ {/if}
diff --git a/src/lib/Types.svelte b/src/lib/Types.svelte index 94706ad..8b5c35b 100644 --- a/src/lib/Types.svelte +++ b/src/lib/Types.svelte @@ -32,12 +32,35 @@ user?: string; }; + // See: https://platform.openai.com/docs/models/model-endpoint-compatibility + export const supportedModels = [ + "gpt-4", + "gpt-4-0314", + "gpt-4-32k", + "gpt-4-32k-0314", + "gpt-3.5-turbo", + "gpt-3.5-turbo-0301", + ]; + type Model = typeof supportedModels[number]; + + type SettingsNumber = { + type: "number"; + default: number; + min: number; + max: number; + step: number; + }; + + export type SettingsSelect = { + type: "select"; + default: Model; + options: Model[]; + }; + export type Settings = { key: string; name: string; - default: number; - type: "number"; - }; + } & (SettingsNumber | SettingsSelect); type ResponseOK = { id: string; @@ -61,4 +84,11 @@ }; export type Response = ResponseOK & ResponseError; + + export type ResponseModels = { + object: "list"; + data: { + id: string; + }[]; + };