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 465 forks source link

Implement vector store endpoints and add assistants API v2 support #405

Closed gehrisandro closed 1 month ago

gehrisandro commented 1 month ago

This PR implements all changes for the assistants v2 API.

I started this PR as a fresh base to start with the v2 API, because reviewing a combination of 2 PRs seems too hard:

johngiaccotto commented 1 month ago

Hey @gehrisandro - When do you think you'll have the PR for the v2 assistants, threads and threads run resources ready? Thanks for you work on this.

gehrisandro commented 1 month ago

The commit for v2 is in progress.

Probably it will ready to try it out, later today.

gehrisandro commented 1 month ago

v2 support committed. Some further testing needed before release.

If one has some time to test and give feedback, any help is highly appreciated.

DevJedis commented 1 month ago

Am coming from a temporary https://github.com/openai-php/client/issues/389#issuecomment-2116430558 hack that was working. When I cloned this branch to test, by trying both factory build and without.

The code:

try {
    $client->threads()->messages()->create($threadId, [
        'role' => 'user',
        'content' => $message,
    ]);
} catch (\Exception $e) {
    return $e->getMessage();
}

Errors:

PHP Warning:  Undefined array key "status" in /home/ubuntu/openai-php/src/Responses/Threads/Messages/ThreadMessageResponse.php on line 77

Warning: Undefined array key "status" in /home/ubuntu/openai-php/src/Responses/Threads/Messages/ThreadMessageResponse.php on line 77
PHP Warning:  Undefined array key "incomplete_details" in /home/ubuntu/openai-php/src/Responses/Threads/Messages/ThreadMessageResponse.php on line 78

Warning: Undefined array key "incomplete_details" in /home/ubuntu/openai-php/src/Responses/Threads/Messages/ThreadMessageResponse.php on line 78
PHP Warning:  Undefined array key "completed_at" in /home/ubuntu/openai-php/src/Responses/Threads/Messages/ThreadMessageResponse.php on line 79

Warning: Undefined array key "completed_at" in /home/ubuntu/openai-php/src/Responses/Threads/Messages/ThreadMessageResponse.php on line 79
PHP Warning:  Undefined array key "incomplete_at" in /home/ubuntu/openai-php/src/Responses/Threads/Messages/ThreadMessageResponse.php on line 80

Warning: Undefined array key "incomplete_at" in /home/ubuntu/openai-php/src/Responses/Threads/Messages/ThreadMessageResponse.php on line 80
PHP Fatal error:  Uncaught TypeError: OpenAI\Responses\Threads\Messages\ThreadMessageResponse::__construct(): Argument #5 ($status) must be of type string, null given, called in /home/ubuntu/openai-php/src/Responses/Threads/Messages/ThreadMessageResponse.php on line 72 and defined in /home/ubuntu/openai-php/src/Responses/Threads/Messages/ThreadMessageResponse.php:32
Stack trace:
#0 /home/ubuntu/openai-php/src/Responses/Threads/Messages/ThreadMessageResponse.php(72): OpenAI\Responses\Threads\Messages\ThreadMessageResponse->__construct()
#1 /home/ubuntu/openai-php/src/Resources/ThreadsMessages.php(31): OpenAI\Responses\Threads\Messages\ThreadMessageResponse::from()
#2 /home/ubuntu/openai-php/test.php(21): OpenAI\Resources\ThreadsMessages->create()
#3 {main}
  thrown in /home/ubuntu/openai-php/src/Responses/Threads/Messages/ThreadMessageResponse.php on line 32

Fatal error: Uncaught TypeError: OpenAI\Responses\Threads\Messages\ThreadMessageResponse::__construct(): Argument #5 ($status) must be of type string, null given, called in /home/ubuntu/openai-php/src/Responses/Threads/Messages/ThreadMessageResponse.php on line 72 and defined in /home/ubuntu/openai-php/src/Responses/Threads/Messages/ThreadMessageResponse.php:32
Stack trace:
#0 /home/ubuntu/openai-php/src/Responses/Threads/Messages/ThreadMessageResponse.php(72): OpenAI\Responses\Threads\Messages\ThreadMessageResponse->__construct()
#1 /home/ubuntu/openai-php/src/Resources/ThreadsMessages.php(31): OpenAI\Responses\Threads\Messages\ThreadMessageResponse::from()
#2 /home/ubuntu/openai-php/test.php(21): OpenAI\Resources\ThreadsMessages->create()
#3 {main}
  thrown in /home/ubuntu/openai-php/src/Responses/Threads/Messages/ThreadMessageResponse.php on line 32

Additional Context:

  1. PHP V: 8.3.7
  2. ThreadId used is one obtained from this same branch's Create Thread.
gehrisandro commented 1 month ago

Hi @DevJedis

Thanks for your comment. I have removed the fields, because they do not exist. This is an error in the OpenAI API documentation.

julescole commented 1 month ago

Hi, thanks for working on this. I'm getting the following error when using file search with the assistants v2 api.

"message": "Undefined array key \"quote\"", "exception": "ErrorException", "file": "/vendor/openai-php/client/src/Responses/Threads/Messages/ThreadMessageResponseContentTextAnnotationFileCitation.php", "line": 38,

gehrisandro commented 1 month ago

Hi @julescole

According to the docs, the "quote" field is required and not nullable: https://github.com/openai/openai-openapi/blob/master/openapi.yaml#L11380

But sadly, often the docs are not accurate. Could you please provide a dump of the plain JSON response?

gehrisandro commented 1 month ago

@julescole I made the change, but would still like to see the raw JSON. Thx 🙏🏼

julescole commented 1 month ago

@julescole I made the change, but would still like to see the raw JSON. Thx 🙏🏼

Great, that's fixed it thanks. None of my citations seem to have the quote set. Here's a list from my most recent request, sorry, i wasn't sure how to output the raw json easily.

["annotations"]=>
            array(16) {
              [0]=>
              array(5) {
                ["type"]=>
                string(13) "file_citation"
                ["text"]=>
                string(18) "【4:0†source】"
                ["start_index"]=>
                int(348)
                ["end_index"]=>
                int(360)
                ["file_citation"]=>
                array(1) {
                  ["file_id"]=>
                  string(29) "file-rx4SHF4Jra6MnHJwW87WI5aJ"
                }
              }
              [1]=>
              array(5) {
                ["type"]=>
                string(13) "file_citation"
                ["text"]=>
                string(18) "【4:1†source】"
                ["start_index"]=>
                int(360)
                ["end_index"]=>
                int(372)
                ["file_citation"]=>
                array(1) {
                  ["file_id"]=>
                  string(29) "file-rx4SHF4Jra6MnHJwW87WI5aJ"
                }
              }
              [2]=>
              array(5) {
                ["type"]=>
                string(13) "file_citation"
                ["text"]=>
                string(18) "【4:6†source】"
                ["start_index"]=>
                int(372)
                ["end_index"]=>
                int(384)
                ["file_citation"]=>
                array(1) {
                  ["file_id"]=>
                  string(29) "file-rx4SHF4Jra6MnHJwW87WI5aJ"
                }
              }
              [3]=>
              array(5) {
                ["type"]=>
                string(13) "file_citation"
                ["text"]=>
                string(18) "【4:3†source】"
                ["start_index"]=>
                int(568)
                ["end_index"]=>
                int(580)
                ["file_citation"]=>
                array(1) {
                  ["file_id"]=>
                  string(29) "file-bPPJdGRF4DJfTVrd0LNV8QjZ"
                }
              }
              [4]=>
              array(5) {
                ["type"]=>
                string(13) "file_citation"
                ["text"]=>
                string(18) "【4:4†source】"
                ["start_index"]=>
                int(580)
                ["end_index"]=>
                int(592)
                ["file_citation"]=>
                array(1) {
                  ["file_id"]=>
                  string(29) "file-bPPJdGRF4DJfTVrd0LNV8QjZ"
                }
              }
              [5]=>
              array(5) {
                ["type"]=>
                string(13) "file_citation"
                ["text"]=>
                string(18) "【4:2†source】"
                ["start_index"]=>
                int(739)
                ["end_index"]=>
                int(751)
                ["file_citation"]=>
                array(1) {
                  ["file_id"]=>
                  string(29) "file-qrfz7EwfSdrhy4QAZv1E6HW4"
                }
              }
              [6]=>
              array(5) {
                ["type"]=>
                string(13) "file_citation"
                ["text"]=>
                string(19) "【4:13†source】"
                ["start_index"]=>
                int(751)
                ["end_index"]=>
                int(764)
                ["file_citation"]=>
                array(1) {
                  ["file_id"]=>
                  string(29) "file-qrfz7EwfSdrhy4QAZv1E6HW4"
                }
              }
              [7]=>
              array(5) {
                ["type"]=>
                string(13) "file_citation"
                ["text"]=>
                string(18) "【4:5†source】"
                ["start_index"]=>
                int(877)
                ["end_index"]=>
                int(889)
                ["file_citation"]=>
                array(1) {
                  ["file_id"]=>
                  string(29) "file-XL0onw6lc98x2S2cHTjJgCGS"
                }
              }
              [8]=>
              array(5) {
                ["type"]=>
                string(13) "file_citation"
                ["text"]=>
                string(18) "【4:7†source】"
                ["start_index"]=>
                int(1016)
                ["end_index"]=>
                int(1028)
                ["file_citation"]=>
                array(1) {
                  ["file_id"]=>
                  string(29) "file-TcOuVW9lGVLIWyo5Jqfn9YUI"
                }
              }
              [9]=>
              array(5) {
                ["type"]=>
                string(13) "file_citation"
                ["text"]=>
                string(19) "【4:16†source】"
                ["start_index"]=>
                int(1028)
                ["end_index"]=>
                int(1041)
                ["file_citation"]=>
                array(1) {
                  ["file_id"]=>
                  string(29) "file-p03m489p4UyKaa9p1OqjNeP0"
                }
              }
              [10]=>
              array(5) {
                ["type"]=>
                string(13) "file_citation"
                ["text"]=>
                string(19) "【4:15†source】"
                ["start_index"]=>
                int(1173)
                ["end_index"]=>
                int(1186)
                ["file_citation"]=>
                array(1) {
                  ["file_id"]=>
                  string(29) "file-kIdUeZTHSsWx4UPAOjDTOEMX"
                }
              }
              [11]=>
              array(5) {
                ["type"]=>
                string(13) "file_citation"
                ["text"]=>
                string(19) "【4:17†source】"
                ["start_index"]=>
                int(1186)
                ["end_index"]=>
                int(1199)
                ["file_citation"]=>
                array(1) {
                  ["file_id"]=>
                  string(29) "file-z5y0wppoZmLjJ8fbTffR8AJ0"
                }
              }
              [12]=>
              array(5) {
                ["type"]=>
                string(13) "file_citation"
                ["text"]=>
                string(18) "【4:8†source】"
                ["start_index"]=>
                int(1330)
                ["end_index"]=>
                int(1342)
                ["file_citation"]=>
                array(1) {
                  ["file_id"]=>
                  string(29) "file-RrLrJpldMqlmfhUlapVcjr0J"
                }
              }
              [13]=>
              array(5) {
                ["type"]=>
                string(13) "file_citation"
                ["text"]=>
                string(19) "【4:10†source】"
                ["start_index"]=>
                int(1342)
                ["end_index"]=>
                int(1355)
                ["file_citation"]=>
                array(1) {
                  ["file_id"]=>
                  string(29) "file-RrLrJpldMqlmfhUlapVcjr0J"
                }
              }
              [14]=>
              array(5) {
                ["type"]=>
                string(13) "file_citation"
                ["text"]=>
                string(19) "【4:19†source】"
                ["start_index"]=>
                int(1503)
                ["end_index"]=>
                int(1516)
                ["file_citation"]=>
                array(1) {
                  ["file_id"]=>
                  string(29) "file-i8f9Tz3K5IPtyxYwiqiwAU2E"
                }
              }
              [15]=>
              array(5) {
                ["type"]=>
                string(13) "file_citation"
                ["text"]=>
                string(19) "【4:12†source】"
                ["start_index"]=>
                int(1772)
                ["end_index"]=>
                int(1785)
                ["file_citation"]=>
                array(1) {
                  ["file_id"]=>
                  string(29) "file-WTORzTtPIbzH1BaCWGHgBJgB"
                }
              }
francost15 commented 2 weeks ago

How to implement the vector store with the wizard so that it can read it?