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

View File

@@ -6,6 +6,7 @@ interface Props {
messageIndex: number; messageIndex: number;
chatStore: ChatStore; chatStore: ChatStore;
setChatStore: (cs: ChatStore) => void; setChatStore: (cs: ChatStore) => void;
update_total_tokens: () => void;
} }
export default function Message(props: Props) { export default function Message(props: Props) {
const { chatStore, messageIndex, setChatStore } = props; const { chatStore, messageIndex, setChatStore } = props;
@@ -70,7 +71,16 @@ export default function Message(props: Props) {
</div> </div>
{chatStore.develop_mode && ( {chatStore.develop_mode && (
<div> <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 <button
onClick={() => { onClick={() => {
chatStore.history.splice(messageIndex, 1); chatStore.history.splice(messageIndex, 1);
@@ -95,7 +105,7 @@ export default function Message(props: Props) {
{showEdit && ( {showEdit && (
<div <div
className={ 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 <textarea
@@ -103,6 +113,7 @@ export default function Message(props: Props) {
value={chat.content} value={chat.content}
onChange={(event: any) => { onChange={(event: any) => {
chat.content = event.target.value; chat.content = event.target.value;
chat.token = calculate_token_length(chat.content);
setChatStore({ ...chatStore }); setChatStore({ ...chatStore });
}} }}
></textarea> ></textarea>