Skip to content

Commit

Permalink
chore: add token speed
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-menlo committed Nov 27, 2023
1 parent bd6659f commit ca4b7e2
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion web/screens/Chat/SimpleTextMessage/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import React, { useContext } from 'react'
import React, { useContext, useEffect, useState } from 'react'

import { ChatCompletionRole, MessageStatus, ThreadMessage } from '@janhq/core'

Expand Down Expand Up @@ -51,6 +51,29 @@ const SimpleTextMessage: React.FC<ThreadMessage> = (props) => {
const parsedText = marked.parse(props.content ?? '')
const isUser = props.role === ChatCompletionRole.User
const isSystem = props.role === ChatCompletionRole.System
const [tokenCount, setTokenCount] = useState(0)

const [lastTimestamp, setLastTimestamp] = useState<number | undefined>()
const [tokenSpeed, setTokenSpeed] = useState(0)

useEffect(() => {
if (props.status === MessageStatus.Ready || !experimentalFeatureEnabed) {
return
}
const currentTimestamp = new Date().getTime() // Get current time in milliseconds
if (!lastTimestamp) {
// If this is the first update, just set the lastTimestamp and return
if (props.content !== '') setLastTimestamp(currentTimestamp)
return
}

const timeDiffInSeconds = (currentTimestamp - lastTimestamp) / 1000 // Time difference in seconds
const totalTokenCount = tokenCount + 1
const averageTokenSpeed = totalTokenCount / timeDiffInSeconds // Calculate average token speed

setTokenSpeed(averageTokenSpeed)
setTokenCount(totalTokenCount)
}, [props.content])

return (
<div className="group mx-auto rounded-xl px-4 lg:w-3/4">
Expand Down Expand Up @@ -88,6 +111,12 @@ const SimpleTextMessage: React.FC<ThreadMessage> = (props) => {
</>
)}
</div>
{experimentalFeatureEnabed &&
(props.status === MessageStatus.Pending || tokenSpeed > 0) && (
<p className="text-xs font-medium text-white">
Token Speed: {Number(tokenSpeed).toFixed(2)}/s
</p>
)}
</div>
)
}
Expand Down

0 comments on commit ca4b7e2

Please sign in to comment.