-
Notifications
You must be signed in to change notification settings - Fork 3
Retry badge moderation when reasoning eats the whole budget #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
boomzero
wants to merge
1
commit into
master
Choose a base branch
from
fix/badge-moderation-truncation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -29,10 +29,13 @@ const os = require("os"); | |||||
| const path = require("path"); | ||||||
|
|
||||||
| const { | ||||||
| BadgeModerationAttempts, | ||||||
| BadgeModerationMaxTokens, | ||||||
| BadgeModerationModel, | ||||||
| BadgeModerationPrompt, | ||||||
| BadgeModerationSchema, | ||||||
| BadgeRuleReasons, | ||||||
| ModerationReplyTruncated, | ||||||
| ReadModerationVerdict, | ||||||
| } = require("../Source/Process.ts"); | ||||||
|
|
||||||
|
|
@@ -66,7 +69,7 @@ function token() { | |||||
| return match ? match[1] : null; | ||||||
| } | ||||||
|
|
||||||
| async function moderate(content, bearer) { | ||||||
| async function ask(content, bearer) { | ||||||
| const response = await fetch( | ||||||
| "https://api.cloudflare.com/client/v4/accounts/" + ACCOUNT_ID + "/ai/run/" + BadgeModerationModel, | ||||||
| { | ||||||
|
|
@@ -78,19 +81,32 @@ async function moderate(content, bearer) { | |||||
| { role: "user", content: "<badge>" + content + "</badge>" }, | ||||||
| ], | ||||||
| temperature: 0, | ||||||
| max_completion_tokens: 1024, | ||||||
| max_completion_tokens: BadgeModerationMaxTokens, | ||||||
| response_format: { type: "json_schema", json_schema: BadgeModerationSchema }, | ||||||
| }), | ||||||
| } | ||||||
| ); | ||||||
| const body = await response.json(); | ||||||
| if (!body.success) { | ||||||
| return { error: JSON.stringify(body.errors || body) }; | ||||||
| return response.json(); | ||||||
| } | ||||||
|
|
||||||
| // Same retry EditBadge does, so the neuron count printed below is what a real edit | ||||||
| // of this text would cost, retries included. | ||||||
| async function moderate(content, bearer) { | ||||||
| let neurons = 0; | ||||||
| let retries = 0; | ||||||
| for (let attempt = 0; attempt < BadgeModerationAttempts; attempt++) { | ||||||
| const body = await ask(content, bearer); | ||||||
| if (!body.success) { | ||||||
| return { error: JSON.stringify(body.errors || body), neurons }; | ||||||
| } | ||||||
| neurons += body.result?.usage?.neurons || 0; | ||||||
| const verdict = ReadModerationVerdict(body.result); | ||||||
| if (verdict !== null || !ModerationReplyTruncated(body.result)) { | ||||||
| return { verdict, neurons, retries }; | ||||||
| } | ||||||
| retries++; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: Two truncated replies are reported as “retried 2x”, although only one retry was made after the initial call. Count only truncations that actually lead to another attempt so exhausted retries match production behavior and tool output. Prompt for AI agents
Suggested change
|
||||||
| } | ||||||
| return { | ||||||
| verdict: ReadModerationVerdict(body.result), | ||||||
| neurons: body.result?.usage?.neurons, | ||||||
| }; | ||||||
| return { verdict: null, neurons, retries }; | ||||||
| } | ||||||
|
|
||||||
| async function main() { | ||||||
|
|
@@ -123,19 +139,21 @@ async function main() { | |||||
| console.log("BLOCKED " + JSON.stringify(content) + " " + blocked + " (no model call)"); | ||||||
| continue; | ||||||
| } | ||||||
| const { verdict, neurons, error } = await moderate(content, bearer); | ||||||
| const { verdict, neurons, retries, error } = await moderate(content, bearer); | ||||||
| spent += neurons || 0; | ||||||
| if (error) { | ||||||
| console.log("ERROR " + JSON.stringify(content) + " " + error); | ||||||
| continue; | ||||||
| } | ||||||
| spent += neurons || 0; | ||||||
| const retried = retries > 0 ? " (retried " + retries + "x after truncation)" : ""; | ||||||
| if (verdict === null) { | ||||||
| console.log("UNUSABLE " + JSON.stringify(content) + " model reply failed validation, edit would fail closed"); | ||||||
| console.log("UNUSABLE " + JSON.stringify(content) + | ||||||
| " model reply failed validation, edit would fail closed" + retried); | ||||||
| } else if (verdict.allowed) { | ||||||
| console.log("ALLOW " + JSON.stringify(content)); | ||||||
| console.log("ALLOW " + JSON.stringify(content) + retried); | ||||||
| } else { | ||||||
| console.log("REJECT " + JSON.stringify(content) + | ||||||
| " rule " + verdict.rule + " — 标签内容" + BadgeRuleReasons[verdict.rule] + ",请修改后重试"); | ||||||
| " rule " + verdict.rule + " — 标签内容" + BadgeRuleReasons[verdict.rule] + ",请修改后重试" + retried); | ||||||
| } | ||||||
| } | ||||||
| if (spent > 0) { | ||||||
|
|
||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P3: A second truncated reply logs “retrying” although no attempts remain and no retry occurs. Guard this warning to attempts that can actually continue, so operational logs accurately distinguish a retry from final failure.
Prompt for AI agents