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