Skip to content

Commit 56fab83

Browse files
committed
cr
1 parent 8dc2a39 commit 56fab83

File tree

5 files changed

+85
-14
lines changed

5 files changed

+85
-14
lines changed

libs/providers/langchain-google/package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@
6262
"types": "./dist/index.d.cts",
6363
"default": "./dist/index.cjs"
6464
}
65+
},
66+
"./node": {
67+
"input": "./src/node.ts",
68+
"import": {
69+
"types": "./dist/node.d.ts",
70+
"default": "./dist/node.js"
71+
},
72+
"require": {
73+
"types": "./dist/node.d.cts",
74+
"default": "./dist/node.cjs"
75+
}
6576
}
6677
},
6778
"files": [

libs/providers/langchain-google/src/chat_models/base.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,10 @@ export abstract class BaseChatGoogle<
8888

8989
constructor(protected params: BaseChatGoogleParams) {
9090
super(params);
91-
this.model = params.model;
92-
9391
if (!params.apiClient) {
9492
throw new Error("BaseChatGoogle requires an apiClient");
9593
}
94+
this.model = params.model;
9695
this.apiClient = params.apiClient;
9796
}
9897

@@ -435,9 +434,7 @@ export abstract class BaseChatGoogle<
435434

436435
// Determine llm and outputParser based on method
437436
let llm: Runnable<BaseMessage[], AIMessageChunk, CallOptions>;
438-
let outputParser:
439-
| RunnableLambda<BaseMessage, RunOutput>
440-
| JsonOutputKeyToolsParser<RunOutput>;
437+
let outputParser: Runnable<BaseMessage, RunOutput>;
441438

442439
if (method === "jsonMode") {
443440
// Use JSON mode with responseSchema

libs/providers/langchain-google/src/chat_models/index.ts

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getEnvironmentVariable } from "@langchain/core/utils/env";
12
import { WebApiClient } from "../clients/index.js";
23
import {
34
BaseChatGoogle,
@@ -6,16 +7,40 @@ import {
67
getGoogleChatModelParams,
78
} from "./base.js";
89

10+
/**
11+
* Configuration parameters for the ChatGoogleGenerativeAI model.
12+
*
13+
* This interface extends the base Google chat model parameters and adds
14+
* specific configuration options for the Generative AI API.
15+
*/
916
export interface ChatGoogleGenerativeAIParams extends BaseChatGoogleParams {
17+
/**
18+
* Google API key for authentication with the Generative AI API.
19+
*
20+
* If not provided, the model will attempt to use the `GOOGLE_API_KEY`
21+
* environment variable. You can obtain an API key from the
22+
* [Google AI Studio](https://makersuite.google.com/app/apikey).
23+
*/
1024
apiKey?: string;
25+
1126
/** @deprecated Import from `@langchain/google/node` to configure google auth options */
1227
authOptions?: never;
1328
}
1429

30+
/**
31+
* Call options for the ChatGoogleGenerativeAI model.
32+
*
33+
* This interface extends the base Google chat model call options and provides
34+
* configuration for individual model invocations. These options can be passed
35+
* when calling methods like `invoke()`, `stream()`, or `batch()` to customize
36+
* the behavior of a specific request.
37+
*/
1538
export interface ChatGoogleGenerativeAICallOptions
1639
extends BaseChatGoogleCallOptions {}
1740

1841
export class ChatGoogleGenerativeAI extends BaseChatGoogle<ChatGoogleGenerativeAICallOptions> {
42+
apiKey?: string;
43+
1944
_llmType() {
2045
return "generativeai";
2146
}
@@ -34,39 +59,69 @@ export class ChatGoogleGenerativeAI extends BaseChatGoogle<ChatGoogleGenerativeA
3459
paramsArg?: Omit<ChatGoogleGenerativeAIParams, "model">
3560
) {
3661
const params = getGoogleChatModelParams(modelOrParams, paramsArg);
62+
params.apiKey = params?.apiKey ?? getEnvironmentVariable("GOOGLE_API_KEY");
3763
const apiClient = params?.apiClient ?? new WebApiClient(params);
3864
super({ ...params, apiClient });
65+
66+
this.apiKey = params.apiKey;
3967
}
4068
}
4169

70+
/**
71+
* Parameters for configuring the ChatGoogleVertexAI model.
72+
*
73+
* This interface extends the base Google chat model parameters and provides
74+
* configuration options specific to Google Vertex AI. These parameters are
75+
* used when instantiating a new ChatGoogleVertexAI instance.
76+
*/
4277
export interface ChatGoogleVertexAIParams extends BaseChatGoogleParams {
78+
/**
79+
* Google API key for authentication with the Vertex AI API.
80+
*
81+
* If not provided, the model will attempt to use the `GOOGLE_API_KEY`
82+
* environment variable. You can obtain an API key from the
83+
* [Google Cloud Console](https://console.cloud.google.com/).
84+
*/
4385
apiKey?: string;
86+
4487
/** @deprecated Import from `@langchain/google/node` to configure google auth options */
4588
authOptions?: never;
4689
}
4790

91+
/**
92+
* Call options for the ChatGoogleVertexAI model.
93+
*
94+
* This interface extends the base Google chat model call options and provides
95+
* configuration options that can be passed when invoking the ChatGoogleVertexAI model.
96+
* These options allow you to customize the behavior of individual model calls.
97+
*/
4898
export interface ChatGoogleVertexAICallOptions
4999
extends BaseChatGoogleCallOptions {}
50100

51101
export class ChatGoogleVertexAI extends BaseChatGoogle<ChatGoogleVertexAICallOptions> {
102+
apiKey?: string;
103+
104+
_llmType() {
105+
return "vertexai";
106+
}
107+
108+
getBaseUrl() {
109+
return new URL(
110+
`https://aiplatform.googleapis.com/v1/publishers/google/models/`
111+
);
112+
}
113+
52114
constructor(model: string, params?: Omit<ChatGoogleVertexAIParams, "model">);
53115
constructor(params: ChatGoogleVertexAIParams);
54116
constructor(
55117
modelOrParams: string | ChatGoogleVertexAIParams,
56118
paramsArg?: Omit<ChatGoogleVertexAIParams, "model">
57119
) {
58120
const params = getGoogleChatModelParams(modelOrParams, paramsArg);
121+
params.apiKey = params?.apiKey ?? getEnvironmentVariable("GOOGLE_API_KEY");
59122
const apiClient = params?.apiClient ?? new WebApiClient(params);
60123
super({ ...params, apiClient });
61-
}
62124

63-
_llmType() {
64-
return "vertexai";
65-
}
66-
67-
getBaseUrl() {
68-
return new URL(
69-
`https://aiplatform.googleapis.com/v1/publishers/google/models/`
70-
);
125+
this.apiKey = params.apiKey;
71126
}
72127
}

libs/providers/langchain-google/src/chat_models/node.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getEnvironmentVariable } from "@langchain/core/utils/env";
12
import {
23
type ChatGoogleVertexAIParams,
34
ChatGoogleVertexAI,
@@ -27,6 +28,9 @@ class ChatGoogleGenerativeAINode extends ChatGoogleGenerativeAI {
2728
paramsArg?: Omit<ChatGoogleGenerativeAINodeParams, "model">
2829
) {
2930
const params = getGoogleChatModelParams(modelOrParams, paramsArg);
31+
if (!params.googleAuthOptions) {
32+
params.apiKey = params.apiKey ?? getEnvironmentVariable("GOOGLE_API_KEY");
33+
}
3034
if (params.googleAuthOptions) {
3135
params.googleAuthOptions = ensureAuthScopes(
3236
params.googleAuthOptions,
@@ -53,6 +57,9 @@ class ChatGoogleVertexAINode extends ChatGoogleVertexAI {
5357
paramsArg?: Omit<ChatGoogleVertexAINodeParams, "model">
5458
) {
5559
const params = getGoogleChatModelParams(modelOrParams, paramsArg);
60+
if (!params.googleAuthOptions) {
61+
params.apiKey = params.apiKey ?? getEnvironmentVariable("GOOGLE_API_KEY");
62+
}
5663
if (params.googleAuthOptions) {
5764
params.googleAuthOptions = ensureAuthScopes(
5865
params.googleAuthOptions,

libs/providers/langchain-google/src/utils/errors.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ export class RequestError extends GoogleError("request") {
311311
});
312312

313313
const message =
314+
errorBody?.error?.message ??
314315
errorBody?.message ??
315316
errorBody?.error ??
316317
`Request failed with status code ${response.status}`;

0 commit comments

Comments
 (0)