feat: adaptive memory

fixes 9, fixed 10
This commit is contained in:
Jing Hua 2023-03-06 22:50:03 +08:00
parent 5372c48f53
commit c006ccd97a
4 changed files with 31 additions and 3 deletions

View file

@ -4,6 +4,7 @@ import { ChatInterface } from '@type/chat';
import { getChatCompletionStream as getChatCompletionStreamFree } from '@api/freeApi'; import { getChatCompletionStream as getChatCompletionStreamFree } from '@api/freeApi';
import { getChatCompletionStream as getChatCompletionStreamCustom } from '@api/customApi'; import { getChatCompletionStream as getChatCompletionStreamCustom } from '@api/customApi';
import { parseEventSource } from '@api/helper'; import { parseEventSource } from '@api/helper';
import { limitMessageTokens } from '@utils/messageUtils';
const useSubmit = () => { const useSubmit = () => {
const error = useStore((state) => state.error); const error = useStore((state) => state.error);
@ -31,15 +32,21 @@ const useSubmit = () => {
let stream; let stream;
try { try {
const messages = limitMessageTokens(
chats[currentChatIndex].messages,
4000
);
if (messages.length === 0) throw new Error('Message exceed max token!');
if (apiFree) { if (apiFree) {
stream = await getChatCompletionStreamFree( stream = await getChatCompletionStreamFree(
chats[currentChatIndex].messages, messages,
chats[currentChatIndex].config chats[currentChatIndex].config
); );
} else if (apiKey) { } else if (apiKey) {
stream = await getChatCompletionStreamCustom( stream = await getChatCompletionStreamCustom(
apiKey, apiKey,
chats[currentChatIndex].messages, messages,
chats[currentChatIndex].config chats[currentChatIndex].config
); );
} }

19
src/utils/messageUtils.ts Normal file
View file

@ -0,0 +1,19 @@
import { MessageInterface } from '@type/chat';
import countTokens from './countTokens';
export const limitMessageTokens = (
messages: MessageInterface[],
limit: number = 4096
): MessageInterface[] => {
const limitedMessages: MessageInterface[] = [];
let tokenCount = 0;
for (let i = messages.length - 1; i >= 0; i--) {
const count = countTokens(messages[i].content);
if (count + tokenCount > limit) break;
tokenCount += count;
limitedMessages.unshift({ ...messages[i] });
}
return limitedMessages;
};

View file

@ -23,7 +23,8 @@
"@hooks/*": ["./src/hooks/*"], "@hooks/*": ["./src/hooks/*"],
"@constants/*": ["./src/constants/*"], "@constants/*": ["./src/constants/*"],
"@api/*": ["./src/api/*"], "@api/*": ["./src/api/*"],
"@components/*": ["./src/components/*"] "@components/*": ["./src/components/*"],
"@utils/*": ["./src/utils/*"]
} }
}, },
"include": ["src"], "include": ["src"],

View file

@ -13,6 +13,7 @@ export default defineConfig({
'@constants/': new URL('./src/constants/', import.meta.url).pathname, '@constants/': new URL('./src/constants/', import.meta.url).pathname,
'@api/': new URL('./src/api/', import.meta.url).pathname, '@api/': new URL('./src/api/', import.meta.url).pathname,
'@components/': new URL('./src/components/', import.meta.url).pathname, '@components/': new URL('./src/components/', import.meta.url).pathname,
'@utils/': new URL('./src/utils/', import.meta.url).pathname,
}, },
}, },
}); });