Skip to content

Unable to retrieve messages from thread #321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kadnan opened this issue Jan 18, 2024 · 1 comment
Closed

Unable to retrieve messages from thread #321

kadnan opened this issue Jan 18, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@kadnan
Copy link

kadnan commented Jan 18, 2024

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

@kadnan kadnan added the bug Something isn't working label Jan 18, 2024
@gehrisandro
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants