|
1 | | -import { useMemo, useState } from 'react'; |
| 1 | +import { useMemo, useEffect, useState } from 'react'; |
2 | 2 | import { useAppContext } from '../utils/app.context'; |
3 | 3 | import { Message, PendingMessage } from '../utils/types'; |
4 | 4 | import { classNames } from '../utils/misc'; |
@@ -55,6 +55,22 @@ export default function ChatMessage({ |
55 | 55 | const nextSibling = siblingLeafNodeIds[siblingCurrIdx + 1]; |
56 | 56 | const prevSibling = siblingLeafNodeIds[siblingCurrIdx - 1]; |
57 | 57 |
|
| 58 | + const { getConversationTokenTotal, addTokensToConversation } = |
| 59 | + useAppContext(); |
| 60 | + const [hasAddedTokens, setHasAddedTokens] = useState(false); |
| 61 | + |
| 62 | + // Get current conversation token total |
| 63 | + const conversationTotal = getConversationTokenTotal(msg.convId); |
| 64 | + |
| 65 | + // Add tokens to running total when timings are available |
| 66 | + useEffect(() => { |
| 67 | + if (timings && !hasAddedTokens && msg.role === 'assistant') { |
| 68 | + const messageTokens = timings.prompt_n + timings.predicted_n; |
| 69 | + addTokensToConversation(msg.convId, messageTokens); |
| 70 | + setHasAddedTokens(true); |
| 71 | + } |
| 72 | + }, [timings, hasAddedTokens, msg.convId, msg.role, addTokensToConversation]); |
| 73 | + |
58 | 74 | // for reasoning model, we split the message into content and thought |
59 | 75 | // TODO: implement this as remark/rehype plugin in the future |
60 | 76 | const { content, thought, isThinking }: SplitMessage = useMemo(() => { |
@@ -175,19 +191,22 @@ export default function ChatMessage({ |
175 | 191 | role="button" |
176 | 192 | className="cursor-pointer font-semibold text-sm opacity-60" |
177 | 193 | > |
178 | | - Speed: {timings.predicted_per_second.toFixed(1)} t/s |
| 194 | + Speed test: {timings.predicted_per_second.toFixed(1)} t/s | |
| 195 | + Tokens: {timings.prompt_n + timings.predicted_n} this msg,{' '} |
| 196 | + {conversationTotal} total |
179 | 197 | </div> |
180 | 198 | <div className="dropdown-content bg-base-100 z-10 w-64 p-2 shadow mt-4"> |
181 | | - <b>Prompt</b> |
182 | | - <br />- Tokens: {timings.prompt_n} |
183 | | - <br />- Time: {timings.prompt_ms} ms |
184 | | - <br />- Speed: {timings.prompt_per_second.toFixed(1)} t/s |
185 | | - <br /> |
186 | | - <b>Generation</b> |
187 | | - <br />- Tokens: {timings.predicted_n} |
188 | | - <br />- Time: {timings.predicted_ms} ms |
189 | | - <br />- Speed: {timings.predicted_per_second.toFixed(1)} t/s |
| 199 | + <b>This Exchange</b> |
| 200 | + <br />- Prompt: {timings.prompt_n} tokens |
| 201 | + <br />- Generation: {timings.predicted_n} tokens |
| 202 | + <br />- Subtotal: {timings.prompt_n + |
| 203 | + timings.predicted_n}{' '} |
| 204 | + tokens |
| 205 | + <br />- Speed test:{' '} |
| 206 | + {timings.predicted_per_second.toFixed(1)} t/s |
190 | 207 | <br /> |
| 208 | + <b>Conversation Total</b> |
| 209 | + <br />- Used: {conversationTotal} tokens |
191 | 210 | </div> |
192 | 211 | </div> |
193 | 212 | )} |
|
0 commit comments