@adminforth/completion-adapter-google-gemini 2.0.9 → 2.1.0
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/index.js +15 -0
- package/index.ts +43 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -38,6 +38,20 @@ function getGoogleLangChainAgentOptions(extraRequestBodyParameters) {
|
|
|
38
38
|
}
|
|
39
39
|
return options;
|
|
40
40
|
}
|
|
41
|
+
function extractUsedTokens(usageMetadata) {
|
|
42
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
43
|
+
if (!usageMetadata)
|
|
44
|
+
return undefined;
|
|
45
|
+
const input = (_b = (_a = usageMetadata.promptTokenCount) !== null && _a !== void 0 ? _a : usageMetadata.prompt_token_count) !== null && _b !== void 0 ? _b : 0;
|
|
46
|
+
const inputCached = (_d = (_c = usageMetadata.cachedContentTokenCount) !== null && _c !== void 0 ? _c : usageMetadata.cached_content_token_count) !== null && _d !== void 0 ? _d : 0;
|
|
47
|
+
const output = (_f = (_e = usageMetadata.candidatesTokenCount) !== null && _e !== void 0 ? _e : usageMetadata.candidates_token_count) !== null && _f !== void 0 ? _f : 0;
|
|
48
|
+
const thoughts = (_h = (_g = usageMetadata.thoughtsTokenCount) !== null && _g !== void 0 ? _g : usageMetadata.thoughts_token_count) !== null && _h !== void 0 ? _h : 0;
|
|
49
|
+
return {
|
|
50
|
+
input_uncached: Math.max(input - inputCached, 0),
|
|
51
|
+
input_cached: inputCached,
|
|
52
|
+
output: output + thoughts,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
41
55
|
export default class CompletionAdapterGoogleGemini {
|
|
42
56
|
constructor(options) {
|
|
43
57
|
this.complete = (requestOrContent_1, ...args_1) => __awaiter(this, [requestOrContent_1, ...args_1], void 0, function* (requestOrContent, stopOrMaxTokens = 50, maxTokensOrOutputSchema = 50, outputSchema) {
|
|
@@ -74,6 +88,7 @@ export default class CompletionAdapterGoogleGemini {
|
|
|
74
88
|
logger.debug(`Google Gemini SUCCESSFUL API response: ${response}`);
|
|
75
89
|
return {
|
|
76
90
|
content: response.text,
|
|
91
|
+
used_tokens: extractUsedTokens(response.usageMetadata),
|
|
77
92
|
};
|
|
78
93
|
}
|
|
79
94
|
catch (error) {
|
package/index.ts
CHANGED
|
@@ -13,6 +13,23 @@ type CompletionRequestInput = {
|
|
|
13
13
|
|
|
14
14
|
type AgentModelPurpose = "primary" | "summary";
|
|
15
15
|
|
|
16
|
+
type GeminiUsageMetadata = {
|
|
17
|
+
promptTokenCount?: number;
|
|
18
|
+
cachedContentTokenCount?: number;
|
|
19
|
+
candidatesTokenCount?: number;
|
|
20
|
+
thoughtsTokenCount?: number;
|
|
21
|
+
prompt_token_count?: number;
|
|
22
|
+
cached_content_token_count?: number;
|
|
23
|
+
candidates_token_count?: number;
|
|
24
|
+
thoughts_token_count?: number;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
type UsedTokens = {
|
|
28
|
+
input_uncached: number;
|
|
29
|
+
input_cached: number;
|
|
30
|
+
output: number;
|
|
31
|
+
};
|
|
32
|
+
|
|
16
33
|
const GOOGLE_LANGCHAIN_AGENT_OPTION_KEYS = [
|
|
17
34
|
"temperature",
|
|
18
35
|
"topP",
|
|
@@ -47,6 +64,30 @@ function getGoogleLangChainAgentOptions(
|
|
|
47
64
|
return options;
|
|
48
65
|
}
|
|
49
66
|
|
|
67
|
+
function extractUsedTokens(usageMetadata?: GeminiUsageMetadata): UsedTokens | undefined {
|
|
68
|
+
if (!usageMetadata) return undefined;
|
|
69
|
+
|
|
70
|
+
const input = usageMetadata.promptTokenCount ?? usageMetadata.prompt_token_count ?? 0;
|
|
71
|
+
const inputCached =
|
|
72
|
+
usageMetadata.cachedContentTokenCount ??
|
|
73
|
+
usageMetadata.cached_content_token_count ??
|
|
74
|
+
0;
|
|
75
|
+
const output =
|
|
76
|
+
usageMetadata.candidatesTokenCount ??
|
|
77
|
+
usageMetadata.candidates_token_count ??
|
|
78
|
+
0;
|
|
79
|
+
const thoughts =
|
|
80
|
+
usageMetadata.thoughtsTokenCount ??
|
|
81
|
+
usageMetadata.thoughts_token_count ??
|
|
82
|
+
0;
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
input_uncached: Math.max(input - inputCached, 0),
|
|
86
|
+
input_cached: inputCached,
|
|
87
|
+
output: output + thoughts,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
50
91
|
export default class CompletionAdapterGoogleGemini
|
|
51
92
|
implements CompletionAdapter
|
|
52
93
|
{
|
|
@@ -103,6 +144,7 @@ export default class CompletionAdapterGoogleGemini
|
|
|
103
144
|
content?: string;
|
|
104
145
|
finishReason?: string;
|
|
105
146
|
error?: string;
|
|
147
|
+
used_tokens?: UsedTokens;
|
|
106
148
|
}> => {
|
|
107
149
|
const request =
|
|
108
150
|
typeof requestOrContent === "string"
|
|
@@ -150,6 +192,7 @@ export default class CompletionAdapterGoogleGemini
|
|
|
150
192
|
logger.debug(`Google Gemini SUCCESSFUL API response: ${response}`);
|
|
151
193
|
return {
|
|
152
194
|
content: response.text,
|
|
195
|
+
used_tokens: extractUsedTokens(response.usageMetadata),
|
|
153
196
|
};
|
|
154
197
|
} catch (error) {
|
|
155
198
|
logger.error(`Error during Google Gemini API call: ${error}`);
|