The runs I create with the openai-php library fail direct in 100% of cases. What am I doing wrong? I do not have much experience with php but this is the test script.
<?php
require '../../../dev/vendor/autoload.php';
$apiKey = getenv('ASSISTANT_API_KEY');
@ini_set('zlib.output_compression',0);
@ini_set('implicit_flush',1);
@ob_end_clean();
set_time_limit(0);
ob_implicit_flush(1);
$client = OpenAI::client($apiKey);
echo "Creating thread...<br/>";
$response = $client->threads()->create([]);
$array = $response->toArray();
$threadId = $response->id;
echo "Thread created, id:" . $response->id . "<br/>";
echo "Adding message Hello World...<br/>";
$response = $client->threads()->messages()->create($threadId,
[
'role' => 'user',
'content' => 'Hello World!',
]
);
echo "Message created, id:" . $response->id . "<br/>";
echo "Creating a run...<br/>";
$response = $client->threads()->runs()->create(
threadId: $threadId,
parameters: [
'assistant_id' => 'asst_wYzcGyBRwvpcKY3G4O0fEM5A',
],
);
echo "Run created, id:" . $response->id . "<br/>";
$runId = $response->id;
echo "Waiting for run result...<br/>";
for ($i=0; $i <= 20; $i++) {
$response = $client->threads()->runs()->retrieve(
threadId: $threadId,
runId: $runId,
);
$runStatus = $response->status;
if(strcmp($runStatus, "in_progress")==0){
echo "In progres... <br/>";
}
if(strcmp($runStatus, "failed")==0){
echo "Failed... <br/>";
echo json_encode($response->toArray(), JSON_PRETTY_PRINT);
echo "<br/>";
break;
}
if(strcmp($runStatus, "completed") ==0){
echo "In completed... <br/>";
break;
}
sleep(1);
}
echo "Run complete<br/>";
?>
The result:
Creating thread...
Thread created, id:thread_ZvHJYWZjoYXf04CR6BJbw67C
Adding message Hello World...
Message created, id:msg_Z1Y6Ei4j9b5EKI48BtSzHvot
Creating a run...
Run created, id:run_jR2oc8y3wLqWS8jwfcizK70I
Waiting for run result...
In progres...
Failed...
{
"id": "run_jR2oc8y3wLqWS8jwfcizK70I",
"object": "thread.run",
"created_at": 1710458772,
"assistant_id": "asst_wYzcGyBRwvpcKY3G4O0fEM5A",
"thread_id": "thread_ZvHJYWZjoYXf04CR6BJbw67C",
"status": "failed",
"started_at": 1710458772,
"expires_at": null,
"cancelled_at": null,
"failed_at": 1710458773,
"completed_at": null,
"last_error": {
"code": "server_error",
"message": "Sorry, something went wrong."
},
"model": "gpt-4-1106-preview",
"instructions": "You're helping debugging why the api calls to you are not working. Please share everything , all technical details you know about the interaction and the communication aspects. ",
"tools": [],
"file_ids": [],
"metadata": [],
"usage": {
"prompt_tokens": 0,
"completion_tokens": 0,
"total_tokens": 0
}
}
Run complete
When I use the api to create the tread and message and use the platform.openai.com website the run completes fine. (I used an online json formatter to format it nicely as above.)
The error message is: Sorry, something went wrong.
Thanks for any help!
The problem was with OpenAI and not with the code. I used an api key restricted to threads write access, thread write access and assistant read access and these did not work.
I set the api key to full access and now it works.