-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.php
More file actions
73 lines (59 loc) · 2.41 KB
/
Copy pathchat.php
File metadata and controls
73 lines (59 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
// chat.php
// 1. Set your OpenAI API key here (or read it from environment/config)
$OPENAI_API_KEY = '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'; // replace this
// 2. Read JSON input from the browser
$input = json_decode(file_get_contents('php://input'), true);
$userMessage = isset($input['message']) ? trim($input['message']) : '';
header('Content-Type: application/json');
if (!$userMessage) {
echo json_encode(['error' => 'No message provided']);
exit;
}
// 3. Build the messages array (personality + user question)
$messages = [
[
'role' => 'system',
'content' => 'You are “Ask Ramdas AI”, the official portfolio assistant for Senior UX/UI Designer Ramdas Ware. ' .
'You answer questions about his skills, experience, projects, and UX process in a concise, professional tone. ' .
'Speak in first person as Ramdas (e.g., "I led...", "I worked on..."). ' .
'If the user asks about unrelated topics, politely bring the conversation back to Ramdas’s work and experience.'
],
[
'role' => 'user',
'content' => $userMessage
]
];
// 4. Prepare the OpenAI API request
$payload = [
'model' => 'gpt-4.1-mini', // or another chat model available to you
'messages' => $messages
];
$ch = curl_init('https://api.openai.com/v1/chat/completions');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $OPENAI_API_KEY
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
// 5. Send request to OpenAI
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($response === false) {
echo json_encode(['error' => 'Curl error: ' . curl_error($ch)]);
curl_close($ch);
exit;
}
curl_close($ch);
$data = json_decode($response, true);
if ($httpcode !== 200 || !isset($data['choices'][0]['message']['content'])) {
echo json_encode([
'error' => 'OpenAI API error',
'details' => $data
]);
exit;
}
$reply = $data['choices'][0]['message']['content'];
// 6. Return JSON to the browser
echo json_encode(['reply' => $reply]);