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