35428-vm/api/generate_personas.php
Flatlogic Bot 2e3424ad5c v.2
2025-11-02 19:42:55 +00:00

135 lines
4.1 KiB
PHP

<?php
// api/generate_personas.php
require_once __DIR__ . '/../db/config.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;
}
// ... (Anthropic API call logic remains the same)
$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);
echo json_encode(['error' => 'Failed to communicate with AI service.', 'details' => json_decode($response)]);
exit;
}
$result = json_decode($response, true);
$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;
}
$personas_data = json_decode($content_json, true);
if (json_last_error() !== JSON_ERROR_NONE || !isset($personas_data['personas'])) {
http_response_code(500);
echo json_encode(['error' => 'Could not parse personas from AI response.', 'raw_content' => $content_json]);
exit;
}
// --- Database Integration ---
try {
$pdo = db();
// 1. Create a new session
$stmt = $pdo->prepare("INSERT INTO sessions (target_audience, business_idea) VALUES (?, ?)");
$stmt->execute([$audience, $idea]);
$session_id = $pdo->lastInsertId();
// 2. Save each persona
$saved_personas = [];
$persona_stmt = $pdo->prepare(
"INSERT INTO personas (session_id, name, age, occupation, traits, concerns, style) VALUES (?, ?, ?, ?, ?, ?, ?)"
);
foreach ($personas_data['personas'] as $p) {
$persona_stmt->execute([
$session_id,
$p['name'],
$p['age'],
$p['occupation'],
$p['traits'],
$p['concerns'],
$p['style']
]);
$p['id'] = $pdo->lastInsertId(); // Add the new DB ID to the persona object
$saved_personas[] = $p;
}
// 3. Return the session ID and the updated persona data
echo json_encode([
'session_id' => $session_id,
'personas' => $saved_personas
]);
} catch (Exception $e) {
http_response_code(500);
// In a real app, log this error instead of echoing it.
echo json_encode(['error' => 'Database error while saving session.', 'details' => $e->getMessage()]);
exit;
}