Skip to content
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

feat: implement cache on getPrompt and getPromptById methods #86

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

Matthieu-OD
Copy link
Collaborator

Add Prompt Caching Functionality

Changes

  • Implemented a prompt caching system using a separate class SharedCachePrompt

Implementation Details

  • Cache keys are generated in three formats:

  • id

  • name

  • name:version

  • Caching logic implemented on both getPrompt and getPromptById

  • On each successful get prompt we will cache the prompt with keys id, name, name:version

  • Added fallback to cached prompts when API calls fail, with warning logs

  • To keep in mind this cache will be cleared each time the application Is restarted

@Matthieu-OD Matthieu-OD force-pushed the matt/eng-2115-add-client-caching-for-prompts branch from 2a95ead to 15c1428 Compare November 19, 2024 09:15
@Matthieu-OD Matthieu-OD self-assigned this Nov 19, 2024
@Matthieu-OD Matthieu-OD force-pushed the matt/eng-2115-add-client-caching-for-prompts branch from f04386c to 9625e9f Compare November 19, 2024 09:59
Copy link
Collaborator

@clementsirieix clementsirieix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks that's great, I'm being a bit cautious with adding core layers such as caching as we will need to reuse and maintain them.

src/api.ts Outdated
if (id) {
return id;
} else if (name && version) {
} else if (name && (version || version === 0)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is actually a way to do that explicitly:

Suggested change
} else if (name && (version || version === 0)) {
} else if (name && typeof version === "number") {

src/api.ts Outdated
Comment on lines 344 to 346
id?: string,
name?: string,
version?: number
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When handling only optional params in js it's recommended to use an object because it's not great ux to do:

instance.getPromptCacheKey(undefined, undefined, version)
// vs
instance.getPromptCacheKey({ version })

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now I understand why auto-complete suggested a object thx

src/api.ts Outdated
return SharedCache.instance;
}

public getPromptCacheKey(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that the cache is not prompt only, let's not leak that business logic in the cache layer.

src/api.ts Outdated Show resolved Hide resolved
src/api.ts Outdated
return this.get(key);
}

public putPrompt(prompt: Prompt): void {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

src/api.ts Outdated
@@ -325,6 +325,63 @@ type CreateAttachmentParams = {
metadata?: Maybe<Record<string, any>>;
};

export class SharedCache {
private static instance: SharedCache | null = null;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's simplify even more:

src/api.ts Outdated
@@ -340,6 +397,8 @@ type CreateAttachmentParams = {
* Then you can use the `api` object to make calls to the Literal service.
*/
export class API {
/** @ignore */
public cache: SharedCache;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be internal

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

used it to simplify my tests implementation and forgot to change it later

@Matthieu-OD Matthieu-OD force-pushed the matt/eng-2115-add-client-caching-for-prompts branch 2 times, most recently from 2cae4a1 to 856baf6 Compare November 21, 2024 16:03
@Matthieu-OD Matthieu-OD force-pushed the matt/eng-2115-add-client-caching-for-prompts branch from 856baf6 to f1902e7 Compare November 21, 2024 16:04
Copy link
Collaborator

@clementsirieix clementsirieix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there, 1 blocking and 2 nits.

src/api.ts Outdated Show resolved Hide resolved
src/api.ts Outdated Show resolved Hide resolved
src/cache/utils.ts Outdated Show resolved Hide resolved
@Matthieu-OD
Copy link
Collaborator Author

I'm going to wait for the PR on the python sdk to be approved to merge both at the same time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants