openai-php / client

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

Unable to retrieve messages from thread #321

Closed kadnan closed 8 months ago

kadnan commented 8 months ago

Description

I am trying to run an assistant, >threads()->messages()->list and $allMessages->data is returning the following response only:

Run status: completed
Array
(
    [0] => OpenAI\Responses\Threads\Messages\ThreadMessageResponse Object
        (
            [id] => msg_Mc6EQ9OjSZ0zUPf5h28a4Bqs
            [object] => thread.message
            [createdAt] => 1705610963
            [threadId] => thread_cSCy95o2DWDTR2UaY0HjlFOS
            [role] => user
            [content] => Array
                (
                    [0] => OpenAI\Responses\Threads\Messages\ThreadMessageResponseContentTextObject Object
                        (
                            [type] => text
                            [text] => OpenAI\Responses\Threads\Messages\ThreadMessageResponseContentText Object
                                (
                                    [value] => Write me description for Icecream Machine
                                    [annotations] => Array
                                        (
                                        )

                                )

                        )

                )

            [assistantId] => 
            [runId] => 
            [fileIds] => Array
                (
                )

            [metadata] => Array
                (
                )

            [meta:OpenAI\Responses\Threads\Messages\ThreadMessageResponse:private] => OpenAI\Responses\Meta\MetaInformation Object
                (
                    [requestId] => fe3cce0834ec01c0585230b7c048a8e9
                    [openai] => OpenAI\Responses\Meta\MetaInformationOpenAI Object
                        (
                            [model] => 
                            [organization] => user-mcgdqt1tm3gqg7bictrvyogv
                            [version] => 2020-10-01
                            [processingMs] => 83
                        )

                    [requestLimit] => 
                    [tokenLimit] => 
                )

        )

)

It is not returning the response by the Assistant.

Steps To Reproduce

The code:

$openAI = OpenAI::client(KEY);
// $response = $openAI->assistants()->retrieve(ASSISTANT_ID_PRODUCT_OFFER_WRITER);
$result = createRunThread(ASSISTANT_ID_PRODUCT_OFFER_WRITER,"Write me description for Icecream Machine",$openAI);

Function Definition

function createRunThread($assistantId, $userPrompt, $openAIClient)
{
    $threadID = $runID = 0;

    // Thread Creation
    $thread = $openAIClient->threads()->create([]);
    $threadID = $thread->id;

    //Message Creation
    $message = $openAIClient
        ->threads()
        ->messages()
        ->create($threadID, [
            'role' => 'user',
            'content' => $userPrompt,
        ]);

    //Run Creation
    $run = $openAIClient->threads()->runs()->create(
        threadId: $threadID,
        parameters: [
            'assistant_id' => $assistantId,
        ],
    );
    $runID = $run->id;

    //Periodically retrieve the Run to check status and see if it has completed
    //Should print "in_progress" several times before completing
    while ($run->status != "completed") {
        $response = $openAIClient->threads()
            ->runs()
            ->retrieve(
                threadId: $threadID,
                runId: $runID,
            );
        print("Run status: {$response->status}\n");

        if($response->status == "completed") {
            break;
        }
        sleep(1);
    }
    //Retrieve messages added by the Assistant to the thread
    $allMessages = $openAIClient->threads()
        ->messages()
        ->list($threadID);

    print_r($allMessages->data);
    return null;
}

OpenAI PHP Client Version

0.8.1

PHP Version

8.1

Notes

No response

gehrisandro commented 8 months ago

Hi @kadnan

Not sure what the issue is here, but looks like your code is a bit messed up. You are not updating the $run.

Maybe my example here does help you: https://gehri.dev/blog/how-to-use-the-openai-assistants-api-in-php-and-laravel

Otherwise please provide a sample repository to which reproduces the issue.