-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
204 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { isProModel } from '@/lib/model'; | ||
import { useSearchStore } from '@/lib/store/local-history'; | ||
import { useConfigStore, useSearchState } from '@/lib/store/local-store'; | ||
import { compressHistory } from '@/lib/tools/compress-history'; | ||
|
||
export function useCompressHistory() { | ||
const { activeSearch, updateActiveSearch } = useSearchStore(); | ||
const { isCompressHistory, setIsCompressHistory } = useSearchState(); | ||
|
||
const compressHistoryMessages = async () => { | ||
console.log('compressHistoryMessages'); | ||
if (isCompressHistory) return; | ||
if (!activeSearch?.messages) return; | ||
|
||
const messages = activeSearch.messages; | ||
const totalMessages = messages.length; | ||
if (totalMessages < 4) return; | ||
|
||
const model = useConfigStore.getState().model; | ||
if (!isProModel(model)) { | ||
return; | ||
} | ||
|
||
console.log('compressHistoryMessages totalMessages:', totalMessages); | ||
|
||
try { | ||
const compressIndex = activeSearch.lastCompressIndex || 0; | ||
const newMessagesToCompress = messages.slice(compressIndex); | ||
console.log('compressHistoryMessages newMessagesToCompress:', newMessagesToCompress, compressIndex); | ||
if (newMessagesToCompress.length < 4 || newMessagesToCompress.length > 6) { | ||
return; | ||
} | ||
setIsCompressHistory(true); | ||
const newSummary = await compressHistory(newMessagesToCompress, activeSearch.summary); | ||
if (newSummary.length > 0) { | ||
const newCompressIndex = totalMessages; | ||
updateActiveSearch({ summary: newSummary, lastCompressIndex: newCompressIndex }); | ||
} | ||
} catch (error) { | ||
console.error('Failed to compress history:', error); | ||
} finally { | ||
setIsCompressHistory(false); | ||
} | ||
}; | ||
return { compressHistoryMessages }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use server'; | ||
|
||
import { getLLM } from '@/lib/llm/llm'; | ||
import { formatHistoryMessages } from '@/lib/llm/utils'; | ||
import { GPT_4o_MIMI } from '@/lib/model'; | ||
import { Message } from '@/lib/types'; | ||
import { generateText } from 'ai'; | ||
|
||
export async function compressHistory(messages: Message[], previousSummary?: string): Promise<string> { | ||
if (messages.length < 4) { | ||
return ''; | ||
} | ||
|
||
try { | ||
console.log('compressHistory messages:', messages); | ||
console.log('compressHistory previousSummary:', previousSummary); | ||
console.time('compressHistory'); | ||
|
||
const systemPrompt = `You're an assistant who's good at extracting key takeaways from conversations and summarizing them. Please summarize according to the user's needs. ${ | ||
previousSummary | ||
? 'Please incorporate the previous summary with new messages to create an updated comprehensive summary.' | ||
: 'Create a new summary from the messages.' | ||
}`; | ||
|
||
const userPrompt = previousSummary | ||
? `Previous Summary: ${previousSummary}\n\nNew Messages: ${formatHistoryMessages(messages)}\n\nPlease create an updated summary incorporating both the previous summary and new messages. Limit to 400 tokens.` | ||
: `${formatHistoryMessages(messages)}\nPlease summarize the above conversation and retain key information. Limit to 400 tokens.`; | ||
|
||
const { text } = await generateText({ | ||
model: getLLM(GPT_4o_MIMI), | ||
messages: [ | ||
{ | ||
content: systemPrompt, | ||
role: 'system', | ||
}, | ||
{ | ||
content: userPrompt, | ||
role: 'user', | ||
}, | ||
], | ||
}); | ||
|
||
console.timeEnd('compressHistory'); | ||
console.log('compressHistory text:', text); | ||
return text; | ||
} catch (error) { | ||
console.error('Error compress history:', error); | ||
return previousSummary || ''; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.