From 292849f0a38c92b64ba28a33c02ed58181098a15 Mon Sep 17 00:00:00 2001 From: MILLER/F Date: Fri, 10 Mar 2023 02:30:00 +0700 Subject: [PATCH] [AI] Use chatGPT endpoint (#29332) * Use chat_completion for AI-Paragraph --------- Co-authored-by: artpi --- .../_inc/lib/class-jetpack-ai-helper.php | 20 +++++++++++++---- .../jetpack/changelog/add-ai_use_chat_gpt | 4 ++++ .../extensions/blocks/ai-paragraph/edit.js | 12 +++++----- .../blocks/ai-paragraph/test/edit.js | 22 +++++++++++-------- 4 files changed, 40 insertions(+), 18 deletions(-) create mode 100644 projects/plugins/jetpack/changelog/add-ai_use_chat_gpt diff --git a/projects/plugins/jetpack/_inc/lib/class-jetpack-ai-helper.php b/projects/plugins/jetpack/_inc/lib/class-jetpack-ai-helper.php index 9f3f03c46d7ee..d968a77b34a70 100644 --- a/projects/plugins/jetpack/_inc/lib/class-jetpack-ai-helper.php +++ b/projects/plugins/jetpack/_inc/lib/class-jetpack-ai-helper.php @@ -21,7 +21,7 @@ class Jetpack_AI_Helper { * * @var int */ - public static $text_completion_cooldown_seconds = 60; + public static $text_completion_cooldown_seconds = 15; /** * Cache images for a prompt for a month. @@ -154,14 +154,26 @@ public static function get_gpt_completion( $content, $post_id ) { \require_lib( 'openai' ); } - $result = ( new OpenAI( 'openai', array( 'post_id' => $post_id ) ) )->request_gpt_completion( $content ); + // Set the content for chatGPT endpoint + $data = array( + array( + 'role' => 'user', + 'content' => $content, + ), + ); + + $result = ( new OpenAI( 'openai', array( 'post_id' => $post_id ) ) )->request_chat_completion( $data ); + if ( is_wp_error( $result ) ) { return $result; } + + $response = $result->choices[0]->message->content; + // In case of Jetpack we are setting a transient on the WPCOM and not the remote site. I think the 'get_current_user_id' may default for the connection owner at this point but we'll deal with this later. - set_transient( self::transient_name_for_completion(), $result, self::$text_completion_cooldown_seconds ); + set_transient( self::transient_name_for_completion(), $response, self::$text_completion_cooldown_seconds ); self::mark_post_as_ai_assisted( $post_id ); - return $result; + return $response; } $response = Client::wpcom_json_api_request_as_user( diff --git a/projects/plugins/jetpack/changelog/add-ai_use_chat_gpt b/projects/plugins/jetpack/changelog/add-ai_use_chat_gpt new file mode 100644 index 0000000000000..4801e63f88f56 --- /dev/null +++ b/projects/plugins/jetpack/changelog/add-ai_use_chat_gpt @@ -0,0 +1,4 @@ +Significance: patch +Type: enhancement + +USe chat GPT API diff --git a/projects/plugins/jetpack/extensions/blocks/ai-paragraph/edit.js b/projects/plugins/jetpack/extensions/blocks/ai-paragraph/edit.js index 26bfafa884782..eba7e709b699c 100644 --- a/projects/plugins/jetpack/extensions/blocks/ai-paragraph/edit.js +++ b/projects/plugins/jetpack/extensions/blocks/ai-paragraph/edit.js @@ -10,7 +10,7 @@ import { sprintf, __ } from '@wordpress/i18n'; import { name as aiParagraphBlockName } from './index'; // Maximum number of characters we send from the content -export const MAXIMUM_NUMBER_OF_CHARACTERS_SENT_FROM_CONTENT = 240; +export const MAXIMUM_NUMBER_OF_CHARACTERS_SENT_FROM_CONTENT = 1024; // Creates the prompt that will eventually be sent to OpenAI. It uses the current post title, content (before the actual AI block) - or a slice of it if too long, and tags + categories names export const createPrompt = ( @@ -39,11 +39,11 @@ export const createPrompt = ( if ( postTitle ) { prompt = sprintf( /** translators: This will be the beginning of a prompt that will be sent to OpenAI based on the post title. */ - __( "This is a post titled '%1$s' ", 'jetpack' ), + __( "Please help me write a short piece of a blog post titled '%1$s'", 'jetpack' ), postTitle ); } else { - prompt = __( 'This is a post', 'jetpack' ); + prompt = __( 'Please help me write a short piece of a blog post', 'jetpack' ); } if ( categoriesNames ) { @@ -56,9 +56,11 @@ export const createPrompt = ( prompt += sprintf( __( " and tagged '%1$s'", 'jetpack' ), tagsNames ); } + prompt += __( '. Please only output generated content ready for publishing.', 'jetpack' ); + if ( shorter_content ) { /** translators: This will be the end of a prompt that will be sent to OpenAI with the last MAXIMUM_NUMBER_OF_CHARACTERS_SENT_FROM_CONTENT characters of content.*/ - prompt += sprintf( __( ':\n\n … %s', 'jetpack' ), shorter_content ); // eslint-disable-line @wordpress/i18n-no-collapsible-whitespace + prompt += sprintf( __( ' Please continue from here:\n\n … %s', 'jetpack' ), shorter_content ); // eslint-disable-line @wordpress/i18n-no-collapsible-whitespace } return prompt.trim(); @@ -207,7 +209,7 @@ export default function Edit( { attributes, setAttributes, clientId } ) { data: data, } ) .then( res => { - const result = res.prompts[ 0 ].text.trim().replaceAll( '\n', '
' ); + const result = res.trim().replaceAll( '\n', '
' ); setAttributes( { content: result } ); setIsLoadingCompletion( false ); } ) diff --git a/projects/plugins/jetpack/extensions/blocks/ai-paragraph/test/edit.js b/projects/plugins/jetpack/extensions/blocks/ai-paragraph/test/edit.js index f94d27f7e7823..c73acbd2af05c 100644 --- a/projects/plugins/jetpack/extensions/blocks/ai-paragraph/test/edit.js +++ b/projects/plugins/jetpack/extensions/blocks/ai-paragraph/test/edit.js @@ -17,38 +17,42 @@ describe( 'AIParagraphEdit', () => { expect( createPrompt( '', [], '', '' ) ).toBeFalsy(); // Test contents - expect( createPrompt( 'title', [], '', '' ) ).toBe( "This is a post titled 'title'" ); + expect( createPrompt( 'title', [], '', '' ) ).toBe( + "Please help me write a short piece of a blog post titled 'title'. Please only output generated content ready for publishing." + ); expect( createPrompt( 'title', [ fakeBlock, { attributes: { whatever: 'content' } } ], '', '' ) - ).toBe( "This is a post titled 'title' :\n\n … content" ); + ).toBe( + "Please help me write a short piece of a blog post titled 'title'. Please only output generated content ready for publishing. Please continue from here:\n\n … content" + ); // Test that
are being translated. And content trimmed expect( createPrompt( 'title', [ fakeBlockWithBr ], '', '' ) ).toBe( - "This is a post titled 'title' :\n\n … content\ncontent2" + "Please help me write a short piece of a blog post titled 'title'. Please only output generated content ready for publishing. Please continue from here:\n\n … content\ncontent2" ); expect( createPrompt( 'title', [ fakeBlock, fakeBlock ], 'cat 1', '' ) ).toBe( - "This is a post titled 'title' , published in categories 'cat 1':\n\n … content\ncontent" + "Please help me write a short piece of a blog post titled 'title', published in categories 'cat 1'. Please only output generated content ready for publishing. Please continue from here:\n\n … content\ncontent" ); // Test MAX content length expect( createPrompt( 'title', [ fakeBlockWithVeryLongContent ] ) ).toBe( - "This is a post titled 'title' :\n\n … " + + "Please help me write a short piece of a blog post titled 'title'. Please only output generated content ready for publishing. Please continue from here:\n\n … " + longContent.slice( -MAXIMUM_NUMBER_OF_CHARACTERS_SENT_FROM_CONTENT ) ); // Test only cats expect( createPrompt( '', [ fakeBlock ], 'cat1', 'tag1' ) ).toBe( - "This is a post, published in categories 'cat1' and tagged 'tag1':\n\n … content" + "Please help me write a short piece of a blog post, published in categories 'cat1' and tagged 'tag1'. Please only output generated content ready for publishing. Please continue from here:\n\n … content" ); expect( createPrompt( '', [ fakeBlock ], 'cat1, cat2', 'tag1' ) ).toBe( - "This is a post, published in categories 'cat1, cat2' and tagged 'tag1':\n\n … content" + "Please help me write a short piece of a blog post, published in categories 'cat1, cat2' and tagged 'tag1'. Please only output generated content ready for publishing. Please continue from here:\n\n … content" ); expect( createPrompt( '', [], 'cat1, cat2', 'tag1' ) ).toBe( - "This is a post, published in categories 'cat1, cat2' and tagged 'tag1'" + "Please help me write a short piece of a blog post, published in categories 'cat1, cat2' and tagged 'tag1'. Please only output generated content ready for publishing." ); expect( createPrompt( 'title', [], 'cat1, cat2', 'tag1' ) ).toBe( - "This is a post titled 'title' , published in categories 'cat1, cat2' and tagged 'tag1'" + "Please help me write a short piece of a blog post titled 'title', published in categories 'cat1, cat2' and tagged 'tag1'. Please only output generated content ready for publishing." ); } ); } );