openai-php / client

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

[Bug]: Cannot use `assistants=v2` #453

Closed OskarStark closed 4 months ago

OskarStark commented 4 months ago

Description

In ThreadRunResponse.php line 89:

  [ErrorException]
  Warning: Undefined array key "file_ids"

Exception trace:
  at /Users/oskar.stark/dev/sensio/poc-backend/vendor/openai-php/client/src/Responses/Threads/Runs/ThreadRunResponse.php:89
 OpenAI\Responses\Threads\Runs\ThreadRunResponse::from() at /Users/oskar.stark/dev/sensio/poc-backend/vendor/openai-php/client/src/Resources/Threads.php:51
 OpenAI\Resources\Threads->createAndRun() at /Users/oskar.stark/dev/sensio/poc-backend/src/OpenAI/Assistant.php:27
 App\OpenAI\Assistant->ask() at /Users/oskar.stark/dev/sensio/poc-backend/src/Command/TestChatbotCommand.php:68

Steps To Reproduce

Create a factory like

<?php

declare(strict_types=1);

namespace App\OpenAI;

use OpenAI\Client;

/**
 * @author Oskar Stark <oskarstark@googlemail.com>
 */
final readonly class OpenAIClientFactory
{
    public static function create(string $apiKey): Client
    {
        return \OpenAI::factory()
            ->withApiKey($apiKey)
            ->withHttpHeader('OpenAI-Beta', 'assistants=v2')
            ->make();
    }
}

and use it like:

<?php

declare(strict_types=1);

namespace App\OpenAI;

use App\OpenAI\Value\Answer;
use App\OpenAI\Value\AssistantId;
use App\OpenAI\Value\ThreadId;
use OpenAI\Client as OpenAI;
use OskarStark\Value\TrimmedNonEmptyString;

/**
 * @author Oskar Stark <oskarstark@googlemail.com>
 */
final readonly class Assistant
{
    public function __construct(
        private OpenAI $openAI,
    ) {
    }

    public function ask(AssistantId $assistant, string $question): string
    {
        $thread = $this->openAI->threads()->createAndRun([
            'assistant_id' => $assistant->value,
            'thread' => [
                'messages' => [
                    [
                        'role' => 'user',
                        'content' => $question,
                    ],
                ],
            ],
        ]);

        $done = false;

        while (!$done) {
            $run = $this->openAI->threads()->runs()->retrieve(
                threadId: $thread->threadId,
                runId: $thread->id,
            );

            if ('completed' === $run->status) {
                $done = true;
            }
        }

        $messagesData = $this->openAI->threads()->messages()->list(
            $thread->threadId,
        );

        $message = '';

        foreach ($messagesData->data as $messageData) {
            $message = $this->openAI->threads()->messages()->retrieve(
                threadId: $thread->threadId,
                messageId: $messageData->id,
            )->content[0]->text->value;

            break;
        }

        var_dump($message);

        return $message;
    }
}

OpenAI PHP Client Version

0.8.5

PHP Version

8.3

Notes

No response

OskarStark commented 4 months ago

Looks like it worked with the latest v0.10.2