Skip to content

Commit

Permalink
Pass the post title to our endpoint so we have the most up-to-date ti…
Browse files Browse the repository at this point in the history
…tle to use in our prompt
  • Loading branch information
dkotter committed Jul 27, 2023
1 parent fe72197 commit eb5be16
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 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 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

0 comments on commit eb5be16

Please sign in to comment.