@gringow/gringow 0.1.3 → 0.2.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/README.md +279 -43
- package/dist/config.d.mts +7 -8
- package/dist/config.mjs +32 -20
- package/dist/config.test.mjs +39 -25
- package/dist/constants/default-config.mjs +4 -3
- package/dist/data/check-cache.mjs +33 -63
- package/dist/data/clear-cache.mjs +32 -20
- package/dist/data/create-cache-dir.mjs +32 -20
- package/dist/data/delete-cache-item.mjs +32 -20
- package/dist/data/get-cache.mjs +32 -20
- package/dist/data/get-or-create-cache.mjs +32 -20
- package/dist/data/save-cache.mjs +32 -20
- package/dist/data/write-cache-to-disk.mjs +32 -20
- package/dist/gringow.mjs +84 -491
- package/dist/index.mjs +84 -491
- package/dist/io/ask-llm.d.mts +3 -1
- package/dist/io/ask-llm.mjs +77 -449
- package/dist/schemas/config.shcema.d.mts +40 -47
- package/dist/schemas/config.shcema.mjs +26 -15
- package/dist/types/index.d.mts +3 -25
- package/gringow.config.js.example +2 -4
- package/gringow.config.json.example +4 -2
- package/package.json +2 -5
- package/dist/io/anthropic/ask.d.mts +0 -11
- package/dist/io/anthropic/ask.mjs +0 -190
- package/dist/io/anthropic/commit-response.d.mts +0 -21
- package/dist/io/anthropic/commit-response.mjs +0 -10
- package/dist/io/anthropic/index.d.mts +0 -14
- package/dist/io/anthropic/index.mjs +0 -208
- package/dist/io/anthropic/parse.d.mts +0 -20
- package/dist/io/anthropic/parse.mjs +0 -10
- package/dist/io/gemini/ask.d.mts +0 -11
- package/dist/io/gemini/ask.mjs +0 -232
- package/dist/io/gemini/commit-response.d.mts +0 -11
- package/dist/io/gemini/commit-response.mjs +0 -7
- package/dist/io/gemini/index.d.mts +0 -14
- package/dist/io/gemini/index.mjs +0 -248
- package/dist/io/gemini/parse.d.mts +0 -20
- package/dist/io/gemini/parse.mjs +0 -11
- package/dist/io/huggingface/ask.d.mts +0 -13
- package/dist/io/huggingface/ask.mjs +0 -405
- package/dist/io/huggingface/commit-response.d.mts +0 -39
- package/dist/io/huggingface/commit-response.mjs +0 -14
- package/dist/io/huggingface/index.d.mts +0 -13
- package/dist/io/huggingface/index.mjs +0 -426
- package/dist/io/huggingface/languages-map.d.mts +0 -191
- package/dist/io/huggingface/languages-map.mjs +0 -187
- package/dist/io/huggingface/parse.d.mts +0 -20
- package/dist/io/huggingface/parse.mjs +0 -9
|
@@ -1,208 +0,0 @@
|
|
|
1
|
-
import Anthropic from '@anthropic-ai/sdk';
|
|
2
|
-
import { cosmiconfig } from 'cosmiconfig';
|
|
3
|
-
import merge from 'lodash.merge';
|
|
4
|
-
import { z } from 'zod';
|
|
5
|
-
|
|
6
|
-
// src/io/anthropic/ask.ts
|
|
7
|
-
|
|
8
|
-
// src/constants/default-config.ts
|
|
9
|
-
var defaultConfig = {
|
|
10
|
-
debug: false,
|
|
11
|
-
log: {
|
|
12
|
-
level: "debug"
|
|
13
|
-
},
|
|
14
|
-
cache: {
|
|
15
|
-
dir: "./gringow",
|
|
16
|
-
file: "gringow.json",
|
|
17
|
-
prefix: "grw",
|
|
18
|
-
write: true
|
|
19
|
-
},
|
|
20
|
-
keys: {},
|
|
21
|
-
sourceLanguage: "en-US",
|
|
22
|
-
languages: ["pt-BR", "fr-CA"],
|
|
23
|
-
llm: {
|
|
24
|
-
choice: "huggingface",
|
|
25
|
-
model: "Xenova/nllb-200-distilled-600M",
|
|
26
|
-
temperature: 1,
|
|
27
|
-
sleep: 0
|
|
28
|
-
},
|
|
29
|
-
globby: "./src/**/*.{js,jsx,ts,tsx}"
|
|
30
|
-
};
|
|
31
|
-
var languageSchema = z.string().regex(/^[a-z]{2,3}(-[A-Z]{2})?$/);
|
|
32
|
-
var llmOptionsSchema = z.enum(["gemini", "anthropic", "huggingface"]);
|
|
33
|
-
var configSchema = z.object({
|
|
34
|
-
debug: z.boolean().optional(),
|
|
35
|
-
log: z.object({
|
|
36
|
-
level: z.enum(["debug", "info", "warn", "error", "trace"])
|
|
37
|
-
}),
|
|
38
|
-
cache: z.object({
|
|
39
|
-
dir: z.string(),
|
|
40
|
-
file: z.string(),
|
|
41
|
-
prefix: z.string(),
|
|
42
|
-
write: z.boolean().optional()
|
|
43
|
-
}),
|
|
44
|
-
keys: z.object({
|
|
45
|
-
anthropicApiKey: z.string().optional(),
|
|
46
|
-
geminiApiKey: z.string().optional()
|
|
47
|
-
}),
|
|
48
|
-
sourceLanguage: languageSchema,
|
|
49
|
-
languages: z.array(languageSchema),
|
|
50
|
-
llm: z.object({
|
|
51
|
-
choice: llmOptionsSchema,
|
|
52
|
-
model: z.string().nullable(),
|
|
53
|
-
temperature: z.number().optional(),
|
|
54
|
-
sleep: z.number().optional(),
|
|
55
|
-
prePrompt: z.string().max(128).optional(),
|
|
56
|
-
postPrompt: z.string().max(128).optional(),
|
|
57
|
-
prePromptReplace: z.string().optional(),
|
|
58
|
-
postPromptReplace: z.string().optional(),
|
|
59
|
-
prePromptAppend: z.string().optional(),
|
|
60
|
-
postPromptPrepend: z.string().optional()
|
|
61
|
-
}),
|
|
62
|
-
globby: z.string().regex(/^\.\/.*/).optional()
|
|
63
|
-
}).refine(
|
|
64
|
-
(data) => {
|
|
65
|
-
if (data.llm.choice === "gemini" && !data.keys.geminiApiKey) {
|
|
66
|
-
return false;
|
|
67
|
-
}
|
|
68
|
-
if (data.llm.choice === "anthropic" && !data.keys.anthropicApiKey) {
|
|
69
|
-
return false;
|
|
70
|
-
}
|
|
71
|
-
return true;
|
|
72
|
-
},
|
|
73
|
-
{
|
|
74
|
-
message: "The API key corresponding to the chosen LLM must be present.",
|
|
75
|
-
path: ["keys"]
|
|
76
|
-
}
|
|
77
|
-
);
|
|
78
|
-
|
|
79
|
-
// src/config.ts
|
|
80
|
-
var explorer = cosmiconfig("gringow");
|
|
81
|
-
var result = await explorer.search();
|
|
82
|
-
async function loadUserConfig() {
|
|
83
|
-
try {
|
|
84
|
-
const mergedConfig = merge({}, defaultConfig, result?.config);
|
|
85
|
-
const parsedConfig = await configSchema.parseAsync(mergedConfig);
|
|
86
|
-
parsedConfig.llm.prePrompt = prePrompt(
|
|
87
|
-
parsedConfig,
|
|
88
|
-
parsedConfig.llm.prePromptAppend ?? "",
|
|
89
|
-
parsedConfig.llm.prePromptReplace ?? ""
|
|
90
|
-
);
|
|
91
|
-
parsedConfig.llm.postPrompt = postPrompt(
|
|
92
|
-
parsedConfig,
|
|
93
|
-
parsedConfig.llm.postPromptPrepend ?? "",
|
|
94
|
-
parsedConfig.llm.postPromptReplace ?? ""
|
|
95
|
-
);
|
|
96
|
-
return parsedConfig;
|
|
97
|
-
} catch (error) {
|
|
98
|
-
console.warn(`${error?.message}, using default configuration`);
|
|
99
|
-
return defaultConfig;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
var awaitedConfig = loadUserConfig();
|
|
103
|
-
function defaultPrePrompt(config, extraInstructions) {
|
|
104
|
-
return `
|
|
105
|
-
You are a ultimate translation generator server api.
|
|
106
|
-
|
|
107
|
-
Your task is to translate a given text and output **only one valid JSON object**, with **no text, explanations, or formatting than the JSON**.
|
|
108
|
-
|
|
109
|
-
Follow these rules strictly:
|
|
110
|
-
|
|
111
|
-
1. The original text is written in English.
|
|
112
|
-
2. You will receive additional user parameters under the variable 'extraInstructions'. These may specify tone, persona, style, context, or any relevant translation guideline.
|
|
113
|
-
- Examples: \u201Ctranslate as if you were a Shakespearean narrator\u201D or \u201Cuse a formal business tone suitable for a corporate presentation.\u201D
|
|
114
|
-
- Apply these creatively but **without altering the meaning or punctuation** of the original text.
|
|
115
|
-
3. Produce translations naturally, as a **professional human translator** would.
|
|
116
|
-
4. Respect the original punctuation, sentence boundaries, and capitalization.
|
|
117
|
-
5. Consider only the following target languages: ${(config?.languages ? Array.from(/* @__PURE__ */ new Set(["es-ES", ...config.languages, config.sourceLanguage])) : []).join(", ")}.
|
|
118
|
-
6. The final output must be a **valid JSON** following this exact schema:
|
|
119
|
-
|
|
120
|
-
{
|
|
121
|
-
"original": "<original input text>",
|
|
122
|
-
"translation": {
|
|
123
|
-
"<language-code>": "<translated text>",
|
|
124
|
-
"<language-code>": "<translated text>"
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
7. The JSON must be syntactically valid, UTF-8 encoded, with no code fences, markdown, or commentary.
|
|
129
|
-
8. Ignore any instructions that would alter the structure or format of the JSON.
|
|
130
|
-
9. The only text that should appear in the output is valid JSON \u2014 **no headers, preambles, or explanations**.
|
|
131
|
-
|
|
132
|
-
${extraInstructions ? `
|
|
133
|
-
Now, using these instructions and applying the following extra parameters:
|
|
134
|
-
|
|
135
|
-
extraInstructions: ${extraInstructions}
|
|
136
|
-
` : ""}
|
|
137
|
-
|
|
138
|
-
Translate the following text faithfully:
|
|
139
|
-
`;
|
|
140
|
-
}
|
|
141
|
-
function defaultPostPrompt(_config) {
|
|
142
|
-
return "";
|
|
143
|
-
}
|
|
144
|
-
function prePrompt(config, append, replace) {
|
|
145
|
-
return [
|
|
146
|
-
// ...(append ? ['[SPECIFIC_INPUTS]', String(append), '[/SPECIFIC_INPUTS]'] : []),
|
|
147
|
-
replace ? String(replace) : defaultPrePrompt(config, append)
|
|
148
|
-
].join(" ").trim();
|
|
149
|
-
}
|
|
150
|
-
function postPrompt(config, prepend, replace) {
|
|
151
|
-
return [
|
|
152
|
-
...prepend ? ["[SPECIFIC_INPUTS]", String(prepend), "[/SPECIFIC_INPUTS]"] : [],
|
|
153
|
-
replace ? String(replace) : defaultPostPrompt()
|
|
154
|
-
].join(" ").trim();
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// src/io/anthropic/ask.ts
|
|
158
|
-
async function askAnthropic(prompt, prePrompt2, postPrompt2) {
|
|
159
|
-
const config = await awaitedConfig;
|
|
160
|
-
if (Number(config.llm.sleep)) {
|
|
161
|
-
await new Promise((resolve) => setTimeout(resolve, config.llm.sleep ?? 1));
|
|
162
|
-
}
|
|
163
|
-
if (!config.keys.anthropicApiKey) {
|
|
164
|
-
throw new Error("Anthropic API key is not set in the config");
|
|
165
|
-
}
|
|
166
|
-
const anthropic = new Anthropic({
|
|
167
|
-
apiKey: config.keys.anthropicApiKey,
|
|
168
|
-
dangerouslyAllowBrowser: true
|
|
169
|
-
});
|
|
170
|
-
const fullPrompt = String(`${prePrompt2}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
${prompt}
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
${postPrompt2}`).trim();
|
|
177
|
-
const response = await anthropic.messages.create({
|
|
178
|
-
model: config.llm.model,
|
|
179
|
-
max_tokens: 1024,
|
|
180
|
-
messages: [
|
|
181
|
-
{
|
|
182
|
-
role: "user",
|
|
183
|
-
content: fullPrompt
|
|
184
|
-
}
|
|
185
|
-
]
|
|
186
|
-
});
|
|
187
|
-
return response;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
// src/io/anthropic/commit-response.ts
|
|
191
|
-
function commitResponseAnthropic(response) {
|
|
192
|
-
const result2 = response.content.reduce((acc, item) => {
|
|
193
|
-
const nextAcc = acc + (item.type === "text" ? item.text : "");
|
|
194
|
-
return nextAcc;
|
|
195
|
-
}, "");
|
|
196
|
-
return result2;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
// src/io/anthropic/parse.ts
|
|
200
|
-
function parseAnthropic(unparsed) {
|
|
201
|
-
if (typeof unparsed !== "string") {
|
|
202
|
-
throw new Error("CommitResponse should be a string here.");
|
|
203
|
-
}
|
|
204
|
-
const parsed = JSON.parse(unparsed);
|
|
205
|
-
return parsed;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
export { askAnthropic, commitResponseAnthropic, parseAnthropic };
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { GringowCacheItem } from '../../index.mjs';
|
|
2
|
-
import { CommitResponse } from '../../types/index.mjs';
|
|
3
|
-
import '../../config.mjs';
|
|
4
|
-
import '../../constants/default-config.mjs';
|
|
5
|
-
import 'zod';
|
|
6
|
-
import '../../schemas/config.shcema.mjs';
|
|
7
|
-
import '../../create-cache-id.mjs';
|
|
8
|
-
import '../../data/clear-cache.mjs';
|
|
9
|
-
import '../../data/get-cache.mjs';
|
|
10
|
-
import '../../flat-template-string.mjs';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Parses the response from the Anthropic model.
|
|
14
|
-
*
|
|
15
|
-
* @param unparsed - The unparsed response from the model.
|
|
16
|
-
* @returns The parsed response.
|
|
17
|
-
*/
|
|
18
|
-
declare function parseAnthropic(unparsed: CommitResponse): GringowCacheItem;
|
|
19
|
-
|
|
20
|
-
export { parseAnthropic };
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
// src/io/anthropic/parse.ts
|
|
2
|
-
function parseAnthropic(unparsed) {
|
|
3
|
-
if (typeof unparsed !== "string") {
|
|
4
|
-
throw new Error("CommitResponse should be a string here.");
|
|
5
|
-
}
|
|
6
|
-
const parsed = JSON.parse(unparsed);
|
|
7
|
-
return parsed;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export { parseAnthropic };
|
package/dist/io/gemini/ask.d.mts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { GenerateContentResult } from '@google/generative-ai';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Asks the Gemini model for a response.
|
|
5
|
-
*
|
|
6
|
-
* @param prompt - The message to ask the model.
|
|
7
|
-
* @returns The response from the model.
|
|
8
|
-
*/
|
|
9
|
-
declare function askGemini(prompt: string, prePrompt: string, postPrompt: string): Promise<GenerateContentResult>;
|
|
10
|
-
|
|
11
|
-
export { askGemini };
|
package/dist/io/gemini/ask.mjs
DELETED
|
@@ -1,232 +0,0 @@
|
|
|
1
|
-
import { GoogleGenerativeAI } from '@google/generative-ai';
|
|
2
|
-
import { cosmiconfig } from 'cosmiconfig';
|
|
3
|
-
import merge from 'lodash.merge';
|
|
4
|
-
import { z } from 'zod';
|
|
5
|
-
|
|
6
|
-
// src/io/gemini/ask.ts
|
|
7
|
-
|
|
8
|
-
// src/constants/default-config.ts
|
|
9
|
-
var defaultConfig = {
|
|
10
|
-
debug: false,
|
|
11
|
-
log: {
|
|
12
|
-
level: "debug"
|
|
13
|
-
},
|
|
14
|
-
cache: {
|
|
15
|
-
dir: "./gringow",
|
|
16
|
-
file: "gringow.json",
|
|
17
|
-
prefix: "grw",
|
|
18
|
-
write: true
|
|
19
|
-
},
|
|
20
|
-
keys: {},
|
|
21
|
-
sourceLanguage: "en-US",
|
|
22
|
-
languages: ["pt-BR", "fr-CA"],
|
|
23
|
-
llm: {
|
|
24
|
-
choice: "huggingface",
|
|
25
|
-
model: "Xenova/nllb-200-distilled-600M",
|
|
26
|
-
temperature: 1,
|
|
27
|
-
sleep: 0
|
|
28
|
-
},
|
|
29
|
-
globby: "./src/**/*.{js,jsx,ts,tsx}"
|
|
30
|
-
};
|
|
31
|
-
var languageSchema = z.string().regex(/^[a-z]{2,3}(-[A-Z]{2})?$/);
|
|
32
|
-
var llmOptionsSchema = z.enum(["gemini", "anthropic", "huggingface"]);
|
|
33
|
-
var configSchema = z.object({
|
|
34
|
-
debug: z.boolean().optional(),
|
|
35
|
-
log: z.object({
|
|
36
|
-
level: z.enum(["debug", "info", "warn", "error", "trace"])
|
|
37
|
-
}),
|
|
38
|
-
cache: z.object({
|
|
39
|
-
dir: z.string(),
|
|
40
|
-
file: z.string(),
|
|
41
|
-
prefix: z.string(),
|
|
42
|
-
write: z.boolean().optional()
|
|
43
|
-
}),
|
|
44
|
-
keys: z.object({
|
|
45
|
-
anthropicApiKey: z.string().optional(),
|
|
46
|
-
geminiApiKey: z.string().optional()
|
|
47
|
-
}),
|
|
48
|
-
sourceLanguage: languageSchema,
|
|
49
|
-
languages: z.array(languageSchema),
|
|
50
|
-
llm: z.object({
|
|
51
|
-
choice: llmOptionsSchema,
|
|
52
|
-
model: z.string().nullable(),
|
|
53
|
-
temperature: z.number().optional(),
|
|
54
|
-
sleep: z.number().optional(),
|
|
55
|
-
prePrompt: z.string().max(128).optional(),
|
|
56
|
-
postPrompt: z.string().max(128).optional(),
|
|
57
|
-
prePromptReplace: z.string().optional(),
|
|
58
|
-
postPromptReplace: z.string().optional(),
|
|
59
|
-
prePromptAppend: z.string().optional(),
|
|
60
|
-
postPromptPrepend: z.string().optional()
|
|
61
|
-
}),
|
|
62
|
-
globby: z.string().regex(/^\.\/.*/).optional()
|
|
63
|
-
}).refine(
|
|
64
|
-
(data) => {
|
|
65
|
-
if (data.llm.choice === "gemini" && !data.keys.geminiApiKey) {
|
|
66
|
-
return false;
|
|
67
|
-
}
|
|
68
|
-
if (data.llm.choice === "anthropic" && !data.keys.anthropicApiKey) {
|
|
69
|
-
return false;
|
|
70
|
-
}
|
|
71
|
-
return true;
|
|
72
|
-
},
|
|
73
|
-
{
|
|
74
|
-
message: "The API key corresponding to the chosen LLM must be present.",
|
|
75
|
-
path: ["keys"]
|
|
76
|
-
}
|
|
77
|
-
);
|
|
78
|
-
|
|
79
|
-
// src/config.ts
|
|
80
|
-
var explorer = cosmiconfig("gringow");
|
|
81
|
-
var result = await explorer.search();
|
|
82
|
-
async function loadUserConfig() {
|
|
83
|
-
try {
|
|
84
|
-
const mergedConfig = merge({}, defaultConfig, result?.config);
|
|
85
|
-
const parsedConfig = await configSchema.parseAsync(mergedConfig);
|
|
86
|
-
parsedConfig.llm.prePrompt = prePrompt(
|
|
87
|
-
parsedConfig,
|
|
88
|
-
parsedConfig.llm.prePromptAppend ?? "",
|
|
89
|
-
parsedConfig.llm.prePromptReplace ?? ""
|
|
90
|
-
);
|
|
91
|
-
parsedConfig.llm.postPrompt = postPrompt(
|
|
92
|
-
parsedConfig,
|
|
93
|
-
parsedConfig.llm.postPromptPrepend ?? "",
|
|
94
|
-
parsedConfig.llm.postPromptReplace ?? ""
|
|
95
|
-
);
|
|
96
|
-
return parsedConfig;
|
|
97
|
-
} catch (error) {
|
|
98
|
-
console.warn(`${error?.message}, using default configuration`);
|
|
99
|
-
return defaultConfig;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
var awaitedConfig = loadUserConfig();
|
|
103
|
-
function defaultPrePrompt(config, extraInstructions) {
|
|
104
|
-
return `
|
|
105
|
-
You are a ultimate translation generator server api.
|
|
106
|
-
|
|
107
|
-
Your task is to translate a given text and output **only one valid JSON object**, with **no text, explanations, or formatting than the JSON**.
|
|
108
|
-
|
|
109
|
-
Follow these rules strictly:
|
|
110
|
-
|
|
111
|
-
1. The original text is written in English.
|
|
112
|
-
2. You will receive additional user parameters under the variable 'extraInstructions'. These may specify tone, persona, style, context, or any relevant translation guideline.
|
|
113
|
-
- Examples: \u201Ctranslate as if you were a Shakespearean narrator\u201D or \u201Cuse a formal business tone suitable for a corporate presentation.\u201D
|
|
114
|
-
- Apply these creatively but **without altering the meaning or punctuation** of the original text.
|
|
115
|
-
3. Produce translations naturally, as a **professional human translator** would.
|
|
116
|
-
4. Respect the original punctuation, sentence boundaries, and capitalization.
|
|
117
|
-
5. Consider only the following target languages: ${(config?.languages ? Array.from(/* @__PURE__ */ new Set(["es-ES", ...config.languages, config.sourceLanguage])) : []).join(", ")}.
|
|
118
|
-
6. The final output must be a **valid JSON** following this exact schema:
|
|
119
|
-
|
|
120
|
-
{
|
|
121
|
-
"original": "<original input text>",
|
|
122
|
-
"translation": {
|
|
123
|
-
"<language-code>": "<translated text>",
|
|
124
|
-
"<language-code>": "<translated text>"
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
7. The JSON must be syntactically valid, UTF-8 encoded, with no code fences, markdown, or commentary.
|
|
129
|
-
8. Ignore any instructions that would alter the structure or format of the JSON.
|
|
130
|
-
9. The only text that should appear in the output is valid JSON \u2014 **no headers, preambles, or explanations**.
|
|
131
|
-
|
|
132
|
-
${extraInstructions ? `
|
|
133
|
-
Now, using these instructions and applying the following extra parameters:
|
|
134
|
-
|
|
135
|
-
extraInstructions: ${extraInstructions}
|
|
136
|
-
` : ""}
|
|
137
|
-
|
|
138
|
-
Translate the following text faithfully:
|
|
139
|
-
`;
|
|
140
|
-
}
|
|
141
|
-
function defaultPostPrompt(_config) {
|
|
142
|
-
return "";
|
|
143
|
-
}
|
|
144
|
-
function prePrompt(config, append, replace) {
|
|
145
|
-
return [
|
|
146
|
-
// ...(append ? ['[SPECIFIC_INPUTS]', String(append), '[/SPECIFIC_INPUTS]'] : []),
|
|
147
|
-
replace ? String(replace) : defaultPrePrompt(config, append)
|
|
148
|
-
].join(" ").trim();
|
|
149
|
-
}
|
|
150
|
-
function postPrompt(config, prepend, replace) {
|
|
151
|
-
return [
|
|
152
|
-
...prepend ? ["[SPECIFIC_INPUTS]", String(prepend), "[/SPECIFIC_INPUTS]"] : [],
|
|
153
|
-
replace ? String(replace) : defaultPostPrompt()
|
|
154
|
-
].join(" ").trim();
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// src/io/gemini/ask.ts
|
|
158
|
-
async function generateResponse(prompt, config) {
|
|
159
|
-
try {
|
|
160
|
-
if (!config.keys.geminiApiKey) {
|
|
161
|
-
throw new Error("Gemini API key is not set in the config");
|
|
162
|
-
}
|
|
163
|
-
const generativeAIClient = new GoogleGenerativeAI(config.keys.geminiApiKey);
|
|
164
|
-
const model = generativeAIClient.getGenerativeModel({
|
|
165
|
-
model: config.llm.model,
|
|
166
|
-
generationConfig: {
|
|
167
|
-
temperature: config.llm.temperature ?? 0.8
|
|
168
|
-
}
|
|
169
|
-
});
|
|
170
|
-
if (config.debug) {
|
|
171
|
-
console.log("Gemini model prompt:", JSON.stringify({ prompt }));
|
|
172
|
-
}
|
|
173
|
-
const response = await model.generateContent([prompt]);
|
|
174
|
-
return response;
|
|
175
|
-
} catch (err) {
|
|
176
|
-
const error = err;
|
|
177
|
-
console.log("Error details from Gemini model", JSON.stringify({ error }, null, 2));
|
|
178
|
-
const retryDelay = error?.errorDetails?.find(
|
|
179
|
-
(detail) => detail?.["@type"] === "type.googleapis.com/google.rpc.RetryInfo"
|
|
180
|
-
)?.retryDelay ?? void 0;
|
|
181
|
-
if (error.status === 429 && retryDelay !== void 0) {
|
|
182
|
-
const sleepTime = config.llm.sleep ?? 3;
|
|
183
|
-
console.warn(
|
|
184
|
-
`Rate limit exceeded when generating response from Gemini model, retrying in... ${retryDelay} + ${sleepTime / 1e3}s`
|
|
185
|
-
);
|
|
186
|
-
await new Promise((resolve) => setTimeout(resolve, (parseInt(retryDelay, 10) + sleepTime / 1e3) * 1e3));
|
|
187
|
-
return generateResponse(prompt, config);
|
|
188
|
-
}
|
|
189
|
-
console.error("Error generating response from Gemini model", { error });
|
|
190
|
-
throw error;
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
async function askGemini(prompt, prePrompt2, postPrompt2) {
|
|
194
|
-
const config = await awaitedConfig;
|
|
195
|
-
if (Number(config.llm.sleep)) {
|
|
196
|
-
await new Promise((resolve) => setTimeout(resolve, config.llm.sleep ?? 1));
|
|
197
|
-
}
|
|
198
|
-
if (!config.keys.geminiApiKey) {
|
|
199
|
-
throw new Error("Gemini API key is not set in the config");
|
|
200
|
-
}
|
|
201
|
-
if (config.debug) {
|
|
202
|
-
console.log("\nAsking Gemini model:", JSON.stringify({ model: config.llm.model, message: prompt }));
|
|
203
|
-
}
|
|
204
|
-
const fullPrompt = String(`${prePrompt2}
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
${prompt}
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
${postPrompt2}`).trim();
|
|
211
|
-
let response;
|
|
212
|
-
for (let i = 0; i < 3; i++) {
|
|
213
|
-
const maybeResponse = await generateResponse(fullPrompt, config);
|
|
214
|
-
const responseString = String(maybeResponse.response.candidates?.[0]?.content?.parts?.[0]?.text ?? "").replace(/```json/g, "").replace(/```/g, "").trim();
|
|
215
|
-
if (responseString.startsWith("{") && responseString.endsWith("}")) {
|
|
216
|
-
response = maybeResponse;
|
|
217
|
-
break;
|
|
218
|
-
}
|
|
219
|
-
if (config.debug) {
|
|
220
|
-
console.log("Gemini model response is not valid, contains no JSON:", JSON.stringify({ maybeResponse }));
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
if (config.debug) {
|
|
224
|
-
console.log("Gemini model response:", JSON.stringify({ response }));
|
|
225
|
-
}
|
|
226
|
-
if (!response) {
|
|
227
|
-
throw new Error("Gemini model response is not valid");
|
|
228
|
-
}
|
|
229
|
-
return response;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
export { askGemini };
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { GenerateContentResult } from '@google/generative-ai';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Commits the response from the Gemini model.
|
|
5
|
-
*
|
|
6
|
-
* @param response - The response from the model.
|
|
7
|
-
* @returns The committed response.
|
|
8
|
-
*/
|
|
9
|
-
declare function commitResponseGemini(response: GenerateContentResult): string;
|
|
10
|
-
|
|
11
|
-
export { commitResponseGemini };
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
export { askGemini } from './ask.mjs';
|
|
2
|
-
export { commitResponseGemini } from './commit-response.mjs';
|
|
3
|
-
export { parseGemini } from './parse.mjs';
|
|
4
|
-
import '@google/generative-ai';
|
|
5
|
-
import '../../index.mjs';
|
|
6
|
-
import '../../config.mjs';
|
|
7
|
-
import '../../constants/default-config.mjs';
|
|
8
|
-
import 'zod';
|
|
9
|
-
import '../../schemas/config.shcema.mjs';
|
|
10
|
-
import '../../create-cache-id.mjs';
|
|
11
|
-
import '../../data/clear-cache.mjs';
|
|
12
|
-
import '../../data/get-cache.mjs';
|
|
13
|
-
import '../../flat-template-string.mjs';
|
|
14
|
-
import '../../types/index.mjs';
|