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

CYS - AI Improvements #42800

Merged
merged 22 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
bdde72b
Add the response_format to the params for requests to AI to ensure it…
nefeline Dec 11, 2023
481d0a9
Update the prompt to define the search term for images from Pexels.
nefeline Dec 12, 2023
36f036b
Streamline product content update and the product image uploads withi…
nefeline Dec 12, 2023
7a6cfb0
IMprove prompts for images and product content generation.
nefeline Dec 13, 2023
f506ea4
Update the prompt for defining the search term on Pexels and drop dou…
nefeline Dec 13, 2023
65f16fc
Update the prompts and the position for the titles on content generat…
nefeline Dec 13, 2023
6f4a3fb
Set the 'woocommerce_ai_managed_images' transient for the products en…
nefeline Dec 13, 2023
d744810
Update the prompt for defining the search term for images.
nefeline Dec 13, 2023
c0767ac
Remove unused methods from the PatternsHelper class.
nefeline Dec 13, 2023
9267cb5
Update the prompt for AI content generation in patterns.
nefeline Dec 13, 2023
3d58349
Update the prompts for the testimonials and the Hero Product 3 Split …
nefeline Dec 13, 2023
c9a5a6d
Update the position for the main title in the Hero Product 3 Split pa…
nefeline Dec 13, 2023
c62a1b2
Reduce the size of the images uploaded to the media library for produ…
nefeline Dec 13, 2023
4ee49af
Add docblock and update prompts.
nefeline Dec 13, 2023
38d8eaa
Update the default size of the images received from Pexels to be down…
nefeline Dec 13, 2023
76a20c7
Update prompts for the Hero Product 3 Split pattern.
nefeline Dec 13, 2023
d1c3405
Merge branch 'trunk' into update/ai-requests-improvements
nefeline Dec 13, 2023
101ef5c
Merge branch 'trunk' into update/ai-requests-improvements
nefeline Dec 14, 2023
dcf5ba8
Add changefile(s) from automation for the following project(s): wooco…
invalid-email-address Dec 14, 2023
1e77920
Merge branch 'trunk' into update/ai-requests-improvements
nefeline Dec 14, 2023
883bf5b
Address lint errors.
nefeline Dec 14, 2023
d1c7034
Update array formatting
nefeline Dec 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions plugins/woocommerce/patterns/hero-product-3-split.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

use Automattic\WooCommerce\Blocks\Patterns\PatternsHelper;

$main_title = $content['titles'][0]['default'] ?? '';
$first_title = $content['titles'][1]['default'] ?? '';
$second_title = $content['titles'][2]['default'] ?? '';
$third_title = $content['titles'][3]['default'] ?? '';
$main_title = $content['titles'][3]['default'] ?? '';
$first_title = $content['titles'][0]['default'] ?? '';
$second_title = $content['titles'][1]['default'] ?? '';
$third_title = $content['titles'][2]['default'] ?? '';

$first_description = $content['descriptions'][0]['default'] ?? '';
$second_description = $content['descriptions'][1]['default'] ?? '';
Expand Down
8 changes: 4 additions & 4 deletions plugins/woocommerce/patterns/testimonials-3-columns.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

use Automattic\WooCommerce\Blocks\Patterns\PatternsHelper;

$main_header = $content['titles'][0]['default'] ?? '';
$first_review = $content['titles'][1]['default'] ?? '';
$second_review = $content['titles'][2]['default'] ?? '';
$third_review = $content['titles'][3]['default'] ?? '';
$main_header = $content['titles'][3]['default'] ?? '';
$first_review = $content['titles'][0]['default'] ?? '';
$second_review = $content['titles'][1]['default'] ?? '';
$third_review = $content['titles'][2]['default'] ?? '';
$first_description = $content['descriptions'][0]['default'] ?? '';
$second_description = $content['descriptions'][1]['default'] ?? '';
$third_description = $content['descriptions'][2]['default'] ?? '';
Expand Down
41 changes: 26 additions & 15 deletions plugins/woocommerce/src/Blocks/AI/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,29 @@ class Connection {
* @param string $token The JWT token.
* @param string $prompt The prompt to send to the API.
* @param int $timeout The timeout for the request.
* @param string $response_format The response format.
*
* @return mixed
*/
public function fetch_ai_response( $token, $prompt, $timeout = 15 ) {
public function fetch_ai_response( $token, $prompt, $timeout = 15, $response_format = null ) {
if ( $token instanceof \WP_Error ) {
return $token;
}

$body = array(
'feature' => 'woocommerce_blocks_patterns',
'prompt' => $prompt,
'token' => $token,
);

if ( $response_format ) {
$body['response_format'] = $response_format;
}

$response = wp_remote_post(
self::TEXT_COMPLETION_API_URL,
array(
'body' =>
array(
'feature' => 'woocommerce_blocks_patterns',
'prompt' => $prompt,
'token' => $token,
),
'body' => $body,
'timeout' => $timeout,
)
);
Expand All @@ -56,27 +62,32 @@ public function fetch_ai_response( $token, $prompt, $timeout = 15 ) {
* @param string $token The JWT token.
* @param array $prompts The prompts to send to the API.
* @param int $timeout The timeout for the request.
* @param string $response_format The response format.
*
* @return array|WP_Error The responses or a WP_Error object.
*/
public function fetch_ai_responses( $token, array $prompts, $timeout = 15 ) {
public function fetch_ai_responses( $token, array $prompts, $timeout = 15, $response_format = null ) {
if ( $token instanceof \WP_Error ) {
return $token;
}

$requests = array();
foreach ( $prompts as $prompt ) {
$data = array(
'feature' => 'woocommerce_blocks_patterns',
'prompt' => $prompt,
'token' => $token,
);

if ( $response_format ) {
$data['response_format'] = $response_format;
}

$requests[] = array(
'url' => self::TEXT_COMPLETION_API_URL,
'type' => 'POST',
'headers' => array( 'Content-Type' => 'application/json; charset=utf-8' ),
'data' => wp_json_encode(
array(
'feature' => 'woocommerce_blocks_patterns',
'prompt' => $prompt,
'token' => $token,
)
),
'data' => wp_json_encode( $data ),
);
}

Expand Down
6 changes: 3 additions & 3 deletions plugins/woocommerce/src/Blocks/Images/Pexels.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function get_images( $ai_connection, $token, $business_description ) {
return new \WP_Error( 'woocommerce_no_images_found', __( 'No images found.', 'woocommerce' ) );
}

return $refined_images;
return array( 'images' => $refined_images, 'search_term' => $search_term );
}

/**
Expand All @@ -108,7 +108,7 @@ public function get_images( $ai_connection, $token, $business_description ) {
* @return mixed|\WP_Error
*/
private function define_search_term( $ai_connection, $token, $business_description ) {
$prompt = sprintf( 'Based on the description "%s", generate one word that precisely describe what this business is selling. The returned word should be as accurate as possible to describe the products sold, do not include any adjectives or descriptions of the qualities of the product. The generated word must exist in the english dictionary and not be a proper name, the returned word should be simple, do not add any explanations.', $business_description );
$prompt = sprintf( 'You are a teacher. Based on the following business description, \'%s\', describe to a child exactly what this store is selling in one or two words and be as precise as you can possibly be. Do not reply with generic words that could cause confusion and be associated with other businesses as a response. Make sure you do not add double quotes in your response. Do not add any explanations in the response', $business_description );

$response = $ai_connection->fetch_ai_response( $token, $prompt, 30 );

Expand Down Expand Up @@ -141,7 +141,7 @@ private function refine_returned_images_results( $ai_connection, $token, $busine
}
}

$prompt = sprintf( 'Compare the following business description: "%s" with the image titles listed in the JSON below: if the image is not aligned with what the business is selling, delete the description from the list. Do not include any explanations or introductions to the response. The JSON is: %s', $business_description, wp_json_encode( $image_titles ) );
$prompt = sprintf( 'Given that you own a store described as "%s", remove from the following JSON all titles that do not represent products that could be sold on your store: %s', $business_description, wp_json_encode( $image_titles ) );

$response = $ai_connection->fetch_ai_response( $token, $prompt, 30 );

Expand Down
18 changes: 9 additions & 9 deletions plugins/woocommerce/src/Blocks/Patterns/PatternUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ class PatternUpdater {
* @return bool|WP_Error
*/
public function generate_content( $ai_connection, $token, $images, $business_description ) {
if ( empty( $images ) ) {
return new \WP_Error( 'images_not_found', __( 'No images provided for generating AI content.', 'woocommerce' ) );
}

if ( is_wp_error( $images ) ) {
return $images;
}
Expand All @@ -48,6 +44,10 @@ public function generate_content( $ai_connection, $token, $images, $business_des
return $token;
}

if ( ! isset( $images['images'] ) ) {
return new \WP_Error( 'images_not_found', __( 'No images provided for generating AI content.', 'woo-gutenberg-products-block' ) );
}

$last_business_description = get_option( 'last_business_description_with_ai_content_generated' );

if ( $last_business_description === $business_description ) {
Expand All @@ -58,11 +58,11 @@ public function generate_content( $ai_connection, $token, $images, $business_des
}
}

if ( 0 === count( $images ) ) {
if ( 0 === count( $images['images'] ) ) {
$images = get_transient( 'woocommerce_ai_managed_images' );
}

if ( empty( $images ) ) {
if ( empty( $images['images'] ) ) {
return new WP_Error( 'no_images_found', __( 'No images found.', 'woocommerce' ) );
}

Expand All @@ -75,7 +75,7 @@ public function generate_content( $ai_connection, $token, $images, $business_des
return $patterns_dictionary;
}

$patterns = $this->assign_selected_images_to_patterns( $patterns_dictionary, $images );
$patterns = $this->assign_selected_images_to_patterns( $patterns_dictionary, $images['images'] );

if ( is_wp_error( $patterns ) ) {
return new WP_Error( 'failed_to_set_pattern_images', __( 'Failed to set the pattern images.', 'woocommerce' ) );
Expand Down Expand Up @@ -218,7 +218,7 @@ private function format_prompts_for_ai( array $prompts, string $business_descrip
$formatted_prompts = [];
foreach ( $prompts as $prompt ) {
$formatted_prompts[] = sprintf(
"Given the following description '%s' generate long texts for the sections using the following prompts for each one of them: `'%s'`. Ensure each entry is unique and does not repeat the given examples. The response should be an array of data in JSON format. Each element should be an object with the pattern name as the key, and the generated content as values. Do not include backticks or the word json in the response. Here's an example format: `'%s'`",
"You are an experienced writer. Given a business described as: '%s', generate content for the sections using the following prompts for each one of them: `'%s'`, always making sure that the expected number of characters is respected. The response should be an array of data in JSON format. Each element should be an object with the pattern name as the key, and the generated content as values. Here's an example format: `'%s'`",
$business_description,
wp_json_encode( $prompt ),
wp_json_encode( $expected_results_format[ $i ] )
Expand All @@ -245,7 +245,7 @@ private function fetch_and_validate_ai_responses( $ai_connection, $token, $forma
$success = false;
while ( $ai_request_retries < 5 && ! $success ) {
$ai_request_retries ++;
$ai_responses = $ai_connection->fetch_ai_responses( $token, $formatted_prompts, 60 );
$ai_responses = $ai_connection->fetch_ai_responses( $token, $formatted_prompts, 60, 'json_object' );
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


if ( is_wp_error( $ai_responses ) ) {
continue;
Expand Down
46 changes: 0 additions & 46 deletions plugins/woocommerce/src/Blocks/Patterns/PatternsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,52 +8,6 @@
* Pattern Images Helper class.
*/
class PatternsHelper {
/**
* Returns the pattern content.
*
* @param string $pattern_slug The pattern slug.
*
* @return array The pattern content.
*/
public static function get_pattern_content( string $pattern_slug ) {
$pattern = self::get_patterns_dictionary( $pattern_slug );

if ( empty( $pattern ) ) {
return array();
}

if ( ! isset( $pattern['content'] ) ) {
return array();
}

return $pattern['content'];
}

/**
* Returns the pattern images.
*
* @param string $pattern_slug The pattern slug.
*
* @return array The pattern images.
*/
public static function get_pattern_images( string $pattern_slug ): array {
$pattern = self::get_patterns_dictionary( $pattern_slug );

if ( empty( $pattern ) ) {
return array();
}

if ( ! isset( $pattern['images'] ) ) {
return array();
}

if ( ! isset( $pattern['images_total'] ) ) {
return array();
}

return $pattern['images'];
}

/**
* Returns the image for the given pattern.
*
Expand Down