Skip to content

Forum in maintenance, we will back soon 🙂

AI Tools With WordP...
 
Notifications
Clear all

AI Tools With WordPress in 5 Minutes not working: string did not match the expected pattern

10 Posts
3 Users
2 Reactions
129 Views
(@google-filmsoundacademy)
Posts: 5
Active Member
Topic starter
 

Hi! hope you can help me. I followed instructions on the video but I'm getting this message: An error occurred: The string did not match the expected pattern.
how  can I fix it?
Thanks!

 
Posted : 05/13/2024 9:10 pm
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
 

@google-filmsoundacademy is this "Solved" or do you still need help?

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

 
Posted : 05/14/2024 1:19 am
(@google-filmsoundacademy)
Posts: 5
Active Member
Topic starter
 

Hi. I still have the issue. Did you provide a solution? can't see anything here. 

 
Posted : 05/14/2024 10:31 am
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
 

@google-filmsoundacademy you need to provide your code. You can take a screenshot or preferably provide a link to a public GitHub repository.

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

 
Posted : 05/14/2024 1:31 pm
(@google-filmsoundacademy)
Posts: 5
Active Member
Topic starter
 

I just followed the steps on the website/video. The only thing I changed was the API key

For html/java

<div id="text-generation-tool">
    <input type="text" id="topic" placeholder="Your Topic...">
    <button id="generate-button">Generate Story!</button>
    <div id="result-container" style="display: none;">
        <div class="result-wrapper">
            <div class="result-content">
                <textarea id="result" readonly></textarea>
            </div>
            <div class="copy-button-container">
                <button id="copy-button">Copy</button>
            </div>
        </div>
    </div>
    <div id="loading" class="loader" style="display: none;"></div>
</div>

<style>
    /* Basic styles for the text generation tool */
    #text-generation-tool {
        width: 100%;
        max-width: 600px;
        margin: 0 auto;
        font-family: Arial, sans-serif;
    }

    #topic {
        width: 100%;
        padding: 15px;
        margin-bottom: 20px;
        font-size: 16px;
        border-radius: 5px;
        border: 1px solid #ddd;
    }

    #generate-button {
        display: block;
        width: 100%;
        padding: 15px;
        margin-bottom: 20px;
        font-size: 16px;
        border: none;
        border-radius: 5px;
        color: #fff;
        background-color: #3498db;
        cursor: pointer;
        transition: background-color 0.3s ease;
    }

    #generate-button:hover {
        background-color: #2980b9;
    }

    #result-container {
        display: none;
        margin-bottom: 20px;
    }

    .result-wrapper {
        position: relative;
        overflow: hidden;
    }

    .result-content {
        display: flex;
    }

    #result {
        flex: 1;
        height: 400px;
        padding: 15px;
        font-size: 16px;
        border-radius: 5px;
        border: 1px solid #ddd;
        background-color: #f9f9f9;
    }

    .copy-button-container {
        margin-top: 10px;
        text-align: right;
    }

    #copy-button {
        padding: 8px 12px;
        font-size: 14px;
        border: none;
        border-radius: 5px;
        color: #fff;
        background-color: #3498db;
        cursor: pointer;
        transition: background-color 0.3s ease;
    }

    #copy-button:hover {
        background-color: #2980b9;
    }

    /* CSS for the loader */
    .loader {
        display: block;
        margin: 50px auto;
        border: 16px solid #f3f3f3; /* Light grey */
        border-top: 16px solid #3498db; /* Blue */
        border-radius: 50%;
        width: 50px;
        height: 50px;
        animation: spin 1s linear infinite;
    }

    @keyframes spin {
        0% { transform: rotate(0deg); }
        100% { transform: rotate(360deg); }
    }
</style>

<script>
    document.getElementById("generate-button").addEventListener("click", function(e){
        e.preventDefault();
        
        
        var generateButton = document.getElementById("generate-button");
        
        if (generateButton.disabled) {
            return; // Prevent multiple clicks while content is being generated
        }
        
        generateButton.disabled = true;
        
        
        
        var topic = document.getElementById('topic').value;
        var prompt = "Generate a 3 sentence story about " + topic;
        var loading = document.getElementById('loading');
        var result = document.getElementById('result');
        var resultC = document.getElementById('result-container');

        


        

        loading.style.display = 'block';
        result.style.display = 'none'; // hide result textarea
        resultC.style.display = 'none';
        

        var formData = new FormData();
        formData.append('action', 'openai_generate_text');
        formData.append('prompt', prompt);

        fetch('/wp-admin/admin-ajax.php', {
            method: 'POST',
            body: formData
        })
        .then(response => response.json())
        .then(data => {
            loading.style.display = 'none';
            if(data.success) {
                result.value = data.data.choices[0].message.content; 
                result.style.display = 'block'; // show result textarea
                resultC.style.display = 'block';
                generateButton.disabled = false;
            } else {
                result.value = 'An error occurred: ' + data.data;
                result.style.display = 'block'; // show result textarea
                resultC.style.display = 'block';
                generateButton.disabled = false;
            }
        })
        .catch(error => {
            loading.style.display = 'none';
            result.value = 'An error occurred: ' + error.message;
            result.style.display = 'block'; // show result textarea
            resultC.style.display = 'block';
            generateButton.disabled = false;
            
        });
    });
    
    var copyButton = document.getElementById('copy-button');
    copyButton.addEventListener('click', function() {
        var result = document.getElementById('result');
        result.select();
        document.execCommand('copy');
        alert('Copied to clipboard!');
    });
</script>

For functions.php

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 : 05/14/2024 3:10 pm
Hasan Aboul Hasan
(@admin)
Posts: 1252
Member Admin
 

Do you have active credits in OpenAI ?

did you check the browser console to see the error?

 
Posted : 05/15/2024 8:42 am
SSAdvisor reacted
(@google-filmsoundacademy)
Posts: 5
Active Member
Topic starter
 

@admin To be sincere, I don't even know if I have active credits. I was hoping that all I have to do was to follow the instructions on your video and all would be good. I'm not a programmer 😐 

I think I'll leave it after all. 

Thanks anyway 🙂 🙂 

 
Posted : 05/15/2024 2:23 pm
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
 

@google-filmsoundacademy

Posted by: @google-filmsoundacademy

I'm not a programmer

Are you good with scenarios of "if this happens then this should happen?" If yes, then you ARE a programmer. As for OpenAI credits see this, https://help.openai.com/en/articles/6643435-how-do-i-get-more-tokens-or-increase-my-monthly-usage-limits

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

 
Posted : 05/15/2024 2:45 pm
(@google-filmsoundacademy)
Posts: 5
Active Member
Topic starter
 

@ssadvisor Thanks. I checked this and the page might as well be in Chinese. I can handle "if... then..." to a certain extent. But understanding these credits is going to require a bit of a learning curve. Will have another go at the script once I understand more about these credits. I'm on a paid ChatGPT account but I don't suppose that entitles me to credits. It's probably why the script not working. 
Thanks for pointing this out.

 
Posted : 05/16/2024 1:15 pm
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
 

@google-filmsoundacademy the monthly subscription doesn't fund the API usage. The API token count usage is billed as you use it but you have to pre-purchase the tokens. The tokens are rather cheap to use but you do have to fund them up front.

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

 
Posted : 05/16/2024 2:13 pm
Share: