1
1
<?php
2
-
3
- $ API_KEY = 'your_api_key_here ' ;
4
-
5
2
// Function to call chatGPT endpoint with prompt
6
3
// Returns response, time taken and number of tokens for request and response
7
4
if (!function_exists ("callchatgpt " )) {
8
5
function callchatgpt ($ prompt ,$ temp ) {
9
6
// Set the API endpoint and parameters
10
- $ url = 'https://api.openai.com/v1/completions ' ;
7
+ $ url = 'https://api.openai.com/v1/chat/ completions ' ;
11
8
$ parameters = array (
12
- 'model ' => 'text-davinci-003 ' ,
13
- 'prompt ' => $ prompt ,
9
+ 'model ' => 'gpt-3.5-turbo-1106 ' ,
10
+ 'messages ' => array (
11
+ array ('role ' => 'user ' ,
12
+ 'content ' => $ prompt )),
14
13
'max_tokens ' => 500 , // each token is about 3/4 of a word and costs around $0.02 per thousand tokens
15
14
'temperature ' => $ temp , // 0 = most deterministic, 1 is most creative
16
15
'presence_penalty ' => 1 // -2 = encourage repetition, 2 is avoid repetition
17
16
);
18
17
19
- // Set the API key in the header of the request
18
+ // Set the API key
20
19
$ headers = array (
21
20
'Content-Type: application/json ' ,
22
- 'Authorization: Bearer ' . $ API_KEY
21
+ 'Authorization: Bearer <YOUR_API_KEY> '
23
22
);
24
23
25
24
// Send the request to the API
@@ -39,7 +38,7 @@ function callchatgpt($prompt,$temp) {
39
38
$ duration = $ end_time - $ start_time ;
40
39
41
40
$ chatgpt_output = [
42
- "response " => json_decode ($ response , true )['choices ' ][0 ]['text ' ],
41
+ "response " => json_decode ($ response , true )['choices ' ][0 ]['message ' ][ ' content ' ],
43
42
"time " => $ duration ,
44
43
"request_token " => json_decode ($ response , true )['usage ' ]['prompt_tokens ' ],
45
44
"response_token " => json_decode ($ response , true )['usage ' ]['completion_tokens ' ]
@@ -65,7 +64,7 @@ function calldalle($prompt) {
65
64
// Set the API key
66
65
$ headers = array (
67
66
'Content-Type: application/json ' ,
68
- 'Authorization: Bearer ' . $ API_KEY
67
+ 'Authorization: Bearer <YOUR_API_KEY> '
69
68
);
70
69
71
70
// Send the request to the API
@@ -221,7 +220,7 @@ function calldalle($prompt) {
221
220
$ limerick_text .= "<p><em>Generated in " . $ limerick_response ['time ' ] . " seconds " ;
222
221
$ limerick_text .= "using " . $ limerick_response ['request_token ' ] . " request tokens " ;
223
222
$ limerick_text .= "and " . $ limerick_response ['response_token ' ] . " response tokens " ;
224
- $ limerick_text .= "and the text-davinci-003 model from OpenAI with a temperature of 0.5. " ;
223
+ $ limerick_text .= "and the gpt-3.5-turbo-1106 model from OpenAI with a temperature of 0.5. " ;
225
224
$ limerick_text .= "</em></p> " ;
226
225
// TODO: Add in the monetary cost of this
227
226
@@ -240,7 +239,7 @@ function calldalle($prompt) {
240
239
$ action_text .= "<p><em>Generated in " . $ action_response ['time ' ] . " seconds " ;
241
240
$ action_text .= "using " . $ action_response ['request_token ' ] . " request tokens " ;
242
241
$ action_text .= "and " . $ action_response ['response_token ' ] . " response tokens " ;
243
- $ action_text .= "and the text-davinci-003 model from OpenAI with a temperature of 0.7. " ;
242
+ $ action_text .= "and the gpt-3.5-turbo-1106 model from OpenAI with a temperature of 0.7. " ;
244
243
$ action_text .= "</em></p> " ;
245
244
// TODO: Add in the monetary cost of this
246
245
@@ -259,7 +258,7 @@ function calldalle($prompt) {
259
258
$ passages_text .= "<p><em>Generated in " . $ passages_response ['time ' ] . " seconds " ;
260
259
$ passages_text .= "using " . $ passages_response ['request_token ' ] . " request tokens " ;
261
260
$ passages_text .= "and " . $ passages_response ['response_token ' ] . " response tokens " ;
262
- $ passages_text .= "and the text-davinci-003 model from OpenAI with a temperature of 0.5. " ;
261
+ $ passages_text .= "and the gpt-3.5-turbo-1106 model from OpenAI with a temperature of 0.5. " ;
263
262
$ passages_text .= "</em></p> " ;
264
263
// TODO: Add in the monetary cost of this
265
264
0 commit comments