From efa2f89a040520a4332eda4791431758f936702b Mon Sep 17 00:00:00 2001 From: juji Date: Tue, 13 Aug 2024 22:07:53 +0700 Subject: [PATCH 1/3] support json output for google-genai --- libs/langchain-google-genai/src/chat_models.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/libs/langchain-google-genai/src/chat_models.ts b/libs/langchain-google-genai/src/chat_models.ts index ddd407df6031..79408f21843f 100644 --- a/libs/langchain-google-genai/src/chat_models.ts +++ b/libs/langchain-google-genai/src/chat_models.ts @@ -167,6 +167,9 @@ export interface GoogleGenerativeAIChatInput /** Whether to stream the results or not */ streaming?: boolean; + + /** Whether the response will be json or not */ + json?: boolean; } /** @@ -244,6 +247,8 @@ export class ChatGoogleGenerativeAI streamUsage = true; + json = false + private client: GenerativeModel; get _isMultimodalModel() { @@ -310,6 +315,8 @@ export class ChatGoogleGenerativeAI this.streaming = fields?.streaming ?? this.streaming; + this.json = fields?.json ?? false + this.client = new GenerativeAI(this.apiKey).getGenerativeModel( { model: this.model, @@ -321,6 +328,7 @@ export class ChatGoogleGenerativeAI temperature: this.temperature, topP: this.topP, topK: this.topK, + ...this.json ? { responseMimeType: "application/json" } : {} }, }, { From 66f5e68e59ba6e1309bd1d5cff51c7fff38b7067 Mon Sep 17 00:00:00 2001 From: Brace Sproul Date: Thu, 15 Aug 2024 14:19:56 -0700 Subject: [PATCH 2/3] Apply suggestions from code review --- libs/langchain-google-genai/src/chat_models.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/libs/langchain-google-genai/src/chat_models.ts b/libs/langchain-google-genai/src/chat_models.ts index 79408f21843f..6f6402fc21ae 100644 --- a/libs/langchain-google-genai/src/chat_models.ts +++ b/libs/langchain-google-genai/src/chat_models.ts @@ -168,7 +168,10 @@ export interface GoogleGenerativeAIChatInput /** Whether to stream the results or not */ streaming?: boolean; - /** Whether the response will be json or not */ + /** + * Whether or not to force the model to respond with JSON. + * @default false + */ json?: boolean; } @@ -247,7 +250,6 @@ export class ChatGoogleGenerativeAI streamUsage = true; - json = false private client: GenerativeModel; @@ -315,7 +317,6 @@ export class ChatGoogleGenerativeAI this.streaming = fields?.streaming ?? this.streaming; - this.json = fields?.json ?? false this.client = new GenerativeAI(this.apiKey).getGenerativeModel( { @@ -328,7 +329,7 @@ export class ChatGoogleGenerativeAI temperature: this.temperature, topP: this.topP, topK: this.topK, - ...this.json ? { responseMimeType: "application/json" } : {} + ...fields?.json ? { responseMimeType: "application/json" } : {} }, }, { From 1dd8bd3ce87e6567486603e21d7425132ad9c382 Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Fri, 16 Aug 2024 02:38:49 -0700 Subject: [PATCH 3/3] Add test --- .../langchain-google-genai/src/chat_models.ts | 7 ++++--- .../src/tests/chat_models.int.test.ts | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/libs/langchain-google-genai/src/chat_models.ts b/libs/langchain-google-genai/src/chat_models.ts index 6f6402fc21ae..197febd63bbb 100644 --- a/libs/langchain-google-genai/src/chat_models.ts +++ b/libs/langchain-google-genai/src/chat_models.ts @@ -75,6 +75,8 @@ export interface GoogleGenerativeAIChatInput extends BaseChatModelParams, Pick { /** + * @deprecated Use "model" instead. + * * Model Name to use * * Alias for `model` @@ -170,6 +172,7 @@ export interface GoogleGenerativeAIChatInput /** * Whether or not to force the model to respond with JSON. + * Available for `gemini-1.5` models and later. * @default false */ json?: boolean; @@ -250,7 +253,6 @@ export class ChatGoogleGenerativeAI streamUsage = true; - private client: GenerativeModel; get _isMultimodalModel() { @@ -317,7 +319,6 @@ export class ChatGoogleGenerativeAI this.streaming = fields?.streaming ?? this.streaming; - this.client = new GenerativeAI(this.apiKey).getGenerativeModel( { model: this.model, @@ -329,7 +330,7 @@ export class ChatGoogleGenerativeAI temperature: this.temperature, topP: this.topP, topK: this.topK, - ...fields?.json ? { responseMimeType: "application/json" } : {} + ...(fields?.json ? { responseMimeType: "application/json" } : {}), }, }, { diff --git a/libs/langchain-google-genai/src/tests/chat_models.int.test.ts b/libs/langchain-google-genai/src/tests/chat_models.int.test.ts index 4e4791495759..ddf5aee6bfc7 100644 --- a/libs/langchain-google-genai/src/tests/chat_models.int.test.ts +++ b/libs/langchain-google-genai/src/tests/chat_models.int.test.ts @@ -519,3 +519,22 @@ test("Invoke token count usage_metadata", async () => { res.usage_metadata.input_tokens + res.usage_metadata.output_tokens ); }); + +test("Invoke with JSON mode", async () => { + const model = new ChatGoogleGenerativeAI({ + model: "gemini-1.5-flash", + temperature: 0, + maxOutputTokens: 10, + json: true, + }); + const res = await model.invoke("Why is the sky blue? Be concise."); + expect(res?.usage_metadata).toBeDefined(); + if (!res?.usage_metadata) { + return; + } + expect(res.usage_metadata.input_tokens).toBeGreaterThan(1); + expect(res.usage_metadata.output_tokens).toBeGreaterThan(1); + expect(res.usage_metadata.total_tokens).toBe( + res.usage_metadata.input_tokens + res.usage_metadata.output_tokens + ); +});