Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
ai.php
1<?php
3
4use Bitrix\AI\Context;
5use Bitrix\AI\Engine;
6use Bitrix\AI\Engine\IEngine;
7use Bitrix\AI\Tuning;
8use Bitrix\AI\Tuning\Type;
14
15class Ai
16{
17 private const TUNING_CODE_IMAGE = 'landing_allow_image_generate';
18 private const TUNING_CODE_TEXT = 'landing_allow_text_generate';
19
24 public static function isImageAvailable(): bool
25 {
26 if (!Loader::includeModule('ai'))
27 {
28 return false;
29 }
30
31 $engine = Engine::getByCategory('image', Context::getFake());
32 if (!$engine)
33 {
34 return false;
35 }
36
37 return true;
38 }
39
44 public static function isImageActive(): bool
45 {
46 if (!self::isImageAvailable())
47 {
48 return false;
49 }
50
51 $default = false;
52 $setting = (new Tuning\Manager())->getItem(self::TUNING_CODE_IMAGE);
53
54 return $setting ? (bool)$setting->getValue() : $default;
55 }
56
61 public static function isTextAvailable(): bool
62 {
63 if (!Loader::includeModule('ai'))
64 {
65 return false;
66 }
67
68 $engine = Engine::getByCategory('text', Context::getFake());
69 if (!$engine)
70 {
71 return false;
72 }
73
74 return true;
75 }
76
81 public static function isCopilotAvailable(): bool
82 {
83 if (!self::isTextAvailable())
84 {
85 return false;
86 }
87
88 if (\Bitrix\Main\Config\Option::get('landing', 'enable_copilot', 'N') === 'Y')
89 {
90 return true;
91 }
92
93 return false;
94 }
95
100 public static function isTextActive(): bool
101 {
102 if (!self::isTextAvailable())
103 {
104 return false;
105 }
106
107 $default = false;
108 $setting = (new Tuning\Manager())->getItem(self::TUNING_CODE_TEXT);
109
110 return $setting ? (bool)$setting->getValue() : $default;
111 }
112
117 public static function onTuningLoad(): Entity\EventResult
118 {
119 $result = new Entity\EventResult;
120 $items = [];
121 $groups = [];
122
123 if (Engine::getByCategory('image', Context::getFake()))
124 {
125 $items[self::TUNING_CODE_IMAGE] = [
126 'group' => Tuning\Defaults::GROUP_IMAGE,
127 'header' => Loc::getMessage('LANDING_CONNECTOR_AI_ALLOW_IMAGE_COPILOT_DESC'),
128 'title' => Loc::getMessage('LANDING_CONNECTOR_AI_ALLOW_COPILOT_TITLE'),
129 'type' => Type::BOOLEAN,
130 'default' => true,
131 'sort' => 300,
132 ];
133 }
134
135 if (Engine::getByCategory('text', Context::getFake()))
136 {
137 $items[self::TUNING_CODE_TEXT] = [
138 'group' => Tuning\Defaults::GROUP_TEXT,
139 'header' => Loc::getMessage('LANDING_CONNECTOR_AI_ALLOW_TEXT_COPILOT_DESC'),
140 'title' => Loc::getMessage('LANDING_CONNECTOR_AI_ALLOW_COPILOT_TITLE'),
141 'type' => Type::BOOLEAN,
142 'default' => true,
143 'sort' => 300,
144 ];
145 }
146
147 $result->modifyFields([
148 'items' => $items,
149 'groups' => $groups,
150 ]);
151
152 return $result;
153 }
154
161 public static function onBeforeCompletions(Event $event): EventResult
162 {
164 $engine = $event->getParameter('engine');
165 $category = $engine->getCategory();
166 $module = $engine->getContext()->getModuleId();
167
168 $config = new Tuning\Manager();
169 $configItem = $config->getItem("{$module}_allow_{$category}_generate");
170
171 if ($configItem && $configItem->getValue())
172 {
173 return new EventResult(EventResult::SUCCESS);
174 }
175 else
176 {
177 return new EventResult(EventResult::ERROR);
178 }
179 }
180}
static isTextAvailable()
Definition ai.php:61
static isImageAvailable()
Definition ai.php:24
static isCopilotAvailable()
Definition ai.php:81
getParameter($key)
Definition event.php:80
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29