@@ -30,56 +30,54 @@ public function send(AiTeacherRequest $request)
30
30
{
31
31
$ messages = $ request ->get ('messages ' );
32
32
33
- if (! empty ($ messages ) and is_array ($ messages )) {
34
- $ completion = new Completion ;
35
- $ completion ->setMaxTokens (2000 );
36
-
37
- $ context = [
38
- [
39
- 'role ' => $ completion ::SYSTEM ,
40
- 'text ' => 'Ты учитель информатики. Твоя задача - подробно и понятно отвечать на вопросы или объяснять. ' ,
41
- ],
42
- ];
43
-
44
- foreach ($ messages as $ message ) {
45
- if ($ message ['isTeacher ' ]) {
46
- $ context [] = [
47
- 'role ' => $ completion ::ASSISTANT ,
48
- 'text ' => $ message ['text ' ],
49
- ];
50
- } else {
51
- $ validator = Validator::make ($ message , [
52
- 'text ' => 'required|max:2000 ' ,
53
- ]);
54
-
55
- if ($ validator ->fails ()) {
56
- return response ()->json ([
57
- 'code ' => 413 ,
58
- 'message ' => 'Payload Too Large ' ,
59
- ], 413 );
60
- }
61
-
62
- $ context [] = [
63
- 'role ' => $ completion ::USER ,
64
- 'text ' => $ message ['text ' ],
65
- ];
33
+ $ completion = new Completion ;
34
+ $ completion ->setMaxTokens (2000 );
35
+
36
+ $ context = [
37
+ [
38
+ 'role ' => $ completion ::SYSTEM ,
39
+ 'text ' => 'Ты учитель информатики. Твоя задача - подробно и понятно отвечать на вопросы или объяснять. ' ,
40
+ ],
41
+ ];
42
+
43
+ foreach ($ messages as $ message ) {
44
+ if ($ message ['isTeacher ' ]) {
45
+ $ context [] = [
46
+ 'role ' => $ completion ::ASSISTANT ,
47
+ 'text ' => $ message ['text ' ],
48
+ ];
49
+ } else {
50
+ $ validator = Validator::make ($ message , [
51
+ 'text ' => 'required|max:2000 ' ,
52
+ ]);
53
+
54
+ if ($ validator ->fails ()) {
55
+ return response ()->json ([
56
+ 'code ' => 413 ,
57
+ 'message ' => 'Payload Too Large ' ,
58
+ ], 413 );
66
59
}
60
+
61
+ $ context [] = [
62
+ 'role ' => $ completion ::USER ,
63
+ 'text ' => $ message ['text ' ],
64
+ ];
67
65
}
66
+ }
68
67
69
- $ completion ->setModelUri ($ this ->folderId , 'yandexgpt-lite/latest ' )
70
- ->setTextMaxCount (21 )
71
- ->setTextLength (50000 )
72
- ->addText ($ context );
68
+ $ completion ->setModelUri ($ this ->folderId , 'yandexgpt-lite/latest ' )
69
+ ->setTextMaxCount (21 )
70
+ ->setTextLength (50000 )
71
+ ->addText ($ context );
73
72
74
- $ result = $ this ->yandexGpt ->request ($ completion );
73
+ $ result = $ this ->yandexGpt ->request ($ completion );
75
74
76
- $ response = json_decode ($ result , true );
75
+ $ response = json_decode ($ result , true );
77
76
78
- return response ()->json ([
79
- 'text ' => $ response ['result ' ]['alternatives ' ][0 ]['message ' ]['text ' ],
80
- 'isTeacher ' => true ,
81
- 'data ' => now (),
82
- ]);
83
- }
77
+ return response ()->json ([
78
+ 'text ' => $ response ['result ' ]['alternatives ' ][0 ]['message ' ]['text ' ],
79
+ 'isTeacher ' => true ,
80
+ 'data ' => now (),
81
+ ]);
84
82
}
85
83
}
0 commit comments