-
Notifications
You must be signed in to change notification settings - Fork 2
feat: implement cache on getPrompt and getPromptById methods #86
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
Merged
Matthieu-OD
merged 11 commits into
main
from
matt/eng-2115-add-client-caching-for-prompts
Nov 29, 2024
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b3828dd
feat: implement cache class in api.ts
Matthieu-OD e2706f0
feat: adds cache to getPrompt methods
Matthieu-OD 15c1428
fix: tests and implementation of nameVersionIndex
Matthieu-OD 9625e9f
refactor: move logic to getPromptWithQuery
Matthieu-OD bbe697e
refactor: if null in response get from cache
Matthieu-OD fc9888a
feat: adds timeout logic
Matthieu-OD 0460dc4
feat: wip removing lock, simplifying and generalising
Matthieu-OD 3dab603
fix: finishes to fix all tests
Matthieu-OD f1902e7
feat: refactor and fix tests
Matthieu-OD b03d76d
feat: replace manager by utils
Matthieu-OD d2b3b53
refactor: apply clem comments
Matthieu-OD 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,36 @@ | ||
const cache: Map<string, any> = new Map(); | ||
|
||
class SharedCache { | ||
private static instance: SharedCache; | ||
|
||
public constructor() { | ||
if (SharedCache.instance) { | ||
throw new Error('SharedCache can only be created once'); | ||
} | ||
SharedCache.instance = this; | ||
} | ||
|
||
public getInstance(): SharedCache { | ||
return this; | ||
} | ||
|
||
public getCache(): Map<string, any> { | ||
return cache; | ||
} | ||
|
||
public get(key: string): any { | ||
return cache.get(key); | ||
} | ||
|
||
public put(key: string, value: any): void { | ||
cache.set(key, value); | ||
} | ||
|
||
public clear(): void { | ||
cache.clear(); | ||
} | ||
} | ||
|
||
export const sharedCache = new SharedCache(); | ||
|
||
export default sharedCache; |
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,27 @@ | ||
import { Prompt } from '../prompt-engineering/prompt'; | ||
import { sharedCache } from './sharedcache'; | ||
Matthieu-OD marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
export function getPromptCacheKey({ | ||
id, | ||
name, | ||
version | ||
}: { | ||
id?: string; | ||
name?: string; | ||
version?: number; | ||
}): string { | ||
if (id) { | ||
return id; | ||
} else if (name && typeof version === 'number') { | ||
return `${name}:${version}`; | ||
} else if (name) { | ||
return name; | ||
} | ||
throw new Error('Either id or name must be provided'); | ||
} | ||
|
||
export function putPrompt(prompt: Prompt): void { | ||
sharedCache.put(prompt.id, prompt); | ||
sharedCache.put(prompt.name, prompt); | ||
sharedCache.put(`${prompt.name}:${prompt.version}`, prompt); | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.