Skip to content

Commit

Permalink
update google-genai version and add method to create model from cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Chau Nguyen authored and KevinZJN committed Nov 13, 2024
1 parent a173e30 commit 5146e14
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions libs/langchain-google-genai/src/chat_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import {
GenerateContentRequest,
SafetySetting,
Part as GenerativeAIPart,
ModelParams,
RequestOptions,
} from "@google/generative-ai";
import {
CachedContentCreateParams,
GoogleAICacheManager as CacheManager,
} from '@google/generative-ai/server';
import { CallbackManagerForLLMRun } from "@langchain/core/callbacks/manager";
import {
AIMessageChunk,
Expand Down Expand Up @@ -77,7 +83,7 @@ export interface GoogleGenerativeAIChatCallOptions
*/
export interface GoogleGenerativeAIChatInput
extends BaseChatModelParams,
Pick<GoogleGenerativeAIChatCallOptions, "streamUsage"> {
Pick<GoogleGenerativeAIChatCallOptions, "streamUsage"> {
/**
* @deprecated Use "model" instead.
*
Expand Down Expand Up @@ -519,8 +525,7 @@ export interface GoogleGenerativeAIChatInput
*/
export class ChatGoogleGenerativeAI
extends BaseChatModel<GoogleGenerativeAIChatCallOptions, AIMessageChunk>
implements GoogleGenerativeAIChatInput
{
implements GoogleGenerativeAIChatInput {
static lc_name() {
return "ChatGoogleGenerativeAI";
}
Expand Down Expand Up @@ -563,6 +568,8 @@ export class ChatGoogleGenerativeAI

streamUsage = true;

cacheManager?: CacheManager;

private client: GenerativeModel;

get _isMultimodalModel() {
Expand Down Expand Up @@ -609,9 +616,9 @@ export class ChatGoogleGenerativeAI
if (!this.apiKey) {
throw new Error(
"Please set an API key for Google GenerativeAI " +
"in the environment variable GOOGLE_API_KEY " +
"or in the `apiKey` field of the " +
"ChatGoogleGenerativeAI constructor"
"in the environment variable GOOGLE_API_KEY " +
"or in the `apiKey` field of the " +
"ChatGoogleGenerativeAI constructor"
);
}

Expand Down Expand Up @@ -651,6 +658,25 @@ export class ChatGoogleGenerativeAI
this.streamUsage = fields?.streamUsage ?? this.streamUsage;
}

async addCachedContent(cachedContentCreateParams: CachedContentCreateParams,
modelParams?: Partial<ModelParams>, requestOptions?: RequestOptions
) {
if (!this.apiKey) {
throw new Error(
"Please set an API key for Google GenerativeAI " +
"in the environment variable GOOGLE_API_KEY " +
"or in the `apiKey` field of the " +
"ChatGoogleGenerativeAI constructor"
);
}
this.cacheManager = this.cacheManager ? this.cacheManager : new CacheManager(this.apiKey);
this.client = new GenerativeAI(this.apiKey)
.getGenerativeModelFromCachedContent(
await this.cacheManager.create(cachedContentCreateParams),
modelParams, requestOptions
);
}

getLsParams(options: this["ParsedCallOptions"]): LangSmithParams {
return {
ls_provider: "google_genai",
Expand Down Expand Up @@ -887,9 +913,9 @@ export class ChatGoogleGenerativeAI
):
| Runnable<BaseLanguageModelInput, RunOutput>
| Runnable<
BaseLanguageModelInput,
{ raw: BaseMessage; parsed: RunOutput }
> {
BaseLanguageModelInput,
{ raw: BaseMessage; parsed: RunOutput }
> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const schema: z.ZodType<RunOutput> | Record<string, any> = outputSchema;
const name = config?.name;
Expand Down

0 comments on commit 5146e14

Please sign in to comment.