xref: /plugin/aichat/Chunk.php (revision 9b3d1b36b01775ec335ec281af23d89587095d71)
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    /**
38     * @return int
39     */
40    public function getId()
41    {
42        return $this->id;
43    }
44
45    /**
46     * @param int $id
47     */
48    public function setId($id): void
49    {
50        $this->id = $id;
51    }
52
53    /**
54     * @return string
55     */
56    public function getPage()
57    {
58        return $this->page;
59    }
60
61    /**
62     * @param string $page
63     */
64    public function setPage($page): void
65    {
66        $this->page = $page;
67    }
68
69    /**
70     * @return string
71     */
72    public function getText()
73    {
74        return $this->text;
75    }
76
77    /**
78     * @param string $text
79     */
80    public function setText($text): void
81    {
82        $this->text = $text;
83    }
84
85    /**
86     * @return float[]
87     */
88    public function getEmbedding()
89    {
90        return $this->embedding;
91    }
92
93    /**
94     * @param float[] $embedding
95     */
96    public function setEmbedding($embedding): void
97    {
98        $this->embedding = $embedding;
99    }
100
101    /**
102     * @return int
103     */
104    public function getCreated()
105    {
106        return $this->created;
107    }
108
109    /**
110     * @param int $created
111     */
112    public function setCreated($created): void
113    {
114        $this->created = $created;
115    }
116
117    /**
118     * @return int
119     */
120    public function getScore()
121    {
122        return $this->score;
123    }
124
125    /**
126     * @param int
127     */
128    public function setScore($score): void
129    {
130        $this->score = $score;
131    }
132
133
134
135    /**
136     * Create a Chunk from a JSON string
137     *
138     * @param string $json
139     * @return Chunk
140     */
141    static public function fromJSON($json)
142    {
143        $data = json_decode($json, true);
144        return new self(
145            $data['page'],
146            $data['id'],
147            $data['text'],
148            $data['embedding'],
149            $data['created']
150        );
151    }
152
153    /** @inheritdoc */
154    public function jsonSerialize()
155    {
156        return [
157            'page' => $this->page,
158            'id' => $this->id,
159            'text' => $this->text,
160            'embedding' => $this->embedding,
161            'created' => $this->created,
162        ];
163    }
164}
165