I have developed a PHP program that utilizes the OpenAI API v1 and it works flawlessly. The program employs REST API to interact with an assistant to which I have passed some files.
Currently, I am attempting to migrate from API v1 to API v2. Despite reading the migration guide, I am encountering difficulties. Specifically, I am receiving the following error:
“Run creation requires a Thread with a user message or an assistant with instructions.”
Here is the function I am using. Could someone please assist me in migrating it?
// Create a Thread
$threadResponse = callOpenAI(‘threads’, array(), $apiKey,‘POST’);
// Add a message to the thread
$data = [‘role’ => $role, ‘content’ => $question ,‘file_ids’ => $fileId];
$response=callOpenAI(‘threads/’ . $threadId . ‘/messages’, $data, $apiKey,‘POST’);
// Run th Thread
$data = [“assistant_id”=> $assistantId,“additional_instructions”=>null];
$runResponse = callOpenAI(‘threads/’ . $threadId . ‘/runs’, $data, $apiKey,‘POST’);
$runId=$runResponse[‘id’];
do {
$run = callOpenAI(‘threads/’ . $threadId . ‘/runs/’ . $runId, , $apiKey, ‘GET’);
$status = $run[‘status’]; // Assuming the response has a ‘status’ field
// echo $status;
if ($status !== ‘completed’)
{
sleep(5);
}
}
while ($status !== ‘completed’);
// Retrieve the Message !!!
$response=callOpenAI(‘threads/’ . $threadId . ‘/messages’, , $apiKey, ‘GET’);
Regards
Giuseppe
Thank you very much.