@lobehub/chat
Version:
Lobe Chat - an open-source, high-performance chatbot framework that supports speech synthesis, multimodal, and extensible Function Call plugin system. Supports one-click free deployment of your private ChatGPT/LLM web application.
33 lines (26 loc) • 693 B
text/typescript
import { debounce } from 'lodash-es';
import { startTransition, useCallback, useEffect, useState } from 'react';
import { encodeAsync } from '@/utils/tokenizer';
export const useTokenCount = (input: string = '') => {
const [value, setNum] = useState(0);
const debouncedEncode = useCallback(
debounce((text: string) => {
encodeAsync(text)
.then(setNum)
.catch(() => {
setNum(text.length);
});
}, 300),
[],
);
useEffect(() => {
startTransition(() => {
debouncedEncode(input || '');
});
// 清理函数
return () => {
debouncedEncode.cancel();
};
}, [input, debouncedEncode]);
return value;
};