@gringow/gringow 0.1.2 → 0.1.4
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 +313 -35
- package/dist/browser.d.mts +2 -1
- package/dist/browser.mjs +61 -4
- package/dist/config.d.mts +1 -0
- package/dist/config.mjs +5 -3
- package/dist/config.test.mjs +52 -67
- package/dist/constants/default-config.mjs +1 -0
- package/dist/{data/create-cache-id.d.mts → create-cache-id.d.mts} +2 -2
- package/dist/create-cache-id.mjs +54 -0
- package/dist/create-cache-id.test.d.mts +2 -0
- package/dist/create-cache-id.test.mjs +18464 -0
- package/dist/data/check-cache.d.mts +1 -1
- package/dist/data/check-cache.mjs +14 -7
- package/dist/data/clear-cache.mjs +14 -7
- package/dist/data/create-cache-dir.mjs +5 -3
- package/dist/data/delete-cache-item.d.mts +1 -1
- package/dist/data/delete-cache-item.mjs +14 -7
- package/dist/data/get-cache.mjs +5 -3
- package/dist/data/get-or-create-cache.d.mts +1 -1
- package/dist/data/get-or-create-cache.mjs +14 -7
- package/dist/data/md5.d.mts +13 -0
- package/dist/data/md5.mjs +47 -0
- package/dist/data/md5.test.d.mts +2 -0
- package/dist/data/md5.test.mjs +18457 -0
- package/dist/data/save-cache.d.mts +2 -2
- package/dist/data/save-cache.mjs +14 -7
- package/dist/data/write-cache-to-disk.d.mts +1 -1
- package/dist/data/write-cache-to-disk.mjs +14 -7
- package/dist/flat-template-string.test.mjs +10 -10
- package/dist/gringow.d.mts +1 -1
- package/dist/gringow.mjs +100 -35
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +100 -35
- package/dist/interpolate-template.d.mts +3 -0
- package/dist/interpolate-template.mjs +14 -0
- package/dist/io/anthropic/ask.mjs +5 -3
- package/dist/io/anthropic/commit-response.d.mts +1 -1
- package/dist/io/anthropic/index.d.mts +1 -1
- package/dist/io/anthropic/index.mjs +5 -3
- package/dist/io/anthropic/parse.d.mts +1 -1
- package/dist/io/ask-llm.d.mts +1 -1
- package/dist/io/ask-llm.mjs +38 -25
- package/dist/io/gemini/ask.mjs +38 -25
- package/dist/io/gemini/index.d.mts +1 -1
- package/dist/io/gemini/index.mjs +38 -25
- package/dist/io/gemini/parse.d.mts +1 -1
- package/dist/io/huggingface/ask.d.mts +1 -1
- package/dist/io/huggingface/ask.mjs +5 -3
- package/dist/io/huggingface/commit-response.d.mts +1 -1
- package/dist/io/huggingface/index.d.mts +1 -1
- package/dist/io/huggingface/index.mjs +5 -3
- package/dist/io/huggingface/parse.d.mts +1 -1
- package/dist/logger.test.mjs +10 -10
- package/dist/schemas/config.shcema.d.mts +5 -0
- package/dist/schemas/config.shcema.mjs +1 -0
- package/dist/types/index.d.mts +1 -1
- package/package.json +10 -10
- package/dist/data/create-cache-id.mjs +0 -10
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// src/interpolate-template.ts
|
|
2
|
+
function interpolateTemplate(template, values) {
|
|
3
|
+
if (!values || values.length === 0) return template;
|
|
4
|
+
let valueIndex = 0;
|
|
5
|
+
return template.replace(/\$\{[^}]*\}/g, () => {
|
|
6
|
+
if (valueIndex < values.length) {
|
|
7
|
+
const value = values[valueIndex++];
|
|
8
|
+
return value === null || value === void 0 ? "" : String(value);
|
|
9
|
+
}
|
|
10
|
+
return "";
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export { interpolateTemplate };
|
|
@@ -19,6 +19,7 @@ var defaultConfig = {
|
|
|
19
19
|
},
|
|
20
20
|
keys: {},
|
|
21
21
|
sourceLanguage: "en-US",
|
|
22
|
+
defaultLanguage: "pt-BR",
|
|
22
23
|
languages: ["pt-BR", "fr-CA"],
|
|
23
24
|
llm: {
|
|
24
25
|
choice: "huggingface",
|
|
@@ -46,6 +47,7 @@ var configSchema = z.object({
|
|
|
46
47
|
geminiApiKey: z.string().optional()
|
|
47
48
|
}),
|
|
48
49
|
sourceLanguage: languageSchema,
|
|
50
|
+
defaultLanguage: languageSchema,
|
|
49
51
|
languages: z.array(languageSchema),
|
|
50
52
|
llm: z.object({
|
|
51
53
|
choice: llmOptionsSchema,
|
|
@@ -102,9 +104,9 @@ async function loadUserConfig() {
|
|
|
102
104
|
var awaitedConfig = loadUserConfig();
|
|
103
105
|
function defaultPrePrompt(config, extraInstructions) {
|
|
104
106
|
return `
|
|
105
|
-
You are a translation generator.
|
|
107
|
+
You are a ultimate translation generator server api.
|
|
106
108
|
|
|
107
|
-
Your task is to output **only one valid JSON object**, with **no text, explanations, or formatting
|
|
109
|
+
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
110
|
|
|
109
111
|
Follow these rules strictly:
|
|
110
112
|
|
|
@@ -114,7 +116,7 @@ Follow these rules strictly:
|
|
|
114
116
|
- Apply these creatively but **without altering the meaning or punctuation** of the original text.
|
|
115
117
|
3. Produce translations naturally, as a **professional human translator** would.
|
|
116
118
|
4. Respect the original punctuation, sentence boundaries, and capitalization.
|
|
117
|
-
5. Consider only the following target languages: ${(config?.languages ? config.languages : []).join(", ")}.
|
|
119
|
+
5. Consider only the following target languages: ${(config?.languages ? Array.from(/* @__PURE__ */ new Set(["es-ES", ...config.languages, config.sourceLanguage])) : []).join(", ")}.
|
|
118
120
|
6. The final output must be a **valid JSON** following this exact schema:
|
|
119
121
|
|
|
120
122
|
{
|
|
@@ -5,8 +5,8 @@ import '../../index.mjs';
|
|
|
5
5
|
import '../../config.mjs';
|
|
6
6
|
import '../../constants/default-config.mjs';
|
|
7
7
|
import '../../schemas/config.shcema.mjs';
|
|
8
|
+
import '../../create-cache-id.mjs';
|
|
8
9
|
import '../../data/clear-cache.mjs';
|
|
9
|
-
import '../../data/create-cache-id.mjs';
|
|
10
10
|
import '../../data/get-cache.mjs';
|
|
11
11
|
import '../../flat-template-string.mjs';
|
|
12
12
|
|
|
@@ -8,7 +8,7 @@ import '../../index.mjs';
|
|
|
8
8
|
import '../../config.mjs';
|
|
9
9
|
import '../../constants/default-config.mjs';
|
|
10
10
|
import '../../schemas/config.shcema.mjs';
|
|
11
|
+
import '../../create-cache-id.mjs';
|
|
11
12
|
import '../../data/clear-cache.mjs';
|
|
12
|
-
import '../../data/create-cache-id.mjs';
|
|
13
13
|
import '../../data/get-cache.mjs';
|
|
14
14
|
import '../../flat-template-string.mjs';
|
|
@@ -19,6 +19,7 @@ var defaultConfig = {
|
|
|
19
19
|
},
|
|
20
20
|
keys: {},
|
|
21
21
|
sourceLanguage: "en-US",
|
|
22
|
+
defaultLanguage: "pt-BR",
|
|
22
23
|
languages: ["pt-BR", "fr-CA"],
|
|
23
24
|
llm: {
|
|
24
25
|
choice: "huggingface",
|
|
@@ -46,6 +47,7 @@ var configSchema = z.object({
|
|
|
46
47
|
geminiApiKey: z.string().optional()
|
|
47
48
|
}),
|
|
48
49
|
sourceLanguage: languageSchema,
|
|
50
|
+
defaultLanguage: languageSchema,
|
|
49
51
|
languages: z.array(languageSchema),
|
|
50
52
|
llm: z.object({
|
|
51
53
|
choice: llmOptionsSchema,
|
|
@@ -102,9 +104,9 @@ async function loadUserConfig() {
|
|
|
102
104
|
var awaitedConfig = loadUserConfig();
|
|
103
105
|
function defaultPrePrompt(config, extraInstructions) {
|
|
104
106
|
return `
|
|
105
|
-
You are a translation generator.
|
|
107
|
+
You are a ultimate translation generator server api.
|
|
106
108
|
|
|
107
|
-
Your task is to output **only one valid JSON object**, with **no text, explanations, or formatting
|
|
109
|
+
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
110
|
|
|
109
111
|
Follow these rules strictly:
|
|
110
112
|
|
|
@@ -114,7 +116,7 @@ Follow these rules strictly:
|
|
|
114
116
|
- Apply these creatively but **without altering the meaning or punctuation** of the original text.
|
|
115
117
|
3. Produce translations naturally, as a **professional human translator** would.
|
|
116
118
|
4. Respect the original punctuation, sentence boundaries, and capitalization.
|
|
117
|
-
5. Consider only the following target languages: ${(config?.languages ? config.languages : []).join(", ")}.
|
|
119
|
+
5. Consider only the following target languages: ${(config?.languages ? Array.from(/* @__PURE__ */ new Set(["es-ES", ...config.languages, config.sourceLanguage])) : []).join(", ")}.
|
|
118
120
|
6. The final output must be a **valid JSON** following this exact schema:
|
|
119
121
|
|
|
120
122
|
{
|
|
@@ -4,8 +4,8 @@ import '../../config.mjs';
|
|
|
4
4
|
import '../../constants/default-config.mjs';
|
|
5
5
|
import 'zod';
|
|
6
6
|
import '../../schemas/config.shcema.mjs';
|
|
7
|
+
import '../../create-cache-id.mjs';
|
|
7
8
|
import '../../data/clear-cache.mjs';
|
|
8
|
-
import '../../data/create-cache-id.mjs';
|
|
9
9
|
import '../../data/get-cache.mjs';
|
|
10
10
|
import '../../flat-template-string.mjs';
|
|
11
11
|
|
package/dist/io/ask-llm.d.mts
CHANGED
|
@@ -3,8 +3,8 @@ import '../config.mjs';
|
|
|
3
3
|
import '../constants/default-config.mjs';
|
|
4
4
|
import 'zod';
|
|
5
5
|
import '../schemas/config.shcema.mjs';
|
|
6
|
+
import '../create-cache-id.mjs';
|
|
6
7
|
import '../data/clear-cache.mjs';
|
|
7
|
-
import '../data/create-cache-id.mjs';
|
|
8
8
|
import '../data/get-cache.mjs';
|
|
9
9
|
import '../flat-template-string.mjs';
|
|
10
10
|
|
package/dist/io/ask-llm.mjs
CHANGED
|
@@ -22,6 +22,7 @@ var defaultConfig = {
|
|
|
22
22
|
},
|
|
23
23
|
keys: {},
|
|
24
24
|
sourceLanguage: "en-US",
|
|
25
|
+
defaultLanguage: "pt-BR",
|
|
25
26
|
languages: ["pt-BR", "fr-CA"],
|
|
26
27
|
llm: {
|
|
27
28
|
choice: "huggingface",
|
|
@@ -49,6 +50,7 @@ var configSchema = z.object({
|
|
|
49
50
|
geminiApiKey: z.string().optional()
|
|
50
51
|
}),
|
|
51
52
|
sourceLanguage: languageSchema,
|
|
53
|
+
defaultLanguage: languageSchema,
|
|
52
54
|
languages: z.array(languageSchema),
|
|
53
55
|
llm: z.object({
|
|
54
56
|
choice: llmOptionsSchema,
|
|
@@ -105,9 +107,9 @@ async function loadUserConfig() {
|
|
|
105
107
|
var awaitedConfig = loadUserConfig();
|
|
106
108
|
function defaultPrePrompt(config, extraInstructions) {
|
|
107
109
|
return `
|
|
108
|
-
You are a translation generator.
|
|
110
|
+
You are a ultimate translation generator server api.
|
|
109
111
|
|
|
110
|
-
Your task is to output **only one valid JSON object**, with **no text, explanations, or formatting
|
|
112
|
+
Your task is to translate a given text and output **only one valid JSON object**, with **no text, explanations, or formatting than the JSON**.
|
|
111
113
|
|
|
112
114
|
Follow these rules strictly:
|
|
113
115
|
|
|
@@ -117,7 +119,7 @@ Follow these rules strictly:
|
|
|
117
119
|
- Apply these creatively but **without altering the meaning or punctuation** of the original text.
|
|
118
120
|
3. Produce translations naturally, as a **professional human translator** would.
|
|
119
121
|
4. Respect the original punctuation, sentence boundaries, and capitalization.
|
|
120
|
-
5. Consider only the following target languages: ${(config?.languages ? config.languages : []).join(", ")}.
|
|
122
|
+
5. Consider only the following target languages: ${(config?.languages ? Array.from(/* @__PURE__ */ new Set(["es-ES", ...config.languages, config.sourceLanguage])) : []).join(", ")}.
|
|
121
123
|
6. The final output must be a **valid JSON** following this exact schema:
|
|
122
124
|
|
|
123
125
|
{
|
|
@@ -205,23 +207,40 @@ function parseAnthropic(unparsed) {
|
|
|
205
207
|
const parsed = JSON.parse(unparsed);
|
|
206
208
|
return parsed;
|
|
207
209
|
}
|
|
208
|
-
async function generateResponse(prompt,
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
}
|
|
213
|
-
const generativeAIClient = new GoogleGenerativeAI(config.keys.geminiApiKey);
|
|
214
|
-
const model = generativeAIClient.getGenerativeModel({
|
|
215
|
-
model: config.llm.model,
|
|
216
|
-
generationConfig: {
|
|
217
|
-
temperature
|
|
210
|
+
async function generateResponse(prompt, config) {
|
|
211
|
+
try {
|
|
212
|
+
if (!config.keys.geminiApiKey) {
|
|
213
|
+
throw new Error("Gemini API key is not set in the config");
|
|
218
214
|
}
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
215
|
+
const generativeAIClient = new GoogleGenerativeAI(config.keys.geminiApiKey);
|
|
216
|
+
const model = generativeAIClient.getGenerativeModel({
|
|
217
|
+
model: config.llm.model,
|
|
218
|
+
generationConfig: {
|
|
219
|
+
temperature: config.llm.temperature ?? 0.8
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
if (config.debug) {
|
|
223
|
+
console.log("Gemini model prompt:", JSON.stringify({ prompt }));
|
|
224
|
+
}
|
|
225
|
+
const response = await model.generateContent([prompt]);
|
|
226
|
+
return response;
|
|
227
|
+
} catch (err) {
|
|
228
|
+
const error = err;
|
|
229
|
+
console.log("Error details from Gemini model", JSON.stringify({ error }, null, 2));
|
|
230
|
+
const retryDelay = error?.errorDetails?.find(
|
|
231
|
+
(detail) => detail?.["@type"] === "type.googleapis.com/google.rpc.RetryInfo"
|
|
232
|
+
)?.retryDelay ?? void 0;
|
|
233
|
+
if (error.status === 429 && retryDelay !== void 0) {
|
|
234
|
+
const sleepTime = config.llm.sleep ?? 3;
|
|
235
|
+
console.warn(
|
|
236
|
+
`Rate limit exceeded when generating response from Gemini model, retrying in... ${retryDelay} + ${sleepTime / 1e3}s`
|
|
237
|
+
);
|
|
238
|
+
await new Promise((resolve) => setTimeout(resolve, (parseInt(retryDelay, 10) + sleepTime / 1e3) * 1e3));
|
|
239
|
+
return generateResponse(prompt, config);
|
|
240
|
+
}
|
|
241
|
+
console.error("Error generating response from Gemini model", { error });
|
|
242
|
+
throw error;
|
|
222
243
|
}
|
|
223
|
-
const response = await model.generateContent([prompt]);
|
|
224
|
-
return response;
|
|
225
244
|
}
|
|
226
245
|
async function askGemini(prompt, prePrompt2, postPrompt2) {
|
|
227
246
|
const config = await awaitedConfig;
|
|
@@ -243,13 +262,7 @@ ${prompt}
|
|
|
243
262
|
${postPrompt2}`).trim();
|
|
244
263
|
let response;
|
|
245
264
|
for (let i = 0; i < 3; i++) {
|
|
246
|
-
const maybeResponse = await generateResponse(fullPrompt, config
|
|
247
|
-
console.log(
|
|
248
|
-
"maybeResponse:",
|
|
249
|
-
"\n\n",
|
|
250
|
-
JSON.stringify({ maybeResponse: maybeResponse.response.candidates?.[0]?.content?.parts?.[0]?.text }),
|
|
251
|
-
"\n"
|
|
252
|
-
);
|
|
265
|
+
const maybeResponse = await generateResponse(fullPrompt, config);
|
|
253
266
|
const responseString = String(maybeResponse.response.candidates?.[0]?.content?.parts?.[0]?.text ?? "").replace(/```json/g, "").replace(/```/g, "").trim();
|
|
254
267
|
if (responseString.startsWith("{") && responseString.endsWith("}")) {
|
|
255
268
|
response = maybeResponse;
|
package/dist/io/gemini/ask.mjs
CHANGED
|
@@ -19,6 +19,7 @@ var defaultConfig = {
|
|
|
19
19
|
},
|
|
20
20
|
keys: {},
|
|
21
21
|
sourceLanguage: "en-US",
|
|
22
|
+
defaultLanguage: "pt-BR",
|
|
22
23
|
languages: ["pt-BR", "fr-CA"],
|
|
23
24
|
llm: {
|
|
24
25
|
choice: "huggingface",
|
|
@@ -46,6 +47,7 @@ var configSchema = z.object({
|
|
|
46
47
|
geminiApiKey: z.string().optional()
|
|
47
48
|
}),
|
|
48
49
|
sourceLanguage: languageSchema,
|
|
50
|
+
defaultLanguage: languageSchema,
|
|
49
51
|
languages: z.array(languageSchema),
|
|
50
52
|
llm: z.object({
|
|
51
53
|
choice: llmOptionsSchema,
|
|
@@ -102,9 +104,9 @@ async function loadUserConfig() {
|
|
|
102
104
|
var awaitedConfig = loadUserConfig();
|
|
103
105
|
function defaultPrePrompt(config, extraInstructions) {
|
|
104
106
|
return `
|
|
105
|
-
You are a translation generator.
|
|
107
|
+
You are a ultimate translation generator server api.
|
|
106
108
|
|
|
107
|
-
Your task is to output **only one valid JSON object**, with **no text, explanations, or formatting
|
|
109
|
+
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
110
|
|
|
109
111
|
Follow these rules strictly:
|
|
110
112
|
|
|
@@ -114,7 +116,7 @@ Follow these rules strictly:
|
|
|
114
116
|
- Apply these creatively but **without altering the meaning or punctuation** of the original text.
|
|
115
117
|
3. Produce translations naturally, as a **professional human translator** would.
|
|
116
118
|
4. Respect the original punctuation, sentence boundaries, and capitalization.
|
|
117
|
-
5. Consider only the following target languages: ${(config?.languages ? config.languages : []).join(", ")}.
|
|
119
|
+
5. Consider only the following target languages: ${(config?.languages ? Array.from(/* @__PURE__ */ new Set(["es-ES", ...config.languages, config.sourceLanguage])) : []).join(", ")}.
|
|
118
120
|
6. The final output must be a **valid JSON** following this exact schema:
|
|
119
121
|
|
|
120
122
|
{
|
|
@@ -155,23 +157,40 @@ function postPrompt(config, prepend, replace) {
|
|
|
155
157
|
}
|
|
156
158
|
|
|
157
159
|
// src/io/gemini/ask.ts
|
|
158
|
-
async function generateResponse(prompt,
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
}
|
|
163
|
-
const generativeAIClient = new GoogleGenerativeAI(config.keys.geminiApiKey);
|
|
164
|
-
const model = generativeAIClient.getGenerativeModel({
|
|
165
|
-
model: config.llm.model,
|
|
166
|
-
generationConfig: {
|
|
167
|
-
temperature
|
|
160
|
+
async function generateResponse(prompt, config) {
|
|
161
|
+
try {
|
|
162
|
+
if (!config.keys.geminiApiKey) {
|
|
163
|
+
throw new Error("Gemini API key is not set in the config");
|
|
168
164
|
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
165
|
+
const generativeAIClient = new GoogleGenerativeAI(config.keys.geminiApiKey);
|
|
166
|
+
const model = generativeAIClient.getGenerativeModel({
|
|
167
|
+
model: config.llm.model,
|
|
168
|
+
generationConfig: {
|
|
169
|
+
temperature: config.llm.temperature ?? 0.8
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
if (config.debug) {
|
|
173
|
+
console.log("Gemini model prompt:", JSON.stringify({ prompt }));
|
|
174
|
+
}
|
|
175
|
+
const response = await model.generateContent([prompt]);
|
|
176
|
+
return response;
|
|
177
|
+
} catch (err) {
|
|
178
|
+
const error = err;
|
|
179
|
+
console.log("Error details from Gemini model", JSON.stringify({ error }, null, 2));
|
|
180
|
+
const retryDelay = error?.errorDetails?.find(
|
|
181
|
+
(detail) => detail?.["@type"] === "type.googleapis.com/google.rpc.RetryInfo"
|
|
182
|
+
)?.retryDelay ?? void 0;
|
|
183
|
+
if (error.status === 429 && retryDelay !== void 0) {
|
|
184
|
+
const sleepTime = config.llm.sleep ?? 3;
|
|
185
|
+
console.warn(
|
|
186
|
+
`Rate limit exceeded when generating response from Gemini model, retrying in... ${retryDelay} + ${sleepTime / 1e3}s`
|
|
187
|
+
);
|
|
188
|
+
await new Promise((resolve) => setTimeout(resolve, (parseInt(retryDelay, 10) + sleepTime / 1e3) * 1e3));
|
|
189
|
+
return generateResponse(prompt, config);
|
|
190
|
+
}
|
|
191
|
+
console.error("Error generating response from Gemini model", { error });
|
|
192
|
+
throw error;
|
|
172
193
|
}
|
|
173
|
-
const response = await model.generateContent([prompt]);
|
|
174
|
-
return response;
|
|
175
194
|
}
|
|
176
195
|
async function askGemini(prompt, prePrompt2, postPrompt2) {
|
|
177
196
|
const config = await awaitedConfig;
|
|
@@ -193,13 +212,7 @@ ${prompt}
|
|
|
193
212
|
${postPrompt2}`).trim();
|
|
194
213
|
let response;
|
|
195
214
|
for (let i = 0; i < 3; i++) {
|
|
196
|
-
const maybeResponse = await generateResponse(fullPrompt, config
|
|
197
|
-
console.log(
|
|
198
|
-
"maybeResponse:",
|
|
199
|
-
"\n\n",
|
|
200
|
-
JSON.stringify({ maybeResponse: maybeResponse.response.candidates?.[0]?.content?.parts?.[0]?.text }),
|
|
201
|
-
"\n"
|
|
202
|
-
);
|
|
215
|
+
const maybeResponse = await generateResponse(fullPrompt, config);
|
|
203
216
|
const responseString = String(maybeResponse.response.candidates?.[0]?.content?.parts?.[0]?.text ?? "").replace(/```json/g, "").replace(/```/g, "").trim();
|
|
204
217
|
if (responseString.startsWith("{") && responseString.endsWith("}")) {
|
|
205
218
|
response = maybeResponse;
|
|
@@ -7,8 +7,8 @@ import '../../config.mjs';
|
|
|
7
7
|
import '../../constants/default-config.mjs';
|
|
8
8
|
import 'zod';
|
|
9
9
|
import '../../schemas/config.shcema.mjs';
|
|
10
|
+
import '../../create-cache-id.mjs';
|
|
10
11
|
import '../../data/clear-cache.mjs';
|
|
11
|
-
import '../../data/create-cache-id.mjs';
|
|
12
12
|
import '../../data/get-cache.mjs';
|
|
13
13
|
import '../../flat-template-string.mjs';
|
|
14
14
|
import '../../types/index.mjs';
|
package/dist/io/gemini/index.mjs
CHANGED
|
@@ -19,6 +19,7 @@ var defaultConfig = {
|
|
|
19
19
|
},
|
|
20
20
|
keys: {},
|
|
21
21
|
sourceLanguage: "en-US",
|
|
22
|
+
defaultLanguage: "pt-BR",
|
|
22
23
|
languages: ["pt-BR", "fr-CA"],
|
|
23
24
|
llm: {
|
|
24
25
|
choice: "huggingface",
|
|
@@ -46,6 +47,7 @@ var configSchema = z.object({
|
|
|
46
47
|
geminiApiKey: z.string().optional()
|
|
47
48
|
}),
|
|
48
49
|
sourceLanguage: languageSchema,
|
|
50
|
+
defaultLanguage: languageSchema,
|
|
49
51
|
languages: z.array(languageSchema),
|
|
50
52
|
llm: z.object({
|
|
51
53
|
choice: llmOptionsSchema,
|
|
@@ -102,9 +104,9 @@ async function loadUserConfig() {
|
|
|
102
104
|
var awaitedConfig = loadUserConfig();
|
|
103
105
|
function defaultPrePrompt(config, extraInstructions) {
|
|
104
106
|
return `
|
|
105
|
-
You are a translation generator.
|
|
107
|
+
You are a ultimate translation generator server api.
|
|
106
108
|
|
|
107
|
-
Your task is to output **only one valid JSON object**, with **no text, explanations, or formatting
|
|
109
|
+
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
110
|
|
|
109
111
|
Follow these rules strictly:
|
|
110
112
|
|
|
@@ -114,7 +116,7 @@ Follow these rules strictly:
|
|
|
114
116
|
- Apply these creatively but **without altering the meaning or punctuation** of the original text.
|
|
115
117
|
3. Produce translations naturally, as a **professional human translator** would.
|
|
116
118
|
4. Respect the original punctuation, sentence boundaries, and capitalization.
|
|
117
|
-
5. Consider only the following target languages: ${(config?.languages ? config.languages : []).join(", ")}.
|
|
119
|
+
5. Consider only the following target languages: ${(config?.languages ? Array.from(/* @__PURE__ */ new Set(["es-ES", ...config.languages, config.sourceLanguage])) : []).join(", ")}.
|
|
118
120
|
6. The final output must be a **valid JSON** following this exact schema:
|
|
119
121
|
|
|
120
122
|
{
|
|
@@ -155,23 +157,40 @@ function postPrompt(config, prepend, replace) {
|
|
|
155
157
|
}
|
|
156
158
|
|
|
157
159
|
// src/io/gemini/ask.ts
|
|
158
|
-
async function generateResponse(prompt,
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
}
|
|
163
|
-
const generativeAIClient = new GoogleGenerativeAI(config.keys.geminiApiKey);
|
|
164
|
-
const model = generativeAIClient.getGenerativeModel({
|
|
165
|
-
model: config.llm.model,
|
|
166
|
-
generationConfig: {
|
|
167
|
-
temperature
|
|
160
|
+
async function generateResponse(prompt, config) {
|
|
161
|
+
try {
|
|
162
|
+
if (!config.keys.geminiApiKey) {
|
|
163
|
+
throw new Error("Gemini API key is not set in the config");
|
|
168
164
|
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
165
|
+
const generativeAIClient = new GoogleGenerativeAI(config.keys.geminiApiKey);
|
|
166
|
+
const model = generativeAIClient.getGenerativeModel({
|
|
167
|
+
model: config.llm.model,
|
|
168
|
+
generationConfig: {
|
|
169
|
+
temperature: config.llm.temperature ?? 0.8
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
if (config.debug) {
|
|
173
|
+
console.log("Gemini model prompt:", JSON.stringify({ prompt }));
|
|
174
|
+
}
|
|
175
|
+
const response = await model.generateContent([prompt]);
|
|
176
|
+
return response;
|
|
177
|
+
} catch (err) {
|
|
178
|
+
const error = err;
|
|
179
|
+
console.log("Error details from Gemini model", JSON.stringify({ error }, null, 2));
|
|
180
|
+
const retryDelay = error?.errorDetails?.find(
|
|
181
|
+
(detail) => detail?.["@type"] === "type.googleapis.com/google.rpc.RetryInfo"
|
|
182
|
+
)?.retryDelay ?? void 0;
|
|
183
|
+
if (error.status === 429 && retryDelay !== void 0) {
|
|
184
|
+
const sleepTime = config.llm.sleep ?? 3;
|
|
185
|
+
console.warn(
|
|
186
|
+
`Rate limit exceeded when generating response from Gemini model, retrying in... ${retryDelay} + ${sleepTime / 1e3}s`
|
|
187
|
+
);
|
|
188
|
+
await new Promise((resolve) => setTimeout(resolve, (parseInt(retryDelay, 10) + sleepTime / 1e3) * 1e3));
|
|
189
|
+
return generateResponse(prompt, config);
|
|
190
|
+
}
|
|
191
|
+
console.error("Error generating response from Gemini model", { error });
|
|
192
|
+
throw error;
|
|
172
193
|
}
|
|
173
|
-
const response = await model.generateContent([prompt]);
|
|
174
|
-
return response;
|
|
175
194
|
}
|
|
176
195
|
async function askGemini(prompt, prePrompt2, postPrompt2) {
|
|
177
196
|
const config = await awaitedConfig;
|
|
@@ -193,13 +212,7 @@ ${prompt}
|
|
|
193
212
|
${postPrompt2}`).trim();
|
|
194
213
|
let response;
|
|
195
214
|
for (let i = 0; i < 3; i++) {
|
|
196
|
-
const maybeResponse = await generateResponse(fullPrompt, config
|
|
197
|
-
console.log(
|
|
198
|
-
"maybeResponse:",
|
|
199
|
-
"\n\n",
|
|
200
|
-
JSON.stringify({ maybeResponse: maybeResponse.response.candidates?.[0]?.content?.parts?.[0]?.text }),
|
|
201
|
-
"\n"
|
|
202
|
-
);
|
|
215
|
+
const maybeResponse = await generateResponse(fullPrompt, config);
|
|
203
216
|
const responseString = String(maybeResponse.response.candidates?.[0]?.content?.parts?.[0]?.text ?? "").replace(/```json/g, "").replace(/```/g, "").trim();
|
|
204
217
|
if (responseString.startsWith("{") && responseString.endsWith("}")) {
|
|
205
218
|
response = maybeResponse;
|
|
@@ -4,8 +4,8 @@ import '../../config.mjs';
|
|
|
4
4
|
import '../../constants/default-config.mjs';
|
|
5
5
|
import 'zod';
|
|
6
6
|
import '../../schemas/config.shcema.mjs';
|
|
7
|
+
import '../../create-cache-id.mjs';
|
|
7
8
|
import '../../data/clear-cache.mjs';
|
|
8
|
-
import '../../data/create-cache-id.mjs';
|
|
9
9
|
import '../../data/get-cache.mjs';
|
|
10
10
|
import '../../flat-template-string.mjs';
|
|
11
11
|
|
|
@@ -6,8 +6,8 @@ import '../../index.mjs';
|
|
|
6
6
|
import '../../config.mjs';
|
|
7
7
|
import '../../constants/default-config.mjs';
|
|
8
8
|
import '../../schemas/config.shcema.mjs';
|
|
9
|
+
import '../../create-cache-id.mjs';
|
|
9
10
|
import '../../data/clear-cache.mjs';
|
|
10
|
-
import '../../data/create-cache-id.mjs';
|
|
11
11
|
import '../../data/get-cache.mjs';
|
|
12
12
|
import '../../flat-template-string.mjs';
|
|
13
13
|
import './parse.mjs';
|
|
@@ -20,6 +20,7 @@ var defaultConfig = {
|
|
|
20
20
|
},
|
|
21
21
|
keys: {},
|
|
22
22
|
sourceLanguage: "en-US",
|
|
23
|
+
defaultLanguage: "pt-BR",
|
|
23
24
|
languages: ["pt-BR", "fr-CA"],
|
|
24
25
|
llm: {
|
|
25
26
|
choice: "huggingface",
|
|
@@ -47,6 +48,7 @@ var configSchema = z.object({
|
|
|
47
48
|
geminiApiKey: z.string().optional()
|
|
48
49
|
}),
|
|
49
50
|
sourceLanguage: languageSchema,
|
|
51
|
+
defaultLanguage: languageSchema,
|
|
50
52
|
languages: z.array(languageSchema),
|
|
51
53
|
llm: z.object({
|
|
52
54
|
choice: llmOptionsSchema,
|
|
@@ -103,9 +105,9 @@ async function loadUserConfig() {
|
|
|
103
105
|
var awaitedConfig = loadUserConfig();
|
|
104
106
|
function defaultPrePrompt(config, extraInstructions) {
|
|
105
107
|
return `
|
|
106
|
-
You are a translation generator.
|
|
108
|
+
You are a ultimate translation generator server api.
|
|
107
109
|
|
|
108
|
-
Your task is to output **only one valid JSON object**, with **no text, explanations, or formatting
|
|
110
|
+
Your task is to translate a given text and output **only one valid JSON object**, with **no text, explanations, or formatting than the JSON**.
|
|
109
111
|
|
|
110
112
|
Follow these rules strictly:
|
|
111
113
|
|
|
@@ -115,7 +117,7 @@ Follow these rules strictly:
|
|
|
115
117
|
- Apply these creatively but **without altering the meaning or punctuation** of the original text.
|
|
116
118
|
3. Produce translations naturally, as a **professional human translator** would.
|
|
117
119
|
4. Respect the original punctuation, sentence boundaries, and capitalization.
|
|
118
|
-
5. Consider only the following target languages: ${(config?.languages ? config.languages : []).join(", ")}.
|
|
120
|
+
5. Consider only the following target languages: ${(config?.languages ? Array.from(/* @__PURE__ */ new Set(["es-ES", ...config.languages, config.sourceLanguage])) : []).join(", ")}.
|
|
119
121
|
6. The final output must be a **valid JSON** following this exact schema:
|
|
120
122
|
|
|
121
123
|
{
|
|
@@ -6,8 +6,8 @@ import '../../index.mjs';
|
|
|
6
6
|
import '../../config.mjs';
|
|
7
7
|
import '../../constants/default-config.mjs';
|
|
8
8
|
import '../../schemas/config.shcema.mjs';
|
|
9
|
+
import '../../create-cache-id.mjs';
|
|
9
10
|
import '../../data/clear-cache.mjs';
|
|
10
|
-
import '../../data/create-cache-id.mjs';
|
|
11
11
|
import '../../data/get-cache.mjs';
|
|
12
12
|
import '../../flat-template-string.mjs';
|
|
13
13
|
|
|
@@ -7,7 +7,7 @@ import '../../index.mjs';
|
|
|
7
7
|
import '../../config.mjs';
|
|
8
8
|
import '../../constants/default-config.mjs';
|
|
9
9
|
import '../../schemas/config.shcema.mjs';
|
|
10
|
+
import '../../create-cache-id.mjs';
|
|
10
11
|
import '../../data/clear-cache.mjs';
|
|
11
|
-
import '../../data/create-cache-id.mjs';
|
|
12
12
|
import '../../data/get-cache.mjs';
|
|
13
13
|
import '../../flat-template-string.mjs';
|
|
@@ -20,6 +20,7 @@ var defaultConfig = {
|
|
|
20
20
|
},
|
|
21
21
|
keys: {},
|
|
22
22
|
sourceLanguage: "en-US",
|
|
23
|
+
defaultLanguage: "pt-BR",
|
|
23
24
|
languages: ["pt-BR", "fr-CA"],
|
|
24
25
|
llm: {
|
|
25
26
|
choice: "huggingface",
|
|
@@ -47,6 +48,7 @@ var configSchema = z.object({
|
|
|
47
48
|
geminiApiKey: z.string().optional()
|
|
48
49
|
}),
|
|
49
50
|
sourceLanguage: languageSchema,
|
|
51
|
+
defaultLanguage: languageSchema,
|
|
50
52
|
languages: z.array(languageSchema),
|
|
51
53
|
llm: z.object({
|
|
52
54
|
choice: llmOptionsSchema,
|
|
@@ -103,9 +105,9 @@ async function loadUserConfig() {
|
|
|
103
105
|
var awaitedConfig = loadUserConfig();
|
|
104
106
|
function defaultPrePrompt(config, extraInstructions) {
|
|
105
107
|
return `
|
|
106
|
-
You are a translation generator.
|
|
108
|
+
You are a ultimate translation generator server api.
|
|
107
109
|
|
|
108
|
-
Your task is to output **only one valid JSON object**, with **no text, explanations, or formatting
|
|
110
|
+
Your task is to translate a given text and output **only one valid JSON object**, with **no text, explanations, or formatting than the JSON**.
|
|
109
111
|
|
|
110
112
|
Follow these rules strictly:
|
|
111
113
|
|
|
@@ -115,7 +117,7 @@ Follow these rules strictly:
|
|
|
115
117
|
- Apply these creatively but **without altering the meaning or punctuation** of the original text.
|
|
116
118
|
3. Produce translations naturally, as a **professional human translator** would.
|
|
117
119
|
4. Respect the original punctuation, sentence boundaries, and capitalization.
|
|
118
|
-
5. Consider only the following target languages: ${(config?.languages ? config.languages : []).join(", ")}.
|
|
120
|
+
5. Consider only the following target languages: ${(config?.languages ? Array.from(/* @__PURE__ */ new Set(["es-ES", ...config.languages, config.sourceLanguage])) : []).join(", ")}.
|
|
119
121
|
6. The final output must be a **valid JSON** following this exact schema:
|
|
120
122
|
|
|
121
123
|
{
|
|
@@ -4,8 +4,8 @@ import '../../config.mjs';
|
|
|
4
4
|
import '../../constants/default-config.mjs';
|
|
5
5
|
import 'zod';
|
|
6
6
|
import '../../schemas/config.shcema.mjs';
|
|
7
|
+
import '../../create-cache-id.mjs';
|
|
7
8
|
import '../../data/clear-cache.mjs';
|
|
8
|
-
import '../../data/create-cache-id.mjs';
|
|
9
9
|
import '../../data/get-cache.mjs';
|
|
10
10
|
import '../../flat-template-string.mjs';
|
|
11
11
|
|