Skip to content

Forum in maintenance, we will back soon 🙂

AI Tools How To Get...
 
Notifications
Clear all

AI Tools How To Get different Json Response other than titles

11 Posts
4 Users
3 Reactions
212 Views
 ndam
(@google-ndam)
Posts: 11
Active Member
Topic starter
 

I am neither a developer nor a programmer i just saw Mr. Hassan's video and tried to build the AI tools as mentioned in the video I changed the prompt to generate a short story about... and when  I put the topic it gave me but titles. That is my worry. my question is how do I use the snippet code to get different outputs like blog posts or YouTube scripts for example? thanks very much for at least replying to my first question

 
Posted : 03/24/2024 5:10 am
(@husein)
Posts: 536
Member Moderator
 

@google-ndam The code snippet provided in the video is coded to generate a set of titles in a JSON format, so you can edit the prompt to generate anything in the form of titles or short sentences. However to generate a short story or something like that you're gonna need to edit the code snippet.

 
Posted : 03/24/2024 9:25 am
Hasan Aboul Hasan
(@admin)
Posts: 1257
Member Admin
 

you need basic text response, check this guide: https://learnwithhasan.com/create-ai-tools-with-wordpress/

 
Posted : 03/24/2024 1:11 pm
 ndam
(@google-ndam)
Posts: 11
Active Member
Topic starter
 

@admin i am using but Google API key

how can i point this code to use it thanks

 

function openai_generate_text() {
    // Get the topic from the AJAX request
    $prompt = $_POST['prompt'];

    // OpenAI API URL and key
    $api_url = 'https://api.openai.com/v1/chat/completions';
    $api_key = 'sk-XXX';  // Replace with your actual OpenAI API key

    // Headers for the OpenAI API
    $headers = [
        'Content-Type' => 'application/json',
        'Authorization' => 'Bearer ' . $api_key
    ];

    // Body for the OpenAI API
    $body = [
        'model' => 'gpt-3.5-turbo',
        'messages' => [['role' => 'user', 'content' => $prompt]],
        'temperature' => 0.7
    ];

    // Args for the WordPress HTTP API
    $args = [
        'method' => 'POST',
        'headers' => $headers,
        'body' => json_encode($body),
        'timeout' => 120
    ];

    // Send the request
    $response = wp_remote_request($api_url, $args);

    // Handle the response
    if (is_wp_error($response)) {
        $error_message = $response->get_error_message();
        wp_send_json_error("Something went wrong: $error_message");
    } else {
        $body = wp_remote_retrieve_body($response);
        $data = json_decode($body, true);

        if (json_last_error() !== JSON_ERROR_NONE) {
            wp_send_json_error('Invalid JSON in API response');
        } elseif (!isset($data['choices'])) {
            wp_send_json_error('API request failed. Response: ' . $body);
        } else {
            wp_send_json_success($data);
        }
    }

    // Always die in functions echoing AJAX content
   wp_die();
}

add_action('wp_ajax_openai_generate_text', 'openai_generate_text');
add_action('wp_ajax_nopriv_openai_generate_text', 'openai_generate_text');
 
Posted : 03/25/2024 10:22 am
 ndam
(@google-ndam)
Posts: 11
Active Member
Topic starter
 

@admin how can i point the js code to use google API since you were using openai API in the example thanks

 
Posted : 03/25/2024 10:26 am
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
 

@google-ndam you change the API in the code to follow the Google API documentation.

Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack

 
Posted : 03/25/2024 12:53 pm
 ndam
(@google-ndam)
Posts: 11
Active Member
Topic starter
 

@husein please help me change this api from openai to google i waish to uase the free google api key instead thanks

function openai_generate_text() {
    // Get the topic from the AJAX request
    $prompt = $_POST['prompt'];

    // OpenAI API URL and key
    $api_url = 'https://api.openai.com/v1/chat/completions';
    $api_key = 'sk-XXX';  // Replace with your actual OpenAI API key

    // Headers for the OpenAI API
    $headers = [
        'Content-Type' => 'application/json',
        'Authorization' => 'Bearer ' . $api_key
    ];

    // Body for the OpenAI API
    $body = [
        'model' => 'gpt-3.5-turbo',
        'messages' => [['role' => 'user', 'content' => $prompt]],
        'temperature' => 0.7
    ];

    // Args for the WordPress HTTP API
    $args = [
        'method' => 'POST',
        'headers' => $headers,
        'body' => json_encode($body),
        'timeout' => 120
    ];

    // Send the request
    $response = wp_remote_request($api_url, $args);

    // Handle the response
    if (is_wp_error($response)) {
        $error_message = $response->get_error_message();
        wp_send_json_error("Something went wrong: $error_message");
    } else {
        $body = wp_remote_retrieve_body($response);
        $data = json_decode($body, true);

        if (json_last_error() !== JSON_ERROR_NONE) {
            wp_send_json_error('Invalid JSON in API response');
        } elseif (!isset($data['choices'])) {
            wp_send_json_error('API request failed. Response: ' . $body);
        } else {
            wp_send_json_success($data);
        }
    }

    // Always die in functions echoing AJAX content
   wp_die();
}

add_action('wp_ajax_openai_generate_text', 'openai_generate_text');
add_action('wp_ajax_nopriv_openai_generate_text', 'openai_generate_text');
 
Posted : 03/26/2024 11:19 am
(@husein)
Posts: 536
Member Moderator
 

@google-ndam Why are you spamming? You can use ChatGPT or Google Gemini UIs to help you convert the code. Try doing that and if it didn't work tell use where is your problem

 
Posted : 03/26/2024 12:43 pm
 ndam
(@google-ndam)
Posts: 11
Active Member
Topic starter
 

@husein Sorry for the spammy questions will not ask questions again nice day

 
Posted : 03/26/2024 1:41 pm
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
 

@google-ndam you can ask questions of the type, here is what I've tried but the result wasn't what I expected; can you help me determine why? The spam is your repeated requests for the same things. This forum doesn't offer "do it for me" type of posts because our time is precious.

Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack

 
Posted : 03/26/2024 2:47 pm
 ndam
(@google-ndam)
Posts: 11
Active Member
Topic starter
 

@ssadvisor Thanks For The Clarification. It makes sense to me now and sorry for overreacting. now I know the rules

 
Posted : 03/27/2024 4:51 am
Share: