1f6ef2e50SAndreas Gohr<?php 2f6ef2e50SAndreas Gohr 3f6ef2e50SAndreas Gohrnamespace dokuwiki\plugin\aichat; 4f6ef2e50SAndreas Gohr 530b9cbc7Ssplitbrainclass Chunk implements \JsonSerializable, \Stringable 6f6ef2e50SAndreas Gohr{ 7f6ef2e50SAndreas Gohr /** @var int */ 8f6ef2e50SAndreas Gohr protected $created; 9e33a1d7aSAndreas Gohr /** @var string */ 10e33a1d7aSAndreas Gohr protected $language; 11f6ef2e50SAndreas Gohr 12f6ef2e50SAndreas Gohr /** 13f6ef2e50SAndreas Gohr * @param string $page 14f6ef2e50SAndreas Gohr * @param int $id 15f6ef2e50SAndreas Gohr * @param string $text 16f6ef2e50SAndreas Gohr * @param float[] $embedding 17f6ef2e50SAndreas Gohr * @param int $created 1830b9cbc7Ssplitbrain * @param int $score 19f6ef2e50SAndreas Gohr */ 20441edf84SAndreas Gohr public function __construct( 21441edf84SAndreas Gohr protected $page, 22441edf84SAndreas Gohr protected $id, 23441edf84SAndreas Gohr protected $text, 24441edf84SAndreas Gohr protected $embedding, 25441edf84SAndreas Gohr $lang = '', 26441edf84SAndreas Gohr $created = '', 27441edf84SAndreas Gohr protected $score = 0 28*4a647d20SAndreas Gohr ) 29*4a647d20SAndreas Gohr { 30e33a1d7aSAndreas Gohr $this->language = $lang ?: $this->determineLanguage(); 319b3d1b36SAndreas Gohr $this->created = $created ?: time(); 32f6ef2e50SAndreas Gohr } 33f6ef2e50SAndreas Gohr 3430b9cbc7Ssplitbrain public function __toString(): string 3501f06932SAndreas Gohr { 36c2b7a1f7SAndreas Gohr $string = $this->page . '#' . $this->id; 37c2b7a1f7SAndreas Gohr if ($this->score) { 38*4a647d20SAndreas Gohr $string .= sprintf(' (%.2f)', $this->score); 39c2b7a1f7SAndreas Gohr } 40c2b7a1f7SAndreas Gohr return $string; 4101f06932SAndreas Gohr } 4201f06932SAndreas Gohr 43f6ef2e50SAndreas Gohr /** 44f6ef2e50SAndreas Gohr * @return int 45f6ef2e50SAndreas Gohr */ 46f6ef2e50SAndreas Gohr public function getId() 47f6ef2e50SAndreas Gohr { 48f6ef2e50SAndreas Gohr return $this->id; 49f6ef2e50SAndreas Gohr } 50f6ef2e50SAndreas Gohr 51f6ef2e50SAndreas Gohr /** 52f6ef2e50SAndreas Gohr * @param int $id 53f6ef2e50SAndreas Gohr */ 54f6ef2e50SAndreas Gohr public function setId($id): void 55f6ef2e50SAndreas Gohr { 56f6ef2e50SAndreas Gohr $this->id = $id; 57f6ef2e50SAndreas Gohr } 58f6ef2e50SAndreas Gohr 59f6ef2e50SAndreas Gohr /** 60f6ef2e50SAndreas Gohr * @return string 61f6ef2e50SAndreas Gohr */ 62f6ef2e50SAndreas Gohr public function getPage() 63f6ef2e50SAndreas Gohr { 64f6ef2e50SAndreas Gohr return $this->page; 65f6ef2e50SAndreas Gohr } 66f6ef2e50SAndreas Gohr 67f6ef2e50SAndreas Gohr /** 68f6ef2e50SAndreas Gohr * @param string $page 69f6ef2e50SAndreas Gohr */ 70f6ef2e50SAndreas Gohr public function setPage($page): void 71f6ef2e50SAndreas Gohr { 72f6ef2e50SAndreas Gohr $this->page = $page; 73f6ef2e50SAndreas Gohr } 74f6ef2e50SAndreas Gohr 75f6ef2e50SAndreas Gohr /** 76f6ef2e50SAndreas Gohr * @return string 77f6ef2e50SAndreas Gohr */ 78f6ef2e50SAndreas Gohr public function getText() 79f6ef2e50SAndreas Gohr { 80f6ef2e50SAndreas Gohr return $this->text; 81f6ef2e50SAndreas Gohr } 82f6ef2e50SAndreas Gohr 83f6ef2e50SAndreas Gohr /** 84f6ef2e50SAndreas Gohr * @param string $text 85f6ef2e50SAndreas Gohr */ 86f6ef2e50SAndreas Gohr public function setText($text): void 87f6ef2e50SAndreas Gohr { 88f6ef2e50SAndreas Gohr $this->text = $text; 89f6ef2e50SAndreas Gohr } 90f6ef2e50SAndreas Gohr 91f6ef2e50SAndreas Gohr /** 92f6ef2e50SAndreas Gohr * @return float[] 93f6ef2e50SAndreas Gohr */ 94f6ef2e50SAndreas Gohr public function getEmbedding() 95f6ef2e50SAndreas Gohr { 96f6ef2e50SAndreas Gohr return $this->embedding; 97f6ef2e50SAndreas Gohr } 98f6ef2e50SAndreas Gohr 99f6ef2e50SAndreas Gohr /** 100f6ef2e50SAndreas Gohr * @param float[] $embedding 101f6ef2e50SAndreas Gohr */ 102f6ef2e50SAndreas Gohr public function setEmbedding($embedding): void 103f6ef2e50SAndreas Gohr { 104f6ef2e50SAndreas Gohr $this->embedding = $embedding; 105f6ef2e50SAndreas Gohr } 106f6ef2e50SAndreas Gohr 107f6ef2e50SAndreas Gohr /** 108f6ef2e50SAndreas Gohr * @return int 109f6ef2e50SAndreas Gohr */ 110f6ef2e50SAndreas Gohr public function getCreated() 111f6ef2e50SAndreas Gohr { 112f6ef2e50SAndreas Gohr return $this->created; 113f6ef2e50SAndreas Gohr } 114f6ef2e50SAndreas Gohr 115f6ef2e50SAndreas Gohr /** 116f6ef2e50SAndreas Gohr * @param int $created 117f6ef2e50SAndreas Gohr */ 118f6ef2e50SAndreas Gohr public function setCreated($created): void 119f6ef2e50SAndreas Gohr { 120f6ef2e50SAndreas Gohr $this->created = $created; 121f6ef2e50SAndreas Gohr } 122f6ef2e50SAndreas Gohr 123f6ef2e50SAndreas Gohr /** 1249b3d1b36SAndreas Gohr * @return int 1259b3d1b36SAndreas Gohr */ 1269b3d1b36SAndreas Gohr public function getScore() 1279b3d1b36SAndreas Gohr { 1289b3d1b36SAndreas Gohr return $this->score; 1299b3d1b36SAndreas Gohr } 1309b3d1b36SAndreas Gohr 1319b3d1b36SAndreas Gohr /** 1329b3d1b36SAndreas Gohr * @param int 1339b3d1b36SAndreas Gohr */ 1349b3d1b36SAndreas Gohr public function setScore($score): void 1359b3d1b36SAndreas Gohr { 1369b3d1b36SAndreas Gohr $this->score = $score; 1379b3d1b36SAndreas Gohr } 1389b3d1b36SAndreas Gohr 139e33a1d7aSAndreas Gohr public function getLanguage(): string 140e33a1d7aSAndreas Gohr { 141e33a1d7aSAndreas Gohr return $this->language; 142e33a1d7aSAndreas Gohr } 143e33a1d7aSAndreas Gohr 144e33a1d7aSAndreas Gohr /** 145e33a1d7aSAndreas Gohr * @param string $language 146e33a1d7aSAndreas Gohr */ 147e33a1d7aSAndreas Gohr public function setLanguage($language): void 148e33a1d7aSAndreas Gohr { 149e33a1d7aSAndreas Gohr $this->language = $language; 150e33a1d7aSAndreas Gohr } 151e33a1d7aSAndreas Gohr 152e33a1d7aSAndreas Gohr /** 153e33a1d7aSAndreas Gohr * Initialize the language of the chunk 154e33a1d7aSAndreas Gohr * 155e33a1d7aSAndreas Gohr * When the translation plugin is available it is used to determine the language, otherwise the default language 156e33a1d7aSAndreas Gohr * is used. 157e33a1d7aSAndreas Gohr * 158e33a1d7aSAndreas Gohr * @return string The lanuaage code 159e33a1d7aSAndreas Gohr */ 160e33a1d7aSAndreas Gohr protected function determineLanguage() 161e33a1d7aSAndreas Gohr { 162e33a1d7aSAndreas Gohr global $conf; 163e33a1d7aSAndreas Gohr /** @var \helper_plugin_translation $trans */ 164e33a1d7aSAndreas Gohr $trans = plugin_load('helper', 'translation'); 165e33a1d7aSAndreas Gohr if ($trans) { 166e33a1d7aSAndreas Gohr $lc = $trans->realLC($trans->getLangPart($this->page)); 167e33a1d7aSAndreas Gohr } else { 168e33a1d7aSAndreas Gohr $lc = $conf['lang']; 169e33a1d7aSAndreas Gohr } 170e33a1d7aSAndreas Gohr return $lc; 171e33a1d7aSAndreas Gohr } 1729b3d1b36SAndreas Gohr 1739b3d1b36SAndreas Gohr 1749b3d1b36SAndreas Gohr /** 175f6ef2e50SAndreas Gohr * Create a Chunk from a JSON string 176f6ef2e50SAndreas Gohr * 177f6ef2e50SAndreas Gohr * @param string $json 178f6ef2e50SAndreas Gohr * @return Chunk 179f6ef2e50SAndreas Gohr */ 1807ebc7895Ssplitbrain public static function fromJSON($json) 181f6ef2e50SAndreas Gohr { 18230b9cbc7Ssplitbrain $data = json_decode($json, true, 512, JSON_THROW_ON_ERROR); 183f6ef2e50SAndreas Gohr return new self( 184f6ef2e50SAndreas Gohr $data['page'], 185f6ef2e50SAndreas Gohr $data['id'], 186f6ef2e50SAndreas Gohr $data['text'], 187f6ef2e50SAndreas Gohr $data['embedding'], 188e33a1d7aSAndreas Gohr $data['language'] ?? '', 189f6ef2e50SAndreas Gohr $data['created'] 190f6ef2e50SAndreas Gohr ); 191f6ef2e50SAndreas Gohr } 192f6ef2e50SAndreas Gohr 193f6ef2e50SAndreas Gohr /** @inheritdoc */ 194f6ef2e50SAndreas Gohr public function jsonSerialize() 195f6ef2e50SAndreas Gohr { 196f6ef2e50SAndreas Gohr return [ 197f6ef2e50SAndreas Gohr 'page' => $this->page, 198f6ef2e50SAndreas Gohr 'id' => $this->id, 199f6ef2e50SAndreas Gohr 'text' => $this->text, 200f6ef2e50SAndreas Gohr 'embedding' => $this->embedding, 201e33a1d7aSAndreas Gohr 'language' => $this->language, 202f6ef2e50SAndreas Gohr 'created' => $this->created, 203f6ef2e50SAndreas Gohr ]; 204f6ef2e50SAndreas Gohr } 205f6ef2e50SAndreas Gohr} 206