-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample-FaqController.php
More file actions
121 lines (102 loc) · 4.5 KB
/
Copy pathexample-FaqController.php
File metadata and controls
121 lines (102 loc) · 4.5 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
require_once __DIR__ . '/../models/FaqModel.php';
class FaqController {
private $model;
private $openaiApiKey;
private $openaiBaseUrl;
public function __construct() {
$this->model = new FaqModel();
$env = parse_ini_file(__DIR__ . '/../../.env');
$this->openaiApiKey = $env['OPENAI_API_KEY'];
$this->openaiBaseUrl = $env['OPENAI_BASE_URL'] ?? "https://generativelanguage.googleapis.com/v1/models/gemini-2.0-flash:generateContent";
}
public function handleRequest() {
if (isset($_GET['question'])) {
$this->handleQuestion();
} else if (isset($_GET['getAll']) && $_GET['getAll'] === 'true') {
$this->getAllFaqs();
}
}
private function handleQuestion() {
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
header('Connection: keep-alive');
header('X-Accel-Buffering: no');
$userQuestion = $_GET['question'];
$knowledgeBase = $this->model->getKnowledgeBase();
try {
$systemPrompt = "
Sen yardımcı bir asistansın. Yalnızca bu bilgilerden faydalanarak, Fırat Üniversitesi hakkında en uygun cevabı ver. Vereceğin cevap 5 cümleden fazla olmasın. Eğer bilgi yoksa \"Cevap bulunamadı.\" yaz. Sıkça sorulan sorular ve cevapları:
{$knowledgeBase}";
$url = $this->openaiBaseUrl . "?key=" . $this->openaiApiKey;
$data = [
'contents' => [
[
'parts' => [
['text' => $systemPrompt],
['text' => $userQuestion]
]
]
],
'generationConfig' => [
'temperature' => 0.7,
'topK' => 40,
'topP' => 0.95,
'maxOutputTokens' => 1024,
]
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json'
]);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) {
static $fullAnswer = "";
$responseData = json_decode($data, true);
if ($responseData && isset($responseData['candidates'][0]['content']['parts'][0]['text'])) {
$content = $responseData['candidates'][0]['content']['parts'][0]['text'];
$fullAnswer = $content;
echo 'data: ' . json_encode(['answer' => $fullAnswer, 'complete' => true]) . "\n\n";
ob_flush();
flush();
}
return strlen($data);
});
$response = curl_exec($ch);
if (curl_errno($ch)) {
throw new Exception('cURL Hatası: ' . curl_error($ch));
}
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpcode != 200) {
if (!headers_sent()) {
http_response_code($httpcode);
echo 'data: ' . json_encode(['error' => 'API HTTP Hatası: ' . $httpcode, 'complete' => true]) . "\n\n";
} else {
echo 'data: ' . json_encode(['error' => 'API HTTP Hatası: ' . $httpcode, 'complete' => true]) . "\n\n";
}
ob_flush();
flush();
}
curl_close($ch);
} catch (Exception $e) {
error_log('API hatası: ' . $e->getMessage());
if (!headers_sent()) {
http_response_code(500);
header('Content-Type: application/json');
echo json_encode(['success' => false, 'error' => 'Sunucu hatası: ' . $e->getMessage()]);
} else {
echo 'data: ' . json_encode(['error' => 'Sunucu hatası: ' . $e->getMessage(), 'complete' => true]) . "\n\n";
ob_flush();
flush();
}
}
}
private function getAllFaqs() {
header('Content-Type: application/json');
$faqs = $this->model->getAllFaqs();
echo json_encode(["success" => true, "data" => $faqs]);
}
}