-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlangchain.ts
More file actions
139 lines (118 loc) · 3.17 KB
/
Copy pathlangchain.ts
File metadata and controls
139 lines (118 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { ChatGoogleGenerativeAI } from "@langchain/google-genai";
import type { AdapterOptions } from "./types.js";
export type AgentModelPurpose = "primary" | "summary";
const GOOGLE_LANGCHAIN_AGENT_OPTION_KEYS = [
"temperature",
"maxOutputTokens",
"topP",
"topK",
"stopSequences",
"safetySettings",
"apiVersion",
"baseUrl",
"customHeaders",
"streaming",
"json",
"streamUsage",
"convertSystemMessageToHumanContent",
"thinkingConfig",
] as const;
function getGoogleLangChainAgentOptions(
extraRequestBodyParameters?: Record<string, unknown>,
) {
const options: Record<string, unknown> = {};
if (!extraRequestBodyParameters) {
return options;
}
for (const key of GOOGLE_LANGCHAIN_AGENT_OPTION_KEYS) {
if (key in extraRequestBodyParameters) {
options[key] = extraRequestBodyParameters[key];
}
}
return options;
}
const GEMINI_SCHEMA_KEYS = new Set([
"description",
"enum",
"format",
"items",
"maxItems",
"minItems",
"nullable",
"properties",
"required",
"type",
]);
function sanitizeGeminiSchema(schema: unknown): unknown {
if (Array.isArray(schema)) {
return schema.map(sanitizeGeminiSchema);
}
if (!schema || typeof schema !== "object") {
return schema;
}
const sanitizedSchema: Record<string, unknown> = {};
for (const [key, value] of Object.entries(schema)) {
if (!GEMINI_SCHEMA_KEYS.has(key)) {
continue;
}
if (key === "properties" && value && typeof value === "object") {
sanitizedSchema.properties = Object.fromEntries(
Object.entries(value).map(([propertyName, propertySchema]) => [
propertyName,
sanitizeGeminiSchema(propertySchema),
]),
);
continue;
}
sanitizedSchema[key] = sanitizeGeminiSchema(value);
}
return sanitizedSchema;
}
function sanitizeGeminiTools(tools: unknown): unknown {
if (!Array.isArray(tools)) {
return tools;
}
return tools.map((tool) => {
if (!tool || typeof tool !== "object" || !Array.isArray(tool.functionDeclarations)) {
return tool;
}
return {
...tool,
functionDeclarations: tool.functionDeclarations.map((declaration: any) => ({
...declaration,
...(declaration.parameters
? { parameters: sanitizeGeminiSchema(declaration.parameters) }
: {}),
})),
};
});
}
export function createLangChainAgentSpec(params: {
options: AdapterOptions;
maxTokens: number;
purpose: AgentModelPurpose;
}) {
const modelOptions = getGoogleLangChainAgentOptions(
params.options.extraRequestBodyParameters,
);
const model = new ChatGoogleGenerativeAI({
model: params.options.model || "gemini-3-flash-preview",
apiKey: params.options.geminiApiKey,
maxOutputTokens: params.maxTokens,
...modelOptions,
} as any);
const getInvocationParams = model.invocationParams.bind(model);
model.invocationParams = ((options) => {
const request = getInvocationParams(options);
if (!request.tools) {
return request;
}
return {
...request,
tools: sanitizeGeminiTools(request.tools) as typeof request.tools,
};
}) as typeof model.invocationParams;
return {
model,
};
}