xref: /plugin/aichat/Chunk.php (revision 441edf84da4c031892d23b5809b2adfb59c6d774)
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     */
20*441edf84SAndreas Gohr    public function __construct(
21*441edf84SAndreas Gohr        protected $page,
22*441edf84SAndreas Gohr        protected $id,
23*441edf84SAndreas Gohr        protected $text,
24*441edf84SAndreas Gohr        protected $embedding,
25*441edf84SAndreas Gohr                  $lang = '',
26*441edf84SAndreas Gohr                  $created = '',
27*441edf84SAndreas Gohr        protected $score = 0
28*441edf84SAndreas Gohr    )
29f6ef2e50SAndreas Gohr    {
30e33a1d7aSAndreas Gohr        $this->language = $lang ?: $this->determineLanguage();
319b3d1b36SAndreas Gohr        $this->created = $created ?: time();
32f6ef2e50SAndreas Gohr    }
33f6ef2e50SAndreas Gohr
3430b9cbc7Ssplitbrain    public function __toString(): string
3501f06932SAndreas Gohr    {
3601f06932SAndreas Gohr        return $this->page . '#' . $this->id;
3701f06932SAndreas Gohr    }
3801f06932SAndreas Gohr
39f6ef2e50SAndreas Gohr    /**
40f6ef2e50SAndreas Gohr     * @return int
41f6ef2e50SAndreas Gohr     */
42f6ef2e50SAndreas Gohr    public function getId()
43f6ef2e50SAndreas Gohr    {
44f6ef2e50SAndreas Gohr        return $this->id;
45f6ef2e50SAndreas Gohr    }
46f6ef2e50SAndreas Gohr
47f6ef2e50SAndreas Gohr    /**
48f6ef2e50SAndreas Gohr     * @param int $id
49f6ef2e50SAndreas Gohr     */
50f6ef2e50SAndreas Gohr    public function setId($id): void
51f6ef2e50SAndreas Gohr    {
52f6ef2e50SAndreas Gohr        $this->id = $id;
53f6ef2e50SAndreas Gohr    }
54f6ef2e50SAndreas Gohr
55f6ef2e50SAndreas Gohr    /**
56f6ef2e50SAndreas Gohr     * @return string
57f6ef2e50SAndreas Gohr     */
58f6ef2e50SAndreas Gohr    public function getPage()
59f6ef2e50SAndreas Gohr    {
60f6ef2e50SAndreas Gohr        return $this->page;
61f6ef2e50SAndreas Gohr    }
62f6ef2e50SAndreas Gohr
63f6ef2e50SAndreas Gohr    /**
64f6ef2e50SAndreas Gohr     * @param string $page
65f6ef2e50SAndreas Gohr     */
66f6ef2e50SAndreas Gohr    public function setPage($page): void
67f6ef2e50SAndreas Gohr    {
68f6ef2e50SAndreas Gohr        $this->page = $page;
69f6ef2e50SAndreas Gohr    }
70f6ef2e50SAndreas Gohr
71f6ef2e50SAndreas Gohr    /**
72f6ef2e50SAndreas Gohr     * @return string
73f6ef2e50SAndreas Gohr     */
74f6ef2e50SAndreas Gohr    public function getText()
75f6ef2e50SAndreas Gohr    {
76f6ef2e50SAndreas Gohr        return $this->text;
77f6ef2e50SAndreas Gohr    }
78f6ef2e50SAndreas Gohr
79f6ef2e50SAndreas Gohr    /**
80f6ef2e50SAndreas Gohr     * @param string $text
81f6ef2e50SAndreas Gohr     */
82f6ef2e50SAndreas Gohr    public function setText($text): void
83f6ef2e50SAndreas Gohr    {
84f6ef2e50SAndreas Gohr        $this->text = $text;
85f6ef2e50SAndreas Gohr    }
86f6ef2e50SAndreas Gohr
87f6ef2e50SAndreas Gohr    /**
88f6ef2e50SAndreas Gohr     * @return float[]
89f6ef2e50SAndreas Gohr     */
90f6ef2e50SAndreas Gohr    public function getEmbedding()
91f6ef2e50SAndreas Gohr    {
92f6ef2e50SAndreas Gohr        return $this->embedding;
93f6ef2e50SAndreas Gohr    }
94f6ef2e50SAndreas Gohr
95f6ef2e50SAndreas Gohr    /**
96f6ef2e50SAndreas Gohr     * @param float[] $embedding
97f6ef2e50SAndreas Gohr     */
98f6ef2e50SAndreas Gohr    public function setEmbedding($embedding): void
99f6ef2e50SAndreas Gohr    {
100f6ef2e50SAndreas Gohr        $this->embedding = $embedding;
101f6ef2e50SAndreas Gohr    }
102f6ef2e50SAndreas Gohr
103f6ef2e50SAndreas Gohr    /**
104f6ef2e50SAndreas Gohr     * @return int
105f6ef2e50SAndreas Gohr     */
106f6ef2e50SAndreas Gohr    public function getCreated()
107f6ef2e50SAndreas Gohr    {
108f6ef2e50SAndreas Gohr        return $this->created;
109f6ef2e50SAndreas Gohr    }
110f6ef2e50SAndreas Gohr
111f6ef2e50SAndreas Gohr    /**
112f6ef2e50SAndreas Gohr     * @param int $created
113f6ef2e50SAndreas Gohr     */
114f6ef2e50SAndreas Gohr    public function setCreated($created): void
115f6ef2e50SAndreas Gohr    {
116f6ef2e50SAndreas Gohr        $this->created = $created;
117f6ef2e50SAndreas Gohr    }
118f6ef2e50SAndreas Gohr
119f6ef2e50SAndreas Gohr    /**
1209b3d1b36SAndreas Gohr     * @return int
1219b3d1b36SAndreas Gohr     */
1229b3d1b36SAndreas Gohr    public function getScore()
1239b3d1b36SAndreas Gohr    {
1249b3d1b36SAndreas Gohr        return $this->score;
1259b3d1b36SAndreas Gohr    }
1269b3d1b36SAndreas Gohr
1279b3d1b36SAndreas Gohr    /**
1289b3d1b36SAndreas Gohr     * @param int
1299b3d1b36SAndreas Gohr     */
1309b3d1b36SAndreas Gohr    public function setScore($score): void
1319b3d1b36SAndreas Gohr    {
1329b3d1b36SAndreas Gohr        $this->score = $score;
1339b3d1b36SAndreas Gohr    }
1349b3d1b36SAndreas Gohr
135e33a1d7aSAndreas Gohr    public function getLanguage(): string
136e33a1d7aSAndreas Gohr    {
137e33a1d7aSAndreas Gohr        return $this->language;
138e33a1d7aSAndreas Gohr    }
139e33a1d7aSAndreas Gohr
140e33a1d7aSAndreas Gohr    /**
141e33a1d7aSAndreas Gohr     * @param string $language
142e33a1d7aSAndreas Gohr     */
143e33a1d7aSAndreas Gohr    public function setLanguage($language): void
144e33a1d7aSAndreas Gohr    {
145e33a1d7aSAndreas Gohr        $this->language = $language;
146e33a1d7aSAndreas Gohr    }
147e33a1d7aSAndreas Gohr
148e33a1d7aSAndreas Gohr    /**
149e33a1d7aSAndreas Gohr     * Initialize the language of the chunk
150e33a1d7aSAndreas Gohr     *
151e33a1d7aSAndreas Gohr     * When the translation plugin is available it is used to determine the language, otherwise the default language
152e33a1d7aSAndreas Gohr     * is used.
153e33a1d7aSAndreas Gohr     *
154e33a1d7aSAndreas Gohr     * @return string The lanuaage code
155e33a1d7aSAndreas Gohr     */
156e33a1d7aSAndreas Gohr    protected function determineLanguage()
157e33a1d7aSAndreas Gohr    {
158e33a1d7aSAndreas Gohr        global $conf;
159e33a1d7aSAndreas Gohr        /** @var \helper_plugin_translation $trans */
160e33a1d7aSAndreas Gohr        $trans = plugin_load('helper', 'translation');
161e33a1d7aSAndreas Gohr        if ($trans) {
162e33a1d7aSAndreas Gohr            $lc = $trans->realLC($trans->getLangPart($this->page));
163e33a1d7aSAndreas Gohr        } else {
164e33a1d7aSAndreas Gohr            $lc = $conf['lang'];
165e33a1d7aSAndreas Gohr        }
166e33a1d7aSAndreas Gohr        return $lc;
167e33a1d7aSAndreas Gohr    }
1689b3d1b36SAndreas Gohr
1699b3d1b36SAndreas Gohr
1709b3d1b36SAndreas Gohr    /**
171f6ef2e50SAndreas Gohr     * Create a Chunk from a JSON string
172f6ef2e50SAndreas Gohr     *
173f6ef2e50SAndreas Gohr     * @param string $json
174f6ef2e50SAndreas Gohr     * @return Chunk
175f6ef2e50SAndreas Gohr     */
1767ebc7895Ssplitbrain    public static function fromJSON($json)
177f6ef2e50SAndreas Gohr    {
17830b9cbc7Ssplitbrain        $data = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
179f6ef2e50SAndreas Gohr        return new self(
180f6ef2e50SAndreas Gohr            $data['page'],
181f6ef2e50SAndreas Gohr            $data['id'],
182f6ef2e50SAndreas Gohr            $data['text'],
183f6ef2e50SAndreas Gohr            $data['embedding'],
184e33a1d7aSAndreas Gohr            $data['language'] ?? '',
185f6ef2e50SAndreas Gohr            $data['created']
186f6ef2e50SAndreas Gohr        );
187f6ef2e50SAndreas Gohr    }
188f6ef2e50SAndreas Gohr
189f6ef2e50SAndreas Gohr    /** @inheritdoc */
190f6ef2e50SAndreas Gohr    public function jsonSerialize()
191f6ef2e50SAndreas Gohr    {
192f6ef2e50SAndreas Gohr        return [
193f6ef2e50SAndreas Gohr            'page' => $this->page,
194f6ef2e50SAndreas Gohr            'id' => $this->id,
195f6ef2e50SAndreas Gohr            'text' => $this->text,
196f6ef2e50SAndreas Gohr            'embedding' => $this->embedding,
197e33a1d7aSAndreas Gohr            'language' => $this->language,
198f6ef2e50SAndreas Gohr            'created' => $this->created,
199f6ef2e50SAndreas Gohr        ];
200f6ef2e50SAndreas Gohr    }
201f6ef2e50SAndreas Gohr}
202