Initial commit

This commit is contained in:
jeremy bayse
2026-06-22 16:40:24 +02:00
commit ad127156ff
78 changed files with 13478 additions and 0 deletions
+119
View File
@@ -0,0 +1,119 @@
<?php
namespace App\Livewire;
use App\Models\Chatbot as ChatbotModel;
use App\Services\MistralService;
use App\Services\WeaviateService;
use Livewire\Component;
use Livewire\Attributes\On;
class Chatbot extends Component
{
public ChatbotModel $chatbot;
public string $userMessage = '';
public array $messages = [];
public bool $isSending = false;
public string $chatbotName = '';
public function mount(ChatbotModel $chatbot)
{
$this->chatbot = $chatbot;
$this->loadSettings();
// Load messages from session specific to this chatbot's slug
$this->messages = session()->get("chat_messages_{$chatbot->slug}", [
[
'role' => 'assistant',
'content' => "Bonjour ! Je suis votre assistant virtuel RAG. Je peux répondre à vos questions en me basant sur le contenu de ce site. Que puis-je faire pour vous aujourd'hui ?"
]
]);
}
#[On('settings-updated')]
public function loadSettings()
{
// Reload chatbot model from database to get fresh settings
$this->chatbot->refresh();
$this->chatbotName = $this->chatbot->name;
}
public function sendMessage(WeaviateService $weaviate, MistralService $mistral)
{
$this->validate([
'userMessage' => 'required|string|max:1000',
]);
$query = trim($this->userMessage);
// 1. Add user message
$this->messages[] = [
'role' => 'user',
'content' => $query
];
$this->userMessage = '';
$this->isSending = true;
// Save to session immediately for visual update
session()->put("chat_messages_{$this->chatbot->slug}", $this->messages);
$this->dispatch('message-sent');
}
public function getResponse(WeaviateService $weaviate, MistralService $mistral)
{
if (!$this->isSending) {
return;
}
$lastMessage = end($this->messages);
if (!$lastMessage || $lastMessage['role'] !== 'user') {
$this->isSending = false;
return;
}
$query = $lastMessage['content'];
// 2. Search Weaviate for context using the chatbot ID
$chunks = $weaviate->search($query, (string) $this->chatbot->id, 4);
// 3. Generate response via Mistral AI
$history = array_slice($this->messages, 0, -1);
$response = $mistral->generateResponse($query, $chunks, $history, $this->chatbot->system_prompt);
// Extract sources
$sources = [];
if (!empty($chunks)) {
foreach ($chunks as $chunk) {
if (isset($chunk['source'])) {
$sources[] = $chunk['source'];
}
}
}
$sources = array_unique($sources);
// 4. Add assistant response
$this->messages[] = [
'role' => 'assistant',
'content' => $response ?? "Désolé, je n'ai pas pu obtenir de réponse.",
'sources' => $sources
];
$this->isSending = false;
// Save to session
session()->put("chat_messages_{$this->chatbot->slug}", $this->messages);
}
public function clearChat()
{
session()->forget("chat_messages_{$this->chatbot->slug}");
$this->mount($this->chatbot);
}
public function render()
{
return view('livewire.chatbot');
}
}