-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(google-genai): Context Caching (#7169)
Co-authored-by: Chau Nguyen <[email protected]> Co-authored-by: jacoblee93 <[email protected]>
- Loading branch information
1 parent
9791bc5
commit 5f62174
Showing
3 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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
84 changes: 84 additions & 0 deletions
84
libs/langchain-google-genai/src/tests/context_caching.int.test.ts
This file contains 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,84 @@ | ||
/* eslint-disable no-process-env */ | ||
|
||
import { test } from "@jest/globals"; | ||
|
||
import { fileURLToPath } from "node:url"; | ||
import * as path from "node:path"; | ||
|
||
import { | ||
FileState, | ||
UploadFileResponse, | ||
GoogleAIFileManager, | ||
GoogleAICacheManager, | ||
} from "@google/generative-ai/server"; | ||
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"); | ||
|
||
const contextCache = new GoogleAICacheManager( | ||
process.env.GOOGLE_API_KEY || "" | ||
); | ||
const fileCache = new GoogleAIFileManager(process.env.GOOGLE_API_KEY || ""); | ||
fileResult = await fileCache.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 fileCache.getFile(name); | ||
while (file.state === FileState.PROCESSING) { | ||
// Sleep for 2 seconds | ||
await new Promise((resolve) => { | ||
setTimeout(resolve, 2_000); | ||
}); | ||
file = await fileCache.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.create({ | ||
model: "models/gemini-1.5-flash-001", | ||
displayName: "gettysburg audio", | ||
systemInstruction, | ||
contents: [ | ||
{ | ||
role: "user", | ||
parts: [ | ||
{ | ||
fileData: { | ||
mimeType: fileResult.file.mimeType, | ||
fileUri: fileResult.file.uri, | ||
}, | ||
}, | ||
], | ||
}, | ||
], | ||
ttlSeconds: 300, | ||
}); | ||
|
||
model.useCachedContent(cachedContent); | ||
}, 10 * 60 * 1000); // Set timeout to 10 minutes to upload file | ||
|
||
test("Test Google AI", async () => { | ||
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(); | ||
}); |