Skip to content

Commit

Permalink
test(js/plugins): add context caching unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cabljac committed Dec 6, 2024
1 parent 9bc336c commit 7130f68
Show file tree
Hide file tree
Showing 6 changed files with 1,053 additions and 19 deletions.
2 changes: 1 addition & 1 deletion js/plugins/googleai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"build:clean": "rimraf ./lib",
"build": "npm-run-all build:clean check compile",
"build:watch": "tsup-node --watch",
"test": "tsx --test ./tests/*_test.ts"
"test": "tsx --test ./tests/**/*_test.ts"
},
"repository": {
"type": "git",
Expand Down
58 changes: 43 additions & 15 deletions js/plugins/googleai/src/context-caching/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,27 @@ export function getContentForCache(
chatRequest: StartChatParams;
cacheConfig?: CacheConfig;
} {
// Ensure modelVersion is provided
if (!modelVersion) {
throw new Error('No model version provided for context caching');
}

// Ensure chatRequest has a history
if (!chatRequest.history?.length) {
throw new Error('No history provided for context caching');
}

// Validate the history length between request and chatRequest
validateHistoryLength(request, chatRequest);

// Extract relevant cached content based on cacheConfigDetails
const { endOfCachedContents, cacheConfig } = cacheConfigDetails;
const cachedContent: CachedContent = {
model: modelVersion,
contents: chatRequest.history.slice(0, endOfCachedContents + 1),
};

// Update the chatRequest history to only include non-cached parts
chatRequest.history = chatRequest.history.slice(endOfCachedContents + 1);

return { cachedContent, chatRequest, cacheConfig };
Expand All @@ -84,6 +94,9 @@ function validateHistoryLength(
}
}

/**
* Looks up context cache using a cache manager and returns the found item, if any.
*/
/**
* Looks up context cache using a cache manager and returns the found item, if any.
*/
Expand All @@ -92,26 +105,41 @@ export async function lookupContextCache(
cacheKey: string,
maxPages = 100,
pageSize = 100
) {
): Promise<CachedContent | null> {
let currentPage = 0;
let pageToken: string | undefined;

while (currentPage < maxPages) {
const { cachedContents, nextPageToken } = await cacheManager.list({
pageSize,
pageToken,
});
const found = cachedContents?.find(
(content) => content.displayName === cacheKey
);
try {
while (currentPage < maxPages) {
const { cachedContents, nextPageToken } = await cacheManager.list({
pageSize,
pageToken,
});

if (found) return found;
if (!nextPageToken) break;
// Check for the cached content by key
const found = cachedContents?.find(
(content) => content.displayName === cacheKey
);

pageToken = nextPageToken;
currentPage++;
if (found) return found; // Return found content

// Stop if there's no next page
if (!nextPageToken) break;

pageToken = nextPageToken;
currentPage++;
}
} catch (error) {
const message =
error instanceof Error ? error.message : 'Unknown Network Error';

throw new GenkitError({
status: 'INTERNAL',
message: `Error looking up context cache: ${message}`,
});
}
return null;

return null; // Return null if not found or on error
}

/**
Expand Down Expand Up @@ -168,7 +196,7 @@ export function validateContextCacheRequest(
/**
* Polyfill function for Array.prototype.findLastIndex for ES2015 compatibility.
*/
function findLastIndex<T>(
export function findLastIndex<T>(
array: T[],
callback: (element: T, index: number, array: T[]) => boolean
): number {
Expand Down
Loading

0 comments on commit 7130f68

Please sign in to comment.