Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display detailed usage with prompt and completion tokens in playground #461

Merged
merged 6 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
Timer,
TokenUsage,
} from '$/app/(private)/projects/[projectId]/versions/[commitUuid]/documents/[documentUuid]/_components/DocumentEditor/Editor/Playground/Chat'
import { LanguageModelUsage } from 'ai'
import { readStreamableValue } from 'ai/rsc'

export default function Chat({
Expand All @@ -34,7 +35,7 @@ export default function Chat({
parameters: Record<string, unknown>
}) {
const [error, setError] = useState<Error | undefined>()
const [tokens, setTokens] = useState<number>(0)
const [usage, setUsage] = useState<LanguageModelUsage | undefined>()
const [isScrolledToBottom, setIsScrolledToBottom] = useState(false)
const [startTime, _] = useState(performance.now())
const [endTime, setEndTime] = useState<number>()
Expand Down Expand Up @@ -99,7 +100,7 @@ export default function Chat({
if (data.isLastStep) setChainLength(messagesCount + 1)
} else if (data.type === ChainEventTypes.Complete) {
setResponseStream(undefined)
setTokens(data.response.usage.totalTokens)
setUsage(data.response.usage)
setEndTime(performance.now())
} else if (data.type === ChainEventTypes.Error) {
setError(new Error(data.error.message))
Expand Down Expand Up @@ -166,7 +167,7 @@ export default function Chat({
<div className='flex relative flex-row w-full items-center justify-center'>
<TokenUsage
isScrolledToBottom={isScrolledToBottom}
tokens={tokens}
usage={usage}
responseStream={responseStream}
/>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ import {
ChatTextArea,
cn,
ErrorMessage,
Icon,
Message,
MessageList,
Text,
Tooltip,
useAutoScroll,
useCurrentCommit,
useCurrentProject,
} from '@latitude-data/web-ui'
import { LanguageModelUsage } from 'ai'
import { readStreamableValue } from 'ai/rsc'

import { DocumentEditorContext } from '..'
Expand All @@ -43,7 +46,7 @@ export default function Chat({
DocumentEditorContext,
)!
const [error, setError] = useState<Error | undefined>()
const [tokens, setTokens] = useState<number>(0)
const [usage, setUsage] = useState<LanguageModelUsage | undefined>()
const [isScrolledToBottom, setIsScrolledToBottom] = useState(false)
const [time, setTime] = useState<number>()
const containerRef = useRef<HTMLDivElement>(null)
Expand Down Expand Up @@ -110,7 +113,7 @@ export default function Chat({
} else if (data.type === ChainEventTypes.StepComplete) {
response = ''
} else if (data.type === ChainEventTypes.Complete) {
setTokens(data.response.usage.totalTokens)
setUsage(data.response.usage)
setTime(performance.now() - start)
} else if (data.type === ChainEventTypes.Error) {
setError(new Error(data.error.message))
Expand Down Expand Up @@ -187,7 +190,7 @@ export default function Chat({
content: data.response.text,
} as AssistantMessage)

setTokens((prev) => prev + data.response.usage.totalTokens)
setUsage(data.response.usage)

setResponseStream(undefined)
}
Expand Down Expand Up @@ -254,7 +257,7 @@ export default function Chat({
<div className='flex relative flex-row w-full items-center justify-center'>
<TokenUsage
isScrolledToBottom={isScrolledToBottom}
tokens={tokens}
usage={usage}
responseStream={responseStream}
/>
<ChatTextArea
Expand Down Expand Up @@ -286,14 +289,14 @@ export function AnimatedDots() {

export function TokenUsage({
isScrolledToBottom,
tokens,
usage,
responseStream,
}: {
isScrolledToBottom: boolean
tokens: number
usage: LanguageModelUsage | undefined
responseStream: string | undefined
}) {
if (!tokens && responseStream === undefined) return null
if (!usage && responseStream === undefined) return null

return (
<div
Expand All @@ -305,7 +308,31 @@ export function TokenUsage({
)}
>
{responseStream === undefined ? (
<Text.H6M color='foregroundMuted'>{tokens} tokens</Text.H6M>
<div className='flex flex-row gap-2'>
<Text.H6M color='foregroundMuted'>
{usage?.totalTokens} tokens
</Text.H6M>
<Tooltip
side='right'
andresgutgon marked this conversation as resolved.
Show resolved Hide resolved
align='center'
sideOffset={15}
delayDuration={250}
trigger={
<div className='flex flex-row items-center gap-x-1'>
<Icon name='info' color='foregroundMuted' />
</div>
}
>
<div className='flex flex-col gap-2'>
<Text.H6M color='foregroundMuted'>
{usage?.promptTokens} prompt tokens
</Text.H6M>
<Text.H6M color='foregroundMuted'>
{usage?.completionTokens} completion tokens
</Text.H6M>
</div>
</Tooltip>
</div>
) : (
<AnimatedDots />
)}
Expand Down
Loading