-
Notifications
You must be signed in to change notification settings - Fork 2k
/
common.ts
135 lines (124 loc) Β· 4.07 KB
/
common.ts
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
import { StructuredToolInterface } from "@langchain/core/tools";
import type {
GeminiTool,
GoogleAIBaseLanguageModelCallOptions,
GoogleAIModelParams,
GoogleAIModelRequestParams,
GoogleLLMModelFamily,
} from "../types.js";
import { isModelGemini, validateGeminiParams } from "./gemini.js";
export function copyAIModelParams(
params: GoogleAIModelParams | undefined,
options: GoogleAIBaseLanguageModelCallOptions | undefined
): GoogleAIModelRequestParams {
return copyAIModelParamsInto(params, options, {});
}
export function copyAIModelParamsInto(
params: GoogleAIModelParams | undefined,
options: GoogleAIBaseLanguageModelCallOptions | undefined,
target: GoogleAIModelParams
): GoogleAIModelRequestParams {
const ret: GoogleAIModelRequestParams = target || {};
ret.modelName = options?.modelName ?? params?.modelName ?? target.modelName;
ret.temperature =
options?.temperature ?? params?.temperature ?? target.temperature;
ret.maxOutputTokens =
options?.maxOutputTokens ??
params?.maxOutputTokens ??
target.maxOutputTokens;
ret.topP = options?.topP ?? params?.topP ?? target.topP;
ret.topK = options?.topK ?? params?.topK ?? target.topK;
ret.stopSequences =
options?.stopSequences ?? params?.stopSequences ?? target.stopSequences;
ret.safetySettings =
options?.safetySettings ?? params?.safetySettings ?? target.safetySettings;
ret.tools = options?.tools;
// Ensure tools are formatted properly for Gemini
const geminiTools = options?.tools
?.map((tool) => {
if (
"function" in tool &&
// eslint-disable-next-line @typescript-eslint/no-explicit-any
"parameters" in (tool.function as Record<string, any>)
) {
// Tool is in OpenAI format. Convert to Gemini then return.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const castTool = tool.function as Record<string, any>;
const cleanedParameters = castTool.parameters;
if ("$schema" in cleanedParameters) {
delete cleanedParameters.$schema;
}
if ("additionalProperties" in cleanedParameters) {
delete cleanedParameters.additionalProperties;
}
const toolInGeminiFormat: GeminiTool = {
functionDeclarations: [
{
name: castTool.name,
description: castTool.description,
parameters: cleanedParameters,
},
],
};
return toolInGeminiFormat;
} else if ("functionDeclarations" in tool) {
return tool;
} else {
return null;
}
})
.filter((tool): tool is GeminiTool => tool !== null);
const structuredOutputTools = options?.tools
?.map((tool) => {
if ("lc_namespace" in tool) {
return tool;
} else {
return null;
}
})
.filter((tool): tool is StructuredToolInterface => tool !== null);
if (
structuredOutputTools &&
structuredOutputTools.length > 0 &&
geminiTools &&
geminiTools.length > 0
) {
throw new Error(
`Cannot mix structured tools with Gemini tools.\nReceived ${structuredOutputTools.length} structured tools and ${geminiTools.length} Gemini tools.`
);
}
ret.tools = geminiTools ?? structuredOutputTools;
return ret;
}
export function modelToFamily(
modelName: string | undefined
): GoogleLLMModelFamily {
if (!modelName) {
return null;
} else if (isModelGemini(modelName)) {
return "gemini";
} else {
return null;
}
}
export function validateModelParams(
params: GoogleAIModelParams | undefined
): void {
const testParams: GoogleAIModelParams = params ?? {};
switch (modelToFamily(testParams.modelName)) {
case "gemini":
return validateGeminiParams(testParams);
default:
throw new Error(
`Unable to verify model params: ${JSON.stringify(params)}`
);
}
}
export function copyAndValidateModelParamsInto(
params: GoogleAIModelParams | undefined,
target: GoogleAIModelParams
): GoogleAIModelParams {
copyAIModelParamsInto(params, undefined, target);
validateModelParams(target);
return target;
}