-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenai.php
More file actions
executable file
·137 lines (111 loc) · 3.8 KB
/
Copy pathopenai.php
File metadata and controls
executable file
·137 lines (111 loc) · 3.8 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
<?php
$post = file_get_contents("php://input");
/*echo "<pre>";
print_r($post);
die;*/
$post = json_decode($post, true);
#$ key here
$prompt = '{
"adjusted_calories": "'.$post['calories'].'",
"meal_type": "'.$post['meal_type'].'"
}
1. create total '.$post['meals'].' meals to cover my adjusted calories for single day
2. name for each meal must be like Meal 1, Meal 2 etc
3. each meal must have dish name as well
4. show total calories for each main meal
5. show protein, carbs and fat info for each mean as well
6. give me cooking method and steps for each meal
7. calories for all meals and snaks combine should be equal to adjusted_calories
8. show ingredients under each meal after calories<br>
9. always create json response
10. cooking steps must be an abject with key and value
11. ingredients must be an abject with key and value
12. user "meals" for meals object, "name" for meal name, "dish_name" for dish name, "calories" for calories, "cooking_method" for cooking method, "cooking_steps" for cooking steps, ingredients" for ingredients, "protein" for protein, "fat" for fat, "carbs" for carbs';
function call_openai_api($api_key, $prompt) {
try{
// Initialize cURL
$openai_api_url_3 = 'https://api.openai.com/v1/chat/completions';
$ch = curl_init($openai_api_url_3);
$messages = array(
array(
'role' => 'user',
'content' => $prompt
)
);
$data = json_encode([
"model"=> "gpt-3.5-turbo", //"gpt-4-1106-preview",
'messages' => $messages,
'max_tokens' => 1500,
'temperature' => 0.7,
'top_p' => 1,
'frequency_penalty' => 0,
'presence_penalty' => 0
]);
// Set HTTP headers
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key
]);
// Set cURL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// Execute the request
$response = curl_exec($ch);
curl_close($ch);
// Handle the response
if (!$response) {
return 'Error: ' . curl_error($ch);
}
$decodedResponse = json_decode($response, true);
/*echo "<pre>test 1: ";
print_r($decodedResponse);*/
return $decodedResponse['choices'][0]['message']['content'];
} catch(Exception $e){
/*echo "<pre>test 2: ";
echo "data: Error: " . $e->getMessage() . "\n\n";
die;*/
}
// Return the response
// return json_decode($response, true)['choices'][0]['text'];
}
function call_openai_api4($api_key, $prompt) {
// Initialize cURL
$openai_api_url_4 = 'https://api.openai.com/v1/engines/gpt-4-1106-preview/completions';
$ch = curl_init($openai_api_url_4);
// Prepare the data for the API request
$data = json_encode([
'prompt' => $prompt,
'max_tokens' => 500, // You can adjust this value based on your needs
'temperature' => 0.7, // Adjust for more creative responses
'top_p' => 1,
'frequency_penalty' => 0,
'presence_penalty' => 0
]);
// Set HTTP headers
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key
]);
// Set cURL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// Execute the request
$response = curl_exec($ch);
curl_close($ch);
// Handle the response
if (!$response) {
return 'Error: ' . curl_error($ch);
}
$decodedResponse = json_decode($response, true);
print_r($decodedResponse);
return $decodedResponse['choices'][0]['text'];
// Return the response
// return json_decode($response, true)['choices'][0]['text'];
}
$response = call_openai_api($api_key, $prompt);
//$response = json_decode($response, true);
echo $response;
die;
?>