-
Notifications
You must be signed in to change notification settings - Fork 2
/
OpenAi.php
54 lines (48 loc) · 1.64 KB
/
OpenAi.php
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
<?php
declare(strict_types=1);
namespace AdrienBrault\Instructrice\LLM\Provider;
use AdrienBrault\Instructrice\LLM\Cost;
use AdrienBrault\Instructrice\LLM\LLMConfig;
use AdrienBrault\Instructrice\LLM\OpenAiToolStrategy;
enum OpenAi: string implements ProviderModel
{
case GPT_35T = 'gpt-3.5-turbo';
case GPT_4T = 'gpt-4-turbo';
case GPT_4O = 'gpt-4o';
public function getApiKeyEnvVar(): ?string
{
return 'OPENAI_API_KEY';
}
public function createConfig(string $apiKey): LLMConfig
{
return new LLMConfig(
'https://api.openai.com/v1/chat/completions',
$this->value,
match ($this) {
self::GPT_35T => 16385,
self::GPT_4T, self::GPT_4O => 128000,
},
match ($this) {
self::GPT_35T => 'GPT-3.5 Turbo',
self::GPT_4T => 'GPT-4 Turbo',
self::GPT_4O => 'GPT-4o',
},
'OpenAI',
match ($this) {
self::GPT_35T => new Cost(0.5, 1.5),
self::GPT_4T => new Cost(10, 30),
self::GPT_4O => new Cost(5, 15),
},
OpenAiToolStrategy::FUNCTION,
headers: [
'Authorization' => 'Bearer ' . $apiKey,
],
maxTokens: 4096,
docUrl: match ($this) {
self::GPT_35T => 'https://platform.openai.com/docs/models/gpt-3-5-turbo',
self::GPT_4T => 'https://platform.openai.com/docs/models/gpt-4-turbo-and-gpt-4',
self::GPT_4O => 'https://platform.openai.com/docs/models/gpt-4o',
}
);
}
}