-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
75 lines (66 loc) · 2.24 KB
/
index.ts
File metadata and controls
75 lines (66 loc) · 2.24 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
import type { AdapterOptions } from "./types.js";
import type { CompletionAdapter } from "adminforth";
import { GoogleGenAI } from "@google/genai";
import pRetry from 'p-retry';
import { logger } from "adminforth";
export default class CompletionAdapterGoogleGemini
implements CompletionAdapter
{
options: AdapterOptions;
constructor(options: AdapterOptions) {
this.options = options;
}
validate() {
if (!this.options.geminiApiKey) {
throw new Error("geminiApiKey is required");
}
}
async measureTokensCount(content: string): Promise<number> {
// Implement token counting logic here
const ai = new GoogleGenAI({
apiKey: this.options.geminiApiKey,
});
const countTokensResponse = await ai.models.countTokens({
model: "gemini-2.0-flash",
contents: content,
});
return countTokensResponse.totalTokens;
}
complete = async (content: string, stop = ["."], maxTokens = 50, outputSchema?: any): Promise<{
content?: string;
finishReason?: string;
error?: string;
}> => {
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: content,
config: {
responseJsonSchema: outputSchema ? outputSchema : undefined,
maxOutputTokens: maxTokens,
...this.options.extraRequestBodyParameters,
},
});
logger.debug(`Google Gemini SUCCESSFUL API response: ${response}`);
return {
content: response.text,
};
} catch (error) {
logger.error(`Error during Google Gemini API call: ${error}`);
throw new Error(`Error during Google Gemini API call: ${JSON.parse(error.message).error.message}`);
}
}
const result = await pRetry(tryToGenerate, {
retries: 5,
onFailedAttempt: ({error, attemptNumber, retriesLeft, retriesConsumed}) => {
logger.debug(`Attempt ${attemptNumber} failed. ${retriesLeft} retries left. ${retriesConsumed} retries consumed.`);
},
})
return result;
};
}