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(google-genai): Context Caching #7169

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions libs/langchain-google-genai/src/chat_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import {
GenerateContentRequest,
SafetySetting,
Part as GenerativeAIPart,
ModelParams,
RequestOptions,
CachedContent,
} from "@google/generative-ai";
import { CallbackManagerForLLMRun } from "@langchain/core/callbacks/manager";
import {
Expand Down Expand Up @@ -651,6 +654,21 @@ export class ChatGoogleGenerativeAI
this.streamUsage = fields?.streamUsage ?? this.streamUsage;
}

enableCachedContent(
cachedContent: CachedContent,
modelParams?: ModelParams,
requestOptions?: RequestOptions
): void {
if (!this.apiKey) return;
this.client = new GenerativeAI(
this.apiKey
).getGenerativeModelFromCachedContent(
cachedContent,
modelParams,
requestOptions
);
}

getLsParams(options: this["ParsedCallOptions"]): LangSmithParams {
return {
ls_provider: "google_genai",
Expand Down
88 changes: 88 additions & 0 deletions libs/langchain-google-genai/src/context_caching.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import {
CachedContentCreateParams,
CachedContentUpdateParams,
FileMetadata,
FileMetadataResponse,
GoogleAICacheManager,
ListCacheResponse,
ListFilesResponse,
ListParams,
UploadFileResponse,
GoogleAIFileManager,
} from "@google/generative-ai/server";
import {
CachedContent,
RequestOptions,
SingleRequestOptions,
} from "@google/generative-ai";

export class GoogleGenerativeAIContextCache {
private fileManager: GoogleAIFileManager;

private cacheManager: GoogleAICacheManager;

constructor(
apiKey: string,
fileManagerRequestOptions?: RequestOptions,
cacheManagerRequestOptions?: RequestOptions
) {
this.fileManager = new GoogleAIFileManager(
apiKey,
fileManagerRequestOptions
);
this.cacheManager = new GoogleAICacheManager(
apiKey,
cacheManagerRequestOptions
);
}

uploadFile(
filePath: string,
fileMetadata: FileMetadata
): Promise<UploadFileResponse> {
return this.fileManager.uploadFile(filePath, fileMetadata);
}

listFiles(
listParams?: ListParams,
requestOptions?: SingleRequestOptions
): Promise<ListFilesResponse> {
return this.fileManager.listFiles(listParams, requestOptions);
}

getFile(
fileId: string,
requestOptions?: SingleRequestOptions
): Promise<FileMetadataResponse> {
return this.fileManager.getFile(fileId, requestOptions);
}

deleteFile(fileId: string): Promise<void> {
return this.fileManager.deleteFile(fileId);
}

createCache(
createOptions: CachedContentCreateParams
): Promise<CachedContent> {
return this.cacheManager.create(createOptions);
}

listCaches(listParams?: ListParams): Promise<ListCacheResponse> {
return this.cacheManager.list(listParams);
}

getCache(name: string): Promise<CachedContent> {
return this.cacheManager.get(name);
}

updateCache(
name: string,
updateParams: CachedContentUpdateParams
): Promise<CachedContent> {
return this.cacheManager.update(name, updateParams);
}

deleteCache(name: string): Promise<void> {
return this.cacheManager.delete(name);
}
}
79 changes: 79 additions & 0 deletions libs/langchain-google-genai/src/tests/context_caching.int.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* eslint-disable no-process-env */

import { test } from "@jest/globals";

import { fileURLToPath } from "node:url";
import * as path from "node:path";

import { FileState, UploadFileResponse } from "@google/generative-ai/server";
import { GoogleGenerativeAIContextCache } from "../context_caching.js";
import { ChatGoogleGenerativeAI } from "../chat_models.js";

const model = new ChatGoogleGenerativeAI({});
let fileResult: UploadFileResponse;

beforeAll(async () => {
// Download video file and save in src/tests/data
// curl -O https://storage.googleapis.com/generativeai-downloads/data/Sherlock_Jr_FullMovie.mp4
const displayName = "Sherlock Jr. video";

const filename = fileURLToPath(import.meta.url);
const dirname = path.dirname(filename);
const pathToVideoFile = path.join(dirname, "/data/Sherlock_Jr_FullMovie.mp4");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we use a smaller file for this test? Maybe one of the files that already exist in the repo?


const contextCache = new GoogleGenerativeAIContextCache(
process.env.GOOGLE_API_KEY || ""
);
fileResult = await contextCache.uploadFile(pathToVideoFile, {
displayName,
mimeType: "video/mp4",
});

const { name } = fileResult.file;

// Poll getFile() on a set interval (2 seconds here) to check file state.
let file = await contextCache.getFile(name);
while (file.state === FileState.PROCESSING) {
// Sleep for 2 seconds
await new Promise((resolve) => {
setTimeout(resolve, 2_000);
});
file = await contextCache.getFile(name);
}

const systemInstruction =
"You are an expert video analyzer, and your job is to answer " +
"the user's query based on the video file you have access to.";
const cachedContent = await contextCache.createCache({
model: "models/gemini-1.5-flash-001",
displayName: "sherlock jr movie",
systemInstruction,
contents: [
{
role: "user",
parts: [
{
fileData: {
mimeType: fileResult.file.mimeType,
fileUri: fileResult.file.uri,
},
},
],
},
],
ttlSeconds: 300,
});

model.enableCachedContent(cachedContent);
}, 10 * 60 * 1000); // Set timeout to 10 minutes to upload file

test("Test Google AI", async () => {
jacoblee93 marked this conversation as resolved.
Show resolved Hide resolved
const res = await model.invoke(
"Introduce different characters in the movie by describing " +
"their personality, looks, and names. Also list the " +
"timestamps they were introduced for the first time."
);

console.log(res);
expect(res).toBeTruthy();
});