1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
EngineManager.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2\Integration\AI;
4
5use Bitrix\AI\Context;
6use Bitrix\AI\Engine;
7use Bitrix\AI\Quality;
8use Bitrix\AI\Tuning\Manager;
9use Bitrix\Im\V2\Analytics\CopilotAnalytics;
10use Bitrix\Im\V2\Chat;
11use Bitrix\Im\V2\Pull\Event\ChangeEngine;
12use Bitrix\Im\V2\Result;
13use Bitrix\Main\Loader;
14use Bitrix\Main\Localization\Loc;
15
17{
21 protected const CATEGORY = 'text';
22
26 protected const QUALITY = 'chat_talk';
27
28 protected static ?string $defaultEngineCode = null;
29
33 protected static ?array $availableEngines = null;
34
35 protected function getAvailableEngineCodes(): array
36 {
37 $codes = [];
38
39 foreach ($this->getAvailableEngines() as $engine)
40 {
41 $codes[] = $engine->getCode();
42 }
43
44 return $codes;
45 }
46
50 protected function getAvailableEngines(): array
51 {
52 if (!self::isAvailable())
53 {
54 return [];
55 }
56
57 if (!isset(self::$availableEngines))
58 {
59 self::$availableEngines = Engine::getListAvailable(self::CATEGORY);
60 }
61
62 return self::$availableEngines;
63 }
64
66 {
67 $result = [];
68 $engines = $this->getAvailableEngines();
69
70 foreach ($engines as $engine)
71 {
72 $result[] = [
73 'code' => $engine->getCode(),
74 'name' => $engine->getName(),
75 'recommended' => $engine->isPreferredForQuality(new Quality(self::QUALITY)),
76 ];
77 }
78
79 return $result;
80 }
81
82 public function updateEngine(Chat $chat, string $engineCode): Result
83 {
84 $result = new Result();
85
86 $error = match (true)
87 {
91 $chat->getEngineCode() === $engineCode => new CopilotError(CopilotError::IDENTICAL_ENGINE),
92 default => null
93 };
94
95 if (isset($error))
96 {
97 return $result->addError($error);
98 }
99
100 $oldEngineName = $this->getEngineNameByCode($chat->getEngineCode() ?? self::getDefaultEngineCode());
104 $result = $chat->setEngineCode($engineCode)->save();
105
106 if ($result->isSuccess())
107 {
108 $this->setLastSelectedEngineCode($engineCode, $chat->getContext()->getUserId());
109 $this->sendUpdateEngineMessage($chat, $engineCode);
110 (new CopilotAnalytics($chat))->addChangeEngine($oldEngineName ?? '');
111
112 $engineName = $this->getEngineNameByCode($engineCode) ?? '';
113 (new ChangeEngine($chat, $engineCode, $engineName))
114 ->setContext($chat->getContext())
115 ->send()
116 ;
117 }
118
119 return $result;
120 }
121
122 protected function sendUpdateEngineMessage(Chat $chat, string $engineCode): void
123 {
124 $engineName = $this->getEngineNameByCode($engineCode);
125 $user = $chat->getContext()->getUser();
126 $genderSuffix = $user->getGender() === 'F' ? '_F' : '';
127 $userId = $user->getId();
128
129 $message = Loc::getMessage(
130 'IM_COPILOT_UPDATE_ENGINE' . $genderSuffix,
131 ['#USER_ID#' => $userId, '#ENGINE#' => $engineName]
132 ) ?? '';
133
135 'FROM_USER_ID' => $userId,
136 'MESSAGE_TYPE' => $chat->getType(),
137 'TO_CHAT_ID' => $chat->getChatId(),
138 'MESSAGE' => $message,
139 'SYSTEM' => 'Y',
140 'PUSH' => 'N'
141 ];
142
144 }
145
146 public static function getDefaultEngineCode(): ?string
147 {
148 if (!self::isAvailable())
149 {
150 return null;
151 }
152
153 if (!isset(self::$defaultEngineCode))
154 {
155 self::$defaultEngineCode =
156 (string)(new Manager())
158 ?->getValue()
159 ;
160 }
161
162 return !empty(self::$defaultEngineCode) ? self::$defaultEngineCode : null;
163 }
164
165 public static function getDefaultEngineName(): ?string
166 {
167 if (!self::isAvailable())
168 {
169 return null;
170 }
171
172 return self::getDefaultEngine()?->getIEngine()->getName();
173 }
174
175 public static function getDefaultEngine(?Context $context = null): ?Engine
176 {
177 if (!self::isAvailable())
178 {
179 return null;
180 }
181
182 $engineCode = self::getDefaultEngineCode();
183 $context ??= Context::getFake();
184
185 if (!isset($engineCode))
186 {
187 return null;
188 }
189
190
191 return Engine::getByCode($engineCode, $context, self::CATEGORY);
192 }
193
194 public function validateEngineCode(?string $code): bool
195 {
196 return in_array($code, $this->getAvailableEngineCodes(), true);
197 }
198
199 public function getEngineByCode(?string $code, ?Context $context = null): ?Engine
200 {
201 if (!isset($code) || !self::isAvailable())
202 {
203 return null;
204 }
205
206 $context ??= Context::getFake();
207
208 return Engine::getByCode($code, $context, self::CATEGORY);
209 }
210
216 public function getEnginesByCodes(array $codes, ?Context $context = null): array
217 {
218 $result = [];
219
220 foreach ($codes as $code)
221 {
222 $engine = $this->getEngineByCode($code, $context);
223 if (isset($engine))
224 {
225 $result[] = $engine;
226 }
227 }
228
229 return $result;
230 }
231
232 public function getEngineNameByCode(?string $code, ?Context $context = null): ?string
233 {
234 if (!isset($code) || !self::isAvailable())
235 {
236 return null;
237 }
238
239 $engine = $this->getEngineByCode($code, $context);
240
241 return $engine?->getIEngine()->getName();
242 }
243
244 public function getLastSelectedEngineCode(int $userId): ?string
245 {
246 $code = null;
247 $codeFromOption = \CUserOptions::GetOption(
248 'im',
249 'lastSelectedEngineCode',
250 false,
251 $userId
252 );
253
254 if (is_string($codeFromOption))
255 {
256 $code = $codeFromOption;
257 }
258
259 return $code;
260 }
261
262 protected function setLastSelectedEngineCode(string $code, int $userId): self
263 {
264 if ($this->validateEngineCode($code))
265 {
266 \CUserOptions::SetOption('im', 'lastSelectedEngineCode', $code, false, $userId);
267 }
268
269 return $this;
270 }
271
272 protected static function isAvailable(): bool
273 {
274 return Loader::includeModule('ai');
275 }
276}
$messageFields
Определения callback_ednaru.php:22
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
static getType($chatData, bool $camelCase=true)
Определения chat.php:45
setLastSelectedEngineCode(string $code, int $userId)
Определения EngineManager.php:262
static string $defaultEngineCode
Определения EngineManager.php:28
getLastSelectedEngineCode(int $userId)
Определения EngineManager.php:244
sendUpdateEngineMessage(Chat $chat, string $engineCode)
Определения EngineManager.php:122
validateEngineCode(?string $code)
Определения EngineManager.php:194
getEngineNameByCode(?string $code, ?Context $context=null)
Определения EngineManager.php:232
static getDefaultEngine(?Context $context=null)
Определения EngineManager.php:175
static array $availableEngines
Определения EngineManager.php:33
getEnginesByCodes(array $codes, ?Context $context=null)
Определения EngineManager.php:216
getEngineByCode(?string $code, ?Context $context=null)
Определения EngineManager.php:199
const SETTING_COPILOT_CHAT_PROVIDER
Определения Restriction.php:17
Определения result.php:20
static Add($arFields)
Определения im_message.php:28
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$context
Определения csv_new_setup.php:223
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
Определения options.php:195
Определения culture.php:9
Определения action.php:3
$user
Определения mysql_to_pgsql.php:33
$message
Определения payment.php:8
$engine
Определения options.php:121
$error
Определения subscription_card_product.php:20