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]: Validation error for Request calling createAndRun() #333

Open bobbingwide opened 4 months ago

bobbingwide commented 4 months ago

Description

I'm trying to use Assistants and Threads in order to benefit from the Code interpreter functionality. Unfortunately I've failed at the second hurdle - using CreateAndRun The first was realising that I didn't need to create an Assistant for each request.

My call is basically straight out of the book.

$this->client = OpenAI::client( $this->OpenAIKey );

$response = $this->client->threads()->createAndRun(
            [ 'assistant_id' => $this->assistant_id,
                'thread' => [
                    'messages' =>
                    [ 'role' => 'user',
                        'content' => 'what time is 1706918400'
                    ]
                ]
            ]
        );

where $this->assistant_id is the ID of an assistant I've created either manually in OpenAI or using the API. An exception is produced but I do not know what to do to fix it.

{
  "error": {
    "message": "1 validation error for Request\nbody -> thread -> messages\n  value is not a valid list (type=type_error.list)",
    "type": "invalid_request_error",
    "param": null,
    "code": null
  }

Stack trace:

#0 C:\apache\htdocs\wordpress\wp-content\plugins\oik-ai\vendor\openai-php\client\src\Transporters\HttpTransporter.php(57): OpenAI\Transporters\HttpTransporter->throwIfJsonError(Array, '{\n  "error": {\n...')
#1 C:\apache\htdocs\wordpress\wp-content\plugins\oik-ai\vendor\openai-php\client\src\Resources\Threads.php(51): OpenAI\Transporters\HttpTransporter->requestObject(Object(OpenAI\ValueObjects\Transporter\Payload))
#2 C:\apache\htdocs\wordpress\wp-content\plugins\oik-ai\classes\class-Oik-AI.php(217): OpenAI\Resources\Threads->createAndRun(Array)
#3 C:\apache\htdocs\wordpress\wp-content\plugins\oik-ai\oik-ai.php(87): Oik_AI->assist('what time is 17...')
#4 C:\apache\htdocs\wordpress\wp-content\plugins\oik-ai\oik-ai.php(43): oik_ai_invoke_assist(Object(Oik_AI))
#5 C:\apache\htdocs\wordpress\wp-content\plugins\oik-ai\oik-ai.php(32): oik_ai_loaded_batch()
#6 C:\apache\htdocs\wordpress\wp-content\plugins\oik-ai\oik-ai.php(107): oik_ai_loaded()
#7 C:\apache\htdocs\wordpress\wp-content\plugins\oik-batch\libs\oik-cli.php(611): require_once('C:\\apache\\htdoc...')
#8 C:\apache\htdocs\wordpress\wp-content\plugins\oik-batch\libs\oik-cli.php(579): oik_batch_run_script('oik-ai.php')
#9 C:\apache\htdocs\wordpress\wp-content\plugins\oik-batch\oik-wp.php(141): oik_batch_run()
#10 C:\apache\htdocs\wordpress\wp-content\plugins\oik-batch\oik-wp.php(180): oik_wp_loaded()
#11 {main}
  thrown in C:\apache\htdocs\wordpress\wp-content\plugins\oik-ai\vendor\openai-php\client\src\Transporters\HttpTransporter.php on line 133

I do understand this could be a question for the OpenAI Community forum.

Someone's having a similar problem, well a similar message, at https://community.openai.com/t/api-calls-to-v1-threads-thread-id-messages-fails-with-file-and-file-ids/613781

The problem that I am trying to resolve by using Assistants is documented in https://github.com/bobbingwide/oik-ai/issues/10 thanks in advance for any help, Herb

PS. The answer that I'm hoping to get is along the lines of "The timestamp 1706918400 corresponds to the date and time 2024-02-03 00:00:00 UTC."

Steps To Reproduce

  1. Write code to make the calls above.
  2. Run the code from your PC in a command prompt

OpenAI PHP Client Version

v0.8.4

PHP Version

8.3.0

Notes

Sorry, but I haven't yet created the minimum code to demonstrate this problem. I'm hoping someone's experienced something similar and can explain the error message.

bobbingwide commented 4 months ago

The minimum code to demonstrate the problem. I created a subfolder called tests with a file called assistant.php Source code of assistant.php with apiKey and assistant_id obfuscated.

<?php
/**
 * Simple test of the Assistant API
 *
 */
require '../vendor/autoload.php';
$client = OpenAI::client( 'sk-obfuscated' );
//print_r( $client );
$response = $client->threads()->createAndRun(
    [ 'assistant_id' => 'asst_obfuscated',
      'thread' => [
          'messages' =>
              [ 'role' => 'user',
                'content' => 'what time is 1706918400'
              ]
      ]
    ]
);

When I run "php assistant.php" I get


Fatal error: Uncaught OpenAI\Exceptions\ErrorException: 1 validation error for Request
body -> thread -> messages
  value is not a valid list (type=type_error.list) in C:\apache\htdocs\wordpress\wp-content\plugins\oik-ai\vendor\openai-php\client\src\Transporters\HttpTransporter.php:133
Stack trace:
#0 C:\apache\htdocs\wordpress\wp-content\plugins\oik-ai\vendor\openai-php\client\src\Transporters\HttpTransporter.php(57): OpenAI\Transporters\HttpTransporter->throwIfJsonError(Array, '{\n  "error": {\n...')
#1 C:\apache\htdocs\wordpress\wp-content\plugins\oik-ai\vendor\openai-php\client\src\Resources\Threads.php(51): OpenAI\Transporters\HttpTransporter->requestObject(Object(OpenAI\ValueObjects\Transporter\Payload))
#2 C:\apache\htdocs\wordpress\wp-content\plugins\oik-ai\tests\assistant.php(9): OpenAI\Resources\Threads->createAndRun(Array)
#3 {main}
  thrown in C:\apache\htdocs\wordpress\wp-content\plugins\oik-ai\vendor\openai-php\client\src\Transporters\HttpTransporter.php on line 133