-
Notifications
You must be signed in to change notification settings - Fork 0
β‘ [performance] Precalculate chat history mapping in fallback chain #69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
APPLEPIE6969
wants to merge
1
commit into
main
Choose a base branch
from
fix-chat-mapping-optimization-7292491612743030950
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,74 @@ | ||
|
|
||
| export interface ChatMessage { | ||
| role: "user" | "ai" | "system"; | ||
| content: string; | ||
| } | ||
|
|
||
| const history: ChatMessage[] = Array.from({ length: 20 }, (_, i) => ({ | ||
| role: i % 2 === 0 ? "user" : "ai", | ||
| content: "This is some content for the chat message " + i | ||
| })); | ||
|
|
||
| const ITERATIONS = 100_000; | ||
|
|
||
| function benchmarkRepeatedMapping() { | ||
| const start = performance.now(); | ||
| for (let i = 0; i < ITERATIONS; i++) { | ||
| // Simulating the 4 blocks in generateTutorResponse | ||
| const h1 = history.map(msg => ({ | ||
| role: msg.role === "user" ? "user" : "model", | ||
| parts: [{ text: msg.content }], | ||
| })); | ||
| const h2 = history.map(msg => ({ | ||
| role: msg.role === "user" ? "user" : "model", | ||
| parts: [{ text: msg.content }], | ||
| })); | ||
| const h3 = history.map(h => ({ | ||
| role: h.role === "ai" ? "assistant" : "user" as any, | ||
| content: h.content | ||
| })); | ||
| const h4 = history.map(h => ({ | ||
| role: h.role === "ai" ? "assistant" : "user" as any, | ||
| content: h.content | ||
| })); | ||
|
|
||
| if (h1.length === 0 || h2.length === 0 || h3.length === 0 || h4.length === 0) { /* prevent optimize */ } | ||
| } | ||
| return performance.now() - start; | ||
| } | ||
|
|
||
| function benchmarkPrecalculatedMapping() { | ||
| const start = performance.now(); | ||
| for (let i = 0; i < ITERATIONS; i++) { | ||
| const geminiHistory = history.map(msg => ({ | ||
| role: msg.role === "user" ? "user" : "model", | ||
| parts: [{ text: msg.content }], | ||
| })); | ||
| const groqHistory = history.map(h => ({ | ||
| role: h.role === "ai" ? "assistant" : "user" as any, | ||
| content: h.content | ||
| })); | ||
|
|
||
| // Simulating use in 4 blocks | ||
| const h1 = geminiHistory; | ||
| const h2 = geminiHistory; | ||
| const h3 = groqHistory; | ||
| const h4 = groqHistory; | ||
|
|
||
| if (h1.length === 0 || h2.length === 0 || h3.length === 0 || h4.length === 0) { /* prevent optimize */ } | ||
| } | ||
| return performance.now() - start; | ||
| } | ||
|
|
||
| console.log(`Benchmarking with ${ITERATIONS} iterations and history length ${history.length}...`); | ||
|
|
||
| const repeatedTime = benchmarkRepeatedMapping(); | ||
| console.log(`Repeated mapping: ${repeatedTime.toFixed(2)}ms`); | ||
|
|
||
| const precalculatedTime = benchmarkPrecalculatedMapping(); | ||
| console.log(`Precalculated mapping: ${precalculatedTime.toFixed(2)}ms`); | ||
|
|
||
| if (repeatedTime > 0) { | ||
| const improvement = ((repeatedTime - precalculatedTime) / repeatedTime * 100).toFixed(2); | ||
| console.log(`Improvement: ${improvement}%`); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π§© Analysis chain
π Script executed:
Repository: APPLEPIE6969/StudyFlow
Length of output: 269
Remove
as anycasts that violate@typescript-eslint/no-explicit-any.Lines 27, 31, and 48 in
lib/chat_optimization.bench.tscontainas anycasts that will fail lint gates. Extract the repeated role-mapping logic into a typed helper function:Suggested fix
π§° Tools
πͺ ESLint
[error] 27-27: Unexpected any. Specify a different type.
(
@typescript-eslint/no-explicit-any)[error] 31-31: Unexpected any. Specify a different type.
(
@typescript-eslint/no-explicit-any)π€ Prompt for AI Agents