Skip to content

Commit

Permalink
[AI] Use chatGPT endpoint (#29332)
Browse files Browse the repository at this point in the history
* Use chat_completion for AI-Paragraph

---------

Co-authored-by: artpi <[email protected]>
  • Loading branch information
millerf and artpi authored Mar 9, 2023
1 parent dbb73e8 commit 292849f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 18 deletions.
20 changes: 16 additions & 4 deletions projects/plugins/jetpack/_inc/lib/class-jetpack-ai-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 4 additions & 0 deletions projects/plugins/jetpack/changelog/add-ai_use_chat_gpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: enhancement

USe chat GPT API
12 changes: 7 additions & 5 deletions projects/plugins/jetpack/extensions/blocks/ai-paragraph/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down Expand Up @@ -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 ) {
Expand All @@ -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();
Expand Down Expand Up @@ -207,7 +209,7 @@ export default function Edit( { attributes, setAttributes, clientId } ) {
data: data,
} )
.then( res => {
const result = res.prompts[ 0 ].text.trim().replaceAll( '\n', '<br/>' );
const result = res.trim().replaceAll( '\n', '<br/>' );
setAttributes( { content: result } );
setIsLoadingCompletion( false );
} )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <BR/> 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."
);
} );
} );

0 comments on commit 292849f

Please sign in to comment.