| """Critique -> revise: the model learns from criticism. |
| |
| For each prompt: the policy answers, the judge writes a criticism, the policy REVISES |
| using that criticism, and if the revision scores higher it becomes a training pair |
| (prompt -> revised). This is how "take criticism into consideration" becomes signal. |
| """ |
|
|
|
|
| def _gen(model, tok, messages, max_new_tokens=512): |
| from core.genutil import chat_generate |
| return chat_generate(model, tok, messages, max_new_tokens=max_new_tokens, |
| do_sample=True, temperature=0.7, top_p=0.95) |
|
|
|
|
| def critique_revise_batch(policy, tok, judge, prompts): |
| """Returns improved (instruction, response) pairs where revision beat the original.""" |
| pairs = [] |
| for p in prompts: |
| first = _gen(policy, tok, [{"role": "user", "content": p}]) |
| verdict = judge.critique(p, first) |
| if verdict["score"] >= 0.9: |
| pairs.append({"instruction": p, "response": first, "score": verdict["score"]}) |
| continue |
| revise_msgs = [ |
| {"role": "user", "content": p}, |
| {"role": "assistant", "content": first}, |
| {"role": "user", "content": f"Criticism: {verdict['criticism']} " |
| f"(weakest: {verdict['weakest']}). Revise to fix this."}, |
| ] |
| revised = _gen(policy, tok, revise_msgs) |
| rscore = judge.score(p, revised) |
| best, bscore = (revised, rscore) if rscore > verdict["score"] else (first, verdict["score"]) |
| pairs.append({"instruction": p, "response": best, "score": bscore}) |
| return pairs |
|
|