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 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
if (ret.maxGenTokens === undefined) ret.maxGenTokens = 2048;
if (ret.maxGenTokens_enabled === undefined) ret.maxGenTokens_enabled = true;
@@ -162,7 +162,7 @@ export function App() {
return ret;
};
const [chatStore, _setChatStore] = useState(newChatStore());
const [chatStore, _setChatStore] = useState(newChatStore({}));
const setChatStore = async (chatStore: ChatStore) => {
// building field for search
chatStore.contents_for_index = BuildFiledForSearch(chatStore);
@@ -212,32 +212,7 @@ export function App() {
);
const handleNewChatStoreWithOldOne = async (chatStore: ChatStore) => {
const newKey = await (
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
),
);
const newKey = await (await db).add(STORAGE_NAME, newChatStore(chatStore));
setSelectedChatIndex(newKey as number);
setAllChatStoreIndexes(await (await db).getAllKeys(STORAGE_NAME));
};

View File

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

View File

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