fix: rein in dbn-legal-agent feedback-loop contamination (stop seqs + JSON extract + system prompt)
This commit is contained in:
@@ -842,8 +842,16 @@ Rules:
|
|||||||
- Respond in {$locale}.
|
- Respond in {$locale}.
|
||||||
PROMPT;
|
PROMPT;
|
||||||
|
|
||||||
|
$sysPrompt = $engine === 'dbn_legal'
|
||||||
|
// dbn-legal-agent has a fine-tune that appends self-rating feedback loops after the
|
||||||
|
// JSON. Explicitly forbid that pattern and stop before it can start.
|
||||||
|
? 'You output valid JSON only. Output the JSON object, then stop immediately. '
|
||||||
|
. 'Do not add any self-assessment, rating, "END OF MESSAGE", feedback loop, '
|
||||||
|
. 'USER/SYSTEM turns, or any text after the closing brace of the JSON object.'
|
||||||
|
: 'You return valid JSON only. No markdown fences.';
|
||||||
|
|
||||||
$messages = [
|
$messages = [
|
||||||
['role' => 'system', 'content' => 'You return valid JSON only. No markdown fences.'],
|
['role' => 'system', 'content' => $sysPrompt],
|
||||||
['role' => 'user', 'content' => $prompt],
|
['role' => 'user', 'content' => $prompt],
|
||||||
];
|
];
|
||||||
$opts = ['json' => true, 'temperature' => $temperature, 'max_tokens' => 3000, 'timeout' => 200];
|
$opts = ['json' => true, 'temperature' => $temperature, 'max_tokens' => 3000, 'timeout' => 200];
|
||||||
@@ -865,9 +873,19 @@ PROMPT;
|
|||||||
'temperature' => $temperature,
|
'temperature' => $temperature,
|
||||||
'max_tokens' => 2800,
|
'max_tokens' => 2800,
|
||||||
'timeout' => 660,
|
'timeout' => 660,
|
||||||
|
// Stop sequences cut generation the moment the feedback loop tries to start.
|
||||||
|
'stop' => ["\nEND OF MESSAGE", "\nPlease rate", "\nUSER:", "지금 번역하기"],
|
||||||
], $emit ? static function () use ($emit): void {
|
], $emit ? static function () use ($emit): void {
|
||||||
$emit('progress', ['detail' => 'dbn-legal-agent generating…']);
|
$emit('progress', ['detail' => 'dbn-legal-agent generating…']);
|
||||||
} : null);
|
} : null);
|
||||||
|
|
||||||
|
// Belt-and-suspenders: even with stop sequences the model may still include
|
||||||
|
// preamble or trailing junk. Extract only the first complete {...} object.
|
||||||
|
$jsonStart = strpos($raw, '{');
|
||||||
|
$jsonEnd = strrpos($raw, '}');
|
||||||
|
if ($jsonStart !== false && $jsonEnd !== false && $jsonEnd > $jsonStart) {
|
||||||
|
$raw = substr($raw, $jsonStart, $jsonEnd - $jsonStart + 1);
|
||||||
|
}
|
||||||
} elseif ($engine === 'gpu') {
|
} elseif ($engine === 'gpu') {
|
||||||
$response = dbnToolsCallGpuLlm($messages, $opts);
|
$response = dbnToolsCallGpuLlm($messages, $opts);
|
||||||
$raw = (string)($response['choices'][0]['message']['content'] ?? '');
|
$raw = (string)($response['choices'][0]['message']['content'] ?? '');
|
||||||
@@ -915,6 +933,9 @@ PROMPT;
|
|||||||
'max_tokens' => $options['max_tokens'] ?? 2800,
|
'max_tokens' => $options['max_tokens'] ?? 2800,
|
||||||
'stream' => true,
|
'stream' => true,
|
||||||
];
|
];
|
||||||
|
if (!empty($options['stop']) && is_array($options['stop'])) {
|
||||||
|
$payload['stop'] = $options['stop'];
|
||||||
|
}
|
||||||
$body = json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
$body = json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||||
$headers = [
|
$headers = [
|
||||||
'Content-Type: application/json',
|
'Content-Type: application/json',
|
||||||
|
|||||||
Reference in New Issue
Block a user