Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
16.67% |
1 / 6 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| OpenAIClient | |
16.67% |
1 / 6 |
|
50.00% |
1 / 2 |
4.31 | |
0.00% |
0 / 1 |
| setClient | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| chat | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Mbvb1223\ChatAiMulticlient\Clients; |
| 6 | |
| 7 | use OpenAI; |
| 8 | use OpenAI\Client; |
| 9 | |
| 10 | class OpenAIClient extends BaseClient |
| 11 | { |
| 12 | protected string $model = 'gpt-3.5-turbo'; |
| 13 | private Client $client; |
| 14 | |
| 15 | public function setClient(string $apiKey): void |
| 16 | { |
| 17 | $this->client = OpenAI::client($apiKey); |
| 18 | } |
| 19 | |
| 20 | public function chat(string $text, string $model = null, array $options = []): string |
| 21 | { |
| 22 | $response = $this->client->chat()->create([ |
| 23 | 'model' => $model ?? $this->getModel(), |
| 24 | 'messages' => [['role' => 'user', 'content' => $text]], |
| 25 | ]); |
| 26 | |
| 27 | return $response->choices[0]->message->content; |
| 28 | } |
| 29 | } |