-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
133 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { CachedContentCreateParams, CachedContentUpdateParams, FileMetadata, FileMetadataResponse, GoogleAICacheManager, ListCacheResponse, ListFilesResponse, ListParams, UploadFileResponse } from "@google/generative-ai/server"; | ||
import { 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); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
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,76 @@ | ||
/* eslint-disable no-process-env */ | ||
|
||
import { test } from "@jest/globals"; | ||
|
||
import { ChatGoogleGenerativeAI } from "../chat_models.js"; | ||
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"; | ||
|
||
const model = new ChatGoogleGenerativeAI({}); | ||
let fileResult: UploadFileResponse; | ||
|
||
beforeAll(async () => { | ||
const displayName = 'Sherlock Jr. video'; | ||
|
||
const filename = fileURLToPath(import.meta.url); | ||
const dirname = path.dirname(filename); | ||
const pathToVideoFile = path.join(dirname, "/data/hotdog.jpg"); | ||
|
||
const contextCache = new GoogleGenerativeAIContextCache(process.env.GOOGLE_API_KEY || ""); | ||
fileResult = await contextCache.uploadFile(pathToVideoFile, { | ||
displayName, | ||
mimeType: 'video/mp4', | ||
}); | ||
|
||
const { name, uri } = 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) { | ||
console.log('Waiting for video to be processed.'); | ||
// Sleep for 2 seconds | ||
await new Promise((resolve) => setTimeout(resolve, 2_000)); | ||
file = await contextCache.getFile(name); | ||
} | ||
console.log(`Video processing complete: ${uri}`); | ||
|
||
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); | ||
}); | ||
|
||
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(); | ||
}); |