add exponential backoff with jitter. increase retry count to 6 instead of 3
All checks were successful
ci/woodpecker/push/ci Pipeline was successful
All checks were successful
ci/woodpecker/push/ci Pipeline was successful
This commit is contained in:
@@ -5,9 +5,8 @@ async function sleep(ms) {
|
|||||||
return new Promise(resolve => setTimeout(resolve, ms));
|
return new Promise(resolve => setTimeout(resolve, ms));
|
||||||
}
|
}
|
||||||
|
|
||||||
async function callOllama(prompt, model = "gemma3:4b", retries = 3) {
|
async function callOllama(prompt, model = "gemma3:4b", retries = 6) {
|
||||||
let attempt = 0;
|
for (let attempt = 1; attempt <= retries; attempt++) {
|
||||||
while (attempt < retries) {
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(OLLAMA_API_URL, {
|
const response = await fetch(OLLAMA_API_URL, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
@@ -31,14 +30,18 @@ async function callOllama(prompt, model = "gemma3:4b", retries = 3) {
|
|||||||
return text;
|
return text;
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
attempt++;
|
|
||||||
console.warn(`⚠️ Ollama call failed (attempt ${attempt}/${retries}): ${err.message}`);
|
console.warn(`⚠️ Ollama call failed (attempt ${attempt}/${retries}): ${err.message}`);
|
||||||
if (attempt >= retries) throw err;
|
if (attempt === retries) throw err;
|
||||||
await sleep(1000 * attempt); // exponential backoff
|
|
||||||
|
// Exponential backoff with jitter
|
||||||
|
const delay = Math.pow(2, attempt) * 1000; // 2^attempt seconds
|
||||||
|
const jitter = Math.random() * 1000; // up to 1 second extra
|
||||||
|
await sleep(delay + jitter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Three-pass dungeon generation with full resiliency and JSON retry
|
* Three-pass dungeon generation with full resiliency and JSON retry
|
||||||
*/
|
*/
|
||||||
@@ -105,11 +108,11 @@ Dungeon description:
|
|||||||
${refined}
|
${refined}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const maxJsonRetries = 3;
|
const maxJsonRetries = 5;
|
||||||
for (let attempt = 1; attempt <= maxJsonRetries; attempt++) {
|
for (let attempt = 1; attempt <= maxJsonRetries; attempt++) {
|
||||||
try {
|
try {
|
||||||
console.log(`📦 JSON pass (attempt ${attempt}/${maxJsonRetries})...`);
|
console.log(`📦 JSON pass (attempt ${attempt}/${maxJsonRetries})...`);
|
||||||
jsonText = await callOllama(jsonPrompt);
|
jsonText = await callOllama(jsonPrompt, "gemma3:4b", 6);
|
||||||
const cleaned = jsonText.replace(/```json|```/g, "").trim();
|
const cleaned = jsonText.replace(/```json|```/g, "").trim();
|
||||||
const result = JSON.parse(cleaned);
|
const result = JSON.parse(cleaned);
|
||||||
console.log("🎉 Dungeon generation complete!");
|
console.log("🎉 Dungeon generation complete!");
|
||||||
|
|||||||
Reference in New Issue
Block a user