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*2071dcedSAndreas Gohr ) { 29e33a1d7aSAndreas Gohr $this->language = $lang ?: $this->determineLanguage(); 309b3d1b36SAndreas Gohr $this->created = $created ?: time(); 31f6ef2e50SAndreas Gohr } 32f6ef2e50SAndreas Gohr 3330b9cbc7Ssplitbrain public function __toString(): string 3401f06932SAndreas Gohr { 35c2b7a1f7SAndreas Gohr $string = $this->page . '#' . $this->id; 36c2b7a1f7SAndreas Gohr if ($this->score) { 374a647d20SAndreas Gohr $string .= sprintf(' (%.2f)', $this->score); 38c2b7a1f7SAndreas Gohr } 39c2b7a1f7SAndreas Gohr return $string; 4001f06932SAndreas Gohr } 4101f06932SAndreas Gohr 42f6ef2e50SAndreas Gohr /** 43f6ef2e50SAndreas Gohr * @return int 44f6ef2e50SAndreas Gohr */ 45f6ef2e50SAndreas Gohr public function getId() 46f6ef2e50SAndreas Gohr { 47f6ef2e50SAndreas Gohr return $this->id; 48f6ef2e50SAndreas Gohr } 49f6ef2e50SAndreas Gohr 50f6ef2e50SAndreas Gohr /** 51f6ef2e50SAndreas Gohr * @param int $id 52f6ef2e50SAndreas Gohr */ 53f6ef2e50SAndreas Gohr public function setId($id): void 54f6ef2e50SAndreas Gohr { 55f6ef2e50SAndreas Gohr $this->id = $id; 56f6ef2e50SAndreas Gohr } 57f6ef2e50SAndreas Gohr 58f6ef2e50SAndreas Gohr /** 59f6ef2e50SAndreas Gohr * @return string 60f6ef2e50SAndreas Gohr */ 61f6ef2e50SAndreas Gohr public function getPage() 62f6ef2e50SAndreas Gohr { 63f6ef2e50SAndreas Gohr return $this->page; 64f6ef2e50SAndreas Gohr } 65f6ef2e50SAndreas Gohr 66f6ef2e50SAndreas Gohr /** 67f6ef2e50SAndreas Gohr * @param string $page 68f6ef2e50SAndreas Gohr */ 69f6ef2e50SAndreas Gohr public function setPage($page): void 70f6ef2e50SAndreas Gohr { 71f6ef2e50SAndreas Gohr $this->page = $page; 72f6ef2e50SAndreas Gohr } 73f6ef2e50SAndreas Gohr 74f6ef2e50SAndreas Gohr /** 75f6ef2e50SAndreas Gohr * @return string 76f6ef2e50SAndreas Gohr */ 77f6ef2e50SAndreas Gohr public function getText() 78f6ef2e50SAndreas Gohr { 79f6ef2e50SAndreas Gohr return $this->text; 80f6ef2e50SAndreas Gohr } 81f6ef2e50SAndreas Gohr 82f6ef2e50SAndreas Gohr /** 83f6ef2e50SAndreas Gohr * @param string $text 84f6ef2e50SAndreas Gohr */ 85f6ef2e50SAndreas Gohr public function setText($text): void 86f6ef2e50SAndreas Gohr { 87f6ef2e50SAndreas Gohr $this->text = $text; 88f6ef2e50SAndreas Gohr } 89f6ef2e50SAndreas Gohr 90f6ef2e50SAndreas Gohr /** 91f6ef2e50SAndreas Gohr * @return float[] 92f6ef2e50SAndreas Gohr */ 93f6ef2e50SAndreas Gohr public function getEmbedding() 94f6ef2e50SAndreas Gohr { 95f6ef2e50SAndreas Gohr return $this->embedding; 96f6ef2e50SAndreas Gohr } 97f6ef2e50SAndreas Gohr 98f6ef2e50SAndreas Gohr /** 99f6ef2e50SAndreas Gohr * @param float[] $embedding 100f6ef2e50SAndreas Gohr */ 101f6ef2e50SAndreas Gohr public function setEmbedding($embedding): void 102f6ef2e50SAndreas Gohr { 103f6ef2e50SAndreas Gohr $this->embedding = $embedding; 104f6ef2e50SAndreas Gohr } 105f6ef2e50SAndreas Gohr 106f6ef2e50SAndreas Gohr /** 107f6ef2e50SAndreas Gohr * @return int 108f6ef2e50SAndreas Gohr */ 109f6ef2e50SAndreas Gohr public function getCreated() 110f6ef2e50SAndreas Gohr { 111f6ef2e50SAndreas Gohr return $this->created; 112f6ef2e50SAndreas Gohr } 113f6ef2e50SAndreas Gohr 114f6ef2e50SAndreas Gohr /** 115f6ef2e50SAndreas Gohr * @param int $created 116f6ef2e50SAndreas Gohr */ 117f6ef2e50SAndreas Gohr public function setCreated($created): void 118f6ef2e50SAndreas Gohr { 119f6ef2e50SAndreas Gohr $this->created = $created; 120f6ef2e50SAndreas Gohr } 121f6ef2e50SAndreas Gohr 122f6ef2e50SAndreas Gohr /** 1239b3d1b36SAndreas Gohr * @return int 1249b3d1b36SAndreas Gohr */ 1259b3d1b36SAndreas Gohr public function getScore() 1269b3d1b36SAndreas Gohr { 1279b3d1b36SAndreas Gohr return $this->score; 1289b3d1b36SAndreas Gohr } 1299b3d1b36SAndreas Gohr 1309b3d1b36SAndreas Gohr /** 1319b3d1b36SAndreas Gohr * @param int 1329b3d1b36SAndreas Gohr */ 1339b3d1b36SAndreas Gohr public function setScore($score): void 1349b3d1b36SAndreas Gohr { 1359b3d1b36SAndreas Gohr $this->score = $score; 1369b3d1b36SAndreas Gohr } 1379b3d1b36SAndreas Gohr 138e33a1d7aSAndreas Gohr public function getLanguage(): string 139e33a1d7aSAndreas Gohr { 140e33a1d7aSAndreas Gohr return $this->language; 141e33a1d7aSAndreas Gohr } 142e33a1d7aSAndreas Gohr 143e33a1d7aSAndreas Gohr /** 144e33a1d7aSAndreas Gohr * @param string $language 145e33a1d7aSAndreas Gohr */ 146e33a1d7aSAndreas Gohr public function setLanguage($language): void 147e33a1d7aSAndreas Gohr { 148e33a1d7aSAndreas Gohr $this->language = $language; 149e33a1d7aSAndreas Gohr } 150e33a1d7aSAndreas Gohr 151e33a1d7aSAndreas Gohr /** 152e33a1d7aSAndreas Gohr * Initialize the language of the chunk 153e33a1d7aSAndreas Gohr * 154e33a1d7aSAndreas Gohr * When the translation plugin is available it is used to determine the language, otherwise the default language 155e33a1d7aSAndreas Gohr * is used. 156e33a1d7aSAndreas Gohr * 157e33a1d7aSAndreas Gohr * @return string The lanuaage code 158e33a1d7aSAndreas Gohr */ 159e33a1d7aSAndreas Gohr protected function determineLanguage() 160e33a1d7aSAndreas Gohr { 161e33a1d7aSAndreas Gohr global $conf; 162e33a1d7aSAndreas Gohr /** @var \helper_plugin_translation $trans */ 163e33a1d7aSAndreas Gohr $trans = plugin_load('helper', 'translation'); 164e33a1d7aSAndreas Gohr if ($trans) { 165e33a1d7aSAndreas Gohr $lc = $trans->realLC($trans->getLangPart($this->page)); 166e33a1d7aSAndreas Gohr } else { 167e33a1d7aSAndreas Gohr $lc = $conf['lang']; 168e33a1d7aSAndreas Gohr } 169e33a1d7aSAndreas Gohr return $lc; 170e33a1d7aSAndreas Gohr } 1719b3d1b36SAndreas Gohr 1729b3d1b36SAndreas Gohr 1739b3d1b36SAndreas Gohr /** 174f6ef2e50SAndreas Gohr * Create a Chunk from a JSON string 175f6ef2e50SAndreas Gohr * 176f6ef2e50SAndreas Gohr * @param string $json 177f6ef2e50SAndreas Gohr * @return Chunk 178f6ef2e50SAndreas Gohr */ 1797ebc7895Ssplitbrain public static function fromJSON($json) 180f6ef2e50SAndreas Gohr { 18130b9cbc7Ssplitbrain $data = json_decode($json, true, 512, JSON_THROW_ON_ERROR); 182f6ef2e50SAndreas Gohr return new self( 183f6ef2e50SAndreas Gohr $data['page'], 184f6ef2e50SAndreas Gohr $data['id'], 185f6ef2e50SAndreas Gohr $data['text'], 186f6ef2e50SAndreas Gohr $data['embedding'], 187e33a1d7aSAndreas Gohr $data['language'] ?? '', 188f6ef2e50SAndreas Gohr $data['created'] 189f6ef2e50SAndreas Gohr ); 190f6ef2e50SAndreas Gohr } 191f6ef2e50SAndreas Gohr 192f6ef2e50SAndreas Gohr /** @inheritdoc */ 193f6ef2e50SAndreas Gohr public function jsonSerialize() 194f6ef2e50SAndreas Gohr { 195f6ef2e50SAndreas Gohr return [ 196f6ef2e50SAndreas Gohr 'page' => $this->page, 197f6ef2e50SAndreas Gohr 'id' => $this->id, 198f6ef2e50SAndreas Gohr 'text' => $this->text, 199f6ef2e50SAndreas Gohr 'embedding' => $this->embedding, 200e33a1d7aSAndreas Gohr 'language' => $this->language, 201f6ef2e50SAndreas Gohr 'created' => $this->created, 202f6ef2e50SAndreas Gohr ]; 203f6ef2e50SAndreas Gohr } 204f6ef2e50SAndreas Gohr} 205