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

move to new sitegen with most stuff on worker #12

Merged
merged 1 commit into from
Dec 18, 2023
Merged
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
73 changes: 57 additions & 16 deletions includes/SiteGen/SiteGen.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,16 +341,7 @@ public static function generate_site_meta( $site_info, $identifier, $skip_cache
* @param array $target_audience Generated target audience.
* @param boolean $regenerate If we need to regenerate.
*/
public static function get_home_pages( $site_description, $content_style, $target_audience, $regenerate = true ) {
$generated_content_structures = self::generate_site_meta(
array( 'site_description' => $site_description ),
'contentstructure'
);

// If we got an error, return that right away
if ( array_key_exists( 'error', $generated_content_structures ) ) {
return $generated_content_structures;
}
public static function get_home_pages( $site_description, $content_style, $target_audience, $regenerate = false ) {

// Check if we have the response in cache already
if ( ! $regenerate ) {
Expand All @@ -359,14 +350,64 @@ public static function get_home_pages( $site_description, $content_style, $targe
return $generated_homepages;
}
}

$generated_content_structures = self::get_sitegen_from_cache(
'contentstructure'
);
$keywords = self::generate_site_meta(
array(
'site_description' => $site_description,
'content_style' => $content_style,
),
'keywords'
);
if ( ! $generated_content_structures ) {
$response = wp_remote_post(
NFD_AI_BASE . 'generatePageContent',
array(
'headers' => array(
'Content-Type' => 'application/json',
),
'timeout' => 60,
'body' => wp_json_encode(
array(
'hiivetoken' => HiiveConnection::get_auth_token(),
'prompt' => array(
'site_description' => $site_description,
'keywords' => wp_json_encode( $keywords ),
'content_style' => wp_json_encode( $content_style ),
'target_audience' => wp_json_encode( $target_audience ),
),
'page' => 'home',
)
),
)
);
$response_code = wp_remote_retrieve_response_code( $response );
if ( 200 !== $response_code ) {
if ( 400 === $response_code ) {
$error = json_decode( wp_remote_retrieve_body( $response ), true );
return array(
'error' => $error['payload']['reason'],
);
}
return array(
'error' => __( 'We are unable to process the request at this moment' ),
);
}
$parsed_response = json_decode( wp_remote_retrieve_body( $response ), true );
$generated_content_structures = $parsed_response['contentStructures'];
$generated_patterns = $parsed_response['generatedPatterns'];
$generated_homepages = $parsed_response['pages'];
self::cache_sitegen_response( 'contentStructures', $generated_content_structures );
self::cache_sitegen_response( 'generatedPatterns', $generated_patterns );
self::cache_sitegen_response( 'homepages', $generated_homepages );
return $generated_homepages;
}

$random_homepages = array_rand( $generated_content_structures, 3 );
$generated_homepages = array();
$generated_patterns = self::generate_pattern_content(
$site_description,
$content_style,
$target_audience,
$generated_content_structures
);
$generated_patterns = self::get_sitegen_from_cache( 'generatedPatterns' );

// Choose random categories for the generated patterns and return
foreach ( $random_homepages as $slug ) {
Expand Down
Loading