Skip to content

Commit d07012a

Browse files
authored
Merge pull request #2 from miroslaws8/create_model_endpoints
Feature: add model endpoints
2 parents 24941c2 + 7413629 commit d07012a

File tree

7 files changed

+184
-1
lines changed

7 files changed

+184
-1
lines changed

src/Services/AIModels/General.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Itsimiro\OpenAI\Services\AIModels;
4+
5+
use GuzzleHttp\Exception\GuzzleException;
6+
use Itsimiro\OpenAI\Services\API\ApiService;
7+
use Itsimiro\OpenAI\Services\API\Results\ModelResult;
8+
use Itsimiro\OpenAI\Services\API\Results\ModelsResult;
9+
use Itsimiro\OpenAI\Services\API\UrlService;
10+
11+
class General
12+
{
13+
public function __construct(private readonly ApiService $apiService, private readonly UrlService $urlService)
14+
{}
15+
16+
/**
17+
* @throws GuzzleException
18+
*/
19+
public function getModels(): ModelsResult
20+
{
21+
return $this->apiService->getResult(
22+
$this->apiService->sendRequest('GET', $this->urlService->models()),
23+
ModelsResult::class
24+
);
25+
}
26+
27+
/**
28+
* @throws GuzzleException
29+
*/
30+
public function retrieveModel(string $modelId): ModelResult
31+
{
32+
return $this->apiService->getResult(
33+
$this->apiService->sendRequest('POST', $this->urlService->models().'/'.$modelId),
34+
ModelResult::class
35+
);
36+
}
37+
}

src/Services/API/Results/BodyParameters.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ class BodyParameters
1010
public const MODEL = 'model';
1111
public const CHOICES = 'choices';
1212
public const USAGE = 'usage';
13+
public const OWNER_BY = 'owned_by';
14+
public const PERMISSIONS = 'permissions';
1315
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Itsimiro\OpenAI\Services\API\Results;
4+
5+
use Illuminate\Support\Arr;
6+
use Spatie\LaravelData\Data;
7+
8+
class ModelResult extends Data implements ResultInterface
9+
{
10+
public function __construct(public array $result)
11+
{}
12+
13+
public function getId(): string
14+
{
15+
return Arr::get($this->result, BodyParameters::ID, '');
16+
}
17+
18+
public function getObject(): string
19+
{
20+
return Arr::get($this->result, BodyParameters::OBJECT, '');
21+
}
22+
23+
public function getOwner(): string
24+
{
25+
return Arr::get($this->result, BodyParameters::OWNER_BY, '');
26+
}
27+
28+
public function getPermissions(): array
29+
{
30+
return Arr::get($this->result, BodyParameters::PERMISSIONS, []);
31+
}
32+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Itsimiro\OpenAI\Services\API\Results;
4+
5+
use Spatie\LaravelData\Data;
6+
use Spatie\LaravelData\DataCollection;
7+
8+
class ModelsResult extends Data implements ResultInterface
9+
{
10+
public function __construct(public array $result)
11+
{}
12+
13+
public function getModels(): DataCollection
14+
{
15+
return ModelResult::collection($this->result['data']);
16+
}
17+
}

src/Services/API/UrlService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function fineTune(): string
3434
return $this->buildUrl('/fine-tunes');
3535
}
3636

37-
public function fineTuneModel(): string
37+
public function models(): string
3838
{
3939
return $this->buildUrl('/models');
4040
}

src/Services/OpenAI.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Contracts\Container\Container;
77
use Itsimiro\OpenAI\Services\AIModels\Dalle;
88
use Itsimiro\OpenAI\Services\AIModels\Davinci;
9+
use Itsimiro\OpenAI\Services\AIModels\General;
910
use Itsimiro\OpenAI\Services\AIModels\Whisper;
1011

1112
class OpenAI
@@ -36,4 +37,12 @@ public function getWhisper(): Whisper
3637
{
3738
return $this->container->make(Whisper::class);
3839
}
40+
41+
/**
42+
* @throws BindingResolutionException
43+
*/
44+
public function getGeneral(): General
45+
{
46+
return $this->container->make(General::class);
47+
}
3948
}

tests/Unit/AIModels/GeneralTest.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace Unit\AIModels;
4+
5+
use Illuminate\Contracts\Container\BindingResolutionException;
6+
use Itsimiro\OpenAI\Services\AIModels\General;
7+
use Itsimiro\OpenAI\Services\API\ApiService;
8+
use Itsimiro\OpenAI\Services\API\Results\ModelResult;
9+
use Itsimiro\OpenAI\Services\API\Results\ModelsResult;
10+
use Itsimiro\OpenAI\Services\API\UrlService;
11+
use PHPUnit\Framework\MockObject\Exception;
12+
use PHPUnit\Framework\TestCase;
13+
use Spatie\LaravelData\DataCollection;
14+
15+
class GeneralTest extends TestCase
16+
{
17+
/**
18+
* @throws Exception
19+
* @throws BindingResolutionException
20+
*/
21+
public function testGetModels(): void
22+
{
23+
$app = app();
24+
25+
$expectedModel = $this->getMockModel();
26+
27+
$dataCollection = $this->getMockBuilder(DataCollection::class)
28+
->setConstructorArgs([
29+
'dataClass' => ModelResult::class,
30+
'items' => []
31+
])->getMock();
32+
33+
$dataCollection->method('first')->willReturn(new ModelResult($expectedModel));
34+
35+
$resultObject = $this->createMock(ModelsResult::class);
36+
$resultObject->method('getModels')->willReturn($dataCollection);
37+
38+
$apiService = $this->createMock(ApiService::class);
39+
$app->bind(ApiService::class, fn() => $apiService);
40+
$apiService->expects($this->once())->method('getResult')->willReturn($resultObject);
41+
42+
$app->bind(UrlService::class, fn() => $this->createMock(UrlService::class));
43+
44+
$model = $app->make(General::class);
45+
46+
$result = $model->getModels();
47+
48+
$this->assertEquals($expectedModel['id'], $result->getModels()->first()->getId());
49+
}
50+
51+
/**
52+
* @throws Exception
53+
* @throws BindingResolutionException
54+
*/
55+
public function testRetrieveModel(): void
56+
{
57+
$app = app();
58+
59+
$modelId = 'testModel';
60+
61+
$resultObject = $this->createMock(ModelResult::class);
62+
$resultObject->method('getId')->willReturn($modelId);
63+
64+
$apiService = $this->createMock(ApiService::class);
65+
$app->bind(ApiService::class, fn() => $apiService);
66+
$apiService->expects($this->once())->method('getResult')->willReturn($resultObject);
67+
68+
$app->bind(UrlService::class, fn() => $this->createMock(UrlService::class));
69+
70+
$model = $app->make(General::class);
71+
72+
$result = $model->retrieveModel($modelId);
73+
74+
$this->assertEquals($modelId, $result->getId());
75+
}
76+
77+
private function getMockModel(): array
78+
{
79+
return [
80+
'id' => 'text-davinci-003',
81+
'object' => 'model',
82+
'owned_by'=> 'openai',
83+
'permission' => []
84+
];
85+
}
86+
}

0 commit comments

Comments
 (0)