refac: newChatStore to use options

This commit is contained in:
2024-10-15 14:59:10 +08:00
parent 9e74e419c9
commit af2ae82e74
3 changed files with 61 additions and 69 deletions

View File

@@ -142,7 +142,7 @@ export function App() {
const getChatStoreByIndex = async (index: number): Promise<ChatStore> => { const getChatStoreByIndex = async (index: number): Promise<ChatStore> => {
const ret: ChatStore = await (await db).get(STORAGE_NAME, index); const ret: ChatStore = await (await db).get(STORAGE_NAME, index);
if (ret === null || ret === undefined) return newChatStore(); if (ret === null || ret === undefined) return newChatStore({});
// handle read from old version chatstore // handle read from old version chatstore
if (ret.maxGenTokens === undefined) ret.maxGenTokens = 2048; if (ret.maxGenTokens === undefined) ret.maxGenTokens = 2048;
if (ret.maxGenTokens_enabled === undefined) ret.maxGenTokens_enabled = true; if (ret.maxGenTokens_enabled === undefined) ret.maxGenTokens_enabled = true;
@@ -162,7 +162,7 @@ export function App() {
return ret; return ret;
}; };
const [chatStore, _setChatStore] = useState(newChatStore()); const [chatStore, _setChatStore] = useState(newChatStore({}));
const setChatStore = async (chatStore: ChatStore) => { const setChatStore = async (chatStore: ChatStore) => {
// building field for search // building field for search
chatStore.contents_for_index = BuildFiledForSearch(chatStore); chatStore.contents_for_index = BuildFiledForSearch(chatStore);
@@ -212,32 +212,7 @@ export function App() {
); );
const handleNewChatStoreWithOldOne = async (chatStore: ChatStore) => { const handleNewChatStoreWithOldOne = async (chatStore: ChatStore) => {
const newKey = await ( const newKey = await (await db).add(STORAGE_NAME, newChatStore(chatStore));
await db
).add(
STORAGE_NAME,
newChatStore(
chatStore.apiKey,
chatStore.systemMessageContent,
chatStore.apiEndpoint,
chatStore.streamMode,
chatStore.model,
chatStore.temperature,
!!chatStore.develop_mode,
chatStore.whisper_api,
chatStore.whisper_key,
chatStore.tts_api,
chatStore.tts_key,
chatStore.tts_speed,
chatStore.tts_speed_enabled,
chatStore.tts_format,
chatStore.toolsString,
chatStore.image_gen_api,
chatStore.image_gen_key,
chatStore.json_mode,
false, // logprobs default to false
),
);
setSelectedChatIndex(newKey as number); setSelectedChatIndex(newKey as number);
setAllChatStoreIndexes(await (await db).getAllKeys(STORAGE_NAME)); setAllChatStoreIndexes(await (await db).getAllKeys(STORAGE_NAME));
}; };

View File

@@ -7,66 +7,82 @@ import getDefaultParams from "@/utils/getDefaultParam";
import { ChatStore } from "@/types/chatstore"; import { ChatStore } from "@/types/chatstore";
import { models } from "@/types/models"; import { models } from "@/types/models";
export const newChatStore = ( interface NewChatStoreOptions {
apiKey = "", apiKey?: string;
systemMessageContent = "", systemMessageContent?: string;
apiEndpoint = DefaultAPIEndpoint, apiEndpoint?: string;
streamMode = true, streamMode?: boolean;
model = DefaultModel, model?: string;
temperature = 0.7, temperature?: number;
dev = false, dev?: boolean;
whisper_api = "https://api.openai.com/v1/audio/transcriptions", whisper_api?: string;
whisper_key = "", whisper_key?: string;
tts_api = "https://api.openai.com/v1/audio/speech", tts_api?: string;
tts_key = "", tts_key?: string;
tts_speed = 1.0, tts_speed?: number;
tts_speed_enabled = false, tts_speed_enabled?: boolean;
tts_format = "mp3", tts_format?: string;
toolsString = "", toolsString?: string;
image_gen_api = "https://api.openai.com/v1/images/generations", image_gen_api?: string;
image_gen_key = "", image_gen_key?: string;
json_mode = false, json_mode?: boolean;
logprobs = false, logprobs?: boolean;
): ChatStore => { }
export const newChatStore = (options: NewChatStoreOptions): ChatStore => {
return { return {
chatgpt_api_web_version: CHATGPT_API_WEB_VERSION, chatgpt_api_web_version: CHATGPT_API_WEB_VERSION,
systemMessageContent: getDefaultParams("sys", systemMessageContent), systemMessageContent: getDefaultParams(
toolsString, "sys",
options.systemMessageContent ?? "",
),
toolsString: options.toolsString ?? "",
history: [], history: [],
postBeginIndex: 0, postBeginIndex: 0,
tokenMargin: 1024, tokenMargin: 1024,
totalTokens: 0, totalTokens: 0,
maxTokens: getDefaultParams( maxTokens: getDefaultParams(
"max", "max",
models[getDefaultParams("model", model)]?.maxToken ?? 2048, models[getDefaultParams("model", options.model ?? DefaultModel)]
?.maxToken ?? 2048,
), ),
maxGenTokens: 2048, maxGenTokens: 2048,
maxGenTokens_enabled: false, maxGenTokens_enabled: false,
apiKey: getDefaultParams("key", apiKey), apiKey: getDefaultParams("key", options.apiKey ?? ""),
apiEndpoint: getDefaultParams("api", apiEndpoint), apiEndpoint: getDefaultParams(
streamMode: getDefaultParams("mode", streamMode), "api",
model: getDefaultParams("model", model), options.apiEndpoint ?? DefaultAPIEndpoint,
),
streamMode: getDefaultParams("mode", options.streamMode ?? true),
model: getDefaultParams("model", options.model ?? DefaultModel),
responseModelName: "", responseModelName: "",
cost: 0, cost: 0,
temperature: getDefaultParams("temp", temperature), temperature: getDefaultParams("temp", options.temperature ?? 0.7),
temperature_enabled: true, temperature_enabled: true,
top_p: 1, top_p: 1,
top_p_enabled: false, top_p_enabled: false,
presence_penalty: 0, presence_penalty: 0,
frequency_penalty: 0, frequency_penalty: 0,
develop_mode: getDefaultParams("dev", dev), develop_mode: getDefaultParams("dev", options.dev ?? false),
whisper_api: getDefaultParams("whisper-api", whisper_api), whisper_api: getDefaultParams(
whisper_key: getDefaultParams("whisper-key", whisper_key), "whisper-api",
tts_api: getDefaultParams("tts-api", tts_api), options.whisper_api ?? "https://api.openai.com/v1/audio/transcriptions",
tts_key: getDefaultParams("tts-key", tts_key), ),
whisper_key: getDefaultParams("whisper-key", options.whisper_key ?? ""),
tts_api: getDefaultParams(
"tts-api",
options.tts_api ?? "https://api.openai.com/v1/audio/speech",
),
tts_key: getDefaultParams("tts-key", options.tts_key ?? ""),
tts_voice: "alloy", tts_voice: "alloy",
tts_speed: tts_speed, tts_speed: options.tts_speed ?? 1.0,
tts_speed_enabled: tts_speed_enabled, tts_speed_enabled: options.tts_speed_enabled ?? false,
image_gen_api: image_gen_api, image_gen_api:
image_gen_key: image_gen_key, options.image_gen_api ?? "https://api.openai.com/v1/images/generations",
json_mode: json_mode, image_gen_key: options.image_gen_key ?? "",
tts_format: tts_format, json_mode: options.json_mode ?? false,
logprobs, tts_format: options.tts_format ?? "mp3",
logprobs: options.logprobs ?? false,
contents_for_index: [], contents_for_index: [],
}; };
}; };

View File

@@ -1,6 +1,7 @@
function getDefaultParams(param: string, val: string): string; function getDefaultParams(param: string, val: string): string;
function getDefaultParams(param: string, val: number): number; function getDefaultParams(param: string, val: number): number;
function getDefaultParams(param: string, val: boolean): boolean; function getDefaultParams(param: string, val: boolean): boolean;
function getDefaultParams(param: any, val: any) { function getDefaultParams(param: any, val: any) {
const queryParameters = new URLSearchParams(window.location.search); const queryParameters = new URLSearchParams(window.location.search);
const get = queryParameters.get(param); const get = queryParameters.get(param);