-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRestApiOpenAi.php
More file actions
178 lines (156 loc) · 6.25 KB
/
RestApiOpenAi.php
File metadata and controls
178 lines (156 loc) · 6.25 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
/**
* Kelas RestApiOpenAi
*
* Sebuah pembungkus (wrapper) PHP untuk API OpenAI, menyediakan akses mudah ke fungsionalitas utama
* seperti chat completions, pembuatan gambar, embeddings, dan lainnya.
*/
class RestApiOpenAi {
/**
* @var string Kunci API OpenAI.
*/
private $apiKey;
/**
* @var string URL dasar untuk API OpenAI.
*/
private $baseUrl = 'https://api.openai.com/v1/';
/**
* Konstruktor untuk menginisialisasi klien API.
*
* @param string $apiKey Kunci API OpenAI Anda.
*/
public function __construct(string $apiKey) {
if (empty($apiKey)) {
throw new \Exception('OpenAI API key is required.');
}
$this->apiKey = $apiKey;
}
/**
* Mengirimkan permintaan ke API OpenAI.
*
* @param string $endpoint Endpoint API yang akan dipanggil (contoh: 'chat/completions').
* @param array $data Data yang akan dikirim bersama permintaan.
* @param string $method Metode HTTP yang digunakan (contoh: 'POST').
* @param bool $isMultipart Apakah permintaan ini berjenis multipart/form-data (untuk unggahan file).
* @return mixed Respons JSON yang sudah di-decode atau data mentah untuk unduhan file.
*/
private function sendRequest(string $endpoint, array $data, string $method = 'POST', bool $isMultipart = false) {
$ch = curl_init($this->baseUrl . $endpoint);
$headers = [
'Authorization: Bearer ' . $this->apiKey,
];
if ($isMultipart) {
// Untuk unggahan file, cURL akan mengatur header Content-Type secara otomatis.
$payload = $data;
} else {
$headers[] = 'Content-Type: application/json';
$payload = json_encode($data);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_TIMEOUT, 60); // Batas waktu 60 detik
$response = curl_exec($ch);
if (curl_errno($ch)) {
$error = curl_error($ch);
curl_close($ch);
throw new \Exception('cURL Error: ' . $error);
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// Untuk endpoint yang mengembalikan data biner seperti audio
if ($httpCode === 200 && ($endpoint === 'audio/speech')) {
return $response;
}
$decodedResponse = json_decode($response, true);
if ($httpCode >= 400 || isset($decodedResponse['error'])) {
$errorMessage = $decodedResponse['error']['message'] ?? 'An unknown API error occurred.';
throw new \Exception('API Error: ' . $errorMessage . ' (HTTP Code: ' . $httpCode . ')');
}
return $decodedResponse;
}
/**
* Membuat sebuah chat completion.
*
* @param array $params Parameter untuk panggilan API chat completion.
* contoh: [
* 'model' => 'gpt-4',
* 'messages' => [['role' => 'user', 'content' => 'Hello!']],
* 'max_tokens' => 150
* ]
* @return array Respons dari API.
*/
public function chat(array $params): array {
// Atur model default jika tidak disediakan
if (!isset($params['model'])) {
$params['model'] = 'gpt-3.5-turbo';
}
return $this->sendRequest('chat/completions', $params);
}
/**
* Membuat gambar dari prompt teks menggunakan DALL·E.
*
* @param string $prompt Prompt teks untuk pembuatan gambar.
* @param int $n Jumlah gambar yang akan dibuat.
* @param string $size Ukuran gambar yang akan dibuat (contoh: '1024x1024').
* @param string $model Model yang akan digunakan (contoh: 'dall-e-3').
* @return array Respons dari API.
*/
public function generateImage(string $prompt, int $n = 1, string $size = '1024x1024', string $model = 'dall-e-3'): array {
$data = [
'model' => $model,
'prompt' => $prompt,
'n' => $n,
'size' => $size,
];
return $this->sendRequest('images/generations', $data);
}
/**
* Membuat embeddings untuk sebuah teks input.
*
* @param string $input Teks yang akan dibuatkan embedding.
* @param string $model Model yang akan digunakan (contoh: 'text-embedding-3-small').
* @return array Respons dari API.
*/
public function createEmbedding(string $input, string $model = 'text-embedding-3-small'): array {
$data = [
'input' => $input,
'model' => $model,
];
return $this->sendRequest('embeddings', $data);
}
/**
* Mentranskripsikan file audio menjadi teks menggunakan Whisper.
*
* @param string $filePath Path absolut ke file audio.
* @param string $model Model yang akan digunakan (contoh: 'whisper-1').
* @return array Respons dari API dengan teks hasil transkripsi.
*/
public function transcribeAudio(string $filePath, string $model = 'whisper-1'): array {
if (!file_exists($filePath)) {
throw new \Exception("Audio file not found at path: {$filePath}");
}
$data = [
'model' => $model,
'file' => new \CURLFile($filePath),
];
return $this->sendRequest('audio/transcriptions', $data, 'POST', true);
}
/**
* Membuat suara dari teks.
*
* @param string $input Teks yang akan diubah menjadi suara.
* @param string $voice Suara yang akan digunakan (contoh: 'alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer').
* @param string $model Model yang akan digunakan (contoh: 'tts-1' atau 'tts-1-hd').
* @return string Data audio biner mentah (contoh: MP3).
*/
public function createSpeech(string $input, string $voice = 'alloy', string $model = 'tts-1'): string {
$data = [
'model' => $model,
'input' => $input,
'voice' => $voice,
];
return $this->sendRequest('audio/speech', $data);
}
}