36584-vm/api/index.php
2025-12-02 15:04:45 +00:00

83 lines
3.6 KiB
PHP

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
session_start();
header('Content-Type: application/json');
require_once __DIR__ . '/../ai/LocalAIApi.php';
require_once __DIR__ . '/../includes/pexels.php';
$requestBody = file_get_contents('php://input');
$data = json_decode($requestBody, true);
$query = $data['query'] ?? '';
$persona = $data['persona'] ?? 'travel_agent';
if (empty($query)) {
echo json_encode(['error' => 'Query is empty']);
exit;
}
$conversation = $_SESSION['conversation'] ?? [];
$conversation[] = ['role' => 'user', 'content' => $query];
$persona_prompts = [
'travel_agent' => 'You are a helpful travel agent specializing in Poland.',
'historian' => 'You are a historian specializing in Polish history. Your responses should be informative, detailed, and focus on the historical significance of places and events.',
'foodie' => 'You are a food critic and blogger with a passion for Polish cuisine. Your responses should be enthusiastic, descriptive, and focus on food, restaurants, and culinary experiences.',
'adventurer' => 'You are an adventure travel guide who loves exploring the wild side of Poland. Your responses should be exciting, energetic, and focus on outdoor activities, hiking, and unique experiences.'
];
$system_prompt = $persona_prompts[$persona] . ' The user is looking for: ' . $query . '. Provide a travel suggestion with a title, a short description, a 3-day itinerary, and the latitude and longitude of the location. Your response must be in JSON format, like this: {"title": "...", "description": "...", "itinerary": [{"day": 1, "activities": ["...", "..."]}, {"day": 2, "activities": ["...", "..."]}, {"day": 3, "activities": ["...", "..."]}], "location": {"lat": ..., "lng": ...}}';
$messages = array_merge([['role' => 'system', 'content' => $system_prompt]], $conversation);
$resp = LocalAIApi::createResponse(
[
'input' => $messages
]
);
if (!empty($resp['success'])) {
$text = LocalAIApi::extractText($resp);
$aiResponse = json_decode($text, true);
if (json_last_error() !== JSON_ERROR_NONE) {
// If the response is not a valid JSON, we try to extract the title and description manually
// This is a fallback mechanism
$title = "AI Generated Response";
$description = $text;
$itinerary = [];
} else {
$title = $aiResponse['title'] ?? 'AI Generated Response';
$description = $aiResponse['description'] ?? 'No description available.';
$itinerary = $aiResponse['itinerary'] ?? [];
$location = $aiResponse['location'] ?? null;
}
$conversation[] = ['role' => 'assistant', 'content' => $text];
$_SESSION['conversation'] = $conversation;
$pexelsUrl = 'https://api.pexels.com/v1/search?query=' . urlencode($title) . '&orientation=landscape&per_page=1&page=1';
$pexelsData = pexels_get($pexelsUrl);
$imageUrl = 'https://images.pexels.com/photos/1699030/pexels-photo-1699030.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1'; // Default image
if ($pexelsData && !empty($pexelsData['photos'])) {
$photo = $pexelsData['photos'][0];
$imageUrl = $photo['src']['large2x'] ?? ($photo['src']['large'] ?? $photo['src']['original']);
}
echo json_encode([
'title' => $title,
'description' => $description,
'itinerary' => $itinerary,
'location' => $location,
'image' => $imageUrl
]);
} else {
error_log('AI error: ' . ($resp['error'] ?? 'unknown'));
echo json_encode(['error' => 'Failed to get AI response']);
}