Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/aiomatic-automatic-ai-content-writer/res/Qdrant.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 +
3 + function aiomatic_qdrant_run_request( $method, $apikey, $server, $url, $query = null)
4 + {
5 + $headers = "accept: application/json, charset=utf-8\r\ncontent-type: application/json\r\n" .
6 + "api-key: " . $apikey . "\r\n";
7 + $body = $query ? json_encode( $query ) : null;
8 + $url = $server . $url;
9 + $options = [
10 + "headers" => $headers,
11 + "method" => $method,
12 + "timeout" => 90,
13 + "body" => $body,
14 + "sslverify" => false
15 + ];
16 + try
17 + {
18 + $response = wp_remote_request( $url, $options );
19 + if ( is_wp_error( $response ) ) {
20 + throw new Exception( $response->get_error_message() );
21 + }
22 + $responsebod = wp_remote_retrieve_body( $response );
23 + if(empty($responsebod))
24 + {
25 + throw new Exception( 'Empty response returned from Qdrant, code: ' . wp_remote_retrieve_response_code($response) );
26 + }
27 + $jresponse = json_decode( $responsebod, true );
28 + if ( !is_array( $jresponse ) ) {
29 + throw new Exception( 'Exception from Qdrant: ' . $jresponse );
30 + }
31 + return $jresponse;
32 + }
33 + catch ( Exception $e )
34 + {
35 + throw new Exception( '[Qdrant] ' . $e->getMessage() );
36 + }
37 + return [];
38 + }
39 + function aiomatic_qdrant_add_index( $apikey, $server, $name )
40 + {
41 + $dimension = 1536;
42 + $metric = 'Cosine';
43 + $result = aiomatic_qdrant_run_request( 'PUT', $apikey, $server, '/collections/' . $name, [
44 + 'vectors' => [
45 + 'distance' => $metric,
46 + 'size' => $dimension,
47 + ]
48 + ] );
49 + return $name;
50 + }
51 +
52 + function aiomatic_qdrant_delete_index( $apikey, $server, $name )
53 + {
54 + $index = aiomatic_qdrant_run_request( 'DELETE', $apikey, $server, "/collections/" . $name );
55 + $success = !empty( $index );
56 + return $success;
57 + }
58 + function aiomatic_qdrant_list_indexes( $apikey, $server )
59 + {
60 + $indexesIds = aiomatic_qdrant_run_request( 'GET', $apikey, $server, '/collections' );
61 +
62 + $indexes = [];
63 + foreach ( $indexesIds['result']['collections'] as $row ) {
64 + $index = aiomatic_qdrant_run_request( 'GET', $apikey, $server, "/collections/" . $row['name'] )['result'];
65 + $indexes[] = [
66 + 'name' => $row['name'],
67 + 'metric' => $index['config']['params']['vectors']['distance'],
68 + 'dimension' => $index['config']['params']['vectors']['size'],
69 + 'host' => $server,
70 + 'ready' => $index['status'] === "green"
71 + ];
72 + }
73 + return $indexes;
74 + }
75 +
76 + function aiomatic_qdrant_list_vectors( $apikey, $server, $index, $limit, $offset )
77 + {
78 + $vectors = aiomatic_qdrant_run_request( 'POST', $apikey, $server, "/collections/{$index}/points/scroll", [
79 + 'limit' => $limit,
80 + 'offset' => $offset,
81 + 'with_payload' => false,
82 + 'with_vector' => false,
83 + ] );
84 + $vectors = isset( $vectors['result']['points'] ) ? $vectors['result']['points'] : [];
85 + $vectors = array_map( function( $vector ) { return $vector['id']; }, $vectors );
86 + return $vectors;
87 + }
88 +
89 + function aiomatic_qdrant_delete_vectors( $apikey, $server, $index, $ids, $deleteAll = false )
90 + {
91 + if ( $deleteAll )
92 + {
93 + $body =
94 + [
95 + 'filter' =>
96 + [
97 + 'must' =>
98 + [
99 + [
100 + "is_empty" =>
101 + [
102 + "key" => "any"
103 + ]
104 + ]
105 + ]
106 + ]
107 + ];
108 + }
109 + else
110 + {
111 + $body = ['points' => $ids];
112 + }
113 +
114 + $success = aiomatic_qdrant_run_request( 'POST', $apikey, $server, "/collections/{$index}/points/delete", $body );
115 + $success = true;
116 + return $success;
117 + }
118 +
119 + function aiomatic_qdrant_add_vector( $apikey, $server, $index, $vector )
120 + {
121 + $qid = aiomatic_qdrant_get_uuid();
122 + $body = [
123 + 'points' =>
124 + [
125 + [
126 + 'id' => $qid,
127 + 'vector' => $vector['values'],
128 + 'payload' => [
129 + 'title' => $vector['id']
130 + ]
131 + ]
132 + ]
133 + ];
134 +
135 + $res = aiomatic_qdrant_run_request( 'PUT', $apikey, $server, "/collections/{$index}/points", $body );
136 + $success = isset( $res['status'] ) && $res['status'] === "ok";
137 + if ( $success ) {
138 + return $qid;
139 + }
140 + $error = isset( $res['status']['error'] ) ? $res['status']['error'] : 'Unknown error from Qdrant.';
141 + throw new Exception( $error );
142 + }
143 +
144 + function aiomatic_qdrant_query_vectors( $apikey, $server, $index, $maxSelect, $vector )
145 + {
146 + $body = [
147 + 'limit' => $maxSelect,
148 + 'vector' => $vector,
149 + 'with_payload' => true
150 + ];
151 +
152 + $res = aiomatic_qdrant_run_request( 'POST', $apikey, $server, "/collections/{$index}/points/search", $body );
153 + $vectors = isset( $res['result'] ) ? $res['result'] : [];
154 +
155 + foreach ( $vectors as &$vector ) {
156 + $vector['metadata'] = $vector['payload'];
157 + }
158 + return $vectors;
159 + }
160 +
161 + function aiomatic_qdrant_get_vector( $apikey, $server, $index, $vectorId )
162 + {
163 + $vectorId = $vectorId;
164 +
165 + $res = aiomatic_qdrant_run_request( 'GET', $apikey, $server, "/collections/{$index}/points/{$vectorId}" );
166 +
167 + $removeVector = isset( $res['result']['id'] ) ? $res['result'] : null;
168 +
169 + if ( !empty( $removeVector ) ) {
170 + return [
171 + 'id' => $vectorId,
172 + 'type' => isset( $removeVector['payload']['type'] ) ? $removeVector['payload']['type'] : 'manual',
173 + 'title' => isset( $removeVector['payload']['title'] ) ? $removeVector['payload']['title'] : '',
174 + 'values' => isset( $removeVector['vector'] ) ? $removeVector['vector'] : []
175 + ];
176 + }
177 + return null;
178 + }
179 + function aiomatic_qdrant_get_uuid($len = 32, $strong = true)
180 + {
181 + $data = openssl_random_pseudo_bytes($len, $strong);
182 + $data[6] = chr(ord($data[6]) & 0x0f | 0x40);
183 + $data[8] = chr(ord($data[8]) & 0x3f | 0x80);
184 + return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
185 + }
186 + ?>