Alcazard/vibe-coding-api
0
1<?php2 3namespace VibeCoding\Controller;4 5use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;6use Symfony\Component\HttpFoundation\JsonResponse;7use Symfony\Component\HttpFoundation\Request;8use Symfony\Component\HttpFoundation\Response;9use Symfony\Component\Routing\Annotation\Route;10use Symfony\Contracts\HttpClient\HttpClientInterface;11 12class ApiController extends AbstractController13{14 private HttpClientInterface $httpClient;15 16 public function __construct(HttpClientInterface $httpClient)17 {18 $this->httpClient = $httpClient;19 }20 21 #[Route('/api/status', name: 'api_status', methods: ['GET'])]22 public function status(): JsonResponse23 {24 return $this->json([25 'status' => 'online',26 'providers' => [27 'openrouter' => 200,28 'gemini' => 150029 ],30 'version' => '1.0.0'31 ]);32 }33 34 #[Route('/api/generate', name: 'api_generate', methods: ['POST'])]35 public function generate(Request $request): JsonResponse36 {37 $data = json_decode($request->getContent(), true);38 $prompt = $data['prompt'] ?? '';39 40 try {41 // Construction du message avec instructions42 $systemMessage = "Tu es un assistant de programmation. Réponds de façon claire et concise.\n\n";43 $fullPrompt = $systemMessage . "Utilisateur: " . $prompt . "\nAssistant:";44 45 // Appel à l'IA locale (llama.cpp) avec paramètres anti-spam46 $iaResponse = $this->httpClient->request('POST', 'http://localhost:8080/completion', [47 'json' => [48 'prompt' => $fullPrompt,49 'n_predict' => 256, // ← Plus court50 'temperature' => 0.3, // ← Moins créatif51 'top_k' => 30, // ← Limite les choix52 'top_p' => 0.8, // ← Plus sévère53 'repeat_penalty' => 1.3, // ← Anti-spam renforcé54 'repeat_last_n' => 128, // ← Surveille plus longtemps55 'frequency_penalty' => 0.5, // ← Pénalise répétitions56 'presence_penalty' => 0.5, // ← Encourage diversité57 'stop' => ["\n", "\n\n", "Utilisateur:", "Assistant:"], // ← Stop rapide58 'min_p' => 0.0559 ],60 'timeout' => 3061 ]);62 63 $iaData = $iaResponse->toArray();64 $code = $iaData['content'] ?? '// Pas de réponse';65 66 } catch (\Exception $e) {67 $code = '// Erreur IA : ' . $e->getMessage();68 }69 70 return $this->json([71 'code' => $code,72 'model' => 'nanbeige-3b-local'73 ]);74 }75}