prevent undefined on new models

This commit is contained in:
2023-03-31 15:00:57 +08:00
parent 464e417537
commit 11d9b09e36
2 changed files with 7 additions and 5 deletions

View File

@@ -44,7 +44,7 @@ const newChatStore = (
postBeginIndex: 0, postBeginIndex: 0,
tokenMargin: 1024, tokenMargin: 1024,
totalTokens: 0, totalTokens: 0,
maxTokens: models[getDefaultParams("model", model)].maxToken, maxTokens: models[getDefaultParams("model", model)]?.maxToken ?? 4096,
apiKey: getDefaultParams("key", apiKey), apiKey: getDefaultParams("key", apiKey),
apiEndpoint: getDefaultParams("api", apiEndpoint), apiEndpoint: getDefaultParams("api", apiEndpoint),
streamMode: getDefaultParams("mode", streamMode), streamMode: getDefaultParams("mode", streamMode),

View File

@@ -87,7 +87,8 @@ export default function ChatBOX(props: {
// estimate cost // estimate cost
if (chatStore.responseModelName) { if (chatStore.responseModelName) {
chatStore.cost += chatStore.cost +=
token * models[chatStore.responseModelName].price.completion; token *
(models[chatStore.responseModelName]?.price?.completion ?? 0);
let sum = 0; let sum = 0;
for (const msg of chatStore.history for (const msg of chatStore.history
.filter(({ hide }) => !hide) .filter(({ hide }) => !hide)
@@ -95,7 +96,7 @@ export default function ChatBOX(props: {
sum += msg.token; sum += msg.token;
} }
chatStore.cost += chatStore.cost +=
sum * models[chatStore.responseModelName].price.prompt; sum * (models[chatStore.responseModelName]?.price?.prompt ?? 0);
} }
chatStore.history.push({ chatStore.history.push({
role: "assistant", role: "assistant",
@@ -119,10 +120,11 @@ export default function ChatBOX(props: {
chatStore.responseModelName = data.model ?? ""; chatStore.responseModelName = data.model ?? "";
if (data.model) { if (data.model) {
chatStore.cost += chatStore.cost +=
(data.usage.prompt_tokens ?? 0) * models[data.model].price.prompt; (data.usage.prompt_tokens ?? 0) *
(models[data.model]?.price?.prompt ?? 0);
chatStore.cost += chatStore.cost +=
(data.usage.completion_tokens ?? 0) * (data.usage.completion_tokens ?? 0) *
models[data.model].price.completion; (models[data.model]?.price?.completion ?? 0);
} }
const content = client.processFetchResponse(data); const content = client.processFetchResponse(data);
chatStore.history.push({ chatStore.history.push({