openai-php / client

⚡️ OpenAI PHP is a supercharged community-maintained PHP API client that allows you to interact with OpenAI API.
MIT License
4.56k stars 466 forks source link

[Bug]: all runs result with failure code: "server_error", message: "Sorry, something went wrong." #353

Closed PeperMarkreel closed 3 months ago

PeperMarkreel commented 3 months ago

Description

The creating a run with the following php code always ends with failed. If you want to reproduce, please note I do not create an assistant and please point it to an existing assistant of yours. When I open the playground with said thread and ask for a run, the assistant works as expected.

<?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', // REPLACE WITH YOUR SIMPLE ASSISTANT
    ],
);
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 '<pre>' , print_r($response->toArray(), true) , '</pre>';
        echo "<br/>";
        break;
    }
    if(strcmp($runStatus, "completed") ==0){
        echo "In completed... <br/>";
        break;
    }
    sleep(1); 
}

echo "Run complete<br/>";

?>

Output:

Creating thread...
Thread created, id:thread_thQLR5MYUBjOHua11tsRcLey
Adding message Hello World...
Message created, id:msg_nLCiCjT0vX9Fs5m5QTSDv8gd
Creating a run...
Run created, id:run_rp5cUNK8LAE6DXjJmbGbt2tl
Waiting for run result...
In progres...
In progres...
In progres...
In progres...
In progres...
Failed.

Array
(
    [id] => run_rp5cUNK8LAE6DXjJmbGbt2tl
    [object] => thread.run
    [created_at] => 1710498365
    [assistant_id] => asst_wYzcGyBRwvpcKY3G4O0fEM5A
    [thread_id] => thread_thQLR5MYUBjOHua11tsRcLey
    [status] => failed
    [started_at] => 1710498365
    [expires_at] => 
    [cancelled_at] => 
    [failed_at] => 1710498371
    [completed_at] => 
    [last_error] => Array
        (
            [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] => Array
        (
        )

    [file_ids] => Array
        (
        )

    [metadata] => Array
        (
        )

    [usage] => Array
        (
            [prompt_tokens] => 0
            [completion_tokens] => 0
            [total_tokens] => 0
        )

)

Run complete

When I open the playground with said thread and ask for a run, the assistant works as expected.

Steps To Reproduce

See the php script above, change the api key and the assistant id to values valid for your environment and run the script.

OpenAI PHP Client Version

0.8.4

PHP Version

8.1

Notes

Requesting the run steps returns an empty array:

Array
(
    [object] => list
    [data] => Array
        (
        )

    [first_id] => 
    [last_id] => 
    [has_more] => 
)
PeperMarkreel commented 3 months ago

Replacing the api key (with only threads access) with an all access api key solved the problem. Sorry to bother you guys, we might want to leave it online for future reference.