Skip to content

Commit 8dc898e

Browse files
authored
Merge pull request #19 from keiserjb/1.x-1.x
change prompt
2 parents 6e6b671 + 46bcc96 commit 8dc898e

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

modules/openai_devel/openai_devel.module

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,10 @@ function openai_devel_generate_content($options) {
248248
for ($i = 0; $i < $num_nodes; $i++) {
249249
try {
250250
// Include title length in the prompt for OpenAI.
251-
$system_message = $system . " Generate unique content for type '$type'. The title should have approximately $title_length words.";
252-
$unique_prompt = "Generate unique and relevant content for a '{$type}' node. The title should be $title_length words. Avoid using the word 'title' in the title. Ensure the content is engaging and contains several paragraphs. Incorporate specific details or examples to make it stand out. Make this one distinct from any others by focusing on a specific perspective, historical detail, or novel idea.";
251+
$system_message = $system . " Generate unique content for type '$type'. The title should have approximately $title_length words and should not include the word 'title'.";
252+
$unique_prompt = "Generate unique and relevant content for a '{$type}' node. The title should be $title_length words and must not include the word 'title'.
253+
Ensure the content is engaging and contains several paragraphs. Incorporate specific details or examples to make it stand out.
254+
Make this one distinct from any others by focusing on a specific perspective, historical detail, or novel idea.";
253255

254256
$response = $api->chat(
255257
$model,
@@ -263,6 +265,7 @@ function openai_devel_generate_content($options) {
263265

264266
list($title, $body) = openai_devel_extract_title_and_body($response);
265267

268+
266269
// Set language for the node.
267270
$langcode = LANGUAGE_NONE;
268271
if (!empty($languages)) {
@@ -334,27 +337,25 @@ function openai_devel_generate_content($options) {
334337
* Clean the body text by removing unnecessary artifacts.
335338
*/
336339
function openai_devel_extract_title_and_body($response) {
340+
// Split the response into lines.
337341
$lines = explode("\n", $response);
338342

339-
// Remove Markdown-style headers from the title.
340-
$title = trim(array_shift($lines));
341-
// Remove leading `#` or `*` characters from the title.
342-
$title = preg_replace('/^[#*]+/', '', $title);
343+
// Assume the first non-empty line is the title.
344+
$title = '';
345+
foreach ($lines as $line) {
346+
$line = trim($line);
347+
if (!empty($line)) {
348+
$title = $line;
349+
break;
350+
}
351+
}
343352

344-
// Remove trailing `#` or `*` characters from the title.
345-
$title = preg_replace('/[#*]+$/', '', $title);
353+
// Remove artifacts like "Title:" or "The title is".
354+
$title = preg_replace('/\btitle\b/i', '', $title);
355+
$title = preg_replace('/[:\-]+$/', '', $title); // Remove trailing punctuation.
346356

347-
$body = implode("\n", $lines);
357+
// Rejoin the remaining lines as the body.
358+
$body = implode("\n", array_slice($lines, 1));
348359

349360
return [trim($title), trim($body)];
350361
}
351-
352-
353-
354-
355-
356-
357-
358-
359-
360-

0 commit comments

Comments
 (0)