Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update our excerpt prompt #544

Merged
merged 3 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions includes/Classifai/Providers/OpenAI/ChatGPT.php
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ public function generate_excerpt( int $post_id = 0, array $args = [] ) {
array_filter( $args ),
[
'content' => '',
'title' => get_the_title( $post_id ),
]
);

Expand All @@ -557,7 +558,7 @@ public function generate_excerpt( int $post_id = 0, array $args = [] ) {
*
* @return {string} Prompt.
*/
$prompt = apply_filters( 'classifai_chatgpt_excerpt_prompt', 'Provide a teaser for the following text using a maximum of ' . $excerpt_length . ' words', $post_id, $excerpt_length );
$prompt = apply_filters( 'classifai_chatgpt_excerpt_prompt', sprintf( 'Summarize the following message using a maximum of %d words. Ensure this summary pairs well with the following text: %s.', $excerpt_length, $args['title'] ), $post_id, $excerpt_length );

/**
* Filter the request body before sending to ChatGPT.
Expand All @@ -575,12 +576,16 @@ public function generate_excerpt( int $post_id = 0, array $args = [] ) {
[
'model' => $this->chatgpt_model,
'messages' => [
[
'role' => 'system',
'content' => $prompt,
],
[
'role' => 'user',
'content' => $prompt . ': ' . $this->get_content( $post_id, $excerpt_length, true, $args['content'] ) . '',
'content' => $this->get_content( $post_id, $excerpt_length, false, $args['content'] ) . '',
],
],
'temperature' => 0,
'temperature' => 0.9,
],
$post_id
);
Expand Down Expand Up @@ -731,10 +736,10 @@ public function get_content( int $post_id = 0, int $return_length = 0, bool $use
/**
* We then subtract those tokens from the max number of tokens ChatGPT allows
* in a single request, as well as subtracting out the number of tokens in our
* prompt (13). ChatGPT counts both the tokens in the request and in
* prompt (~50). ChatGPT counts both the tokens in the request and in
* the response towards the max.
*/
$max_content_tokens = $this->max_tokens - $return_tokens - 13;
$max_content_tokens = $this->max_tokens - $return_tokens - 50;

if ( empty( $post_content ) ) {
$post = get_post( $post_id );
Expand Down
20 changes: 18 additions & 2 deletions includes/Classifai/Services/LanguageProcessing.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,13 @@ public function register_endpoints() {
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
'validate_callback' => 'rest_validate_request_arg',
'description' => esc_html__( 'Content to generate a title for', 'classifai' ),
'description' => esc_html__( 'Content to summarize into an excerpt.', 'classifai' ),
],
'title' => [
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
'validate_callback' => 'rest_validate_request_arg',
'description' => esc_html__( 'Title of content we want a summary for.', 'classifai' ),
],
],
'permission_callback' => [ $this, 'generate_post_excerpt_permissions_check' ],
Expand Down Expand Up @@ -275,6 +281,7 @@ public function generate_post_tags_permissions_check( WP_REST_Request $request )
public function generate_post_excerpt( WP_REST_Request $request ) {
$post_id = $request->get_param( 'id' );
$content = $request->get_param( 'content' );
$title = $request->get_param( 'title' );

// Find the right provider class.
$provider = find_provider_class( $this->provider_classes ?? [], 'ChatGPT' );
Expand All @@ -284,7 +291,16 @@ public function generate_post_excerpt( WP_REST_Request $request ) {
return $provider;
}

return rest_ensure_response( $provider->rest_endpoint_callback( $post_id, 'excerpt', [ 'content' => $content ] ) );
return rest_ensure_response(
$provider->rest_endpoint_callback(
$post_id,
'excerpt',
[
'content' => $content,
'title' => $title,
]
)
);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/js/post-excerpt/panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function PostExcerpt( { excerpt, onUpdateExcerpt } ) {
const postId = select( 'core/editor' ).getCurrentPostId();
const postContent =
select( 'core/editor' ).getEditedPostAttribute( 'content' );
const postTitle = select( 'core/editor' ).getEditedPostAttribute( 'title' );
const buttonText =
'' === excerpt
? __( 'Generate excerpt', 'classifai' )
Expand All @@ -39,7 +40,7 @@ function PostExcerpt( { excerpt, onUpdateExcerpt } ) {
apiFetch( {
path,
method: 'POST',
data: { id: postId, content: postContent },
data: { id: postId, content: postContent, title: postTitle },
} ).then(
( res ) => {
onUpdateExcerpt( res );
Expand Down
Loading