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