add_action('rest_api_init', function () {
register_rest_route('wow/v1', '/ask', array(
'methods' => 'POST',
'callback' => 'wow_ai_handler',
'permission_callback' => '__return_true'
));
});function wow_ai_handler($request) {$data = $request->get_json_params();
$question = $data['question'] ?? '';
$mode = $data['mode'] ?? 'general';$apiKey = "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
";$system = "You are Professor WOW, a fun, safe, kid-friendly teacher for children ages 5 to 10.
Speak in short, clear sentences. Be encouraging and simple.";if ($mode === "math") {
$system .= " Focus on math. Explain step by step.";
}
if ($mode === "writing") {
$system .= " Help with sentences and writing clearly.";
}
if ($mode === "reading") {
$system .= " Help with sounding out words and understanding reading.";
}$response = wp_remote_post("https://api.openai.com/v1/chat/completions", array(
'headers' => array(
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $apiKey
),
'body' => json_encode(array(
"model" => "gpt-4o-mini",
"messages" => array(
array("role" => "system", "content" => $system),
array("role" => "user", "content" => $question)
),
"max_tokens" => 120
))
));$body = wp_remote_retrieve_body($response);
$json = json_decode($body, true);return array(
"response" => $json["choices"][0]["message"]["content"] ?? "Try again!"
);
}
Skip to content