File size: 1,764 Bytes
6d89f9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
```php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Document;
use App\Services\OCRService;
use App\Services\AIService;

class AssistantController extends Controller
{
    protected $aiService;
    protected $ocrService;

    public function __construct(AIService $aiService, OCRService $ocrService)
    {
        $this->aiService = $aiService;
        $this->ocrService = $ocrService;
    }

    public function question(Request $request)
    {
        $request->validate([
            'question' => 'required|string',
            'document_id' => 'required|integer|exists:documents,id'
        ]);

        $document = Document::findOrFail($request->document_id);
        
        // Check user permissions
        if (!auth()->user()->canAccessDocument($document)) {
            return response()->json(['error' => 'Accès non autorisé'], 403);
        }

        // Extract text from document
        $text = $this->ocrService->extractText($document->file_path);

        // Get AI response
        $response = $this->aiService->askQuestion($request->question, $text, $document->id);

        // Log the interaction
        $this->logInteraction(auth()->id(), $document->id, $request->question, $response);

        return response()->json([
            'response' => $response,
            'document' => $document->name
        ]);
    }

    protected function logInteraction($userId, $documentId, $question, $response)
    {
        \DB::table('assistant_logs')->insert([
            'user_id' => $userId,
            'document_id' => $documentId,
            'question' => $question,
            'response' => $response,
            'created_at' => now(),
            'updated_at' => now()
        ]);
    }
}
```
```