Skip to content

Commit b66f4d8

Browse files
MDL-88314 core_ai: Strip think tags from AI-generated text
1 parent 3a2c8a0 commit b66f4d8

7 files changed

Lines changed: 55 additions & 1 deletion

File tree

public/ai/classes/aiactions/responses/response_explain_text.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ public function set_response_data(array $response): void {
7272
$this->id = $response['id'] ?? null;
7373
$this->fingerprint = $response['fingerprint'] ?? null;
7474
$this->generatedcontent = $response['generatedcontent'] ?? null;
75+
if ($this->generatedcontent !== null) {
76+
$this->generatedcontent = trim(
77+
preg_replace('/<think>.*?<\/think>/is', '', $this->generatedcontent) ?? $this->generatedcontent
78+
);
79+
}
7580
$this->finishreason = $response['finishreason'] ?? null;
7681
$this->prompttokens = $response['prompttokens'] ?? null;
7782
$this->completiontokens = $response['completiontokens'] ?? null;

public/ai/classes/aiactions/responses/response_generate_text.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ public function set_response_data(array $response): void {
7272
$this->id = $response['id'] ?? null;
7373
$this->fingerprint = $response['fingerprint'] ?? null;
7474
$this->generatedcontent = $response['generatedcontent'] ?? null;
75+
if ($this->generatedcontent !== null) {
76+
$this->generatedcontent = trim(
77+
preg_replace('/<think>.*?<\/think>/is', '', $this->generatedcontent) ?? $this->generatedcontent
78+
);
79+
}
7580
$this->finishreason = $response['finishreason'] ?? null;
7681
$this->prompttokens = $response['prompttokens'] ?? null;
7782
$this->completiontokens = $response['completiontokens'] ?? null;

public/ai/classes/aiactions/responses/response_summarise_text.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ public function set_response_data(array $response): void {
7272
$this->id = $response['id'] ?? null;
7373
$this->fingerprint = $response['fingerprint'] ?? null;
7474
$this->generatedcontent = $response['generatedcontent'] ?? null;
75+
if ($this->generatedcontent !== null) {
76+
$this->generatedcontent = trim(
77+
preg_replace('/<think>.*?<\/think>/is', '', $this->generatedcontent) ?? $this->generatedcontent
78+
);
79+
}
7580
$this->finishreason = $response['finishreason'] ?? null;
7681
$this->prompttokens = $response['prompttokens'] ?? null;
7782
$this->completiontokens = $response['completiontokens'] ?? null;

public/ai/placement/editor/classes/external/generate_text.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public static function execute_returns(): external_function_parameters {
128128
VALUE_REQUIRED,
129129
),
130130
'generatedcontent' => new external_value(
131-
PARAM_TEXT,
131+
PARAM_RAW,
132132
'The text generated by AI.',
133133
VALUE_DEFAULT,
134134
),

public/ai/tests/aiactions/responses/response_explain_text_test.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,17 @@ public function test_set_response_data(): void {
7575
$this->assertEquals($body['prompttokens'], $actionresponse->get_response_data()['prompttokens']);
7676
$this->assertEquals($body['completiontokens'], $actionresponse->get_response_data()['completiontokens']);
7777
}
78+
79+
/**
80+
* Test that think tags are stripped from generated content.
81+
*/
82+
public function test_set_response_data_strips_think_tags(): void {
83+
$actionresponse = new response_explain_text(success: true);
84+
$actionresponse->set_response_data([
85+
'generatedcontent' => "<think>Some internal reasoning.\nMultiple lines.</think>The actual response.",
86+
'finishreason' => 'stop',
87+
]);
88+
89+
$this->assertEquals('The actual response.', $actionresponse->get_response_data()['generatedcontent']);
90+
}
7891
}

public/ai/tests/aiactions/responses/response_generate_text_test.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,17 @@ public function test_set_response_data(): void {
7575
$this->assertEquals($body['prompttokens'], $actionresponse->get_response_data()['prompttokens']);
7676
$this->assertEquals($body['completiontokens'], $actionresponse->get_response_data()['completiontokens']);
7777
}
78+
79+
/**
80+
* Test that think tags are stripped from generated content.
81+
*/
82+
public function test_set_response_data_strips_think_tags(): void {
83+
$actionresponse = new response_generate_text(success: true);
84+
$actionresponse->set_response_data([
85+
'generatedcontent' => "<think>Some internal reasoning.\nMultiple lines.</think>The actual response.",
86+
'finishreason' => 'stop',
87+
]);
88+
89+
$this->assertEquals('The actual response.', $actionresponse->get_response_data()['generatedcontent']);
90+
}
7891
}

public/ai/tests/aiactions/responses/response_summarise_text_test.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,17 @@ public function test_set_response_data(): void {
7575
$this->assertEquals($body['prompttokens'], $actionresponse->get_response_data()['prompttokens']);
7676
$this->assertEquals($body['completiontokens'], $actionresponse->get_response_data()['completiontokens']);
7777
}
78+
79+
/**
80+
* Test that think tags are stripped from generated content.
81+
*/
82+
public function test_set_response_data_strips_think_tags(): void {
83+
$actionresponse = new response_summarise_text(success: true);
84+
$actionresponse->set_response_data([
85+
'generatedcontent' => "<think>Some internal reasoning.\nMultiple lines.</think>The actual response.",
86+
'finishreason' => 'stop',
87+
]);
88+
89+
$this->assertEquals('The actual response.', $actionresponse->get_response_data()['generatedcontent']);
90+
}
7891
}

0 commit comments

Comments
 (0)