mirror of
https://github.com/NovaOSS/nova-betterchat.git
synced 2024-11-25 19:43:59 +01:00
feat: adaptive memory
fixes 9, fixed 10
This commit is contained in:
parent
5372c48f53
commit
c006ccd97a
|
@ -4,6 +4,7 @@ import { ChatInterface } from '@type/chat';
|
|||
import { getChatCompletionStream as getChatCompletionStreamFree } from '@api/freeApi';
|
||||
import { getChatCompletionStream as getChatCompletionStreamCustom } from '@api/customApi';
|
||||
import { parseEventSource } from '@api/helper';
|
||||
import { limitMessageTokens } from '@utils/messageUtils';
|
||||
|
||||
const useSubmit = () => {
|
||||
const error = useStore((state) => state.error);
|
||||
|
@ -31,15 +32,21 @@ const useSubmit = () => {
|
|||
let stream;
|
||||
|
||||
try {
|
||||
const messages = limitMessageTokens(
|
||||
chats[currentChatIndex].messages,
|
||||
4000
|
||||
);
|
||||
if (messages.length === 0) throw new Error('Message exceed max token!');
|
||||
|
||||
if (apiFree) {
|
||||
stream = await getChatCompletionStreamFree(
|
||||
chats[currentChatIndex].messages,
|
||||
messages,
|
||||
chats[currentChatIndex].config
|
||||
);
|
||||
} else if (apiKey) {
|
||||
stream = await getChatCompletionStreamCustom(
|
||||
apiKey,
|
||||
chats[currentChatIndex].messages,
|
||||
messages,
|
||||
chats[currentChatIndex].config
|
||||
);
|
||||
}
|
||||
|
|
19
src/utils/messageUtils.ts
Normal file
19
src/utils/messageUtils.ts
Normal 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;
|
||||
};
|
|
@ -23,7 +23,8 @@
|
|||
"@hooks/*": ["./src/hooks/*"],
|
||||
"@constants/*": ["./src/constants/*"],
|
||||
"@api/*": ["./src/api/*"],
|
||||
"@components/*": ["./src/components/*"]
|
||||
"@components/*": ["./src/components/*"],
|
||||
"@utils/*": ["./src/utils/*"]
|
||||
}
|
||||
},
|
||||
"include": ["src"],
|
||||
|
|
|
@ -13,6 +13,7 @@ export default defineConfig({
|
|||
'@constants/': new URL('./src/constants/', import.meta.url).pathname,
|
||||
'@api/': new URL('./src/api/', import.meta.url).pathname,
|
||||
'@components/': new URL('./src/components/', import.meta.url).pathname,
|
||||
'@utils/': new URL('./src/utils/', import.meta.url).pathname,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue