feat: add tokenlens integration for language model usage tracking (#1161)
This commit is contained in:
parent
7327c9a18a
commit
1e539b65b3
8 changed files with 363 additions and 6 deletions
|
|
@ -1,6 +1,6 @@
|
|||
'use client';
|
||||
|
||||
import { DefaultChatTransport } from 'ai';
|
||||
import { DefaultChatTransport, LanguageModelUsage } from 'ai';
|
||||
import { useChat } from '@ai-sdk/react';
|
||||
import { useEffect, useState } from 'react';
|
||||
import useSWR, { useSWRConfig } from 'swr';
|
||||
|
|
@ -49,6 +49,7 @@ export function Chat({
|
|||
const { setDataStream } = useDataStream();
|
||||
|
||||
const [input, setInput] = useState<string>('');
|
||||
const [usage, setUsage] = useState<LanguageModelUsage | undefined>(undefined);
|
||||
|
||||
const {
|
||||
messages,
|
||||
|
|
@ -80,6 +81,9 @@ export function Chat({
|
|||
}),
|
||||
onData: (dataPart) => {
|
||||
setDataStream((ds) => (ds ? [...ds, dataPart] : []));
|
||||
if (dataPart.type === 'data-usage') {
|
||||
setUsage(dataPart.data);
|
||||
}
|
||||
},
|
||||
onFinish: () => {
|
||||
mutate(unstable_serialize(getChatHistoryPaginationKey));
|
||||
|
|
@ -163,6 +167,7 @@ export function Chat({
|
|||
sendMessage={sendMessage}
|
||||
selectedVisibilityType={visibilityType}
|
||||
selectedModelId={initialChatModel}
|
||||
usage={usage}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue