@librechat/agents 2.4.42 → 2.4.44
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/common/enum.cjs +4 -2
- package/dist/cjs/common/enum.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +2 -2
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/llm/google/index.cjs +73 -1
- package/dist/cjs/llm/google/index.cjs.map +1 -1
- package/dist/cjs/llm/google/utils/common.cjs +469 -0
- package/dist/cjs/llm/google/utils/common.cjs.map +1 -0
- package/dist/cjs/llm/providers.cjs +3 -3
- package/dist/cjs/llm/providers.cjs.map +1 -1
- package/dist/cjs/llm/vertexai/index.cjs +330 -0
- package/dist/cjs/llm/vertexai/index.cjs.map +1 -0
- package/dist/cjs/stream.cjs +5 -2
- package/dist/cjs/stream.cjs.map +1 -1
- package/dist/esm/common/enum.mjs +4 -2
- package/dist/esm/common/enum.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +2 -2
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/llm/google/index.mjs +73 -1
- package/dist/esm/llm/google/index.mjs.map +1 -1
- package/dist/esm/llm/google/utils/common.mjs +463 -0
- package/dist/esm/llm/google/utils/common.mjs.map +1 -0
- package/dist/esm/llm/providers.mjs +2 -2
- package/dist/esm/llm/providers.mjs.map +1 -1
- package/dist/esm/llm/vertexai/index.mjs +328 -0
- package/dist/esm/llm/vertexai/index.mjs.map +1 -0
- package/dist/esm/stream.mjs +5 -2
- package/dist/esm/stream.mjs.map +1 -1
- package/dist/types/common/enum.d.ts +5 -3
- package/dist/types/llm/google/index.d.ts +10 -5
- package/dist/types/llm/google/types.d.ts +32 -0
- package/dist/types/llm/google/utils/common.d.ts +19 -0
- package/dist/types/llm/google/utils/tools.d.ts +10 -0
- package/dist/types/llm/google/utils/zod_to_genai_parameters.d.ts +14 -0
- package/dist/types/llm/vertexai/index.d.ts +293 -0
- package/dist/types/types/llm.d.ts +7 -3
- package/dist/types/types/stream.d.ts +5 -0
- package/package.json +1 -1
- package/src/common/enum.ts +4 -2
- package/src/graphs/Graph.ts +10 -6
- package/src/llm/google/index.ts +118 -8
- package/src/llm/google/types.ts +43 -0
- package/src/llm/google/utils/common.ts +632 -0
- package/src/llm/google/utils/tools.ts +160 -0
- package/src/llm/google/utils/zod_to_genai_parameters.ts +88 -0
- package/src/llm/providers.ts +8 -7
- package/src/llm/vertexai/index.ts +360 -0
- package/src/stream.ts +5 -2
- package/src/types/llm.ts +9 -5
- package/src/types/stream.ts +6 -0
- package/src/utils/llmConfig.ts +2 -2
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Tool as GenerativeAITool,
|
|
3
|
+
ToolConfig,
|
|
4
|
+
FunctionCallingMode,
|
|
5
|
+
FunctionDeclaration,
|
|
6
|
+
FunctionDeclarationsTool,
|
|
7
|
+
FunctionDeclarationSchema,
|
|
8
|
+
} from '@google/generative-ai';
|
|
9
|
+
import { ToolChoice } from '@langchain/core/language_models/chat_models';
|
|
10
|
+
import { StructuredToolInterface } from '@langchain/core/tools';
|
|
11
|
+
import { isLangChainTool } from '@langchain/core/utils/function_calling';
|
|
12
|
+
import {
|
|
13
|
+
isOpenAITool,
|
|
14
|
+
ToolDefinition,
|
|
15
|
+
} from '@langchain/core/language_models/base';
|
|
16
|
+
import { convertToGenerativeAITools } from './common';
|
|
17
|
+
import { GoogleGenerativeAIToolType } from '../types';
|
|
18
|
+
import { removeAdditionalProperties } from './zod_to_genai_parameters';
|
|
19
|
+
|
|
20
|
+
export function convertToolsToGenAI(
|
|
21
|
+
tools: GoogleGenerativeAIToolType[],
|
|
22
|
+
extra?: {
|
|
23
|
+
toolChoice?: ToolChoice;
|
|
24
|
+
allowedFunctionNames?: string[];
|
|
25
|
+
}
|
|
26
|
+
): {
|
|
27
|
+
tools: GenerativeAITool[];
|
|
28
|
+
toolConfig?: ToolConfig;
|
|
29
|
+
} {
|
|
30
|
+
// Extract function declaration processing to a separate function
|
|
31
|
+
const genAITools = processTools(tools);
|
|
32
|
+
|
|
33
|
+
// Simplify tool config creation
|
|
34
|
+
const toolConfig = createToolConfig(genAITools, extra);
|
|
35
|
+
|
|
36
|
+
return { tools: genAITools, toolConfig };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function processTools(tools: GoogleGenerativeAIToolType[]): GenerativeAITool[] {
|
|
40
|
+
let functionDeclarationTools: FunctionDeclaration[] = [];
|
|
41
|
+
const genAITools: GenerativeAITool[] = [];
|
|
42
|
+
|
|
43
|
+
tools.forEach((tool) => {
|
|
44
|
+
if (isLangChainTool(tool)) {
|
|
45
|
+
const [convertedTool] = convertToGenerativeAITools([
|
|
46
|
+
tool as StructuredToolInterface,
|
|
47
|
+
]);
|
|
48
|
+
if (convertedTool.functionDeclarations) {
|
|
49
|
+
functionDeclarationTools.push(...convertedTool.functionDeclarations);
|
|
50
|
+
}
|
|
51
|
+
} else if (isOpenAITool(tool)) {
|
|
52
|
+
const { functionDeclarations } = convertOpenAIToolToGenAI(tool);
|
|
53
|
+
if (functionDeclarations) {
|
|
54
|
+
functionDeclarationTools.push(...functionDeclarations);
|
|
55
|
+
} else {
|
|
56
|
+
throw new Error(
|
|
57
|
+
'Failed to convert OpenAI structured tool to GenerativeAI tool'
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
} else {
|
|
61
|
+
genAITools.push(tool as GenerativeAITool);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
const genAIFunctionDeclaration = genAITools.find(
|
|
66
|
+
(t) => 'functionDeclarations' in t
|
|
67
|
+
);
|
|
68
|
+
if (genAIFunctionDeclaration) {
|
|
69
|
+
return genAITools.map((tool) => {
|
|
70
|
+
if (
|
|
71
|
+
functionDeclarationTools.length > 0 &&
|
|
72
|
+
'functionDeclarations' in tool
|
|
73
|
+
) {
|
|
74
|
+
const newTool = {
|
|
75
|
+
functionDeclarations: [
|
|
76
|
+
...(tool.functionDeclarations || []),
|
|
77
|
+
...functionDeclarationTools,
|
|
78
|
+
],
|
|
79
|
+
};
|
|
80
|
+
// Clear the functionDeclarationTools array so it is not passed again
|
|
81
|
+
functionDeclarationTools = [];
|
|
82
|
+
return newTool;
|
|
83
|
+
}
|
|
84
|
+
return tool;
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return [
|
|
89
|
+
...genAITools,
|
|
90
|
+
...(functionDeclarationTools.length > 0
|
|
91
|
+
? [
|
|
92
|
+
{
|
|
93
|
+
functionDeclarations: functionDeclarationTools,
|
|
94
|
+
},
|
|
95
|
+
]
|
|
96
|
+
: []),
|
|
97
|
+
];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function convertOpenAIToolToGenAI(
|
|
101
|
+
tool: ToolDefinition
|
|
102
|
+
): FunctionDeclarationsTool {
|
|
103
|
+
return {
|
|
104
|
+
functionDeclarations: [
|
|
105
|
+
{
|
|
106
|
+
name: tool.function.name,
|
|
107
|
+
description: tool.function.description,
|
|
108
|
+
parameters: removeAdditionalProperties(
|
|
109
|
+
tool.function.parameters
|
|
110
|
+
) as FunctionDeclarationSchema,
|
|
111
|
+
},
|
|
112
|
+
],
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function createToolConfig(
|
|
117
|
+
genAITools: GenerativeAITool[],
|
|
118
|
+
extra?: {
|
|
119
|
+
toolChoice?: ToolChoice;
|
|
120
|
+
allowedFunctionNames?: string[];
|
|
121
|
+
}
|
|
122
|
+
): ToolConfig | undefined {
|
|
123
|
+
if (!genAITools.length || !extra) return undefined;
|
|
124
|
+
|
|
125
|
+
const { toolChoice, allowedFunctionNames } = extra;
|
|
126
|
+
|
|
127
|
+
const modeMap: Record<string, FunctionCallingMode> = {
|
|
128
|
+
any: FunctionCallingMode.ANY,
|
|
129
|
+
auto: FunctionCallingMode.AUTO,
|
|
130
|
+
none: FunctionCallingMode.NONE,
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
if (
|
|
134
|
+
toolChoice != null &&
|
|
135
|
+
['any', 'auto', 'none'].includes(toolChoice as string)
|
|
136
|
+
) {
|
|
137
|
+
return {
|
|
138
|
+
functionCallingConfig: {
|
|
139
|
+
mode: modeMap[toolChoice as keyof typeof modeMap] ?? 'MODE_UNSPECIFIED',
|
|
140
|
+
allowedFunctionNames,
|
|
141
|
+
},
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (typeof toolChoice === 'string' || allowedFunctionNames) {
|
|
146
|
+
return {
|
|
147
|
+
functionCallingConfig: {
|
|
148
|
+
mode: FunctionCallingMode.ANY,
|
|
149
|
+
allowedFunctionNames: [
|
|
150
|
+
...(allowedFunctionNames ?? []),
|
|
151
|
+
...(toolChoice != null && typeof toolChoice === 'string'
|
|
152
|
+
? [toolChoice]
|
|
153
|
+
: []),
|
|
154
|
+
],
|
|
155
|
+
},
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return undefined;
|
|
160
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type FunctionDeclarationSchema as GenerativeAIFunctionDeclarationSchema,
|
|
5
|
+
type SchemaType as FunctionDeclarationSchemaType,
|
|
6
|
+
} from '@google/generative-ai';
|
|
7
|
+
import {
|
|
8
|
+
InteropZodType,
|
|
9
|
+
isInteropZodSchema,
|
|
10
|
+
} from '@langchain/core/utils/types';
|
|
11
|
+
import {
|
|
12
|
+
type JsonSchema7Type,
|
|
13
|
+
toJsonSchema,
|
|
14
|
+
} from '@langchain/core/utils/json_schema';
|
|
15
|
+
|
|
16
|
+
export interface GenerativeAIJsonSchema extends Record<string, unknown> {
|
|
17
|
+
properties?: Record<string, GenerativeAIJsonSchema>;
|
|
18
|
+
type: FunctionDeclarationSchemaType;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface GenerativeAIJsonSchemaDirty extends GenerativeAIJsonSchema {
|
|
22
|
+
properties?: Record<string, GenerativeAIJsonSchemaDirty>;
|
|
23
|
+
additionalProperties?: boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function removeAdditionalProperties(
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
28
|
+
obj: Record<string, any>
|
|
29
|
+
): GenerativeAIJsonSchema {
|
|
30
|
+
if (typeof obj === 'object' && obj !== null) {
|
|
31
|
+
const newObj = { ...obj };
|
|
32
|
+
|
|
33
|
+
if ('additionalProperties' in newObj) {
|
|
34
|
+
delete newObj.additionalProperties;
|
|
35
|
+
}
|
|
36
|
+
if ('$schema' in newObj) {
|
|
37
|
+
delete newObj.$schema;
|
|
38
|
+
}
|
|
39
|
+
if ('strict' in newObj) {
|
|
40
|
+
delete newObj.strict;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
for (const key in newObj) {
|
|
44
|
+
if (key in newObj) {
|
|
45
|
+
if (Array.isArray(newObj[key])) {
|
|
46
|
+
newObj[key] = newObj[key].map(removeAdditionalProperties);
|
|
47
|
+
} else if (typeof newObj[key] === 'object' && newObj[key] !== null) {
|
|
48
|
+
newObj[key] = removeAdditionalProperties(newObj[key]);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return newObj as GenerativeAIJsonSchema;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return obj as GenerativeAIJsonSchema;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function schemaToGenerativeAIParameters<
|
|
60
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
61
|
+
RunOutput extends Record<string, any> = Record<string, any>,
|
|
62
|
+
>(
|
|
63
|
+
schema: InteropZodType<RunOutput> | JsonSchema7Type
|
|
64
|
+
): GenerativeAIFunctionDeclarationSchema {
|
|
65
|
+
// GenerativeAI doesn't accept either the $schema or additionalProperties
|
|
66
|
+
// attributes, so we need to explicitly remove them.
|
|
67
|
+
const jsonSchema = removeAdditionalProperties(
|
|
68
|
+
isInteropZodSchema(schema) ? toJsonSchema(schema) : schema
|
|
69
|
+
);
|
|
70
|
+
const { $schema, ...rest } = jsonSchema;
|
|
71
|
+
|
|
72
|
+
return rest as GenerativeAIFunctionDeclarationSchema;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function jsonSchemaToGeminiParameters(
|
|
76
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
77
|
+
schema: Record<string, any>
|
|
78
|
+
): GenerativeAIFunctionDeclarationSchema {
|
|
79
|
+
// Gemini doesn't accept either the $schema or additionalProperties
|
|
80
|
+
// attributes, so we need to explicitly remove them.
|
|
81
|
+
|
|
82
|
+
const jsonSchema = removeAdditionalProperties(
|
|
83
|
+
schema as GenerativeAIJsonSchemaDirty
|
|
84
|
+
);
|
|
85
|
+
const { $schema, ...rest } = jsonSchema;
|
|
86
|
+
|
|
87
|
+
return rest as GenerativeAIFunctionDeclarationSchema;
|
|
88
|
+
}
|
package/src/llm/providers.ts
CHANGED
|
@@ -3,22 +3,23 @@ import { ChatOllama } from '@langchain/ollama';
|
|
|
3
3
|
import { ChatMistralAI } from '@langchain/mistralai';
|
|
4
4
|
import { ChatBedrockConverse } from '@langchain/aws';
|
|
5
5
|
// import { ChatAnthropic } from '@langchain/anthropic';
|
|
6
|
-
import { ChatVertexAI } from '@langchain/google-vertexai';
|
|
6
|
+
// import { ChatVertexAI } from '@langchain/google-vertexai';
|
|
7
7
|
import { BedrockChat } from '@langchain/community/chat_models/bedrock/web';
|
|
8
8
|
import type {
|
|
9
9
|
ChatModelConstructorMap,
|
|
10
10
|
ProviderOptionsMap,
|
|
11
11
|
ChatModelMap,
|
|
12
12
|
} from '@/types';
|
|
13
|
-
import { CustomChatGoogleGenerativeAI } from '@/llm/google';
|
|
14
|
-
import { CustomAnthropic } from '@/llm/anthropic';
|
|
15
|
-
import { ChatOpenRouter } from '@/llm/openrouter';
|
|
16
13
|
import {
|
|
17
|
-
ChatXAI,
|
|
18
|
-
ChatOpenAI,
|
|
19
|
-
ChatDeepSeek,
|
|
20
14
|
AzureChatOpenAI,
|
|
15
|
+
ChatDeepSeek,
|
|
16
|
+
ChatOpenAI,
|
|
17
|
+
ChatXAI,
|
|
21
18
|
} from '@/llm/openai';
|
|
19
|
+
import { CustomChatGoogleGenerativeAI } from '@/llm/google';
|
|
20
|
+
import { CustomAnthropic } from '@/llm/anthropic';
|
|
21
|
+
import { ChatOpenRouter } from '@/llm/openrouter';
|
|
22
|
+
import { ChatVertexAI } from '@/llm/vertexai';
|
|
22
23
|
import { Providers } from '@/common';
|
|
23
24
|
|
|
24
25
|
export const llmProviders: Partial<ChatModelConstructorMap> = {
|
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
import { ChatGoogle } from '@langchain/google-gauth';
|
|
2
|
+
import { ChatConnection } from '@langchain/google-common';
|
|
3
|
+
import type {
|
|
4
|
+
GeminiRequest,
|
|
5
|
+
GoogleAIModelRequestParams,
|
|
6
|
+
GoogleAbstractedClient,
|
|
7
|
+
} from '@langchain/google-common';
|
|
8
|
+
import type { BaseMessage } from '@langchain/core/messages';
|
|
9
|
+
import type { VertexAIClientOptions } from '@/types';
|
|
10
|
+
|
|
11
|
+
class CustomChatConnection extends ChatConnection<VertexAIClientOptions> {
|
|
12
|
+
async formatData(
|
|
13
|
+
input: BaseMessage[],
|
|
14
|
+
parameters: GoogleAIModelRequestParams
|
|
15
|
+
): Promise<unknown> {
|
|
16
|
+
const formattedData = (await super.formatData(
|
|
17
|
+
input,
|
|
18
|
+
parameters
|
|
19
|
+
)) as GeminiRequest;
|
|
20
|
+
if (
|
|
21
|
+
formattedData.generationConfig?.thinkingConfig?.thinkingBudget === -1 &&
|
|
22
|
+
formattedData.generationConfig.thinkingConfig.includeThoughts === false
|
|
23
|
+
) {
|
|
24
|
+
formattedData.generationConfig.thinkingConfig.includeThoughts = true;
|
|
25
|
+
}
|
|
26
|
+
return formattedData;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Integration with Google Vertex AI chat models.
|
|
32
|
+
*
|
|
33
|
+
* Setup:
|
|
34
|
+
* Install `@langchain/google-vertexai` and set your stringified
|
|
35
|
+
* Vertex AI credentials as an environment variable named `GOOGLE_APPLICATION_CREDENTIALS`.
|
|
36
|
+
*
|
|
37
|
+
* ```bash
|
|
38
|
+
* npm install @langchain/google-vertexai
|
|
39
|
+
* export GOOGLE_APPLICATION_CREDENTIALS="path/to/credentials"
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* ## [Constructor args](https://api.js.langchain.com/classes/_langchain_google_vertexai.index.ChatVertexAI.html#constructor.new_ChatVertexAI)
|
|
43
|
+
*
|
|
44
|
+
* ## [Runtime args](https://api.js.langchain.com/interfaces/langchain_google_common_types.GoogleAIBaseLanguageModelCallOptions.html)
|
|
45
|
+
*
|
|
46
|
+
* Runtime args can be passed as the second argument to any of the base runnable methods `.invoke`. `.stream`, `.batch`, etc.
|
|
47
|
+
* They can also be passed via `.withConfig`, or the second arg in `.bindTools`, like shown in the examples below:
|
|
48
|
+
*
|
|
49
|
+
* ```typescript
|
|
50
|
+
* // When calling `.withConfig`, call options should be passed via the first argument
|
|
51
|
+
* const llmWithArgsBound = llm.withConfig({
|
|
52
|
+
* stop: ["\n"],
|
|
53
|
+
* tools: [...],
|
|
54
|
+
* });
|
|
55
|
+
*
|
|
56
|
+
* // When calling `.bindTools`, call options should be passed via the second argument
|
|
57
|
+
* const llmWithTools = llm.bindTools(
|
|
58
|
+
* [...],
|
|
59
|
+
* {
|
|
60
|
+
* tool_choice: "auto",
|
|
61
|
+
* }
|
|
62
|
+
* );
|
|
63
|
+
* ```
|
|
64
|
+
*
|
|
65
|
+
* ## Examples
|
|
66
|
+
*
|
|
67
|
+
* <details open>
|
|
68
|
+
* <summary><strong>Instantiate</strong></summary>
|
|
69
|
+
*
|
|
70
|
+
* ```typescript
|
|
71
|
+
* import { ChatVertexAI } from '@langchain/google-vertexai';
|
|
72
|
+
*
|
|
73
|
+
* const llm = new ChatVertexAI({
|
|
74
|
+
* model: "gemini-1.5-pro",
|
|
75
|
+
* temperature: 0,
|
|
76
|
+
* // other params...
|
|
77
|
+
* });
|
|
78
|
+
* ```
|
|
79
|
+
* </details>
|
|
80
|
+
*
|
|
81
|
+
* <br />
|
|
82
|
+
*
|
|
83
|
+
* <details>
|
|
84
|
+
* <summary><strong>Invoking</strong></summary>
|
|
85
|
+
*
|
|
86
|
+
* ```typescript
|
|
87
|
+
* const input = `Translate "I love programming" into French.`;
|
|
88
|
+
*
|
|
89
|
+
* // Models also accept a list of chat messages or a formatted prompt
|
|
90
|
+
* const result = await llm.invoke(input);
|
|
91
|
+
* console.log(result);
|
|
92
|
+
* ```
|
|
93
|
+
*
|
|
94
|
+
* ```txt
|
|
95
|
+
* AIMessageChunk {
|
|
96
|
+
* "content": "\"J'adore programmer\" \n\nHere's why this is the best translation:\n\n* **J'adore** means \"I love\" and conveys a strong passion.\n* **Programmer** is the French verb for \"to program.\"\n\nThis translation is natural and idiomatic in French. \n",
|
|
97
|
+
* "additional_kwargs": {},
|
|
98
|
+
* "response_metadata": {},
|
|
99
|
+
* "tool_calls": [],
|
|
100
|
+
* "tool_call_chunks": [],
|
|
101
|
+
* "invalid_tool_calls": [],
|
|
102
|
+
* "usage_metadata": {
|
|
103
|
+
* "input_tokens": 9,
|
|
104
|
+
* "output_tokens": 63,
|
|
105
|
+
* "total_tokens": 72
|
|
106
|
+
* }
|
|
107
|
+
* }
|
|
108
|
+
* ```
|
|
109
|
+
* </details>
|
|
110
|
+
*
|
|
111
|
+
* <br />
|
|
112
|
+
*
|
|
113
|
+
* <details>
|
|
114
|
+
* <summary><strong>Streaming Chunks</strong></summary>
|
|
115
|
+
*
|
|
116
|
+
* ```typescript
|
|
117
|
+
* for await (const chunk of await llm.stream(input)) {
|
|
118
|
+
* console.log(chunk);
|
|
119
|
+
* }
|
|
120
|
+
* ```
|
|
121
|
+
*
|
|
122
|
+
* ```txt
|
|
123
|
+
* AIMessageChunk {
|
|
124
|
+
* "content": "\"",
|
|
125
|
+
* "additional_kwargs": {},
|
|
126
|
+
* "response_metadata": {},
|
|
127
|
+
* "tool_calls": [],
|
|
128
|
+
* "tool_call_chunks": [],
|
|
129
|
+
* "invalid_tool_calls": []
|
|
130
|
+
* }
|
|
131
|
+
* AIMessageChunk {
|
|
132
|
+
* "content": "J'adore programmer\" \n",
|
|
133
|
+
* "additional_kwargs": {},
|
|
134
|
+
* "response_metadata": {},
|
|
135
|
+
* "tool_calls": [],
|
|
136
|
+
* "tool_call_chunks": [],
|
|
137
|
+
* "invalid_tool_calls": []
|
|
138
|
+
* }
|
|
139
|
+
* AIMessageChunk {
|
|
140
|
+
* "content": "",
|
|
141
|
+
* "additional_kwargs": {},
|
|
142
|
+
* "response_metadata": {},
|
|
143
|
+
* "tool_calls": [],
|
|
144
|
+
* "tool_call_chunks": [],
|
|
145
|
+
* "invalid_tool_calls": []
|
|
146
|
+
* }
|
|
147
|
+
* AIMessageChunk {
|
|
148
|
+
* "content": "",
|
|
149
|
+
* "additional_kwargs": {},
|
|
150
|
+
* "response_metadata": {
|
|
151
|
+
* "finishReason": "stop"
|
|
152
|
+
* },
|
|
153
|
+
* "tool_calls": [],
|
|
154
|
+
* "tool_call_chunks": [],
|
|
155
|
+
* "invalid_tool_calls": [],
|
|
156
|
+
* "usage_metadata": {
|
|
157
|
+
* "input_tokens": 9,
|
|
158
|
+
* "output_tokens": 8,
|
|
159
|
+
* "total_tokens": 17
|
|
160
|
+
* }
|
|
161
|
+
* }
|
|
162
|
+
* ```
|
|
163
|
+
* </details>
|
|
164
|
+
*
|
|
165
|
+
* <br />
|
|
166
|
+
*
|
|
167
|
+
* <details>
|
|
168
|
+
* <summary><strong>Aggregate Streamed Chunks</strong></summary>
|
|
169
|
+
*
|
|
170
|
+
* ```typescript
|
|
171
|
+
* import { AIMessageChunk } from '@langchain/core/messages';
|
|
172
|
+
* import { concat } from '@langchain/core/utils/stream';
|
|
173
|
+
*
|
|
174
|
+
* const stream = await llm.stream(input);
|
|
175
|
+
* let full: AIMessageChunk | undefined;
|
|
176
|
+
* for await (const chunk of stream) {
|
|
177
|
+
* full = !full ? chunk : concat(full, chunk);
|
|
178
|
+
* }
|
|
179
|
+
* console.log(full);
|
|
180
|
+
* ```
|
|
181
|
+
*
|
|
182
|
+
* ```txt
|
|
183
|
+
* AIMessageChunk {
|
|
184
|
+
* "content": "\"J'adore programmer\" \n",
|
|
185
|
+
* "additional_kwargs": {},
|
|
186
|
+
* "response_metadata": {
|
|
187
|
+
* "finishReason": "stop"
|
|
188
|
+
* },
|
|
189
|
+
* "tool_calls": [],
|
|
190
|
+
* "tool_call_chunks": [],
|
|
191
|
+
* "invalid_tool_calls": [],
|
|
192
|
+
* "usage_metadata": {
|
|
193
|
+
* "input_tokens": 9,
|
|
194
|
+
* "output_tokens": 8,
|
|
195
|
+
* "total_tokens": 17
|
|
196
|
+
* }
|
|
197
|
+
* }
|
|
198
|
+
* ```
|
|
199
|
+
* </details>
|
|
200
|
+
*
|
|
201
|
+
* <br />
|
|
202
|
+
*
|
|
203
|
+
* <details>
|
|
204
|
+
* <summary><strong>Bind tools</strong></summary>
|
|
205
|
+
*
|
|
206
|
+
* ```typescript
|
|
207
|
+
* import { z } from 'zod';
|
|
208
|
+
*
|
|
209
|
+
* const GetWeather = {
|
|
210
|
+
* name: "GetWeather",
|
|
211
|
+
* description: "Get the current weather in a given location",
|
|
212
|
+
* schema: z.object({
|
|
213
|
+
* location: z.string().describe("The city and state, e.g. San Francisco, CA")
|
|
214
|
+
* }),
|
|
215
|
+
* }
|
|
216
|
+
*
|
|
217
|
+
* const GetPopulation = {
|
|
218
|
+
* name: "GetPopulation",
|
|
219
|
+
* description: "Get the current population in a given location",
|
|
220
|
+
* schema: z.object({
|
|
221
|
+
* location: z.string().describe("The city and state, e.g. San Francisco, CA")
|
|
222
|
+
* }),
|
|
223
|
+
* }
|
|
224
|
+
*
|
|
225
|
+
* const llmWithTools = llm.bindTools([GetWeather, GetPopulation]);
|
|
226
|
+
* const aiMsg = await llmWithTools.invoke(
|
|
227
|
+
* "Which city is hotter today and which is bigger: LA or NY?"
|
|
228
|
+
* );
|
|
229
|
+
* console.log(aiMsg.tool_calls);
|
|
230
|
+
* ```
|
|
231
|
+
*
|
|
232
|
+
* ```txt
|
|
233
|
+
* [
|
|
234
|
+
* {
|
|
235
|
+
* name: 'GetPopulation',
|
|
236
|
+
* args: { location: 'New York City, NY' },
|
|
237
|
+
* id: '33c1c1f47e2f492799c77d2800a43912',
|
|
238
|
+
* type: 'tool_call'
|
|
239
|
+
* }
|
|
240
|
+
* ]
|
|
241
|
+
* ```
|
|
242
|
+
* </details>
|
|
243
|
+
*
|
|
244
|
+
* <br />
|
|
245
|
+
*
|
|
246
|
+
* <details>
|
|
247
|
+
* <summary><strong>Structured Output</strong></summary>
|
|
248
|
+
*
|
|
249
|
+
* ```typescript
|
|
250
|
+
* import { z } from 'zod';
|
|
251
|
+
*
|
|
252
|
+
* const Joke = z.object({
|
|
253
|
+
* setup: z.string().describe("The setup of the joke"),
|
|
254
|
+
* punchline: z.string().describe("The punchline to the joke"),
|
|
255
|
+
* rating: z.number().optional().describe("How funny the joke is, from 1 to 10")
|
|
256
|
+
* }).describe('Joke to tell user.');
|
|
257
|
+
*
|
|
258
|
+
* const structuredLlm = llm.withStructuredOutput(Joke, { name: "Joke" });
|
|
259
|
+
* const jokeResult = await structuredLlm.invoke("Tell me a joke about cats");
|
|
260
|
+
* console.log(jokeResult);
|
|
261
|
+
* ```
|
|
262
|
+
*
|
|
263
|
+
* ```txt
|
|
264
|
+
* {
|
|
265
|
+
* setup: 'What do you call a cat that loves to bowl?',
|
|
266
|
+
* punchline: 'An alley cat!'
|
|
267
|
+
* }
|
|
268
|
+
* ```
|
|
269
|
+
* </details>
|
|
270
|
+
*
|
|
271
|
+
* <br />
|
|
272
|
+
*
|
|
273
|
+
* <details>
|
|
274
|
+
* <summary><strong>Usage Metadata</strong></summary>
|
|
275
|
+
*
|
|
276
|
+
* ```typescript
|
|
277
|
+
* const aiMsgForMetadata = await llm.invoke(input);
|
|
278
|
+
* console.log(aiMsgForMetadata.usage_metadata);
|
|
279
|
+
* ```
|
|
280
|
+
*
|
|
281
|
+
* ```txt
|
|
282
|
+
* { input_tokens: 9, output_tokens: 8, total_tokens: 17 }
|
|
283
|
+
* ```
|
|
284
|
+
* </details>
|
|
285
|
+
*
|
|
286
|
+
* <br />
|
|
287
|
+
*
|
|
288
|
+
* <details>
|
|
289
|
+
* <summary><strong>Stream Usage Metadata</strong></summary>
|
|
290
|
+
*
|
|
291
|
+
* ```typescript
|
|
292
|
+
* const streamForMetadata = await llm.stream(
|
|
293
|
+
* input,
|
|
294
|
+
* {
|
|
295
|
+
* streamUsage: true
|
|
296
|
+
* }
|
|
297
|
+
* );
|
|
298
|
+
* let fullForMetadata: AIMessageChunk | undefined;
|
|
299
|
+
* for await (const chunk of streamForMetadata) {
|
|
300
|
+
* fullForMetadata = !fullForMetadata ? chunk : concat(fullForMetadata, chunk);
|
|
301
|
+
* }
|
|
302
|
+
* console.log(fullForMetadata?.usage_metadata);
|
|
303
|
+
* ```
|
|
304
|
+
*
|
|
305
|
+
* ```txt
|
|
306
|
+
* { input_tokens: 9, output_tokens: 8, total_tokens: 17 }
|
|
307
|
+
* ```
|
|
308
|
+
* </details>
|
|
309
|
+
*
|
|
310
|
+
* <br />
|
|
311
|
+
*/
|
|
312
|
+
export class ChatVertexAI extends ChatGoogle {
|
|
313
|
+
lc_namespace = ['langchain', 'chat_models', 'vertexai'];
|
|
314
|
+
dynamicThinkingBudget = false;
|
|
315
|
+
|
|
316
|
+
static lc_name(): 'ChatVertexAI' {
|
|
317
|
+
return 'ChatVertexAI';
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
constructor(fields?: VertexAIClientOptions) {
|
|
321
|
+
let dynamicThinkingBudget = false;
|
|
322
|
+
if (fields?.thinkingBudget === -1) {
|
|
323
|
+
dynamicThinkingBudget = true;
|
|
324
|
+
fields.thinkingBudget = 1;
|
|
325
|
+
}
|
|
326
|
+
super({
|
|
327
|
+
...fields,
|
|
328
|
+
platformType: 'gcp',
|
|
329
|
+
});
|
|
330
|
+
this.dynamicThinkingBudget = dynamicThinkingBudget;
|
|
331
|
+
}
|
|
332
|
+
invocationParams(
|
|
333
|
+
options?: this['ParsedCallOptions'] | undefined
|
|
334
|
+
): GoogleAIModelRequestParams {
|
|
335
|
+
const params = super.invocationParams(options);
|
|
336
|
+
if (this.dynamicThinkingBudget) {
|
|
337
|
+
params.maxReasoningTokens = -1;
|
|
338
|
+
}
|
|
339
|
+
return params;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
buildConnection(
|
|
343
|
+
fields: VertexAIClientOptions,
|
|
344
|
+
client: GoogleAbstractedClient
|
|
345
|
+
): void {
|
|
346
|
+
this.connection = new CustomChatConnection(
|
|
347
|
+
{ ...fields, ...this },
|
|
348
|
+
this.caller,
|
|
349
|
+
client,
|
|
350
|
+
false
|
|
351
|
+
);
|
|
352
|
+
|
|
353
|
+
this.streamedConnection = new CustomChatConnection(
|
|
354
|
+
{ ...fields, ...this },
|
|
355
|
+
this.caller,
|
|
356
|
+
client,
|
|
357
|
+
true
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
}
|
package/src/stream.ts
CHANGED
|
@@ -244,6 +244,7 @@ hasToolCallChunks: ${hasToolCallChunks}
|
|
|
244
244
|
content.every(
|
|
245
245
|
(c) =>
|
|
246
246
|
(c.type?.startsWith(ContentTypes.THINKING) ?? false) ||
|
|
247
|
+
(c.type?.startsWith(ContentTypes.REASONING) ?? false) ||
|
|
247
248
|
(c.type?.startsWith(ContentTypes.REASONING_CONTENT) ?? false)
|
|
248
249
|
)
|
|
249
250
|
) {
|
|
@@ -252,6 +253,7 @@ hasToolCallChunks: ${hasToolCallChunks}
|
|
|
252
253
|
type: ContentTypes.THINK,
|
|
253
254
|
think:
|
|
254
255
|
(c as t.ThinkingContentText).thinking ??
|
|
256
|
+
(c as Partial<t.GoogleReasoningContentText>).reasoning ??
|
|
255
257
|
(c as Partial<t.BedrockReasoningContentText>).reasoningText?.text ??
|
|
256
258
|
'',
|
|
257
259
|
})),
|
|
@@ -264,8 +266,9 @@ hasToolCallChunks: ${hasToolCallChunks}
|
|
|
264
266
|
| undefined;
|
|
265
267
|
if (
|
|
266
268
|
Array.isArray(chunk.content) &&
|
|
267
|
-
(chunk.content[0]?.type ===
|
|
268
|
-
chunk.content[0]?.type ===
|
|
269
|
+
(chunk.content[0]?.type === ContentTypes.THINKING ||
|
|
270
|
+
chunk.content[0]?.type === ContentTypes.REASONING ||
|
|
271
|
+
chunk.content[0]?.type === ContentTypes.REASONING_CONTENT)
|
|
269
272
|
) {
|
|
270
273
|
reasoning_content = 'valid';
|
|
271
274
|
}
|