@gringow/gringow 0.0.9 → 0.1.1
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 +41 -47
- 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/gringow.d.mts +1 -1
- package/dist/gringow.mjs +80 -39
- package/dist/index.d.mts +5 -1
- package/dist/index.mjs +80 -39
- 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 +43 -34
- 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 +19 -11
- package/dist/io/huggingface/index.mjs +19 -11
- 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 +1 -1
|
@@ -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;
|
|
@@ -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 };
|
package/dist/io/gemini/ask.mjs
CHANGED
|
@@ -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/gemini/ask.ts
|
|
150
|
-
async function generateResponse(
|
|
158
|
+
async function generateResponse(prompt, temperature = 1) {
|
|
151
159
|
const config = await awaitedConfig;
|
|
152
160
|
if (!config.keys.geminiApiKey) {
|
|
153
161
|
throw new Error("Gemini API key is not set in the config");
|
|
@@ -159,20 +167,13 @@ async function generateResponse(message, temperature = 1) {
|
|
|
159
167
|
temperature
|
|
160
168
|
}
|
|
161
169
|
});
|
|
162
|
-
const prompt = String(`${config.llm.prePrompt}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
${message}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
${config.llm.postPrompt}`).trim();
|
|
169
170
|
if (config.debug) {
|
|
170
171
|
console.log("Gemini model prompt:", JSON.stringify({ prompt }));
|
|
171
172
|
}
|
|
172
173
|
const response = await model.generateContent([prompt]);
|
|
173
174
|
return response;
|
|
174
175
|
}
|
|
175
|
-
async function askGemini(
|
|
176
|
+
async function askGemini(prompt, prePrompt2, postPrompt2) {
|
|
176
177
|
const config = await awaitedConfig;
|
|
177
178
|
if (Number(config.llm.sleep)) {
|
|
178
179
|
await new Promise((resolve) => setTimeout(resolve, config.llm.sleep ?? 1));
|
|
@@ -181,11 +182,18 @@ async function askGemini(message) {
|
|
|
181
182
|
throw new Error("Gemini API key is not set in the config");
|
|
182
183
|
}
|
|
183
184
|
if (config.debug) {
|
|
184
|
-
console.log("\nAsking Gemini model:", JSON.stringify({ model: config.llm.model, message }));
|
|
185
|
+
console.log("\nAsking Gemini model:", JSON.stringify({ model: config.llm.model, message: prompt }));
|
|
185
186
|
}
|
|
187
|
+
const fullPrompt = String(`${prePrompt2}
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
${prompt}
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
${postPrompt2}`).trim();
|
|
186
194
|
let response;
|
|
187
195
|
for (let i = 0; i < 3; i++) {
|
|
188
|
-
const maybeResponse = await generateResponse(
|
|
196
|
+
const maybeResponse = await generateResponse(fullPrompt, config.llm.temperature);
|
|
189
197
|
if (!String(maybeResponse.response.candidates?.[0]?.content?.parts?.[0]?.text ?? "").includes("```json")) {
|
|
190
198
|
response = maybeResponse;
|
|
191
199
|
break;
|
package/dist/io/gemini/index.mjs
CHANGED
|
@@ -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/gemini/ask.ts
|
|
150
|
-
async function generateResponse(
|
|
158
|
+
async function generateResponse(prompt, temperature = 1) {
|
|
151
159
|
const config = await awaitedConfig;
|
|
152
160
|
if (!config.keys.geminiApiKey) {
|
|
153
161
|
throw new Error("Gemini API key is not set in the config");
|
|
@@ -159,20 +167,13 @@ async function generateResponse(message, temperature = 1) {
|
|
|
159
167
|
temperature
|
|
160
168
|
}
|
|
161
169
|
});
|
|
162
|
-
const prompt = String(`${config.llm.prePrompt}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
${message}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
${config.llm.postPrompt}`).trim();
|
|
169
170
|
if (config.debug) {
|
|
170
171
|
console.log("Gemini model prompt:", JSON.stringify({ prompt }));
|
|
171
172
|
}
|
|
172
173
|
const response = await model.generateContent([prompt]);
|
|
173
174
|
return response;
|
|
174
175
|
}
|
|
175
|
-
async function askGemini(
|
|
176
|
+
async function askGemini(prompt, prePrompt2, postPrompt2) {
|
|
176
177
|
const config = await awaitedConfig;
|
|
177
178
|
if (Number(config.llm.sleep)) {
|
|
178
179
|
await new Promise((resolve) => setTimeout(resolve, config.llm.sleep ?? 1));
|
|
@@ -181,11 +182,18 @@ async function askGemini(message) {
|
|
|
181
182
|
throw new Error("Gemini API key is not set in the config");
|
|
182
183
|
}
|
|
183
184
|
if (config.debug) {
|
|
184
|
-
console.log("\nAsking Gemini model:", JSON.stringify({ model: config.llm.model, message }));
|
|
185
|
+
console.log("\nAsking Gemini model:", JSON.stringify({ model: config.llm.model, message: prompt }));
|
|
185
186
|
}
|
|
187
|
+
const fullPrompt = String(`${prePrompt2}
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
${prompt}
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
${postPrompt2}`).trim();
|
|
186
194
|
let response;
|
|
187
195
|
for (let i = 0; i < 3; i++) {
|
|
188
|
-
const maybeResponse = await generateResponse(
|
|
196
|
+
const maybeResponse = await generateResponse(fullPrompt, config.llm.temperature);
|
|
189
197
|
if (!String(maybeResponse.response.candidates?.[0]?.content?.parts?.[0]?.text ?? "").includes("```json")) {
|
|
190
198
|
response = maybeResponse;
|
|
191
199
|
break;
|
|
@@ -53,12 +53,12 @@ var configSchema = z.object({
|
|
|
53
53
|
model: z.string().nullable(),
|
|
54
54
|
temperature: z.number().optional(),
|
|
55
55
|
sleep: z.number().optional(),
|
|
56
|
-
prePrompt: z.string().optional(),
|
|
57
|
-
postPrompt: z.string().optional(),
|
|
56
|
+
prePrompt: z.string().max(128).optional(),
|
|
57
|
+
postPrompt: z.string().max(128).optional(),
|
|
58
58
|
prePromptReplace: z.string().optional(),
|
|
59
59
|
postPromptReplace: z.string().optional(),
|
|
60
60
|
prePromptAppend: z.string().optional(),
|
|
61
|
-
|
|
61
|
+
postPromptPrepend: z.string().optional()
|
|
62
62
|
}),
|
|
63
63
|
globby: z.string().regex(/^\.\/.*/).optional()
|
|
64
64
|
}).refine(
|
|
@@ -84,8 +84,16 @@ async function loadUserConfig() {
|
|
|
84
84
|
try {
|
|
85
85
|
const mergedConfig = merge({}, defaultConfig, result?.config);
|
|
86
86
|
const parsedConfig = await configSchema.parseAsync(mergedConfig);
|
|
87
|
-
parsedConfig.llm.prePrompt = prePrompt(
|
|
88
|
-
|
|
87
|
+
parsedConfig.llm.prePrompt = prePrompt(
|
|
88
|
+
parsedConfig,
|
|
89
|
+
parsedConfig.llm.prePromptAppend ?? "",
|
|
90
|
+
parsedConfig.llm.prePromptReplace ?? ""
|
|
91
|
+
);
|
|
92
|
+
parsedConfig.llm.postPrompt = postPrompt(
|
|
93
|
+
parsedConfig,
|
|
94
|
+
parsedConfig.llm.postPromptPrepend ?? "",
|
|
95
|
+
parsedConfig.llm.postPromptReplace ?? ""
|
|
96
|
+
);
|
|
89
97
|
return parsedConfig;
|
|
90
98
|
} catch (error) {
|
|
91
99
|
console.warn(`${error?.message}, using default configuration`);
|
|
@@ -134,16 +142,16 @@ function defaultPostPrompt(_config) {
|
|
|
134
142
|
[/POST-MESSAGE]
|
|
135
143
|
`;
|
|
136
144
|
}
|
|
137
|
-
function prePrompt(config) {
|
|
145
|
+
function prePrompt(config, append, replace) {
|
|
138
146
|
return [
|
|
139
|
-
|
|
140
|
-
|
|
147
|
+
...append ? ["[SPECIFIC_INPUTS]", String(append), "[/SPECIFIC_INPUTS]"] : [],
|
|
148
|
+
replace ? String(replace) : defaultPrePrompt(config)
|
|
141
149
|
].join(" ").trim();
|
|
142
150
|
}
|
|
143
|
-
function postPrompt(config) {
|
|
151
|
+
function postPrompt(config, prepend, replace) {
|
|
144
152
|
return [
|
|
145
|
-
|
|
146
|
-
|
|
153
|
+
...prepend ? ["[SPECIFIC_INPUTS]", String(prepend), "[/SPECIFIC_INPUTS]"] : [],
|
|
154
|
+
replace ? String(replace) : defaultPostPrompt()
|
|
147
155
|
].join(" ").trim();
|
|
148
156
|
}
|
|
149
157
|
|
|
@@ -53,12 +53,12 @@ var configSchema = z.object({
|
|
|
53
53
|
model: z.string().nullable(),
|
|
54
54
|
temperature: z.number().optional(),
|
|
55
55
|
sleep: z.number().optional(),
|
|
56
|
-
prePrompt: z.string().optional(),
|
|
57
|
-
postPrompt: z.string().optional(),
|
|
56
|
+
prePrompt: z.string().max(128).optional(),
|
|
57
|
+
postPrompt: z.string().max(128).optional(),
|
|
58
58
|
prePromptReplace: z.string().optional(),
|
|
59
59
|
postPromptReplace: z.string().optional(),
|
|
60
60
|
prePromptAppend: z.string().optional(),
|
|
61
|
-
|
|
61
|
+
postPromptPrepend: z.string().optional()
|
|
62
62
|
}),
|
|
63
63
|
globby: z.string().regex(/^\.\/.*/).optional()
|
|
64
64
|
}).refine(
|
|
@@ -84,8 +84,16 @@ async function loadUserConfig() {
|
|
|
84
84
|
try {
|
|
85
85
|
const mergedConfig = merge({}, defaultConfig, result?.config);
|
|
86
86
|
const parsedConfig = await configSchema.parseAsync(mergedConfig);
|
|
87
|
-
parsedConfig.llm.prePrompt = prePrompt(
|
|
88
|
-
|
|
87
|
+
parsedConfig.llm.prePrompt = prePrompt(
|
|
88
|
+
parsedConfig,
|
|
89
|
+
parsedConfig.llm.prePromptAppend ?? "",
|
|
90
|
+
parsedConfig.llm.prePromptReplace ?? ""
|
|
91
|
+
);
|
|
92
|
+
parsedConfig.llm.postPrompt = postPrompt(
|
|
93
|
+
parsedConfig,
|
|
94
|
+
parsedConfig.llm.postPromptPrepend ?? "",
|
|
95
|
+
parsedConfig.llm.postPromptReplace ?? ""
|
|
96
|
+
);
|
|
89
97
|
return parsedConfig;
|
|
90
98
|
} catch (error) {
|
|
91
99
|
console.warn(`${error?.message}, using default configuration`);
|
|
@@ -134,16 +142,16 @@ function defaultPostPrompt(_config) {
|
|
|
134
142
|
[/POST-MESSAGE]
|
|
135
143
|
`;
|
|
136
144
|
}
|
|
137
|
-
function prePrompt(config) {
|
|
145
|
+
function prePrompt(config, append, replace) {
|
|
138
146
|
return [
|
|
139
|
-
|
|
140
|
-
|
|
147
|
+
...append ? ["[SPECIFIC_INPUTS]", String(append), "[/SPECIFIC_INPUTS]"] : [],
|
|
148
|
+
replace ? String(replace) : defaultPrePrompt(config)
|
|
141
149
|
].join(" ").trim();
|
|
142
150
|
}
|
|
143
|
-
function postPrompt(config) {
|
|
151
|
+
function postPrompt(config, prepend, replace) {
|
|
144
152
|
return [
|
|
145
|
-
|
|
146
|
-
|
|
153
|
+
...prepend ? ["[SPECIFIC_INPUTS]", String(prepend), "[/SPECIFIC_INPUTS]"] : [],
|
|
154
|
+
replace ? String(replace) : defaultPostPrompt()
|
|
147
155
|
].join(" ").trim();
|
|
148
156
|
}
|
|
149
157
|
|