Spaces:
Runtime error

File size: 2,941 Bytes
af2986e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php

namespace VibeCoding\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\HttpClient\HttpClientInterface;

class ApiController extends AbstractController
{
    private HttpClientInterface $httpClient;

    public function __construct(HttpClientInterface $httpClient)
    {
        $this->httpClient = $httpClient;
    }

    #[Route('/api/status', name: 'api_status', methods: ['GET'])]
    public function status(): JsonResponse
    {
        return $this->json([
            'status' => 'online',
            'providers' => [
                'openrouter' => 200,
                'gemini' => 1500
            ],
            'version' => '1.0.0'
        ]);
    }

    #[Route('/api/generate', name: 'api_generate', methods: ['POST'])]
    public function generate(Request $request): JsonResponse
    {
        $data = json_decode($request->getContent(), true);
        $prompt = $data['prompt'] ?? '';
        
        try {
            // Construction du message avec instructions
            $systemMessage = "Tu es un assistant de programmation. Réponds de façon claire et concise.\n\n";
            $fullPrompt = $systemMessage . "Utilisateur: " . $prompt . "\nAssistant:";
            
            // Appel à l'IA locale (llama.cpp) avec paramètres anti-spam
            $iaResponse = $this->httpClient->request('POST', 'http://localhost:8080/completion', [
                'json' => [
                    'prompt' => $fullPrompt,
                    'n_predict' => 256,                    // ← Plus court
                    'temperature' => 0.3,                   // ← Moins créatif
                    'top_k' => 30,                          // ← Limite les choix
                    'top_p' => 0.8,                          // ← Plus sévère
                    'repeat_penalty' => 1.3,                 // ← Anti-spam renforcé
                    'repeat_last_n' => 128,                  // ← Surveille plus longtemps
                    'frequency_penalty' => 0.5,              // ← Pénalise répétitions
                    'presence_penalty' => 0.5,               // ← Encourage diversité
                    'stop' => ["\n", "\n\n", "Utilisateur:", "Assistant:"],  // ← Stop rapide
                    'min_p' => 0.05
                ],
                'timeout' => 30
            ]);
            
            $iaData = $iaResponse->toArray();
            $code = $iaData['content'] ?? '// Pas de réponse';
            
        } catch (\Exception $e) {
            $code = '// Erreur IA : ' . $e->getMessage();
        }
        
        return $this->json([
            'code' => $code,
            'model' => 'nanbeige-3b-local'
        ]);
    }
}