@gringow/gringow 0.0.8 → 0.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/config.d.mts +5 -5
- package/dist/config.mjs +19 -11
- package/dist/config.test.mjs +62 -53
- package/dist/data/check-cache.mjs +19 -11
- package/dist/data/clear-cache.mjs +19 -11
- package/dist/data/create-cache-dir.mjs +19 -11
- package/dist/data/delete-cache-item.mjs +19 -11
- package/dist/data/get-cache.mjs +19 -11
- package/dist/data/get-or-create-cache.mjs +19 -11
- package/dist/data/save-cache.mjs +19 -11
- package/dist/data/write-cache-to-disk.mjs +19 -11
- package/dist/flat-template-string.test.mjs +21 -6
- package/dist/gringow.d.mts +1 -1
- package/dist/gringow.mjs +81 -40
- package/dist/index.d.mts +5 -1
- package/dist/index.mjs +81 -40
- package/dist/io/anthropic/ask.d.mts +2 -2
- package/dist/io/anthropic/ask.mjs +28 -19
- package/dist/io/anthropic/index.mjs +28 -19
- package/dist/io/ask-llm.d.mts +1 -1
- package/dist/io/ask-llm.mjs +44 -35
- package/dist/io/gemini/ask.d.mts +2 -2
- package/dist/io/gemini/ask.mjs +30 -22
- package/dist/io/gemini/index.mjs +30 -22
- package/dist/io/huggingface/ask.mjs +20 -12
- package/dist/io/huggingface/index.mjs +20 -12
- package/dist/io/huggingface/{languagesMap.mjs → languages-map.mjs} +1 -1
- package/dist/logger.test.mjs +21 -6
- package/dist/schemas/config.shcema.d.mts +19 -19
- package/dist/schemas/config.shcema.mjs +3 -3
- package/dist/types/index.d.mts +1 -1
- package/package.json +7 -4
- /package/dist/io/huggingface/{languagesMap.d.mts → languages-map.d.mts} +0 -0
package/dist/index.mjs
CHANGED
|
@@ -58,12 +58,12 @@ var configSchema = z.object({
|
|
|
58
58
|
model: z.string().nullable(),
|
|
59
59
|
temperature: z.number().optional(),
|
|
60
60
|
sleep: z.number().optional(),
|
|
61
|
-
prePrompt: z.string().optional(),
|
|
62
|
-
postPrompt: z.string().optional(),
|
|
61
|
+
prePrompt: z.string().max(128).optional(),
|
|
62
|
+
postPrompt: z.string().max(128).optional(),
|
|
63
63
|
prePromptReplace: z.string().optional(),
|
|
64
64
|
postPromptReplace: z.string().optional(),
|
|
65
65
|
prePromptAppend: z.string().optional(),
|
|
66
|
-
|
|
66
|
+
postPromptPrepend: z.string().optional()
|
|
67
67
|
}),
|
|
68
68
|
globby: z.string().regex(/^\.\/.*/).optional()
|
|
69
69
|
}).refine(
|
|
@@ -95,8 +95,16 @@ async function loadUserConfig() {
|
|
|
95
95
|
try {
|
|
96
96
|
const mergedConfig = merge({}, defaultConfig, result?.config);
|
|
97
97
|
const parsedConfig = await configSchema.parseAsync(mergedConfig);
|
|
98
|
-
parsedConfig.llm.prePrompt = prePrompt(
|
|
99
|
-
|
|
98
|
+
parsedConfig.llm.prePrompt = prePrompt(
|
|
99
|
+
parsedConfig,
|
|
100
|
+
parsedConfig.llm.prePromptAppend ?? "",
|
|
101
|
+
parsedConfig.llm.prePromptReplace ?? ""
|
|
102
|
+
);
|
|
103
|
+
parsedConfig.llm.postPrompt = postPrompt(
|
|
104
|
+
parsedConfig,
|
|
105
|
+
parsedConfig.llm.postPromptPrepend ?? "",
|
|
106
|
+
parsedConfig.llm.postPromptReplace ?? ""
|
|
107
|
+
);
|
|
100
108
|
return parsedConfig;
|
|
101
109
|
} catch (error) {
|
|
102
110
|
console.warn(`${error?.message}, using default configuration`);
|
|
@@ -145,16 +153,16 @@ function defaultPostPrompt(_config) {
|
|
|
145
153
|
[/POST-MESSAGE]
|
|
146
154
|
`;
|
|
147
155
|
}
|
|
148
|
-
function prePrompt(config) {
|
|
156
|
+
function prePrompt(config, append, replace) {
|
|
149
157
|
return [
|
|
150
|
-
|
|
151
|
-
|
|
158
|
+
...append ? ["[SPECIFIC_INPUTS]", String(append), "[/SPECIFIC_INPUTS]"] : [],
|
|
159
|
+
replace ? String(replace) : defaultPrePrompt(config)
|
|
152
160
|
].join(" ").trim();
|
|
153
161
|
}
|
|
154
|
-
function postPrompt(config) {
|
|
162
|
+
function postPrompt(config, prepend, replace) {
|
|
155
163
|
return [
|
|
156
|
-
|
|
157
|
-
|
|
164
|
+
...prepend ? ["[SPECIFIC_INPUTS]", String(prepend), "[/SPECIFIC_INPUTS]"] : [],
|
|
165
|
+
replace ? String(replace) : defaultPostPrompt()
|
|
158
166
|
].join(" ").trim();
|
|
159
167
|
}
|
|
160
168
|
var optsArg = (opts) => {
|
|
@@ -502,7 +510,7 @@ function flatTemplateString(givenStrings, givenValues) {
|
|
|
502
510
|
}, "");
|
|
503
511
|
return flatted;
|
|
504
512
|
}
|
|
505
|
-
async function askAnthropic(
|
|
513
|
+
async function askAnthropic(prompt, prePrompt2, postPrompt2) {
|
|
506
514
|
const config = await awaitedConfig;
|
|
507
515
|
if (Number(config.llm.sleep)) {
|
|
508
516
|
await new Promise((resolve2) => setTimeout(resolve2, config.llm.sleep ?? 1));
|
|
@@ -514,19 +522,20 @@ async function askAnthropic(message) {
|
|
|
514
522
|
apiKey: config.keys.anthropicApiKey,
|
|
515
523
|
dangerouslyAllowBrowser: true
|
|
516
524
|
});
|
|
525
|
+
const fullPrompt = String(`${prePrompt2}
|
|
526
|
+
|
|
527
|
+
|
|
528
|
+
${prompt}
|
|
529
|
+
|
|
530
|
+
|
|
531
|
+
${postPrompt2}`).trim();
|
|
517
532
|
const response = await anthropic.messages.create({
|
|
518
533
|
model: config.llm.model,
|
|
519
534
|
max_tokens: 1024,
|
|
520
535
|
messages: [
|
|
521
536
|
{
|
|
522
537
|
role: "user",
|
|
523
|
-
content:
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
${message}
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
${config.llm.postPrompt}`
|
|
538
|
+
content: fullPrompt
|
|
530
539
|
}
|
|
531
540
|
]
|
|
532
541
|
});
|
|
@@ -550,7 +559,7 @@ function parseAnthropic(unparsed) {
|
|
|
550
559
|
const parsed = JSON.parse(unparsed);
|
|
551
560
|
return parsed;
|
|
552
561
|
}
|
|
553
|
-
async function generateResponse(
|
|
562
|
+
async function generateResponse(prompt, temperature = 1) {
|
|
554
563
|
const config = await awaitedConfig;
|
|
555
564
|
if (!config.keys.geminiApiKey) {
|
|
556
565
|
throw new Error("Gemini API key is not set in the config");
|
|
@@ -562,20 +571,13 @@ async function generateResponse(message, temperature = 1) {
|
|
|
562
571
|
temperature
|
|
563
572
|
}
|
|
564
573
|
});
|
|
565
|
-
const prompt = String(`${config.llm.prePrompt}
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
${message}
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
${config.llm.postPrompt}`).trim();
|
|
572
574
|
if (config.debug) {
|
|
573
575
|
console.log("Gemini model prompt:", JSON.stringify({ prompt }));
|
|
574
576
|
}
|
|
575
577
|
const response = await model.generateContent([prompt]);
|
|
576
578
|
return response;
|
|
577
579
|
}
|
|
578
|
-
async function askGemini(
|
|
580
|
+
async function askGemini(prompt, prePrompt2, postPrompt2) {
|
|
579
581
|
const config = await awaitedConfig;
|
|
580
582
|
if (Number(config.llm.sleep)) {
|
|
581
583
|
await new Promise((resolve2) => setTimeout(resolve2, config.llm.sleep ?? 1));
|
|
@@ -584,11 +586,18 @@ async function askGemini(message) {
|
|
|
584
586
|
throw new Error("Gemini API key is not set in the config");
|
|
585
587
|
}
|
|
586
588
|
if (config.debug) {
|
|
587
|
-
console.log("\nAsking Gemini model:", JSON.stringify({ model: config.llm.model, message }));
|
|
589
|
+
console.log("\nAsking Gemini model:", JSON.stringify({ model: config.llm.model, message: prompt }));
|
|
588
590
|
}
|
|
591
|
+
const fullPrompt = String(`${prePrompt2}
|
|
592
|
+
|
|
593
|
+
|
|
594
|
+
${prompt}
|
|
595
|
+
|
|
596
|
+
|
|
597
|
+
${postPrompt2}`).trim();
|
|
589
598
|
let response;
|
|
590
599
|
for (let i = 0; i < 3; i++) {
|
|
591
|
-
const maybeResponse = await generateResponse(
|
|
600
|
+
const maybeResponse = await generateResponse(fullPrompt, config.llm.temperature);
|
|
592
601
|
if (!String(maybeResponse.response.candidates?.[0]?.content?.parts?.[0]?.text ?? "").includes("```json")) {
|
|
593
602
|
response = maybeResponse;
|
|
594
603
|
break;
|
|
@@ -622,7 +631,7 @@ function parseGemini(unparsed) {
|
|
|
622
631
|
return parsed;
|
|
623
632
|
}
|
|
624
633
|
|
|
625
|
-
// src/io/huggingface/
|
|
634
|
+
// src/io/huggingface/languages-map.ts
|
|
626
635
|
var languagesMap = {
|
|
627
636
|
"en-US": "eng_Latn",
|
|
628
637
|
"en-GB": "eng_Latn",
|
|
@@ -908,23 +917,49 @@ var llmChoices = {
|
|
|
908
917
|
parse: parseHuggingface
|
|
909
918
|
}
|
|
910
919
|
};
|
|
911
|
-
async function processLLMChoice(llmChoice, prompt) {
|
|
912
|
-
const response = await llmChoice.ask(prompt);
|
|
920
|
+
async function processLLMChoice(llmChoice, prompt, prePrompt2, postPrompt2) {
|
|
921
|
+
const response = await llmChoice.ask(prompt, prePrompt2, postPrompt2);
|
|
913
922
|
const unparsed = llmChoice.commitResponse(response);
|
|
914
923
|
const parsed = llmChoice.parse(unparsed);
|
|
915
924
|
return parsed;
|
|
916
925
|
}
|
|
917
|
-
async function askLLM(prompt) {
|
|
926
|
+
async function askLLM(prompt, prePrompt2, postPrompt2) {
|
|
918
927
|
const config = await awaitedConfig;
|
|
919
928
|
const llmChoiceName = config.llm.choice;
|
|
920
929
|
const llmChoice = llmChoices[llmChoiceName];
|
|
921
930
|
if (!llmChoice) {
|
|
922
931
|
throw new Error("LLMChoice not found");
|
|
923
932
|
}
|
|
924
|
-
return processLLMChoice(llmChoice, prompt);
|
|
933
|
+
return processLLMChoice(llmChoice, prompt, prePrompt2, postPrompt2);
|
|
925
934
|
}
|
|
926
935
|
|
|
927
936
|
// src/gringow.ts
|
|
937
|
+
function getPrependAndAppend(config, cached) {
|
|
938
|
+
const prepend = prePrompt(
|
|
939
|
+
config,
|
|
940
|
+
cached?.prepend ?? config.llm.prePromptAppend ?? "",
|
|
941
|
+
config.llm.prePromptReplace ?? ""
|
|
942
|
+
);
|
|
943
|
+
const append = postPrompt(config, cached?.append ?? config.llm.postPrompt ?? "", config.llm.postPromptReplace ?? "");
|
|
944
|
+
return { prepend, append };
|
|
945
|
+
}
|
|
946
|
+
async function rebuild(cacheId) {
|
|
947
|
+
const config = await awaitedConfig;
|
|
948
|
+
const cached = await checkCache(cacheId);
|
|
949
|
+
if (!cached) throw new Error("Cache item not found");
|
|
950
|
+
const { prepend, append } = getPrependAndAppend(config, cached);
|
|
951
|
+
const translation = await askLLM(cached.original, prepend, append);
|
|
952
|
+
const output = {
|
|
953
|
+
...translation,
|
|
954
|
+
prepend: cached?.prepend ?? void 0,
|
|
955
|
+
append: cached?.append ?? void 0
|
|
956
|
+
};
|
|
957
|
+
console.log("output:", output);
|
|
958
|
+
if (config.cache.write) {
|
|
959
|
+
await writeCacheToDisk(cacheId, output);
|
|
960
|
+
}
|
|
961
|
+
return { cacheId, translation: output };
|
|
962
|
+
}
|
|
928
963
|
async function Gringow(strings, ...values) {
|
|
929
964
|
const config = await awaitedConfig;
|
|
930
965
|
let flatten;
|
|
@@ -935,14 +970,20 @@ async function Gringow(strings, ...values) {
|
|
|
935
970
|
}
|
|
936
971
|
const cacheId = createCacheId(flatten);
|
|
937
972
|
const cached = await checkCache(cacheId);
|
|
938
|
-
if (cached !== void 0 && cached.original !== "undefined") {
|
|
973
|
+
if (cached !== void 0 && cached.original !== "undefined" && !cached.rebuild) {
|
|
939
974
|
return { cacheId, translation: cached };
|
|
940
975
|
}
|
|
941
|
-
const
|
|
976
|
+
const { prepend, append } = getPrependAndAppend(config, cached);
|
|
977
|
+
const translation = await askLLM(flatten, prepend, append);
|
|
978
|
+
const output = {
|
|
979
|
+
...translation,
|
|
980
|
+
prepend: cached?.prepend ?? void 0,
|
|
981
|
+
append: cached?.append ?? void 0
|
|
982
|
+
};
|
|
942
983
|
if (config.cache.write) {
|
|
943
|
-
await writeCacheToDisk(cacheId,
|
|
984
|
+
await writeCacheToDisk(cacheId, output);
|
|
944
985
|
}
|
|
945
|
-
return { cacheId, translation };
|
|
986
|
+
return { cacheId, translation: output };
|
|
946
987
|
}
|
|
947
988
|
|
|
948
|
-
export { awaitedConfig, checkCache, clearCache, createCacheId, Gringow as default, deleteCacheItem, flatTemplateString, getCache, getConfigSource, getOrCreateCache, isLocalConfigFound, writeCacheToDisk };
|
|
989
|
+
export { awaitedConfig, checkCache, clearCache, createCacheId, Gringow as default, deleteCacheItem, flatTemplateString, getCache, getConfigSource, getOrCreateCache, isLocalConfigFound, rebuild, writeCacheToDisk };
|
|
@@ -3,9 +3,9 @@ import Anthropic from '@anthropic-ai/sdk';
|
|
|
3
3
|
/**
|
|
4
4
|
* Asks the Anthropic model for a response.
|
|
5
5
|
*
|
|
6
|
-
* @param
|
|
6
|
+
* @param prompt - The message to ask the model.
|
|
7
7
|
* @returns The response from the model.
|
|
8
8
|
*/
|
|
9
|
-
declare function askAnthropic(
|
|
9
|
+
declare function askAnthropic(prompt: string, prePrompt: string, postPrompt: string): Promise<Anthropic.Messages.Message>;
|
|
10
10
|
|
|
11
11
|
export { askAnthropic };
|
|
@@ -52,12 +52,12 @@ var configSchema = z.object({
|
|
|
52
52
|
model: z.string().nullable(),
|
|
53
53
|
temperature: z.number().optional(),
|
|
54
54
|
sleep: z.number().optional(),
|
|
55
|
-
prePrompt: z.string().optional(),
|
|
56
|
-
postPrompt: z.string().optional(),
|
|
55
|
+
prePrompt: z.string().max(128).optional(),
|
|
56
|
+
postPrompt: z.string().max(128).optional(),
|
|
57
57
|
prePromptReplace: z.string().optional(),
|
|
58
58
|
postPromptReplace: z.string().optional(),
|
|
59
59
|
prePromptAppend: z.string().optional(),
|
|
60
|
-
|
|
60
|
+
postPromptPrepend: z.string().optional()
|
|
61
61
|
}),
|
|
62
62
|
globby: z.string().regex(/^\.\/.*/).optional()
|
|
63
63
|
}).refine(
|
|
@@ -83,8 +83,16 @@ async function loadUserConfig() {
|
|
|
83
83
|
try {
|
|
84
84
|
const mergedConfig = merge({}, defaultConfig, result?.config);
|
|
85
85
|
const parsedConfig = await configSchema.parseAsync(mergedConfig);
|
|
86
|
-
parsedConfig.llm.prePrompt = prePrompt(
|
|
87
|
-
|
|
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
|
+
);
|
|
88
96
|
return parsedConfig;
|
|
89
97
|
} catch (error) {
|
|
90
98
|
console.warn(`${error?.message}, using default configuration`);
|
|
@@ -133,21 +141,21 @@ function defaultPostPrompt(_config) {
|
|
|
133
141
|
[/POST-MESSAGE]
|
|
134
142
|
`;
|
|
135
143
|
}
|
|
136
|
-
function prePrompt(config) {
|
|
144
|
+
function prePrompt(config, append, replace) {
|
|
137
145
|
return [
|
|
138
|
-
|
|
139
|
-
|
|
146
|
+
...append ? ["[SPECIFIC_INPUTS]", String(append), "[/SPECIFIC_INPUTS]"] : [],
|
|
147
|
+
replace ? String(replace) : defaultPrePrompt(config)
|
|
140
148
|
].join(" ").trim();
|
|
141
149
|
}
|
|
142
|
-
function postPrompt(config) {
|
|
150
|
+
function postPrompt(config, prepend, replace) {
|
|
143
151
|
return [
|
|
144
|
-
|
|
145
|
-
|
|
152
|
+
...prepend ? ["[SPECIFIC_INPUTS]", String(prepend), "[/SPECIFIC_INPUTS]"] : [],
|
|
153
|
+
replace ? String(replace) : defaultPostPrompt()
|
|
146
154
|
].join(" ").trim();
|
|
147
155
|
}
|
|
148
156
|
|
|
149
157
|
// src/io/anthropic/ask.ts
|
|
150
|
-
async function askAnthropic(
|
|
158
|
+
async function askAnthropic(prompt, prePrompt2, postPrompt2) {
|
|
151
159
|
const config = await awaitedConfig;
|
|
152
160
|
if (Number(config.llm.sleep)) {
|
|
153
161
|
await new Promise((resolve) => setTimeout(resolve, config.llm.sleep ?? 1));
|
|
@@ -159,19 +167,20 @@ async function askAnthropic(message) {
|
|
|
159
167
|
apiKey: config.keys.anthropicApiKey,
|
|
160
168
|
dangerouslyAllowBrowser: true
|
|
161
169
|
});
|
|
170
|
+
const fullPrompt = String(`${prePrompt2}
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
${prompt}
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
${postPrompt2}`).trim();
|
|
162
177
|
const response = await anthropic.messages.create({
|
|
163
178
|
model: config.llm.model,
|
|
164
179
|
max_tokens: 1024,
|
|
165
180
|
messages: [
|
|
166
181
|
{
|
|
167
182
|
role: "user",
|
|
168
|
-
content:
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
${message}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
${config.llm.postPrompt}`
|
|
183
|
+
content: fullPrompt
|
|
175
184
|
}
|
|
176
185
|
]
|
|
177
186
|
});
|
|
@@ -52,12 +52,12 @@ var configSchema = z.object({
|
|
|
52
52
|
model: z.string().nullable(),
|
|
53
53
|
temperature: z.number().optional(),
|
|
54
54
|
sleep: z.number().optional(),
|
|
55
|
-
prePrompt: z.string().optional(),
|
|
56
|
-
postPrompt: z.string().optional(),
|
|
55
|
+
prePrompt: z.string().max(128).optional(),
|
|
56
|
+
postPrompt: z.string().max(128).optional(),
|
|
57
57
|
prePromptReplace: z.string().optional(),
|
|
58
58
|
postPromptReplace: z.string().optional(),
|
|
59
59
|
prePromptAppend: z.string().optional(),
|
|
60
|
-
|
|
60
|
+
postPromptPrepend: z.string().optional()
|
|
61
61
|
}),
|
|
62
62
|
globby: z.string().regex(/^\.\/.*/).optional()
|
|
63
63
|
}).refine(
|
|
@@ -83,8 +83,16 @@ async function loadUserConfig() {
|
|
|
83
83
|
try {
|
|
84
84
|
const mergedConfig = merge({}, defaultConfig, result?.config);
|
|
85
85
|
const parsedConfig = await configSchema.parseAsync(mergedConfig);
|
|
86
|
-
parsedConfig.llm.prePrompt = prePrompt(
|
|
87
|
-
|
|
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
|
+
);
|
|
88
96
|
return parsedConfig;
|
|
89
97
|
} catch (error) {
|
|
90
98
|
console.warn(`${error?.message}, using default configuration`);
|
|
@@ -133,21 +141,21 @@ function defaultPostPrompt(_config) {
|
|
|
133
141
|
[/POST-MESSAGE]
|
|
134
142
|
`;
|
|
135
143
|
}
|
|
136
|
-
function prePrompt(config) {
|
|
144
|
+
function prePrompt(config, append, replace) {
|
|
137
145
|
return [
|
|
138
|
-
|
|
139
|
-
|
|
146
|
+
...append ? ["[SPECIFIC_INPUTS]", String(append), "[/SPECIFIC_INPUTS]"] : [],
|
|
147
|
+
replace ? String(replace) : defaultPrePrompt(config)
|
|
140
148
|
].join(" ").trim();
|
|
141
149
|
}
|
|
142
|
-
function postPrompt(config) {
|
|
150
|
+
function postPrompt(config, prepend, replace) {
|
|
143
151
|
return [
|
|
144
|
-
|
|
145
|
-
|
|
152
|
+
...prepend ? ["[SPECIFIC_INPUTS]", String(prepend), "[/SPECIFIC_INPUTS]"] : [],
|
|
153
|
+
replace ? String(replace) : defaultPostPrompt()
|
|
146
154
|
].join(" ").trim();
|
|
147
155
|
}
|
|
148
156
|
|
|
149
157
|
// src/io/anthropic/ask.ts
|
|
150
|
-
async function askAnthropic(
|
|
158
|
+
async function askAnthropic(prompt, prePrompt2, postPrompt2) {
|
|
151
159
|
const config = await awaitedConfig;
|
|
152
160
|
if (Number(config.llm.sleep)) {
|
|
153
161
|
await new Promise((resolve) => setTimeout(resolve, config.llm.sleep ?? 1));
|
|
@@ -159,19 +167,20 @@ async function askAnthropic(message) {
|
|
|
159
167
|
apiKey: config.keys.anthropicApiKey,
|
|
160
168
|
dangerouslyAllowBrowser: true
|
|
161
169
|
});
|
|
170
|
+
const fullPrompt = String(`${prePrompt2}
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
${prompt}
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
${postPrompt2}`).trim();
|
|
162
177
|
const response = await anthropic.messages.create({
|
|
163
178
|
model: config.llm.model,
|
|
164
179
|
max_tokens: 1024,
|
|
165
180
|
messages: [
|
|
166
181
|
{
|
|
167
182
|
role: "user",
|
|
168
|
-
content:
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
${message}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
${config.llm.postPrompt}`
|
|
183
|
+
content: fullPrompt
|
|
175
184
|
}
|
|
176
185
|
]
|
|
177
186
|
});
|
package/dist/io/ask-llm.d.mts
CHANGED
|
@@ -14,6 +14,6 @@ import '../flat-template-string.mjs';
|
|
|
14
14
|
* @param prompt - The prompt to ask the model.
|
|
15
15
|
* @returns The parsed response.
|
|
16
16
|
*/
|
|
17
|
-
declare function askLLM(prompt: string): Promise<GringowCacheItem>;
|
|
17
|
+
declare function askLLM(prompt: string, prePrompt: string, postPrompt: string): Promise<GringowCacheItem>;
|
|
18
18
|
|
|
19
19
|
export { askLLM };
|
package/dist/io/ask-llm.mjs
CHANGED
|
@@ -55,12 +55,12 @@ var configSchema = z.object({
|
|
|
55
55
|
model: z.string().nullable(),
|
|
56
56
|
temperature: z.number().optional(),
|
|
57
57
|
sleep: z.number().optional(),
|
|
58
|
-
prePrompt: z.string().optional(),
|
|
59
|
-
postPrompt: z.string().optional(),
|
|
58
|
+
prePrompt: z.string().max(128).optional(),
|
|
59
|
+
postPrompt: z.string().max(128).optional(),
|
|
60
60
|
prePromptReplace: z.string().optional(),
|
|
61
61
|
postPromptReplace: z.string().optional(),
|
|
62
62
|
prePromptAppend: z.string().optional(),
|
|
63
|
-
|
|
63
|
+
postPromptPrepend: z.string().optional()
|
|
64
64
|
}),
|
|
65
65
|
globby: z.string().regex(/^\.\/.*/).optional()
|
|
66
66
|
}).refine(
|
|
@@ -86,8 +86,16 @@ async function loadUserConfig() {
|
|
|
86
86
|
try {
|
|
87
87
|
const mergedConfig = merge({}, defaultConfig, result?.config);
|
|
88
88
|
const parsedConfig = await configSchema.parseAsync(mergedConfig);
|
|
89
|
-
parsedConfig.llm.prePrompt = prePrompt(
|
|
90
|
-
|
|
89
|
+
parsedConfig.llm.prePrompt = prePrompt(
|
|
90
|
+
parsedConfig,
|
|
91
|
+
parsedConfig.llm.prePromptAppend ?? "",
|
|
92
|
+
parsedConfig.llm.prePromptReplace ?? ""
|
|
93
|
+
);
|
|
94
|
+
parsedConfig.llm.postPrompt = postPrompt(
|
|
95
|
+
parsedConfig,
|
|
96
|
+
parsedConfig.llm.postPromptPrepend ?? "",
|
|
97
|
+
parsedConfig.llm.postPromptReplace ?? ""
|
|
98
|
+
);
|
|
91
99
|
return parsedConfig;
|
|
92
100
|
} catch (error) {
|
|
93
101
|
console.warn(`${error?.message}, using default configuration`);
|
|
@@ -136,19 +144,19 @@ function defaultPostPrompt(_config) {
|
|
|
136
144
|
[/POST-MESSAGE]
|
|
137
145
|
`;
|
|
138
146
|
}
|
|
139
|
-
function prePrompt(config) {
|
|
147
|
+
function prePrompt(config, append, replace) {
|
|
140
148
|
return [
|
|
141
|
-
|
|
142
|
-
|
|
149
|
+
...append ? ["[SPECIFIC_INPUTS]", String(append), "[/SPECIFIC_INPUTS]"] : [],
|
|
150
|
+
replace ? String(replace) : defaultPrePrompt(config)
|
|
143
151
|
].join(" ").trim();
|
|
144
152
|
}
|
|
145
|
-
function postPrompt(config) {
|
|
153
|
+
function postPrompt(config, prepend, replace) {
|
|
146
154
|
return [
|
|
147
|
-
|
|
148
|
-
|
|
155
|
+
...prepend ? ["[SPECIFIC_INPUTS]", String(prepend), "[/SPECIFIC_INPUTS]"] : [],
|
|
156
|
+
replace ? String(replace) : defaultPostPrompt()
|
|
149
157
|
].join(" ").trim();
|
|
150
158
|
}
|
|
151
|
-
async function askAnthropic(
|
|
159
|
+
async function askAnthropic(prompt, prePrompt2, postPrompt2) {
|
|
152
160
|
const config = await awaitedConfig;
|
|
153
161
|
if (Number(config.llm.sleep)) {
|
|
154
162
|
await new Promise((resolve) => setTimeout(resolve, config.llm.sleep ?? 1));
|
|
@@ -160,19 +168,20 @@ async function askAnthropic(message) {
|
|
|
160
168
|
apiKey: config.keys.anthropicApiKey,
|
|
161
169
|
dangerouslyAllowBrowser: true
|
|
162
170
|
});
|
|
171
|
+
const fullPrompt = String(`${prePrompt2}
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
${prompt}
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
${postPrompt2}`).trim();
|
|
163
178
|
const response = await anthropic.messages.create({
|
|
164
179
|
model: config.llm.model,
|
|
165
180
|
max_tokens: 1024,
|
|
166
181
|
messages: [
|
|
167
182
|
{
|
|
168
183
|
role: "user",
|
|
169
|
-
content:
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
${message}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
${config.llm.postPrompt}`
|
|
184
|
+
content: fullPrompt
|
|
176
185
|
}
|
|
177
186
|
]
|
|
178
187
|
});
|
|
@@ -196,7 +205,7 @@ function parseAnthropic(unparsed) {
|
|
|
196
205
|
const parsed = JSON.parse(unparsed);
|
|
197
206
|
return parsed;
|
|
198
207
|
}
|
|
199
|
-
async function generateResponse(
|
|
208
|
+
async function generateResponse(prompt, temperature = 1) {
|
|
200
209
|
const config = await awaitedConfig;
|
|
201
210
|
if (!config.keys.geminiApiKey) {
|
|
202
211
|
throw new Error("Gemini API key is not set in the config");
|
|
@@ -208,20 +217,13 @@ async function generateResponse(message, temperature = 1) {
|
|
|
208
217
|
temperature
|
|
209
218
|
}
|
|
210
219
|
});
|
|
211
|
-
const prompt = String(`${config.llm.prePrompt}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
${message}
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
${config.llm.postPrompt}`).trim();
|
|
218
220
|
if (config.debug) {
|
|
219
221
|
console.log("Gemini model prompt:", JSON.stringify({ prompt }));
|
|
220
222
|
}
|
|
221
223
|
const response = await model.generateContent([prompt]);
|
|
222
224
|
return response;
|
|
223
225
|
}
|
|
224
|
-
async function askGemini(
|
|
226
|
+
async function askGemini(prompt, prePrompt2, postPrompt2) {
|
|
225
227
|
const config = await awaitedConfig;
|
|
226
228
|
if (Number(config.llm.sleep)) {
|
|
227
229
|
await new Promise((resolve) => setTimeout(resolve, config.llm.sleep ?? 1));
|
|
@@ -230,11 +232,18 @@ async function askGemini(message) {
|
|
|
230
232
|
throw new Error("Gemini API key is not set in the config");
|
|
231
233
|
}
|
|
232
234
|
if (config.debug) {
|
|
233
|
-
console.log("\nAsking Gemini model:", JSON.stringify({ model: config.llm.model, message }));
|
|
235
|
+
console.log("\nAsking Gemini model:", JSON.stringify({ model: config.llm.model, message: prompt }));
|
|
234
236
|
}
|
|
237
|
+
const fullPrompt = String(`${prePrompt2}
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
${prompt}
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
${postPrompt2}`).trim();
|
|
235
244
|
let response;
|
|
236
245
|
for (let i = 0; i < 3; i++) {
|
|
237
|
-
const maybeResponse = await generateResponse(
|
|
246
|
+
const maybeResponse = await generateResponse(fullPrompt, config.llm.temperature);
|
|
238
247
|
if (!String(maybeResponse.response.candidates?.[0]?.content?.parts?.[0]?.text ?? "").includes("```json")) {
|
|
239
248
|
response = maybeResponse;
|
|
240
249
|
break;
|
|
@@ -268,7 +277,7 @@ function parseGemini(unparsed) {
|
|
|
268
277
|
return parsed;
|
|
269
278
|
}
|
|
270
279
|
|
|
271
|
-
// src/io/huggingface/
|
|
280
|
+
// src/io/huggingface/languages-map.ts
|
|
272
281
|
var languagesMap = {
|
|
273
282
|
"en-US": "eng_Latn",
|
|
274
283
|
"en-GB": "eng_Latn",
|
|
@@ -554,20 +563,20 @@ var llmChoices = {
|
|
|
554
563
|
parse: parseHuggingface
|
|
555
564
|
}
|
|
556
565
|
};
|
|
557
|
-
async function processLLMChoice(llmChoice, prompt) {
|
|
558
|
-
const response = await llmChoice.ask(prompt);
|
|
566
|
+
async function processLLMChoice(llmChoice, prompt, prePrompt2, postPrompt2) {
|
|
567
|
+
const response = await llmChoice.ask(prompt, prePrompt2, postPrompt2);
|
|
559
568
|
const unparsed = llmChoice.commitResponse(response);
|
|
560
569
|
const parsed = llmChoice.parse(unparsed);
|
|
561
570
|
return parsed;
|
|
562
571
|
}
|
|
563
|
-
async function askLLM(prompt) {
|
|
572
|
+
async function askLLM(prompt, prePrompt2, postPrompt2) {
|
|
564
573
|
const config = await awaitedConfig;
|
|
565
574
|
const llmChoiceName = config.llm.choice;
|
|
566
575
|
const llmChoice = llmChoices[llmChoiceName];
|
|
567
576
|
if (!llmChoice) {
|
|
568
577
|
throw new Error("LLMChoice not found");
|
|
569
578
|
}
|
|
570
|
-
return processLLMChoice(llmChoice, prompt);
|
|
579
|
+
return processLLMChoice(llmChoice, prompt, prePrompt2, postPrompt2);
|
|
571
580
|
}
|
|
572
581
|
|
|
573
582
|
export { askLLM };
|
package/dist/io/gemini/ask.d.mts
CHANGED
|
@@ -3,9 +3,9 @@ import { GenerateContentResult } from '@google/generative-ai';
|
|
|
3
3
|
/**
|
|
4
4
|
* Asks the Gemini model for a response.
|
|
5
5
|
*
|
|
6
|
-
* @param
|
|
6
|
+
* @param prompt - The message to ask the model.
|
|
7
7
|
* @returns The response from the model.
|
|
8
8
|
*/
|
|
9
|
-
declare function askGemini(
|
|
9
|
+
declare function askGemini(prompt: string, prePrompt: string, postPrompt: string): Promise<GenerateContentResult>;
|
|
10
10
|
|
|
11
11
|
export { askGemini };
|