1+ <?php
2+
3+ namespace Feature ;
4+
5+ use Tests \NLSearchModelsTestCase ;
6+
7+ class NLSearchModelsTest extends NLSearchModelsTestCase
8+ {
9+ public const RESOURCE_PATH = '/nl_search_models ' ;
10+
11+ public function testCanCreateAModel (): void
12+ {
13+ $ data = [
14+ "id " => "test-collection-model " ,
15+ "model_name " => "openai/gpt-3.5-turbo " ,
16+ "api_key " => $ _ENV ['OPENAI_API_KEY ' ] ?? getenv ('OPENAI_API_KEY ' ),
17+ "system_prompt " => "You are a helpful search assistant. " ,
18+ "max_bytes " => 16384 ,
19+ "temperature " => 0.7
20+ ];
21+
22+ $ response = $ this ->client ()->nlSearchModels ->create ($ data );
23+ $ this ->assertArrayHasKey ('id ' , $ response );
24+ $ this ->assertEquals ('test-collection-model ' , $ response ['id ' ]);
25+ $ this ->assertEquals ('openai/gpt-3.5-turbo ' , $ response ['model_name ' ]);
26+
27+ $ this ->client ()->nlSearchModels ['test-collection-model ' ]->delete ();
28+ }
29+
30+ public function testCanRetrieveAllModels (): void
31+ {
32+ $ testData = [
33+ "id " => "retrieve-test-model " ,
34+ "model_name " => "openai/gpt-3.5-turbo " ,
35+ "api_key " => $ _ENV ['OPENAI_API_KEY ' ] ?? getenv ('OPENAI_API_KEY ' ),
36+ "system_prompt " => "Test model for retrieval. " ,
37+ "max_bytes " => 8192
38+ ];
39+
40+ $ this ->client ()->nlSearchModels ->create ($ testData );
41+
42+ $ response = $ this ->client ()->nlSearchModels ->retrieve ();
43+ $ this ->assertIsArray ($ response );
44+
45+ $ foundModel = false ;
46+ foreach ($ response as $ model ) {
47+ if ($ model ['id ' ] === 'retrieve-test-model ' ) {
48+ $ foundModel = true ;
49+ $ this ->assertEquals ('openai/gpt-3.5-turbo ' , $ model ['model_name ' ]);
50+ break ;
51+ }
52+ }
53+ $ this ->assertTrue ($ foundModel , 'Created test model should be found in the list ' );
54+
55+ $ this ->client ()->nlSearchModels ['retrieve-test-model ' ]->delete ();
56+ }
57+
58+ public function testCreateWithMissingRequiredFields (): void
59+ {
60+ $ incompleteData = [
61+ "model_name " => "openai/gpt-3.5-turbo "
62+ ];
63+
64+ $ this ->expectException (\Typesense \Exceptions \RequestMalformed::class);
65+ $ this ->client ()->nlSearchModels ->create ($ incompleteData );
66+ }
67+
68+ public function testCreateWithInvalidModelName (): void
69+ {
70+ $ invalidData = [
71+ "id " => "invalid-model-test " ,
72+ "model_name " => "invalid/model-name " ,
73+ "api_key " => $ _ENV ['OPENAI_API_KEY ' ] ?? getenv ('OPENAI_API_KEY ' ),
74+ "system_prompt " => "This should fail. " ,
75+ "max_bytes " => 16384
76+ ];
77+
78+ $ this ->expectException (\Typesense \Exceptions \RequestMalformed::class);
79+ $ this ->client ()->nlSearchModels ->create ($ invalidData );
80+ }
81+
82+ public function testUpdate (): void
83+ {
84+ $ data = [
85+ "id " => "test-collection-model " ,
86+ "model_name " => "openai/gpt-3.5-turbo " ,
87+ "api_key " => $ _ENV ['OPENAI_API_KEY ' ] ?? getenv ('OPENAI_API_KEY ' ),
88+ "system_prompt " => "You are a helpful search assistant. " ,
89+ "max_bytes " => 16384 ,
90+ "temperature " => 0.7
91+ ];
92+
93+ $ response = $ this ->client ()->nlSearchModels ->create ($ data );
94+ $ this ->assertArrayHasKey ('id ' , $ response );
95+ $ this ->assertEquals ('test-collection-model ' , $ response ['id ' ]);
96+ $ this ->assertEquals ('openai/gpt-3.5-turbo ' , $ response ['model_name ' ]);
97+
98+ $ response = $ this ->client ()->nlSearchModels ['test-collection-model ' ]->update ([
99+ "temperature " => 0.5
100+ ]);
101+ $ this ->assertArrayHasKey ('id ' , $ response );
102+ $ this ->assertEquals ('test-collection-model ' , $ response ['id ' ]);
103+ $ this ->assertEquals (0.5 , $ response ['temperature ' ]);
104+
105+ $ this ->client ()->nlSearchModels ['test-collection-model ' ]->delete ();
106+ }
107+
108+ public function testDelete (): void
109+ {
110+ $ data = [
111+ "id " => "test-collection-model " ,
112+ "model_name " => "openai/gpt-3.5-turbo " ,
113+ "api_key " => $ _ENV ['OPENAI_API_KEY ' ] ?? getenv ('OPENAI_API_KEY ' ),
114+ "system_prompt " => "You are a helpful search assistant. " ,
115+ "max_bytes " => 16384 ,
116+ "temperature " => 0.7
117+ ];
118+
119+ $ response = $ this ->client ()->nlSearchModels ->create ($ data );
120+ $ this ->assertArrayHasKey ('id ' , $ response );
121+ $ this ->assertEquals ('test-collection-model ' , $ response ['id ' ]);
122+ $ this ->assertEquals ('openai/gpt-3.5-turbo ' , $ response ['model_name ' ]);
123+
124+ $ response = $ this ->client ()->nlSearchModels ['test-collection-model ' ]->delete ();
125+ $ this ->assertArrayHasKey ('id ' , $ response );
126+ $ this ->assertEquals ('test-collection-model ' , $ response ['id ' ]);
127+
128+ }
129+ }
0 commit comments