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