-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai_analysis.php
More file actions
97 lines (82 loc) · 2.81 KB
/
ai_analysis.php
File metadata and controls
97 lines (82 loc) · 2.81 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
<?php
/**
* Proxy pour l'API LLM locale
* Relaie les requêtes du frontend vers le serveur LLM interne (192.168.1.120)
*/
header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');
// Handle preflight
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(204);
exit;
}
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['error' => 'Method not allowed']);
exit;
}
// Read and validate input
$input = file_get_contents('php://input');
$data = json_decode($input, true);
if (!$data || !isset($data['messages']) || !is_array($data['messages'])) {
http_response_code(400);
echo json_encode(['error' => 'Invalid request: messages array required']);
exit;
}
// Build request for local LLM
$llmPayload = json_encode([
'model' => $data['model'] ?? 'qwen2.5-coder:7b',
'messages' => $data['messages']
]);
// Measure time
$startTime = microtime(true);
// Call local LLM server
$ch = curl_init('http://192.168.1.120/v1/chat/completions');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $llmPayload,
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 120,
CURLOPT_CONNECTTIMEOUT => 10
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlError = curl_error($ch);
curl_close($ch);
$elapsed = round(microtime(true) - $startTime, 1);
if ($curlError) {
http_response_code(502);
echo json_encode(['error' => 'LLM server unreachable: ' . $curlError]);
exit;
}
// Parse response and inject timing + token estimation
$responseData = json_decode($response, true);
if ($responseData && isset($responseData['choices'][0]['message']['content'])) {
// Estimate tokens (~4 chars per token for French text)
$promptText = '';
foreach ($data['messages'] as $msg) {
$promptText .= $msg['content'] ?? '';
}
$promptTokens = (int) ceil(mb_strlen($promptText) / 4);
$completionText = $responseData['choices'][0]['message']['content'];
$completionTokens = (int) ceil(mb_strlen($completionText) / 4);
// Add usage and timing info
$responseData['usage'] = [
'prompt_tokens' => $promptTokens,
'completion_tokens' => $completionTokens,
'total_tokens' => $promptTokens + $completionTokens
];
$responseData['timing'] = [
'elapsed_seconds' => $elapsed,
'tokens_per_second' => $elapsed > 0 ? round($completionTokens / $elapsed, 1) : 0
];
http_response_code($httpCode);
echo json_encode($responseData);
} else {
// Pass through as-is if we can't parse
http_response_code($httpCode);
echo $response;
}