xref: /plugin/aichat/Chunk.php (revision 01f06932bbd74c60ea6c93ab68b0d6cf32d05aea)
1f6ef2e50SAndreas Gohr<?php
2f6ef2e50SAndreas Gohr
3f6ef2e50SAndreas Gohrnamespace dokuwiki\plugin\aichat;
4f6ef2e50SAndreas Gohr
5f6ef2e50SAndreas Gohrclass Chunk implements \JsonSerializable
6f6ef2e50SAndreas Gohr{
7f6ef2e50SAndreas Gohr    /** @var string */
8f6ef2e50SAndreas Gohr    protected $page;
9f6ef2e50SAndreas Gohr    /** @var int */
10f6ef2e50SAndreas Gohr    protected $id;
11f6ef2e50SAndreas Gohr    /** @var string */
12f6ef2e50SAndreas Gohr    protected $text;
13f6ef2e50SAndreas Gohr    /** @var float[] */
14f6ef2e50SAndreas Gohr    protected $embedding;
15f6ef2e50SAndreas Gohr    /** @var int */
16f6ef2e50SAndreas Gohr    protected $created;
179b3d1b36SAndreas Gohr    /** @var int */
189b3d1b36SAndreas Gohr    protected $score;
19f6ef2e50SAndreas Gohr
20f6ef2e50SAndreas Gohr    /**
21f6ef2e50SAndreas Gohr     * @param string $page
22f6ef2e50SAndreas Gohr     * @param int $id
23f6ef2e50SAndreas Gohr     * @param string $text
24f6ef2e50SAndreas Gohr     * @param float[] $embedding
25f6ef2e50SAndreas Gohr     * @param int $created
26f6ef2e50SAndreas Gohr     */
279b3d1b36SAndreas Gohr    public function __construct($page, $id, $text, $embedding, $created = '', $score = 0)
28f6ef2e50SAndreas Gohr    {
29f6ef2e50SAndreas Gohr        $this->page = $page;
30f6ef2e50SAndreas Gohr        $this->id = $id;
31f6ef2e50SAndreas Gohr        $this->text = $text;
32f6ef2e50SAndreas Gohr        $this->embedding = $embedding;
339b3d1b36SAndreas Gohr        $this->created = $created ?: time();
349b3d1b36SAndreas Gohr        $this->score = $score;
35f6ef2e50SAndreas Gohr    }
36f6ef2e50SAndreas Gohr
37*01f06932SAndreas Gohr    public function __toString()
38*01f06932SAndreas Gohr    {
39*01f06932SAndreas Gohr        return $this->page . '#' . $this->id;
40*01f06932SAndreas Gohr    }
41*01f06932SAndreas Gohr
42f6ef2e50SAndreas Gohr    /**
43f6ef2e50SAndreas Gohr     * @return int
44f6ef2e50SAndreas Gohr     */
45f6ef2e50SAndreas Gohr    public function getId()
46f6ef2e50SAndreas Gohr    {
47f6ef2e50SAndreas Gohr        return $this->id;
48f6ef2e50SAndreas Gohr    }
49f6ef2e50SAndreas Gohr
50f6ef2e50SAndreas Gohr    /**
51f6ef2e50SAndreas Gohr     * @param int $id
52f6ef2e50SAndreas Gohr     */
53f6ef2e50SAndreas Gohr    public function setId($id): void
54f6ef2e50SAndreas Gohr    {
55f6ef2e50SAndreas Gohr        $this->id = $id;
56f6ef2e50SAndreas Gohr    }
57f6ef2e50SAndreas Gohr
58f6ef2e50SAndreas Gohr    /**
59f6ef2e50SAndreas Gohr     * @return string
60f6ef2e50SAndreas Gohr     */
61f6ef2e50SAndreas Gohr    public function getPage()
62f6ef2e50SAndreas Gohr    {
63f6ef2e50SAndreas Gohr        return $this->page;
64f6ef2e50SAndreas Gohr    }
65f6ef2e50SAndreas Gohr
66f6ef2e50SAndreas Gohr    /**
67f6ef2e50SAndreas Gohr     * @param string $page
68f6ef2e50SAndreas Gohr     */
69f6ef2e50SAndreas Gohr    public function setPage($page): void
70f6ef2e50SAndreas Gohr    {
71f6ef2e50SAndreas Gohr        $this->page = $page;
72f6ef2e50SAndreas Gohr    }
73f6ef2e50SAndreas Gohr
74f6ef2e50SAndreas Gohr    /**
75f6ef2e50SAndreas Gohr     * @return string
76f6ef2e50SAndreas Gohr     */
77f6ef2e50SAndreas Gohr    public function getText()
78f6ef2e50SAndreas Gohr    {
79f6ef2e50SAndreas Gohr        return $this->text;
80f6ef2e50SAndreas Gohr    }
81f6ef2e50SAndreas Gohr
82f6ef2e50SAndreas Gohr    /**
83f6ef2e50SAndreas Gohr     * @param string $text
84f6ef2e50SAndreas Gohr     */
85f6ef2e50SAndreas Gohr    public function setText($text): void
86f6ef2e50SAndreas Gohr    {
87f6ef2e50SAndreas Gohr        $this->text = $text;
88f6ef2e50SAndreas Gohr    }
89f6ef2e50SAndreas Gohr
90f6ef2e50SAndreas Gohr    /**
91f6ef2e50SAndreas Gohr     * @return float[]
92f6ef2e50SAndreas Gohr     */
93f6ef2e50SAndreas Gohr    public function getEmbedding()
94f6ef2e50SAndreas Gohr    {
95f6ef2e50SAndreas Gohr        return $this->embedding;
96f6ef2e50SAndreas Gohr    }
97f6ef2e50SAndreas Gohr
98f6ef2e50SAndreas Gohr    /**
99f6ef2e50SAndreas Gohr     * @param float[] $embedding
100f6ef2e50SAndreas Gohr     */
101f6ef2e50SAndreas Gohr    public function setEmbedding($embedding): void
102f6ef2e50SAndreas Gohr    {
103f6ef2e50SAndreas Gohr        $this->embedding = $embedding;
104f6ef2e50SAndreas Gohr    }
105f6ef2e50SAndreas Gohr
106f6ef2e50SAndreas Gohr    /**
107f6ef2e50SAndreas Gohr     * @return int
108f6ef2e50SAndreas Gohr     */
109f6ef2e50SAndreas Gohr    public function getCreated()
110f6ef2e50SAndreas Gohr    {
111f6ef2e50SAndreas Gohr        return $this->created;
112f6ef2e50SAndreas Gohr    }
113f6ef2e50SAndreas Gohr
114f6ef2e50SAndreas Gohr    /**
115f6ef2e50SAndreas Gohr     * @param int $created
116f6ef2e50SAndreas Gohr     */
117f6ef2e50SAndreas Gohr    public function setCreated($created): void
118f6ef2e50SAndreas Gohr    {
119f6ef2e50SAndreas Gohr        $this->created = $created;
120f6ef2e50SAndreas Gohr    }
121f6ef2e50SAndreas Gohr
122f6ef2e50SAndreas Gohr    /**
1239b3d1b36SAndreas Gohr     * @return int
1249b3d1b36SAndreas Gohr     */
1259b3d1b36SAndreas Gohr    public function getScore()
1269b3d1b36SAndreas Gohr    {
1279b3d1b36SAndreas Gohr        return $this->score;
1289b3d1b36SAndreas Gohr    }
1299b3d1b36SAndreas Gohr
1309b3d1b36SAndreas Gohr    /**
1319b3d1b36SAndreas Gohr     * @param int
1329b3d1b36SAndreas Gohr     */
1339b3d1b36SAndreas Gohr    public function setScore($score): void
1349b3d1b36SAndreas Gohr    {
1359b3d1b36SAndreas Gohr        $this->score = $score;
1369b3d1b36SAndreas Gohr    }
1379b3d1b36SAndreas Gohr
1389b3d1b36SAndreas Gohr
1399b3d1b36SAndreas Gohr
1409b3d1b36SAndreas Gohr    /**
141f6ef2e50SAndreas Gohr     * Create a Chunk from a JSON string
142f6ef2e50SAndreas Gohr     *
143f6ef2e50SAndreas Gohr     * @param string $json
144f6ef2e50SAndreas Gohr     * @return Chunk
145f6ef2e50SAndreas Gohr     */
146f6ef2e50SAndreas Gohr    static public function fromJSON($json)
147f6ef2e50SAndreas Gohr    {
148f6ef2e50SAndreas Gohr        $data = json_decode($json, true);
149f6ef2e50SAndreas Gohr        return new self(
150f6ef2e50SAndreas Gohr            $data['page'],
151f6ef2e50SAndreas Gohr            $data['id'],
152f6ef2e50SAndreas Gohr            $data['text'],
153f6ef2e50SAndreas Gohr            $data['embedding'],
154f6ef2e50SAndreas Gohr            $data['created']
155f6ef2e50SAndreas Gohr        );
156f6ef2e50SAndreas Gohr    }
157f6ef2e50SAndreas Gohr
158f6ef2e50SAndreas Gohr    /** @inheritdoc */
159f6ef2e50SAndreas Gohr    public function jsonSerialize()
160f6ef2e50SAndreas Gohr    {
161f6ef2e50SAndreas Gohr        return [
162f6ef2e50SAndreas Gohr            'page' => $this->page,
163f6ef2e50SAndreas Gohr            'id' => $this->id,
164f6ef2e50SAndreas Gohr            'text' => $this->text,
165f6ef2e50SAndreas Gohr            'embedding' => $this->embedding,
166f6ef2e50SAndreas Gohr            'created' => $this->created,
167f6ef2e50SAndreas Gohr        ];
168f6ef2e50SAndreas Gohr    }
169f6ef2e50SAndreas Gohr}
170