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