Repository navigation
Expand file tree
/
Copy pathNLSearchModels.php
More file actions
120 lines (105 loc) · 2.5 KB
/
Copy pathNLSearchModels.php
File metadata and controls
120 lines (105 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
namespace Typesense;
use Http\Client\Exception as HttpClientException;
use Typesense\Exceptions\TypesenseClientError;
/**
* Class NLSearchModels
*
* @package \Typesense
*/
class NLSearchModels implements \ArrayAccess
{
public const RESOURCE_PATH = '/nl_search_models';
/**
* @var ApiCall
*/
private ApiCall $apiCall;
/**
* @var array
*/
private array $nlSearchModels = [];
/**
* NLSearchModels constructor.
*
* @param ApiCall $apiCall
*/
public function __construct(ApiCall $apiCall)
{
$this->apiCall = $apiCall;
}
/**
* @param $id
*
* @return mixed
*/
public function __get($id)
{
if (!isset($this->nlSearchModels[$id])) {
$this->nlSearchModels[$id] = new NLSearchModel($id, $this->apiCall);
}
return $this->nlSearchModels[$id];
}
/**
* Create a new NL search model.
*
* @example
* $client->nlSearchModels->create(['model_name' => 'openai/gpt-4', 'api_key' => '...'])
*
* @see https://typesense.org/docs/latest/api/natural-language-search.html
*
* @param array $params
*
* @return array
* @throws TypesenseClientError|HttpClientException
*/
public function create(array $params): array
{
return $this->apiCall->post(static::RESOURCE_PATH, $params);
}
/**
* Retrieve all NL search models.
*
* @example
* $client->nlSearchModels->retrieve()
*
* @see https://typesense.org/docs/latest/api/natural-language-search.html
*
* @return array
* @throws TypesenseClientError|HttpClientException
*/
public function retrieve(): array
{
return $this->apiCall->get(static::RESOURCE_PATH, []);
}
/**
* @inheritDoc
*/
public function offsetExists($offset): bool
{
return isset($this->nlSearchModels[$offset]);
}
/**
* @inheritDoc
*/
public function offsetGet($offset): NLSearchModel
{
if (!isset($this->nlSearchModels[$offset])) {
$this->nlSearchModels[$offset] = new NLSearchModel($offset, $this->apiCall);
}
return $this->nlSearchModels[$offset];
}
/**
* @inheritDoc
*/
public function offsetSet($offset, $value): void
{
$this->nlSearchModels[$offset] = $value;
}
/**
* @inheritDoc
*/
public function offsetUnset($offset): void
{
unset($this->nlSearchModels[$offset]);
}
}