Passed
Pull Request — master (#7304)
by Yannick
10:38
created

MistralProvider::generateLearnPath()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 87
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 59
c 0
b 0
f 0
nc 9
nop 6
dl 0
loc 87
rs 7.3389

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chamilo\CoreBundle\AiProvider;
6
7
use Chamilo\CoreBundle\Entity\AiRequests;
8
use Chamilo\CoreBundle\Repository\AiRequestsRepository;
9
use Chamilo\CoreBundle\Settings\SettingsManager;
10
use Exception;
11
use RuntimeException;
12
use Symfony\Bundle\SecurityBundle\Security;
13
use Symfony\Component\Security\Core\User\UserInterface;
14
use Symfony\Contracts\HttpClient\HttpClientInterface;
15
16
class MistralProvider implements AiProviderInterface
17
{
18
    private string $apiUrl;
19
    private string $apiKey;
20
    private string $model;
21
    private float $temperature;
22
    private HttpClientInterface $httpClient;
23
    private AiRequestsRepository $aiRequestsRepository;
24
    private Security $security;
25
26
    public function __construct(
27
        HttpClientInterface $httpClient,
28
        SettingsManager $settingsManager,
29
        AiRequestsRepository $aiRequestsRepository,
30
        Security $security
31
    ) {
32
        $this->httpClient = $httpClient;
33
        $this->aiRequestsRepository = $aiRequestsRepository;
34
        $this->security = $security;
35
36
        // Get AI providers from settings
37
        $configJson = $settingsManager->getSetting('ai_helpers.ai_providers', true);
38
        $config = json_decode($configJson, true) ?? [];
39
40
        if (!isset($config['mistral'])) {
41
            throw new RuntimeException('Mistral configuration is missing.');
42
        }
43
        if (!isset($config['mistral']['text'])) {
44
            throw new RuntimeException('DeepSeek configuration for text processing is missing.');
45
        }
46
47
        $this->apiKey = $config['mistral']['api_key'] ?? '';
48
        $this->apiUrl = $config['mistral']['text']['url'] ?? 'https://api.mistral.ai/v1/chat/completions';
49
        $this->model = $config['mistral']['text']['model'] ?? 'mistral-large-latest';
50
        $this->temperature = $config['mistral']['text']['temperature'] ?? 0.7;
51
52
        if (empty($this->apiKey)) {
53
            throw new RuntimeException('Mistral API key is missing.');
54
        }
55
    }
56
57
    public function generateQuestions(string $topic, int $numQuestions, string $questionType, string $language): ?string
58
    {
59
        $prompt = \sprintf(
60
            'Generate %d "%s" questions in Aiken format in the %s language about "%s".
61
            Ensure each question follows this format:
62
63
            1. The question text.
64
            A. Option A
65
            B. Option B
66
            C. Option C
67
            D. Option D
68
            ANSWER: (Correct answer letter)
69
70
            The output should be plain text without additional symbols or markdown.',
71
            $numQuestions,
72
            $questionType,
73
            $language,
74
            $topic
75
        );
76
77
        return $this->requestMistralAI($prompt, 'quiz');
78
    }
79
80
    public function generateLearnPath(string $topic, int $chaptersCount, string $language, int $wordsCount, bool $addTests, int $numQuestions): ?array
81
    {
82
        // Step 1: Generate the Table of Contents
83
        $tableOfContentsPrompt = \sprintf(
84
            'Generate a structured table of contents for a course in "%s" with %d chapters on "%s".
85
            Return a numbered list, each chapter on a new line. No conclusion.',
86
            $language,
87
            $chaptersCount,
88
            $topic
89
        );
90
91
        $lpStructure = $this->requestMistralAI($tableOfContentsPrompt, 'learnpath');
92
        if (!$lpStructure) {
93
            return ['success' => false, 'message' => 'Failed to generate course structure.'];
94
        }
95
96
        // Step 2: Generate content for each chapter
97
        $lpItems = [];
98
        $chapters = explode("\n", trim($lpStructure));
99
        foreach ($chapters as $index => $chapterTitle) {
100
            $chapterTitle = trim($chapterTitle);
101
            if (empty($chapterTitle)) {
102
                continue;
103
            }
104
105
            $chapterPrompt = \sprintf(
106
                'Create a learning chapter in HTML for "%s" in "%s" with %d words.
107
                Title: "%s". Assume the reader already knows the context.',
108
                $topic,
109
                $language,
110
                $wordsCount,
111
                $chapterTitle
112
            );
113
114
            $chapterContent = $this->requestMistralAI($chapterPrompt, 'learnpath');
115
            if (!$chapterContent) {
116
                continue;
117
            }
118
119
            $lpItems[] = [
120
                'title' => $chapterTitle,
121
                'content' => "<html><head><title>{$chapterTitle}</title></head><body>{$chapterContent}</body></html>",
122
            ];
123
        }
124
125
        // Step 3: Generate quizzes if enabled
126
        $quizItems = [];
127
        if ($addTests) {
128
            foreach ($lpItems as &$chapter) {
129
                $quizPrompt = \sprintf(
130
                    'Generate %d multiple-choice questions in Aiken format in %s about "%s".
131
            Ensure each question follows this format:
132
133
            1. The question text.
134
            A. Option A
135
            B. Option B
136
            C. Option C
137
            D. Option D
138
            ANSWER: (Correct answer letter)
139
140
            Each question must have exactly 4 options and one answer line.
141
            Return only valid questions without extra text.',
142
                    $numQuestions,
143
                    $language,
144
                    $chapter['title']
145
                );
146
147
                $quizContent = $this->requestMistralAI($quizPrompt, 'learnpath');
148
149
                if ($quizContent) {
150
                    $validQuestions = $this->filterValidAikenQuestions($quizContent);
151
152
                    if (!empty($validQuestions)) {
153
                        $quizItems[] = [
154
                            'title' => 'Quiz: '.$chapter['title'],
155
                            'content' => implode("\n\n", $validQuestions),
156
                        ];
157
                    }
158
                }
159
            }
160
        }
161
162
        return [
163
            'success' => true,
164
            'topic' => $topic,
165
            'lp_items' => $lpItems,
166
            'quiz_items' => $quizItems,
167
        ];
168
    }
169
170
    private function filterValidAikenQuestions(string $quizContent): array
171
    {
172
        $questions = preg_split('/\n{2,}/', trim($quizContent));
173
174
        $validQuestions = [];
175
        foreach ($questions as $questionBlock) {
176
            $lines = explode("\n", trim($questionBlock));
177
178
            if (\count($lines) < 6) {
179
                continue;
180
            }
181
182
            $options = \array_slice($lines, 1, 4);
183
            $validOptions = array_filter($options, fn ($line) => preg_match('/^[A-D]\. .+/', $line));
184
185
            $answerLine = end($lines);
186
            if (4 === \count($validOptions) && preg_match('/^ANSWER: [A-D]$/', $answerLine)) {
187
                $validQuestions[] = implode("\n", $lines);
188
            }
189
        }
190
191
        return $validQuestions;
192
    }
193
194
    private function requestMistralAI(string $prompt, string $toolName): ?string
195
    {
196
        $userId = $this->getUserId();
197
        if (!$userId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $userId of type integer|null is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
198
            throw new RuntimeException('User not authenticated.');
199
        }
200
201
        $payload = [
202
            'model' => $this->model,
203
            'messages' => [
204
                ['role' => 'system', 'content' => 'You are a helpful AI assistant that generates structured educational content.'],
205
                ['role' => 'user', 'content' => $prompt],
206
            ],
207
            'temperature' => $this->temperature,
208
            'max_tokens' => 1000,
209
        ];
210
211
        try {
212
            $response = $this->httpClient->request('POST', $this->apiUrl, [
213
                'headers' => [
214
                    'Authorization' => 'Bearer '.$this->apiKey,
215
                    'Content-Type' => 'application/json',
216
                ],
217
                'json' => $payload,
218
            ]);
219
220
            $statusCode = $response->getStatusCode();
221
            $data = $response->toArray();
222
223
            if (200 === $statusCode && isset($data['choices'][0]['message']['content'])) {
224
                $generatedContent = $data['choices'][0]['message']['content'];
225
226
                $aiRequest = new AiRequests();
227
                $aiRequest->setUserId($userId)
228
                    ->setToolName($toolName)
229
                    ->setRequestText($prompt)
230
                    ->setPromptTokens($data['usage']['prompt_tokens'] ?? 0)
231
                    ->setCompletionTokens($data['usage']['completion_tokens'] ?? 0)
232
                    ->setTotalTokens($data['usage']['total_tokens'] ?? 0)
233
                    ->setAiProvider('mistral')
234
                ;
235
236
                $this->aiRequestsRepository->save($aiRequest);
237
238
                return $generatedContent;
239
            }
240
241
            return null;
242
        } catch (Exception $e) {
243
            error_log('[AI][Mistral] Exception: '.$e->getMessage());
244
245
            return null;
246
        }
247
    }
248
249
    public function gradeOpenAnswer(string $prompt, string $toolName): ?string
250
    {
251
        return $this->requestMistralAI($prompt, $toolName);
252
    }
253
254
    private function getUserId(): ?int
255
    {
256
        $user = $this->security->getUser();
257
258
        return $user instanceof UserInterface ? $user->getId() : null;
259
    }
260
}
261