1<?php 2 3namespace dokuwiki\plugin\aichat; 4 5class Chunk implements \JsonSerializable, \Stringable 6{ 7 /** @var int */ 8 protected $created; 9 /** @var string */ 10 protected $language; 11 12 /** 13 * @param string $page 14 * @param int $id 15 * @param string $text 16 * @param float[] $embedding 17 * @param int $created 18 * @param int $score 19 */ 20 public function __construct( 21 protected $page, 22 protected $id, 23 protected $text, 24 protected $embedding, 25 $lang = '', 26 $created = '', 27 protected $score = 0 28 ) { 29 $this->language = $lang ?: $this->determineLanguage(); 30 $this->created = $created ?: time(); 31 } 32 33 public function __toString(): string 34 { 35 return $this->page . '#' . $this->id; 36 } 37 38 /** 39 * @return int 40 */ 41 public function getId() 42 { 43 return $this->id; 44 } 45 46 /** 47 * @param int $id 48 */ 49 public function setId($id): void 50 { 51 $this->id = $id; 52 } 53 54 /** 55 * @return string 56 */ 57 public function getPage() 58 { 59 return $this->page; 60 } 61 62 /** 63 * @param string $page 64 */ 65 public function setPage($page): void 66 { 67 $this->page = $page; 68 } 69 70 /** 71 * @return string 72 */ 73 public function getText() 74 { 75 return $this->text; 76 } 77 78 /** 79 * @param string $text 80 */ 81 public function setText($text): void 82 { 83 $this->text = $text; 84 } 85 86 /** 87 * @return float[] 88 */ 89 public function getEmbedding() 90 { 91 return $this->embedding; 92 } 93 94 /** 95 * @param float[] $embedding 96 */ 97 public function setEmbedding($embedding): void 98 { 99 $this->embedding = $embedding; 100 } 101 102 /** 103 * @return int 104 */ 105 public function getCreated() 106 { 107 return $this->created; 108 } 109 110 /** 111 * @param int $created 112 */ 113 public function setCreated($created): void 114 { 115 $this->created = $created; 116 } 117 118 /** 119 * @return int 120 */ 121 public function getScore() 122 { 123 return $this->score; 124 } 125 126 /** 127 * @param int 128 */ 129 public function setScore($score): void 130 { 131 $this->score = $score; 132 } 133 134 public function getLanguage(): string 135 { 136 return $this->language; 137 } 138 139 /** 140 * @param string $language 141 */ 142 public function setLanguage($language): void 143 { 144 $this->language = $language; 145 } 146 147 /** 148 * Initialize the language of the chunk 149 * 150 * When the translation plugin is available it is used to determine the language, otherwise the default language 151 * is used. 152 * 153 * @return string The lanuaage code 154 */ 155 protected function determineLanguage() 156 { 157 global $conf; 158 /** @var \helper_plugin_translation $trans */ 159 $trans = plugin_load('helper', 'translation'); 160 if ($trans) { 161 $lc = $trans->realLC($trans->getLangPart($this->page)); 162 } else { 163 $lc = $conf['lang']; 164 } 165 return $lc; 166 } 167 168 169 /** 170 * Create a Chunk from a JSON string 171 * 172 * @param string $json 173 * @return Chunk 174 */ 175 public static function fromJSON($json) 176 { 177 $data = json_decode($json, true, 512, JSON_THROW_ON_ERROR); 178 return new self( 179 $data['page'], 180 $data['id'], 181 $data['text'], 182 $data['embedding'], 183 $data['language'] ?? '', 184 $data['created'] 185 ); 186 } 187 188 /** @inheritdoc */ 189 public function jsonSerialize() 190 { 191 return [ 192 'page' => $this->page, 193 'id' => $this->id, 194 'text' => $this->text, 195 'embedding' => $this->embedding, 196 'language' => $this->language, 197 'created' => $this->created, 198 ]; 199 } 200} 201