100 lines
3.1 KiB
PHP
100 lines
3.1 KiB
PHP
<?php
|
|
// api/generate_personas.php
|
|
|
|
require_once __DIR__ . '/config.php';
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
http_response_code(405);
|
|
echo json_encode(['error' => 'Method Not Allowed']);
|
|
exit;
|
|
}
|
|
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
$audience = $input['audience'] ?? '';
|
|
$idea = $input['idea'] ?? '';
|
|
|
|
if (empty($audience) || empty($idea)) {
|
|
http_response_code(400);
|
|
echo json_encode(['error' => 'Audience and Idea are required.']);
|
|
exit;
|
|
}
|
|
|
|
if (ANTHROPIC_API_KEY === 'YOUR_ANTHROPIC_API_KEY_HERE') {
|
|
http_response_code(500);
|
|
echo json_encode(['error' => 'Anthropic API key is not configured. Please add it to api/config.php.']);
|
|
exit;
|
|
}
|
|
|
|
$prompt = "Based on the following business idea and target audience in Egypt/MENA, create 3 distinct user personas.
|
|
|
|
Target Audience: "$audience"
|
|
Business Idea: "$idea"
|
|
|
|
For each persona, provide:
|
|
- A culturally relevant Egyptian/MENA name.
|
|
- Age and a realistic occupation in the Egyptian market.
|
|
- 2-3 key personality traits.
|
|
- Main concerns and priorities relevant to the Egyptian/MENA context.
|
|
- A typical communication style.
|
|
|
|
Please return the response as a JSON object with a single key 'personas' which is an array of the 3 persona objects. Do not include any other text or explanation outside of the JSON object.
|
|
Each persona object in the array should have the following keys: 'name', 'age', 'occupation', 'traits', 'concerns', 'style'.";
|
|
|
|
$data = [
|
|
'model' => 'claude-3-sonnet-20240229',
|
|
'max_tokens' => 2048,
|
|
'messages' => [
|
|
[
|
|
'role' => 'user',
|
|
'content' => $prompt,
|
|
],
|
|
],
|
|
];
|
|
|
|
$ch = curl_init('https://api.anthropic.com/v1/messages');
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
'x-api-key: ' . ANTHROPIC_API_KEY,
|
|
'anthropic-version: 2023-06-01',
|
|
'content-type: application/json',
|
|
]);
|
|
|
|
$response = curl_exec($ch);
|
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
|
|
if ($http_code !== 200) {
|
|
http_response_code(502);
|
|
// Forwarding a sanitized error is better for debugging without exposing too much.
|
|
echo json_encode(['error' => 'Failed to communicate with AI service.', 'details' => json_decode($response)]);
|
|
exit;
|
|
}
|
|
|
|
$result = json_decode($response, true);
|
|
|
|
// The response from Claude is inside content block.
|
|
$content_json = $result['content'][0]['text'] ?? null;
|
|
|
|
if (!$content_json) {
|
|
http_response_code(500);
|
|
echo json_encode(['error' => 'Invalid response format from AI service.']);
|
|
exit;
|
|
}
|
|
|
|
// The actual persona data is expected to be a JSON string.
|
|
// We need to decode it one more time.
|
|
$personas_data = json_decode($content_json, true);
|
|
|
|
if (json_last_error() !== JSON_ERROR_NONE || !isset($personas_data['personas'])) {
|
|
// Fallback if the AI didn't return perfect JSON
|
|
http_response_code(500);
|
|
echo json_encode(['error' => 'Could not parse personas from AI response.', 'raw_content' => $content_json]);
|
|
exit;
|
|
}
|
|
|
|
echo json_encode($personas_data);
|