xref: /plugin/aichat/Chunk.php (revision f6ef2e505783ac17f756e44bf15c66238362377a)
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
18    /**
19     * @param string $page
20     * @param int $id
21     * @param string $text
22     * @param float[] $embedding
23     * @param int $created
24     */
25    public function __construct($page, $id, $text, $embedding, $created = '')
26    {
27        $this->page = $page;
28        $this->id = $id;
29        $this->text = $text;
30        $this->embedding = $embedding;
31        $this->created = $created ? $created : time();
32    }
33
34    /**
35     * @return int
36     */
37    public function getId()
38    {
39        return $this->id;
40    }
41
42    /**
43     * @param int $id
44     */
45    public function setId($id): void
46    {
47        $this->id = $id;
48    }
49
50    /**
51     * @return string
52     */
53    public function getPage()
54    {
55        return $this->page;
56    }
57
58    /**
59     * @param string $page
60     */
61    public function setPage($page): void
62    {
63        $this->page = $page;
64    }
65
66    /**
67     * @return string
68     */
69    public function getText()
70    {
71        return $this->text;
72    }
73
74    /**
75     * @param string $text
76     */
77    public function setText($text): void
78    {
79        $this->text = $text;
80    }
81
82    /**
83     * @return float[]
84     */
85    public function getEmbedding()
86    {
87        return $this->embedding;
88    }
89
90    /**
91     * @param float[] $embedding
92     */
93    public function setEmbedding($embedding): void
94    {
95        $this->embedding = $embedding;
96    }
97
98    /**
99     * @return int
100     */
101    public function getCreated()
102    {
103        return $this->created;
104    }
105
106    /**
107     * @param int $created
108     */
109    public function setCreated($created): void
110    {
111        $this->created = $created;
112    }
113
114    /**
115     * Create a Chunk from a JSON string
116     *
117     * @param string $json
118     * @return Chunk
119     */
120    static public function fromJSON($json)
121    {
122        $data = json_decode($json, true);
123        return new self(
124            $data['page'],
125            $data['id'],
126            $data['text'],
127            $data['embedding'],
128            $data['created']
129        );
130    }
131
132    /** @inheritdoc */
133    public function jsonSerialize()
134    {
135        return [
136            'page' => $this->page,
137            'id' => $this->id,
138            'text' => $this->text,
139            'embedding' => $this->embedding,
140            'created' => $this->created,
141        ];
142    }
143}
144