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

add function and use it to refine prompt before generating home pages #28

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
51 changes: 49 additions & 2 deletions includes/SiteGen/SiteGen.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,45 @@ private static function cache_sitegen_response( $identifier, $response ) {
update_option( NFD_SITEGEN_OPTION . '-' . strtolower( $identifier ), $response );
}

/**
* Function to trim down the promopt when it excedes a certain number of tokens
*
* @param string $prompt The user prompt
*/
private static function get_refined_prompt( $prompt ) {
// Try and see if we have the refined propmt already
$refined_propmt = get_option( NFD_SITEGEN_OPTION . '-refined-prompt', false );
if ( $refined_propmt ) {
return $refined_propmt;
} else {
$response = wp_remote_post(
NFD_AI_BASE . 'refineSiteDescription',
array(
'headers' => array(
'Content-Type' => 'application/json',
),
'timeout' => 60,
'body' => wp_json_encode(
array(
'hiivetoken' => HiiveConnection::get_auth_token(),
'prompt' => $prompt,
)
),
)
);

if ( wp_remote_retrieve_response_code( $response ) !== 200 ) {
return array(
'error' => __( 'We are unable to process the request at this moment' ),
);
}

$prompt_response = json_decode( wp_remote_retrieve_body( $response ), true );
update_option( NFD_SITEGEN_OPTION . '-refined-prompt', $prompt_response );
return $prompt_response;
}
}

/**
* Function to generate the prompt from the JSON input.
*
Expand All @@ -104,6 +143,9 @@ private static function cache_sitegen_response( $identifier, $response ) {
private static function get_prompt_from_info( $site_info ) {
$prompt = '';
foreach ( $site_info as $key => $value ) {
if ( 'site_description' === $key ) {
$value = self::get_refined_prompt( $value );
}
$prompt = $prompt . $key . ': ' . $value . ', ';
}
return $prompt;
Expand Down Expand Up @@ -164,6 +206,8 @@ private static function generate_pattern_content(
}
}

$site_description = self::get_refined_prompt( $site_description );

$keywords = self::generate_site_meta(
array(
'site_description' => $site_description,
Expand Down Expand Up @@ -357,6 +401,8 @@ public static function get_home_pages( $site_description, $content_style, $targe
);
}

$site_description = self::get_refined_prompt( $site_description );

// Check if we have the response in cache already
if ( ! $regenerate ) {
$generated_homepages = self::get_sitegen_from_cache( 'homepages' );
Expand Down Expand Up @@ -502,7 +548,8 @@ public static function get_content_for_page(
$keywords,
$page
) {
$response = wp_remote_post(
$site_description = self::get_refined_prompt( $site_description );
$response = wp_remote_post(
NFD_AI_BASE . 'generatePageContent',
array(
'headers' => array(
Expand All @@ -523,7 +570,7 @@ public static function get_content_for_page(
),
)
);
$response_code = wp_remote_retrieve_response_code( $response );
$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 );
Expand Down
Loading