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