-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgemini.ts
More file actions
156 lines (139 loc) · 4.43 KB
/
Copy pathgemini.ts
File metadata and controls
156 lines (139 loc) · 4.43 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { GoogleGenAI } from "@google/genai";
import { logger } from "adminforth";
import pRetry from "p-retry";
import type { AdapterOptions } from "./types.js";
export type CompletionRequestInput = {
content: string;
maxTokens?: number;
outputSchema?: any;
};
type GeminiUsageMetadata = {
promptTokenCount?: number;
cachedContentTokenCount?: number;
candidatesTokenCount?: number;
thoughtsTokenCount?: number;
prompt_token_count?: number;
cached_content_token_count?: number;
candidates_token_count?: number;
thoughts_token_count?: number;
};
export type UsedTokens = {
input_uncached: number;
input_cached: number;
output: number;
};
export type CompletionResult = {
content?: string;
finishReason?: string;
error?: string;
used_tokens?: UsedTokens;
};
const JSON_CODE_FENCE_RE = /^```(?:json)?\s*([\s\S]*?)\s*```$/i;
function getResponseJsonSchema(outputSchema?: any) {
return outputSchema?.schema ?? outputSchema;
}
function unwrapJsonCodeFence(content: string | undefined) {
return content?.replace(JSON_CODE_FENCE_RE, "$1");
}
function extractUsedTokens(usageMetadata?: GeminiUsageMetadata): UsedTokens | undefined {
if (!usageMetadata) return undefined;
const input = usageMetadata.promptTokenCount ?? usageMetadata.prompt_token_count ?? 0;
const inputCached =
usageMetadata.cachedContentTokenCount ??
usageMetadata.cached_content_token_count ??
0;
const output =
usageMetadata.candidatesTokenCount ??
usageMetadata.candidates_token_count ??
0;
const thoughts =
usageMetadata.thoughtsTokenCount ??
usageMetadata.thoughts_token_count ??
0;
return {
input_uncached: Math.max(input - inputCached, 0),
input_cached: inputCached,
output: output + thoughts,
};
}
export class GeminiService {
constructor(private options: AdapterOptions) {}
validate() {
if (!this.options.geminiApiKey) {
throw new Error("geminiApiKey is required");
}
}
async measureTokensCount(content: string): Promise<number> {
const ai = new GoogleGenAI({
apiKey: this.options.geminiApiKey,
});
const countTokensResponse = await ai.models.countTokens({
model: "gemini-3.6-flash",
contents: content,
});
return countTokensResponse.totalTokens;
}
async complete(request: CompletionRequestInput): Promise<CompletionResult> {
const {
content,
maxTokens: requestMaxTokens = 50,
outputSchema: requestOutputSchema,
} = request;
const responseJsonSchema = getResponseJsonSchema(requestOutputSchema);
const ai = new GoogleGenAI({
apiKey: this.options.geminiApiKey,
});
const tryToGenerate = async () => {
logger.debug("Making Google Gemini API call");
try {
const response = await ai.models.generateContent({
model: this.options.model || "gemini-3-flash-preview",
contents: [
{
role: "user",
parts: [{ text: content }],
},
],
config: {
maxOutputTokens: requestMaxTokens,
...this.options.extraRequestBodyParameters,
...(responseJsonSchema
? {
responseFormat: [
{
text: {
mimeType: "application/json",
schema: responseJsonSchema,
},
},
],
}
: {}),
},
});
logger.debug(`Google Gemini SUCCESSFUL API response: ${response}`);
return {
content: responseJsonSchema
? unwrapJsonCodeFence(response.text)
: response.text,
used_tokens: extractUsedTokens(response.usageMetadata),
};
} catch (error) {
logger.error(`Error during Google Gemini API call: ${error}`);
const errorMessage = error instanceof Error ? error.message : String(error);
let googleErrorMessage = errorMessage;
try {
googleErrorMessage = JSON.parse(errorMessage).error.message;
} catch {
}
throw new Error(`Error during Google Gemini API call: ${googleErrorMessage}`);
}
};
return pRetry(tryToGenerate, {
retries: 5,
onFailedAttempt: ({ error, attemptNumber, retriesLeft, retriesConsumed }) => {
logger.debug(`Attempt ${attemptNumber} failed. ${retriesLeft} retries left. ${retriesConsumed} retries consumed.`);
},
});
}
}