I am trying to get a long article written via the gpt-3.5-turbo API (Completions) using PHP. So first I ask it to generate an outline for a topic and then I loop through the outline and ask it to elaborate on each and I intend to eventually combine them all into one article. But it always stops in the middle of the loop. In my script it mostly stops between 5th to 8th iteration, and via Postman, it stops after 2nd iteration. It never returns an error. I doubt its hitting the API rate limit because the outline is only about 20-40 points long usually and each response is of about 500 tokens.
Outline array:-
Array
(
[0] => Definition of ADHD
[1] => Prevalence and impact of ADHD
[2] => Common symptoms of ADHD
[3] => Diagnostic criteria for ADHD
[4] => Inattentive type
[5] => Hyperactive-impulsive type
[6] => Combined type
[7] => Genetic factors
[8] => Environmental factors
[9] => Brain structure and function
[10] => Anxiety disorders
[11] => Mood disorders
[12] => Learning disabilities
[13] => Medications for ADHD
[14] => Behavioral therapy
[15] => Support and accommodations
[16] => Organization and time management techniques
[17] => Exercise and physical activity
[18] => Mindfulness and stress reduction techniques
[19] => Challenges faced in academic settings
[20] => Strategies for success in school and work
[21] => Symptoms and challenges in adulthood
[22] => Strategies for managing ADHD as an adult
[23] => Support groups and organizations
[24] => Resources for individuals and families
)
PHP:-
$api_key = âAPI_KEYâ;
foreach ($outlinePoints as $point) {
$prompt = âElaborate on the sub-point related to âADHDâ: $pointâ;
$messages = [[âroleâ => âsystemâ, âcontentâ => âYou are a helpful assistant.â], [âroleâ => âuserâ, âcontentâ => $prompt]];
$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
âAuthorization: Bearer $api_keyâ,
âContent-Type: application/jsonâ
));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array(âmodelâ => âgpt-3.5-turboâ, âmessagesâ => $messages)));
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo "cURL Error: " . curl_error($ch);
} else {
$response = json_decode($response, true);
echo $response[âchoicesâ][0][âmessageâ][âcontentâ];
}
curl_close($ch);
}
I am using the âv1/chat/completionsâ endpoint.
Any idea why it wonât finish the loop?