Compare commits

...

9 Commits

2 changed files with 34 additions and 12 deletions

View File

@@ -46,6 +46,7 @@ export default function ChatBOX(props: {
};
const _completeWithStreamMode = async (response: Response) => {
let responseTokenCount = 0;
chatStore.streamMode = true;
// call api, return reponse text
console.log("response", response);
@@ -70,10 +71,12 @@ export default function ChatBOX(props: {
if (!i) return false;
if (i === "data: [DONE]" || i === "data:[DONE]") {
responseDone = true;
responseTokenCount += 1;
return false;
}
return true;
});
responseTokenCount += lines.length;
console.log("lines", lines);
const jsons: ChunkMessage[] = lines
.map((line) => {
@@ -105,13 +108,12 @@ export default function ChatBOX(props: {
// console.log("push to history", allChunkMessage);
const content = allChunkMessage.join("");
const token = calculate_token_length(content);
// estimate cost
let cost = 0;
if (chatStore.responseModelName) {
cost +=
token *
responseTokenCount *
(models[chatStore.responseModelName]?.price?.completion ?? 0);
let sum = 0;
for (const msg of chatStore.history
@@ -129,7 +131,7 @@ export default function ChatBOX(props: {
role: "assistant",
content,
hide: false,
token,
token: responseTokenCount,
});
// manually copy status from client to chatStore
chatStore.maxTokens = client.max_tokens;
@@ -169,7 +171,10 @@ export default function ChatBOX(props: {
if (data.usage.prompt_tokens) {
const userMessageToken = data.usage.prompt_tokens - aboveToken;
console.log("set user message token");
chatStore.history.slice(-1)[0].token = userMessageToken;
if (chatStore.history.filter((msg) => !msg.hide).length > 0) {
chatStore.history.filter((msg) => !msg.hide).slice(-1)[0].token =
userMessageToken;
}
}
chatStore.history.push({
@@ -187,6 +192,10 @@ export default function ChatBOX(props: {
client.apiEndpoint = chatStore.apiEndpoint;
client.sysMessageContent = chatStore.systemMessageContent;
client.tokens_margin = chatStore.tokenMargin;
client.temperature = chatStore.temperature;
client.top_p = chatStore.top_p;
client.frequency_penalty = chatStore.frequency_penalty;
client.presence_penalty = chatStore.presence_penalty;
client.messages = chatStore.history
// only copy non hidden message
.filter(({ hide }) => !hide)
@@ -218,6 +227,7 @@ export default function ChatBOX(props: {
chatStore.totalTokens = client.total_tokens;
console.log("postBeginIndex", chatStore.postBeginIndex);
setShowRetry(false);
setChatStore({ ...chatStore });
} catch (error) {
setShowRetry(true);
@@ -342,8 +352,15 @@ export default function ChatBOX(props: {
chatStore={chatStore}
setChatStore={setChatStore}
messageIndex={messageIndex}
update_total_tokens={update_total_tokens}
/>
))}
{showGenerating && (
<p className="p-2 my-2 animate-pulse dark:text-white message-content">
{generatingMessage || "生成中,最长可能需要一分钟,请保持网络稳定"}
...
</p>
)}
{chatStore.develop_mode && (
<p className="text-center rounded">
<button
@@ -371,12 +388,6 @@ export default function ChatBOX(props: {
</button>
</p>
)}
{showGenerating && (
<p className="p-2 my-2 animate-pulse dark:text-white message-content">
{generatingMessage || "生成中,最长可能需要一分钟,请保持网络稳定"}
...
</p>
)}
<p className="p-2 my-2 text-center opacity-50 dark:text-white">
{chatStore.responseModelName && (
<>Generated by {chatStore.responseModelName}</>

View File

@@ -6,6 +6,7 @@ interface Props {
messageIndex: number;
chatStore: ChatStore;
setChatStore: (cs: ChatStore) => void;
update_total_tokens: () => void;
}
export default function Message(props: Props) {
const { chatStore, messageIndex, setChatStore } = props;
@@ -70,7 +71,16 @@ export default function Message(props: Props) {
</div>
{chatStore.develop_mode && (
<div>
token {chatStore.history[messageIndex].token}
token{" "}
<input
value={chat.token}
className="w-20"
onChange={(event: any) => {
chat.token = parseInt(event.target.value);
props.update_total_tokens();
setChatStore({ ...chatStore });
}}
/>
<button
onClick={() => {
chatStore.history.splice(messageIndex, 1);
@@ -95,7 +105,7 @@ export default function Message(props: Props) {
{showEdit && (
<div
className={
"absolute bg-black bg-opacity-50 w-full h-full top-0 left-0 pt-5 px-5 pb-20 rounded"
"absolute bg-black bg-opacity-50 w-full h-full top-0 left-0 pt-5 px-5 pb-20 rounded z-10"
}
>
<textarea
@@ -103,6 +113,7 @@ export default function Message(props: Props) {
value={chat.content}
onChange={(event: any) => {
chat.content = event.target.value;
chat.token = calculate_token_length(chat.content);
setChatStore({ ...chatStore });
}}
></textarea>