1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
RequestSiteContent.php
См. документацию.
1<?php
2declare(strict_types=1);
3
4namespace Bitrix\Landing\Copilot\Generation\Step;
5
6use Bitrix\Landing\Copilot\Connector\AI;
7use Bitrix\Landing\Copilot\Connector\AI\Prompt;
8use Bitrix\Landing\Copilot\Converter;
9use Bitrix\Landing\Copilot\Data\Site;
10use Bitrix\Landing\Copilot\Data\Type\NodeType;
11use Bitrix\Landing\Copilot\Generation\GenerationException;
12use Bitrix\Landing\Copilot\Generation\Markers;
13use Bitrix\Landing\Copilot\Generation\PromptGenerator;
14use Bitrix\Landing\Copilot\Generation\PromptTemplateProvider;
15use Bitrix\Landing\Copilot\Generation\Type\GenerationErrors;
16use Bitrix\Landing\Copilot\Generation\Type\RequestQuotaDto;
17use Bitrix\Main\Web;
18use Exception;
19
21{
22 public function __construct()
23 {
24 parent::__construct();
25 if (class_exists(self::getConnectorClass()))
26 {
27 $this->connector = new (self::getConnectorClass())();
28 }
29 }
30
34 public static function getConnectorClass(): string
35 {
36 return AI\Text::class;
37 }
38
43 {
44 return new RequestQuotaDto(self::getConnectorClass(), 1);
45 }
46
50 protected function getPrompt(): Prompt
51 {
52 $prompt = new Prompt('landing_ai_content');
53 $prompt->setMarkers(Markers::getSiteContentPromptMarkers($this->siteData));
54
55 return $prompt;
56 }
57
61 protected function applyResponse(): bool
62 {
63 $result = $this->request->getResult();
64
65 if ($result)
66 {
68 }
69
70 self::prepareImgNodesData($this->siteData);
71
72 return true;
73 }
74
85 public function verifyResponse(): void
86 {
87 $result = $this->request->getResult();
88
89 if (!$result)
90 {
91 throw new GenerationException(GenerationErrors::notExistResponse);
92 }
93
94 $prompt = $this->getPrompt();
95 $markers = $prompt->getMarkers();
96
97 if (isset($markers['json_schema_blocks']))
98 {
99 $jsonSchemaBlocks = $markers['json_schema_blocks'];
100 try
101 {
102 $jsonSchemaBlocksArray = Web\Json::decode($jsonSchemaBlocks);
103 }
104 catch (Exception)
105 {
106 throw new GenerationException(
107 GenerationErrors::notCorrectResponse,
108 "Response is not decoded.",
109 );
110 }
111 }
112
113 if (isset($result, $jsonSchemaBlocksArray))
114 {
115 //check blocks amount
116 if (isset($jsonSchemaBlocksArray['blocks'], $result['blocks']))
117 {
118 $countBlocksInRequest = count($jsonSchemaBlocksArray['blocks']);
119 $countBlocksInResult = count($result['blocks']);
120 $diffCount = $countBlocksInRequest - $countBlocksInResult;
121 if ($diffCount < 0 || $diffCount > 2)
122 {
123 throw new GenerationException(
124 GenerationErrors::notFullyResponse,
125 "The number of blocks does not match the expected range.",
126 );
127 }
128 }
129 else
130 {
131 throw new GenerationException(
132 GenerationErrors::notFullyResponse,
133 "Not exist blocks.",
134 );
135 }
136
137 //check amount title for menu
138 $filteredResultBlocks = array_filter($result['blocks'], static function ($block)
139 {
140 return isset($block['titleInMenu']) && $block['titleInMenu'] !== '';
141 });
142 $countTitlesInResult = count($filteredResultBlocks);
143 if ($countTitlesInResult < 4)
144 {
145 throw new GenerationException(
146 GenerationErrors::notFullyResponse,
147 "Not enough titles in the menu.",
148 );
149 }
150
151 //check amount nodes
152 if (isset($diffCount) && $diffCount === 0)
153 {
154 $blocksCount = count($result['blocks']);
155 for ($i = 0; $i < $blocksCount - 1; $i++)
156 {
157 $countBlockNodesInRequest = count($jsonSchemaBlocksArray['blocks'][$i]['nodes']);
158 $countBlockNodesInResult = count($result['blocks'][$i]['nodes']);
159 $diffCount = $countBlockNodesInRequest - $countBlockNodesInResult;
160 if ($diffCount < -1 || $diffCount > 1)
161 {
162 throw new GenerationException(
163 GenerationErrors::notFullyResponse,
164 "The number of nodes in block $i does not match the expected range.",
165 );
166 }
167 }
168 }
169 }
170 }
171
172 protected static function prepareImgNodesData(Site $siteData): void
173 {
174 foreach ($siteData->getBlocks() as $blockData)
175 {
176 if ($blockData->isSeparator() || $blockData->isMenu())
177 {
178 continue;
179 }
180
181 foreach ($blockData->getNodes() as $nodeData)
182 {
183 if ($nodeData->getType() !== NodeType::Img)
184 {
185 continue;
186 }
187
188 if (method_exists($nodeData, 'getGenderData') && !empty($nodeData->getGenderData()))
189 {
190 continue;
191 }
192
193 $countPromptTexts = 0;
194 $promptTexts = $nodeData->getPromptTexts();
195 if (is_array($promptTexts))
196 {
197 $countPromptTexts = count($promptTexts);
198 }
199
200 $countPlaceholders = 0;
201 $placeholders = $nodeData->getPlaceholders();
202 if (is_array($placeholders))
203 {
204 $countPlaceholders = count($placeholders);
205 }
206
207 if ($countPromptTexts !== $countPlaceholders)
208 {
209 $diffCount = $countPlaceholders - $countPromptTexts;
210 if ($diffCount > 0)
211 {
212 for ($i = 0; $i < $diffCount; $i++)
213 {
214 $promptTexts[] = '';
215 }
216 }
217 }
218
219 $updatedPromptTexts = [];
220 foreach ($promptTexts as $promptText)
221 {
222 if (is_string($promptText) && $promptText !== '')
223 {
224 $updatedPromptTexts[] = $promptText;
225 }
226 else
227 {
228 $promptGenerator = new PromptGenerator(new PromptTemplateProvider(), $siteData);
229 $updatedPromptTexts[] = $promptGenerator->getRandomReservePromptText();
230 }
231 }
232 $nodeData->setPromptTexts($updatedPromptTexts);
233 }
234 }
235 }
236}
static initSiteContent(Site $siteData, array $json)
Определения Json.php:194
static getSiteContentPromptMarkers(Data\Site $siteData)
Определения Markers.php:59
static decode($data)
Определения json.php:50
$result
Определения get_property_values.php:14
Определения cookies.php:2
$i
Определения factura.php:643
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936