From a63f347a195c60ea83eff54cf134b67748f7ccb2 Mon Sep 17 00:00:00 2001 From: joacir Date: Mon, 27 Nov 2023 17:58:15 -0300 Subject: [PATCH 01/30] Create assistants --- src/OpenAi.php | 16 +++++++++++++++- src/Url.php | 9 +++++++++ tests/OpenAiTest.php | 15 +++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/OpenAi.php b/src/OpenAi.php index ed38d98..8dc437d 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -428,6 +428,20 @@ public function embeddings($opts) return $this->sendRequest($url, 'POST', $opts); } + /** + * @param $opts + * @return bool|string + */ + public function createAssistant($opts) + { + $opts['model'] = $opts['model'] ?? $this->chatModel; + $this->headers[] = 'OpenAI-Beta: assistants=v1'; + $url = Url::assistantsUrl(); + $this->baseUrl($url); + + return $this->sendRequest($url, 'POST', $opts); + } + /** * @param int $timeout */ @@ -549,7 +563,7 @@ private function sendRequest(string $url, string $method, array $opts = []) curl_close($curl); if (!$response) throw new Exception(curl_error($curl)); - + return $response; } diff --git a/src/Url.php b/src/Url.php index 992712c..acd6c47 100644 --- a/src/Url.php +++ b/src/Url.php @@ -160,4 +160,13 @@ public static function chatUrl(): string { return self::OPEN_AI_URL . "/chat/completions"; } + + /** + * @param + * @return string + */ + public static function assistantsUrl(): string + { + return self::OPEN_AI_URL . "/assistants"; + } } diff --git a/tests/OpenAiTest.php b/tests/OpenAiTest.php index 7d15198..c85789b 100644 --- a/tests/OpenAiTest.php +++ b/tests/OpenAiTest.php @@ -254,3 +254,18 @@ $this->assertStringContainsString('text', $result); })->group('working'); + +it('should create a assistant', function () use ($open_ai) { + $assistant = $open_ai->createAssistant([ + 'model' => 'gpt-3.5-turbo', + 'name' => 'my assistant', + 'description' => 'my assistant description', + 'instructions' => 'you should cordially help me', + 'tools' => [], + 'file_ids' => [], + ]); + + $this->assertStringContainsString('id', $assistant); + $this->assertStringContainsString('"object": "assistant"', $assistant); + $this->assertStringContainsString('"name": "my assistant"', $assistant); +})->group('working'); From 368630531a40cd36cbbe070019a29c852b20e3f3 Mon Sep 17 00:00:00 2001 From: joacir Date: Tue, 28 Nov 2023 09:48:34 -0300 Subject: [PATCH 02/30] Retrieve assistant --- src/OpenAi.php | 17 +++++++++++++++-- tests/OpenAiTest.php | 10 +++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/OpenAi.php b/src/OpenAi.php index 8dc437d..85bb1d1 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -429,19 +429,32 @@ public function embeddings($opts) } /** - * @param $opts + * @param array $opts * @return bool|string */ public function createAssistant($opts) { $opts['model'] = $opts['model'] ?? $this->chatModel; - $this->headers[] = 'OpenAI-Beta: assistants=v1'; + $this->headers[] = 'OpenAI-Beta: assistants=v1'; $url = Url::assistantsUrl(); $this->baseUrl($url); return $this->sendRequest($url, 'POST', $opts); } + /** + * @param string $id + * @return bool|string + */ + public function retrieveAssistant($id) + { + $this->headers[] = 'OpenAI-Beta: assistants=v1'; + $url = Url::assistantsUrl() . '/' . $id; + $this->baseUrl($url); + + return $this->sendRequest($url, 'GET'); + } + /** * @param int $timeout */ diff --git a/tests/OpenAiTest.php b/tests/OpenAiTest.php index c85789b..271e4b1 100644 --- a/tests/OpenAiTest.php +++ b/tests/OpenAiTest.php @@ -255,7 +255,7 @@ $this->assertStringContainsString('text', $result); })->group('working'); -it('should create a assistant', function () use ($open_ai) { +it('should handle create a assistant', function () use ($open_ai) { $assistant = $open_ai->createAssistant([ 'model' => 'gpt-3.5-turbo', 'name' => 'my assistant', @@ -269,3 +269,11 @@ $this->assertStringContainsString('"object": "assistant"', $assistant); $this->assertStringContainsString('"name": "my assistant"', $assistant); })->group('working'); + +it('should handle retrieve a assistant', function () use ($open_ai) { + $assistant = $open_ai->retrieveAssistant('asst_rzJqKRfpQI2JMj1gYGKQOZZo'); + + $this->assertStringContainsString('id', $assistant); + $this->assertStringContainsString('"object": "assistant"', $assistant); + $this->assertStringContainsString('"name": "my assistant"', $assistant); +})->group('working'); From e2a76695addaf9a8df96b8a64139136c9683d187 Mon Sep 17 00:00:00 2001 From: joacir Date: Tue, 28 Nov 2023 10:11:58 -0300 Subject: [PATCH 03/30] Modify assistant --- src/OpenAi.php | 14 ++++++++++++++ tests/OpenAiTest.php | 11 +++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/OpenAi.php b/src/OpenAi.php index 85bb1d1..9de8115 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -455,6 +455,20 @@ public function retrieveAssistant($id) return $this->sendRequest($url, 'GET'); } + /** + * @param string $id + * @param array $opts + * @return bool|string + */ + public function modifyAssistant($id, $opts) + { + $this->headers[] = 'OpenAI-Beta: assistants=v1'; + $url = Url::assistantsUrl() . '/' . $id; + $this->baseUrl($url); + + return $this->sendRequest($url, 'POST', $opts); + } + /** * @param int $timeout */ diff --git a/tests/OpenAiTest.php b/tests/OpenAiTest.php index 271e4b1..35f484b 100644 --- a/tests/OpenAiTest.php +++ b/tests/OpenAiTest.php @@ -277,3 +277,14 @@ $this->assertStringContainsString('"object": "assistant"', $assistant); $this->assertStringContainsString('"name": "my assistant"', $assistant); })->group('working'); + +it('should handle modify a assistant', function () use ($open_ai) { + $assistant = $open_ai->modifyAssistant('asst_rzJqKRfpQI2JMj1gYGKQOZZo', [ + 'name' => 'my modified assistant', + 'instructions' => 'you should cordially help me again', + ]); + + $this->assertStringContainsString('id', $assistant); + $this->assertStringContainsString('"object": "assistant"', $assistant); + $this->assertStringContainsString('"name": "my modified assistant"', $assistant); +})->group('working'); From f79adc4a26603519511d5a8a0cf976aeeb7e2383 Mon Sep 17 00:00:00 2001 From: joacir Date: Tue, 28 Nov 2023 10:13:56 -0300 Subject: [PATCH 04/30] Delete assistant --- src/OpenAi.php | 13 +++++++++++++ tests/OpenAiTest.php | 7 +++++++ 2 files changed, 20 insertions(+) diff --git a/src/OpenAi.php b/src/OpenAi.php index 9de8115..3f70e8a 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -469,6 +469,19 @@ public function modifyAssistant($id, $opts) return $this->sendRequest($url, 'POST', $opts); } + /** + * @param string $id + * @return bool|string + */ + public function deleteAssistant($id) + { + $this->headers[] = 'OpenAI-Beta: assistants=v1'; + $url = Url::assistantsUrl() . '/' . $id; + $this->baseUrl($url); + + return $this->sendRequest($url, 'DELETE'); + } + /** * @param int $timeout */ diff --git a/tests/OpenAiTest.php b/tests/OpenAiTest.php index 35f484b..9e8a1ce 100644 --- a/tests/OpenAiTest.php +++ b/tests/OpenAiTest.php @@ -288,3 +288,10 @@ $this->assertStringContainsString('"object": "assistant"', $assistant); $this->assertStringContainsString('"name": "my modified assistant"', $assistant); })->group('working'); + +it('should handle delete a assistant', function () use ($open_ai) { + $assistant = $open_ai->deleteAssistant('asst_rzJqKRfpQI2JMj1gYGKQOZZo'); + + $this->assertStringContainsString('id', $assistant); + $this->assertStringContainsString('"deleted": true', $assistant); +})->group('working'); From 7c3113a9059efab61eecd5b5f607aa512c47e772 Mon Sep 17 00:00:00 2001 From: joacir Date: Tue, 28 Nov 2023 10:22:13 -0300 Subject: [PATCH 05/30] List assistants --- src/OpenAi.php | 16 ++++++++++++++++ tests/OpenAiTest.php | 9 +++++++++ 2 files changed, 25 insertions(+) diff --git a/src/OpenAi.php b/src/OpenAi.php index 3f70e8a..0789f2e 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -482,6 +482,22 @@ public function deleteAssistant($id) return $this->sendRequest($url, 'DELETE'); } + /** + * @param array $query + * @return bool|string + */ + public function listAssistants($query = []) + { + $this->headers[] = 'OpenAI-Beta: assistants=v1'; + $url = Url::assistantsUrl(); + if (count($query) > 0) { + $url .= '?' . http_build_query($query); + } + $this->baseUrl($url); + + return $this->sendRequest($url, 'GET'); + } + /** * @param int $timeout */ diff --git a/tests/OpenAiTest.php b/tests/OpenAiTest.php index 9e8a1ce..c21e0a3 100644 --- a/tests/OpenAiTest.php +++ b/tests/OpenAiTest.php @@ -295,3 +295,12 @@ $this->assertStringContainsString('id', $assistant); $this->assertStringContainsString('"deleted": true', $assistant); })->group('working'); + +it('should handle list assistants', function () use ($open_ai) { + $query = ['limit' => 10]; + $assistants = $open_ai->listAssistants($query); + + $this->assertStringContainsString('"object": "list"', $assistants); + $this->assertStringContainsString('data', $assistants); + $this->assertStringContainsString('id', $assistants); +})->group('working'); From bab5f0577ba5e0c3ff8a798db90c88fe6b5e1f52 Mon Sep 17 00:00:00 2001 From: joacir Date: Tue, 28 Nov 2023 10:57:16 -0300 Subject: [PATCH 06/30] Create a assistant file --- files/assistant-file.txt | 1 + src/OpenAi.php | 14 ++++++++++++++ tests/OpenAiTest.php | 14 ++++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 files/assistant-file.txt diff --git a/files/assistant-file.txt b/files/assistant-file.txt new file mode 100644 index 0000000..8e94277 --- /dev/null +++ b/files/assistant-file.txt @@ -0,0 +1 @@ +you are a exceptional assistant \ No newline at end of file diff --git a/src/OpenAi.php b/src/OpenAi.php index 0789f2e..92cdffc 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -498,6 +498,20 @@ public function listAssistants($query = []) return $this->sendRequest($url, 'GET'); } + /** + * @param string $id + * @param string $fileId + * @return bool|string + */ + public function createAssistantFile($id, $fileId) + { + $this->headers[] = 'OpenAI-Beta: assistants=v1'; + $url = Url::assistantsUrl() . '/' . $id . '/files'; + $this->baseUrl($url); + + return $this->sendRequest($url, 'POST', ['file_id' => $fileId]); + } + /** * @param int $timeout */ diff --git a/tests/OpenAiTest.php b/tests/OpenAiTest.php index c21e0a3..e4d6322 100644 --- a/tests/OpenAiTest.php +++ b/tests/OpenAiTest.php @@ -304,3 +304,17 @@ $this->assertStringContainsString('data', $assistants); $this->assertStringContainsString('id', $assistants); })->group('working'); + +it('should handle create a assistant file', function () use ($open_ai) { + $upload = curl_file_create(__DIR__ . '/../files/assistant-file.txt'); + $file = json_decode($open_ai->uploadFile([ + 'purpose' => 'assistants', + 'file' => $upload, + ]), true); + $assistantId = 'asst_zT1LLZ8dWnuFCrMFzqxFOhzz'; + + $assistantFile = $open_ai->createAssistantFile($assistantId, $file['id']); + + $this->assertStringContainsString('id', $assistantFile); + $this->assertStringContainsString('"object": "assistant.file"', $assistantFile); +})->group('working'); From cd45d47ebb2b1870e0a020af8dbdde51cb7c19ae Mon Sep 17 00:00:00 2001 From: joacir Date: Tue, 28 Nov 2023 11:09:08 -0300 Subject: [PATCH 07/30] Retrieve a assistant file --- src/OpenAi.php | 14 ++++++++++++++ tests/OpenAiTest.php | 10 ++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/OpenAi.php b/src/OpenAi.php index 92cdffc..2e4ebb9 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -512,6 +512,20 @@ public function createAssistantFile($id, $fileId) return $this->sendRequest($url, 'POST', ['file_id' => $fileId]); } + /** + * @param string $id + * @param string $fileId + * @return bool|string + */ + public function retrieveAssistantFile($id, $fileId) + { + $this->headers[] = 'OpenAI-Beta: assistants=v1'; + $url = Url::assistantsUrl() . '/' . $id . '/files/' . $fileId; + $this->baseUrl($url); + + return $this->sendRequest($url, 'GET'); + } + /** * @param int $timeout */ diff --git a/tests/OpenAiTest.php b/tests/OpenAiTest.php index e4d6322..ae3b9e6 100644 --- a/tests/OpenAiTest.php +++ b/tests/OpenAiTest.php @@ -318,3 +318,13 @@ $this->assertStringContainsString('id', $assistantFile); $this->assertStringContainsString('"object": "assistant.file"', $assistantFile); })->group('working'); + +it('should handle retrieve a assistant file', function () use ($open_ai) { + $assistantId = 'asst_zT1LLZ8dWnuFCrMFzqxFOhzz'; + $fileId = 'file-CRLcY63DiHphWuBrmDWZVCgA'; + + $assistantFile = $open_ai->retrieveAssistantFile($assistantId, $fileId); + + $this->assertStringContainsString('id', $assistantFile); + $this->assertStringContainsString('"object": "assistant.file"', $assistantFile); +})->group('working'); From 98d4bb172fbe31e6a3f8aab8c291e3d5a9b295d4 Mon Sep 17 00:00:00 2001 From: joacir Date: Tue, 28 Nov 2023 11:16:09 -0300 Subject: [PATCH 08/30] List assistant files --- src/OpenAi.php | 17 +++++++++++++++++ tests/OpenAiTest.php | 11 +++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/OpenAi.php b/src/OpenAi.php index 2e4ebb9..a4bfd51 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -526,6 +526,23 @@ public function retrieveAssistantFile($id, $fileId) return $this->sendRequest($url, 'GET'); } + /** + * @param string $id + * @param array $query + * @return bool|string + */ + public function listAssistantFiles($id, $query = []) + { + $this->headers[] = 'OpenAI-Beta: assistants=v1'; + $url = Url::assistantsUrl() . '/' . $id . '/files'; + if (count($query) > 0) { + $url .= '?' . http_build_query($query); + } + $this->baseUrl($url); + + return $this->sendRequest($url, 'GET'); + } + /** * @param int $timeout */ diff --git a/tests/OpenAiTest.php b/tests/OpenAiTest.php index ae3b9e6..5e11a3b 100644 --- a/tests/OpenAiTest.php +++ b/tests/OpenAiTest.php @@ -328,3 +328,14 @@ $this->assertStringContainsString('id', $assistantFile); $this->assertStringContainsString('"object": "assistant.file"', $assistantFile); })->group('working'); + +it('should handle list assistant files', function () use ($open_ai) { + $assistantId = 'asst_zT1LLZ8dWnuFCrMFzqxFOhzz'; + $query = ['limit' => 10]; + + $assistants = $open_ai->listAssistantFiles($assistantId, $query); + + $this->assertStringContainsString('"object": "list"', $assistants); + $this->assertStringContainsString('data', $assistants); + $this->assertStringContainsString('id', $assistants); +})->group('working'); From 0317b189d80363bfa119791df0b7bffe8db7661d Mon Sep 17 00:00:00 2001 From: joacir Date: Tue, 28 Nov 2023 11:22:15 -0300 Subject: [PATCH 09/30] Delete assistant file --- src/OpenAi.php | 14 ++++++++++++++ tests/OpenAiTest.php | 18 ++++++++++++++---- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/OpenAi.php b/src/OpenAi.php index a4bfd51..94a6b64 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -543,6 +543,20 @@ public function listAssistantFiles($id, $query = []) return $this->sendRequest($url, 'GET'); } + /** + * @param string $id + * @param string $fileId + * @return bool|string + */ + public function deleteAssistantFile($id, $fileId) + { + $this->headers[] = 'OpenAI-Beta: assistants=v1'; + $url = Url::assistantsUrl() . '/' . $id . '/files/' . $fileId; + $this->baseUrl($url); + + return $this->sendRequest($url, 'DELETE'); + } + /** * @param int $timeout */ diff --git a/tests/OpenAiTest.php b/tests/OpenAiTest.php index 5e11a3b..783bb54 100644 --- a/tests/OpenAiTest.php +++ b/tests/OpenAiTest.php @@ -333,9 +333,19 @@ $assistantId = 'asst_zT1LLZ8dWnuFCrMFzqxFOhzz'; $query = ['limit' => 10]; - $assistants = $open_ai->listAssistantFiles($assistantId, $query); + $files = $open_ai->listAssistantFiles($assistantId, $query); - $this->assertStringContainsString('"object": "list"', $assistants); - $this->assertStringContainsString('data', $assistants); - $this->assertStringContainsString('id', $assistants); + $this->assertStringContainsString('"object": "list"', $files); + $this->assertStringContainsString('data', $files); + $this->assertStringContainsString('id', $files); +})->group('working'); + +it('should handle delete a assistant file', function () use ($open_ai) { + $assistantId = 'asst_zT1LLZ8dWnuFCrMFzqxFOhzz'; + $fileId = 'file-CRLcY63DiHphWuBrmDWZVCgA'; + + $file = $open_ai->deleteAssistantFile($assistantId, $fileId); + + $this->assertStringContainsString('id', $file); + $this->assertStringContainsString('"deleted": true', $file); })->group('working'); From dd74d7711a7678a70a22934d833a53f800a2a635 Mon Sep 17 00:00:00 2001 From: joacir Date: Tue, 28 Nov 2023 15:11:50 -0300 Subject: [PATCH 10/30] Create thread --- src/OpenAi.php | 13 +++++++++++++ src/Url.php | 9 +++++++++ tests/OpenAiTest.php | 15 +++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/src/OpenAi.php b/src/OpenAi.php index 94a6b64..5333994 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -557,6 +557,19 @@ public function deleteAssistantFile($id, $fileId) return $this->sendRequest($url, 'DELETE'); } + /** + * @param array $opts + * @return bool|string + */ + public function createThread($opts) + { + $this->headers[] = 'OpenAI-Beta: assistants=v1'; + $url = Url::threadsUrl(); + $this->baseUrl($url); + + return $this->sendRequest($url, 'POST', $opts); + } + /** * @param int $timeout */ diff --git a/src/Url.php b/src/Url.php index acd6c47..4808c77 100644 --- a/src/Url.php +++ b/src/Url.php @@ -169,4 +169,13 @@ public static function assistantsUrl(): string { return self::OPEN_AI_URL . "/assistants"; } + + /** + * @param + * @return string + */ + public static function threadsUrl(): string + { + return self::OPEN_AI_URL . "/threads"; + } } diff --git a/tests/OpenAiTest.php b/tests/OpenAiTest.php index 783bb54..78199f0 100644 --- a/tests/OpenAiTest.php +++ b/tests/OpenAiTest.php @@ -349,3 +349,18 @@ $this->assertStringContainsString('id', $file); $this->assertStringContainsString('"deleted": true', $file); })->group('working'); + +it('should handle create a thread', function () use ($open_ai) { + $thread = $open_ai->createThread([ + 'messages' => [ + [ + 'role' => 'user', + 'content' => 'Hello, what is AI?', + 'file_ids' => [], + ], + ], + ]); + + $this->assertStringContainsString('id', $thread); + $this->assertStringContainsString('"object": "thread"', $thread); +})->group('working'); From 681edcbe12f26058a19720e8fd931c4629344346 Mon Sep 17 00:00:00 2001 From: joacir Date: Tue, 28 Nov 2023 15:20:08 -0300 Subject: [PATCH 11/30] Retrieve a thread --- src/OpenAi.php | 15 ++++++++++++++- tests/OpenAiTest.php | 7 +++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/OpenAi.php b/src/OpenAi.php index 5333994..625f140 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -561,7 +561,7 @@ public function deleteAssistantFile($id, $fileId) * @param array $opts * @return bool|string */ - public function createThread($opts) + public function createThread($opts = []) { $this->headers[] = 'OpenAI-Beta: assistants=v1'; $url = Url::threadsUrl(); @@ -570,6 +570,19 @@ public function createThread($opts) return $this->sendRequest($url, 'POST', $opts); } + /** + * @param string $id + * @return bool|string + */ + public function retrieveThread($id) + { + $this->headers[] = 'OpenAI-Beta: assistants=v1'; + $url = Url::threadsUrl() . '/' . $id; + $this->baseUrl($url); + + return $this->sendRequest($url, 'GET'); + } + /** * @param int $timeout */ diff --git a/tests/OpenAiTest.php b/tests/OpenAiTest.php index 78199f0..5d02a3a 100644 --- a/tests/OpenAiTest.php +++ b/tests/OpenAiTest.php @@ -364,3 +364,10 @@ $this->assertStringContainsString('id', $thread); $this->assertStringContainsString('"object": "thread"', $thread); })->group('working'); + +it('should handle retrieve a thread', function () use ($open_ai) { + $thread = $open_ai->retrieveThread('thread_GHGU1zEOVD5z2EXs3e8zU55S'); + + $this->assertStringContainsString('id', $thread); + $this->assertStringContainsString('"object": "thread"', $thread); +})->group('working'); From 776adeb3da8f26b1e45188ac8be4d2277624d4f7 Mon Sep 17 00:00:00 2001 From: joacir Date: Tue, 28 Nov 2023 15:30:24 -0300 Subject: [PATCH 12/30] Modify thread --- src/OpenAi.php | 14 ++++++++++++++ tests/OpenAiTest.php | 11 +++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/OpenAi.php b/src/OpenAi.php index 625f140..ab23b60 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -583,6 +583,20 @@ public function retrieveThread($id) return $this->sendRequest($url, 'GET'); } + /** + * @param string $id + * @param array $opts + * @return bool|string + */ + public function modifyThread($id, $opts) + { + $this->headers[] = 'OpenAI-Beta: assistants=v1'; + $url = Url::threadsUrl() . '/' . $id; + $this->baseUrl($url); + + return $this->sendRequest($url, 'POST', $opts); + } + /** * @param int $timeout */ diff --git a/tests/OpenAiTest.php b/tests/OpenAiTest.php index 5d02a3a..87d5d62 100644 --- a/tests/OpenAiTest.php +++ b/tests/OpenAiTest.php @@ -371,3 +371,14 @@ $this->assertStringContainsString('id', $thread); $this->assertStringContainsString('"object": "thread"', $thread); })->group('working'); + +it('should handle modify a thread', function () use ($open_ai) { + $thread = $open_ai->modifyThread('thread_GHGU1zEOVD5z2EXs3e8zU55S', [ + 'metadata' => [ + 'test' => '1234abcd', + ], + ]); + + $this->assertStringContainsString('id', $thread); + $this->assertStringContainsString('"object": "thread"', $thread); +})->group('working'); From e2c06e38c71c4da0e29214d24d5a333a5b93b594 Mon Sep 17 00:00:00 2001 From: joacir Date: Tue, 28 Nov 2023 15:32:20 -0300 Subject: [PATCH 13/30] Delete a thread --- src/OpenAi.php | 13 +++++++++++++ tests/OpenAiTest.php | 7 +++++++ 2 files changed, 20 insertions(+) diff --git a/src/OpenAi.php b/src/OpenAi.php index ab23b60..ee77f69 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -597,6 +597,19 @@ public function modifyThread($id, $opts) return $this->sendRequest($url, 'POST', $opts); } + /** + * @param string $id + * @return bool|string + */ + public function deleteThread($id) + { + $this->headers[] = 'OpenAI-Beta: assistants=v1'; + $url = Url::threadsUrl() . '/' . $id; + $this->baseUrl($url); + + return $this->sendRequest($url, 'DELETE'); + } + /** * @param int $timeout */ diff --git a/tests/OpenAiTest.php b/tests/OpenAiTest.php index 87d5d62..cd10dcb 100644 --- a/tests/OpenAiTest.php +++ b/tests/OpenAiTest.php @@ -382,3 +382,10 @@ $this->assertStringContainsString('id', $thread); $this->assertStringContainsString('"object": "thread"', $thread); })->group('working'); + +it('should handle delete a thread', function () use ($open_ai) { + $thread = $open_ai->deleteThread('thread_GHGU1zEOVD5z2EXs3e8zU55S'); + + $this->assertStringContainsString('id', $thread); + $this->assertStringContainsString('"deleted": true', $thread); +})->group('working'); From 524c4d617cade416b4db977829956efa92f50c16 Mon Sep 17 00:00:00 2001 From: joacir Date: Tue, 28 Nov 2023 15:46:07 -0300 Subject: [PATCH 14/30] Create message wthin thread --- src/OpenAi.php | 14 ++++++++++++++ tests/OpenAiTest.php | 11 +++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/OpenAi.php b/src/OpenAi.php index ee77f69..dafce00 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -610,6 +610,20 @@ public function deleteThread($id) return $this->sendRequest($url, 'DELETE'); } + /** + * @param string $id + * @param array $opts + * @return bool|string + */ + public function createThreadMessage($id, $opts) + { + $this->headers[] = 'OpenAI-Beta: assistants=v1'; + $url = Url::threadsUrl() . '/' . $id . '/messages'; + $this->baseUrl($url); + + return $this->sendRequest($url, 'POST', $opts); + } + /** * @param int $timeout */ diff --git a/tests/OpenAiTest.php b/tests/OpenAiTest.php index cd10dcb..fc38e99 100644 --- a/tests/OpenAiTest.php +++ b/tests/OpenAiTest.php @@ -389,3 +389,14 @@ $this->assertStringContainsString('id', $thread); $this->assertStringContainsString('"deleted": true', $thread); })->group('working'); + +it('should handle create a message within thread', function () use ($open_ai) { + $thread = json_decode($open_ai->createThread(), true); + $message = $open_ai->createThreadMessage($thread['id'], [ + 'role' => 'user', + 'content' => 'How does AI work? Explain it in simple terms.', + ]); + + $this->assertStringContainsString('id', $message); + $this->assertStringContainsString('"object": "thread.message"', $message); +})->group('working'); From 7e2be2b1bd2ff0c9df2dd5c928e8c676de8ea022 Mon Sep 17 00:00:00 2001 From: joacir Date: Tue, 28 Nov 2023 15:56:37 -0300 Subject: [PATCH 15/30] Retrieve a message within thread --- src/OpenAi.php | 14 ++++++++++++++ tests/OpenAiTest.php | 9 +++++++++ 2 files changed, 23 insertions(+) diff --git a/src/OpenAi.php b/src/OpenAi.php index dafce00..e43cc65 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -624,6 +624,20 @@ public function createThreadMessage($id, $opts) return $this->sendRequest($url, 'POST', $opts); } + /** + * @param string $id + * @param string $messageId + * @return bool|string + */ + public function retrieveThreadMessage($id, $messageId) + { + $this->headers[] = 'OpenAI-Beta: assistants=v1'; + $url = Url::threadsUrl() . '/' . $id . '/messages/' . $messageId; + $this->baseUrl($url); + + return $this->sendRequest($url, 'GET'); + } + /** * @param int $timeout */ diff --git a/tests/OpenAiTest.php b/tests/OpenAiTest.php index fc38e99..063bab3 100644 --- a/tests/OpenAiTest.php +++ b/tests/OpenAiTest.php @@ -400,3 +400,12 @@ $this->assertStringContainsString('id', $message); $this->assertStringContainsString('"object": "thread.message"', $message); })->group('working'); + +it('should handle retrieve a message within thread', function () use ($open_ai) { + $threadId = 'thread_d86alfR2rfF7rASyV4V7hicz'; + $messageId = 'msg_d37P5XgREsm6BItOcppnBO1b'; + $message = $open_ai->retrieveThreadMessage($threadId, $messageId); + + $this->assertStringContainsString('id', $message); + $this->assertStringContainsString('"object": "thread.message"', $message); +})->group('working'); From 80dbda1f192667a49dcbbab9b8340aaee675a42c Mon Sep 17 00:00:00 2001 From: joacir Date: Tue, 28 Nov 2023 16:11:48 -0300 Subject: [PATCH 16/30] Modify message within a thread --- src/OpenAi.php | 15 +++++++++++++++ tests/OpenAiTest.php | 13 +++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/OpenAi.php b/src/OpenAi.php index e43cc65..be2e838 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -638,6 +638,21 @@ public function retrieveThreadMessage($id, $messageId) return $this->sendRequest($url, 'GET'); } + /** + * @param string $id + * @param string $messageId + * @param array $opts + * @return bool|string + */ + public function modifyThreadMessage($id, $messageId, $opts) + { + $this->headers[] = 'OpenAI-Beta: assistants=v1'; + $url = Url::threadsUrl() . '/' . $id . '/messages/' . $messageId; + $this->baseUrl($url); + + return $this->sendRequest($url, 'POST', $opts); + } + /** * @param int $timeout */ diff --git a/tests/OpenAiTest.php b/tests/OpenAiTest.php index 063bab3..c6acd00 100644 --- a/tests/OpenAiTest.php +++ b/tests/OpenAiTest.php @@ -409,3 +409,16 @@ $this->assertStringContainsString('id', $message); $this->assertStringContainsString('"object": "thread.message"', $message); })->group('working'); + +it('should handle modify a message within thread', function () use ($open_ai) { + $threadId = 'thread_d86alfR2rfF7rASyV4V7hicz'; + $messageId = 'msg_d37P5XgREsm6BItOcppnBO1b'; + $message = $open_ai->modifyThreadMessage($threadId, $messageId, [ + 'metadata' => [ + 'test' => '1234abcd', + ], + ]); + + $this->assertStringContainsString('id', $message); + $this->assertStringContainsString('"object": "thread.message"', $message); +})->group('working'); From c790ac1abcef13092c346240ac8b86aedd9723a5 Mon Sep 17 00:00:00 2001 From: joacir Date: Tue, 28 Nov 2023 16:22:44 -0300 Subject: [PATCH 17/30] List thread messages --- src/OpenAi.php | 17 +++++++++++++++++ tests/OpenAiTest.php | 10 ++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/OpenAi.php b/src/OpenAi.php index be2e838..4858971 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -653,6 +653,23 @@ public function modifyThreadMessage($id, $messageId, $opts) return $this->sendRequest($url, 'POST', $opts); } + /** + * @param string $id + * @param array $query + * @return bool|string + */ + public function listThreadMessages($id, $query = []) + { + $this->headers[] = 'OpenAI-Beta: assistants=v1'; + $url = Url::threadsUrl() . '/' . $id . '/messages'; + if (count($query) > 0) { + $url .= '?' . http_build_query($query); + } + $this->baseUrl($url); + + return $this->sendRequest($url, 'GET'); + } + /** * @param int $timeout */ diff --git a/tests/OpenAiTest.php b/tests/OpenAiTest.php index c6acd00..e7e2977 100644 --- a/tests/OpenAiTest.php +++ b/tests/OpenAiTest.php @@ -422,3 +422,13 @@ $this->assertStringContainsString('id', $message); $this->assertStringContainsString('"object": "thread.message"', $message); })->group('working'); + +it('should handle list messages within thread', function () use ($open_ai) { + $threadId = 'thread_d86alfR2rfF7rASyV4V7hicz'; + $query = ['limit' => 10]; + $messages = $open_ai->listThreadMessages($threadId, $query); + + $this->assertStringContainsString('id', $messages); + $this->assertStringContainsString('"object": "list"', $messages); + $this->assertStringContainsString('data', $messages); +})->group('working'); From 8a6f6397be957dfb11331c0292c69c696874869d Mon Sep 17 00:00:00 2001 From: joacir Date: Tue, 28 Nov 2023 16:32:34 -0300 Subject: [PATCH 18/30] Retrieve a file within message --- src/OpenAi.php | 15 +++++++++++++++ tests/OpenAiTest.php | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/OpenAi.php b/src/OpenAi.php index 4858971..301e08d 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -670,6 +670,21 @@ public function listThreadMessages($id, $query = []) return $this->sendRequest($url, 'GET'); } + /** + * @param string $id + * @param string $messageId + * @param string $fileId + * @return bool|string + */ + public function retrieveMessageFile($id, $messageId, $fileId) + { + $this->headers[] = 'OpenAI-Beta: assistants=v1'; + $url = Url::threadsUrl() . '/' . $id . '/messages/' . $messageId . '/files/' . $fileId; + $this->baseUrl($url); + + return $this->sendRequest($url, 'GET'); + } + /** * @param int $timeout */ diff --git a/tests/OpenAiTest.php b/tests/OpenAiTest.php index e7e2977..1cc1ae5 100644 --- a/tests/OpenAiTest.php +++ b/tests/OpenAiTest.php @@ -432,3 +432,18 @@ $this->assertStringContainsString('"object": "list"', $messages); $this->assertStringContainsString('data', $messages); })->group('working'); + +it('should handle retrieve a file within message', function () use ($open_ai) { + $threadId = 'thread_d86alfR2rfF7rASyV4V7hicz'; + $fileId = 'file-CRLcY63DiHphWuBrmDWZVCgA'; + $message = json_decode($open_ai->createThreadMessage($threadId, [ + 'role' => 'user', + 'content' => 'How does AI work? Explain it in simple terms.', + 'file_ids' => [$fileId], + ]), true); + + $file = $open_ai->retrieveMessageFile($threadId, $message['id'], $fileId); + + $this->assertStringContainsString('id', $file); + $this->assertStringContainsString('"object": "thread.message.file"', $file); +})->group('working'); From f48f9e0e6b76b433f3e394eaa3d1465e14a6098d Mon Sep 17 00:00:00 2001 From: joacir Date: Tue, 28 Nov 2023 17:19:11 -0300 Subject: [PATCH 19/30] List files within message --- src/OpenAi.php | 15 +++++++++++++++ tests/OpenAiTest.php | 11 +++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/OpenAi.php b/src/OpenAi.php index 301e08d..fd868f0 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -685,6 +685,21 @@ public function retrieveMessageFile($id, $messageId, $fileId) return $this->sendRequest($url, 'GET'); } + /** + * @param string $id + * @param string $messageId + * @param array $query + * @return bool|string + */ + public function listMessageFiles($id, $messageId) + { + $this->headers[] = 'OpenAI-Beta: assistants=v1'; + $url = Url::threadsUrl() . '/' . $id . '/messages/' . $messageId . '/files'; + $this->baseUrl($url); + + return $this->sendRequest($url, 'GET'); + } + /** * @param int $timeout */ diff --git a/tests/OpenAiTest.php b/tests/OpenAiTest.php index 1cc1ae5..d9ad53a 100644 --- a/tests/OpenAiTest.php +++ b/tests/OpenAiTest.php @@ -447,3 +447,14 @@ $this->assertStringContainsString('id', $file); $this->assertStringContainsString('"object": "thread.message.file"', $file); })->group('working'); + +it('should handle list files within message', function () use ($open_ai) { + $threadId = 'thread_d86alfR2rfF7rASyV4V7hicz'; + $messageId = 'msg_CZ47kAGZugAfeHMX6bmJIukP'; + $query = ['limit' => 10]; + + $files = $open_ai->listMessageFiles($threadId, $messageId, $query); + + $this->assertStringContainsString('id', $files); + $this->assertStringContainsString('"object": "thread.message.file"', $files); +})->group('working'); From b6479ba932fff86c43d5963fdfab577d3f2b64df Mon Sep 17 00:00:00 2001 From: joacir Date: Wed, 29 Nov 2023 10:27:29 -0300 Subject: [PATCH 20/30] Create run of a thread --- src/OpenAi.php | 14 ++++++++++++++ tests/OpenAiTest.php | 17 +++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/OpenAi.php b/src/OpenAi.php index fd868f0..4ae4158 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -700,6 +700,20 @@ public function listMessageFiles($id, $messageId) return $this->sendRequest($url, 'GET'); } + /** + * @param string $id + * @param array $opts + * @return bool|string + */ + public function createRun($id, $opts) + { + $this->headers[] = 'OpenAI-Beta: assistants=v1'; + $url = Url::threadsUrl() . '/' . $id . '/runs'; + $this->baseUrl($url); + + return $this->sendRequest($url, 'POST', $opts); + } + /** * @param int $timeout */ diff --git a/tests/OpenAiTest.php b/tests/OpenAiTest.php index d9ad53a..b15456b 100644 --- a/tests/OpenAiTest.php +++ b/tests/OpenAiTest.php @@ -458,3 +458,20 @@ $this->assertStringContainsString('id', $files); $this->assertStringContainsString('"object": "thread.message.file"', $files); })->group('working'); + +it('should handle create a run of a thread', function () use ($open_ai) { + $assistantId = 'asst_zT1LLZ8dWnuFCrMFzqxFOhzz'; + $thread = json_decode($open_ai->createThread([ + 'messages' => [ + [ + 'role' => 'user', + 'content' => 'Hello, what is AI?', + 'file_ids' => [], + ], + ]]), true); + + $run = $open_ai->createRun($thread['id'], ['assistant_id' => $assistantId]); + + $this->assertStringContainsString('id', $run); + $this->assertStringContainsString('"object": "thread.run"', $run); +})->group('working'); From d114ed8e714d9defaf4c58cbce4bac7e2cf864cb Mon Sep 17 00:00:00 2001 From: joacir Date: Wed, 29 Nov 2023 10:34:07 -0300 Subject: [PATCH 21/30] Retrieve and modify run of a thread --- src/OpenAi.php | 29 +++++++++++++++++++++++++++++ tests/OpenAiTest.php | 22 ++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/OpenAi.php b/src/OpenAi.php index 4ae4158..c533d33 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -714,6 +714,35 @@ public function createRun($id, $opts) return $this->sendRequest($url, 'POST', $opts); } + /** + * @param string $id + * @param string $runId + * @return bool|string + */ + public function retrieveRun($id, $runId) + { + $this->headers[] = 'OpenAI-Beta: assistants=v1'; + $url = Url::threadsUrl() . '/' . $id . '/runs/' . $runId; + $this->baseUrl($url); + + return $this->sendRequest($url, 'GET'); + } + + /** + * @param string $id + * @param string $runId + * @param array $opts + * @return bool|string + */ + public function modifyRun($id, $runId, $opts) + { + $this->headers[] = 'OpenAI-Beta: assistants=v1'; + $url = Url::threadsUrl() . '/' . $id . '/runs/' . $runId; + $this->baseUrl($url); + + return $this->sendRequest($url, 'POST', $opts); + } + /** * @param int $timeout */ diff --git a/tests/OpenAiTest.php b/tests/OpenAiTest.php index b15456b..798656f 100644 --- a/tests/OpenAiTest.php +++ b/tests/OpenAiTest.php @@ -475,3 +475,25 @@ $this->assertStringContainsString('id', $run); $this->assertStringContainsString('"object": "thread.run"', $run); })->group('working'); + +it('should handle retrieve a run of a thread', function () use ($open_ai) { + $threadId = 'thread_JZbzCYpYgpNb79FNeneO3cGI'; + $runId = 'run_xBKYFcD2Jg3gnfrje6fhiyXj'; + + $run = $open_ai->retrieveRun($threadId, $runId); + + $this->assertStringContainsString('id', $run); + $this->assertStringContainsString('"object": "thread.run"', $run); +})->group('working'); + +it('should handle modify a run of a thread', function () use ($open_ai) { + $threadId = 'thread_JZbzCYpYgpNb79FNeneO3cGI'; + $runId = 'run_xBKYFcD2Jg3gnfrje6fhiyXj'; + + $run = $open_ai->modifyRun($threadId, $runId, [ + 'metadata' => ['test' => 'abcd1234'] + ]); + + $this->assertStringContainsString('id', $run); + $this->assertStringContainsString('"object": "thread.run"', $run); +})->group('working'); From aaa98dfb19523960fe3564ae6f2ce9c5ec9ddb5a Mon Sep 17 00:00:00 2001 From: joacir Date: Wed, 29 Nov 2023 11:04:58 -0300 Subject: [PATCH 22/30] List runs of a thread --- src/OpenAi.php | 17 +++++++++++++++++ tests/OpenAiTest.php | 11 +++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/OpenAi.php b/src/OpenAi.php index c533d33..02ef404 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -743,6 +743,23 @@ public function modifyRun($id, $runId, $opts) return $this->sendRequest($url, 'POST', $opts); } + /** + * @param string $id + * @param array $query + * @return bool|string + */ + public function listRuns($id, $query = []) + { + $this->headers[] = 'OpenAI-Beta: assistants=v1'; + $url = Url::threadsUrl() . '/' . $id . '/runs'; + if (count($query) > 0) { + $url .= '?' . http_build_query($query); + } + $this->baseUrl($url); + + return $this->sendRequest($url, 'GET'); + } + /** * @param int $timeout */ diff --git a/tests/OpenAiTest.php b/tests/OpenAiTest.php index 798656f..88ca895 100644 --- a/tests/OpenAiTest.php +++ b/tests/OpenAiTest.php @@ -497,3 +497,14 @@ $this->assertStringContainsString('id', $run); $this->assertStringContainsString('"object": "thread.run"', $run); })->group('working'); + +it('should handle list runs of a thread', function () use ($open_ai) { + $threadId = 'thread_JZbzCYpYgpNb79FNeneO3cGI'; + $query = ['limit' => 10]; + + $runs = $open_ai->listRuns($threadId, $query); + + $this->assertStringContainsString('id', $runs); + $this->assertStringContainsString('"object": "list"', $runs); + $this->assertStringContainsString('data', $runs); +})->group('working'); From 6eed32c774d94ca8361fd091d69d009eb1353ca1 Mon Sep 17 00:00:00 2001 From: joacir Date: Wed, 29 Nov 2023 11:20:04 -0300 Subject: [PATCH 23/30] Submit tool outputs within run --- src/OpenAi.php | 15 +++++++++++++++ tests/OpenAiTest.php | 14 ++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/OpenAi.php b/src/OpenAi.php index 02ef404..6d7865a 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -760,6 +760,21 @@ public function listRuns($id, $query = []) return $this->sendRequest($url, 'GET'); } + /** + * @param string $id + * @param string $runId + * @param array $outputs + * @return bool|string + */ + public function submitToolOutputs($id, $runId, $outputs) + { + $this->headers[] = 'OpenAI-Beta: assistants=v1'; + $url = Url::threadsUrl() . '/' . $id . '/runs/' . $runId . '/submit_tool_outputs'; + $this->baseUrl($url); + + return $this->sendRequest($url, 'POST', $outputs); + } + /** * @param int $timeout */ diff --git a/tests/OpenAiTest.php b/tests/OpenAiTest.php index 88ca895..e3c27dd 100644 --- a/tests/OpenAiTest.php +++ b/tests/OpenAiTest.php @@ -508,3 +508,17 @@ $this->assertStringContainsString('"object": "list"', $runs); $this->assertStringContainsString('data', $runs); })->group('working'); + +it('should handle submit tool outputs within run', function () use ($open_ai) { + $threadId = 'thread_JZbzCYpYgpNb79FNeneO3cGI'; + $runId = 'run_xBKYFcD2Jg3gnfrje6fhiyXj'; + $outputs = ['tool_outputs' => [ + ['tool_call_id' => 'call_abc123', 'output' => '28C'], + ]]; + + $run = $open_ai->submitToolOutputs($threadId, $runId, $outputs); + + $this->assertStringContainsString('id', $run); + $this->assertStringContainsString('"object": "thread.run"', $run); + $this->assertStringContainsString('tools', $run); +})->group('working'); From e62858423e7eb00e156864395f0159c873843163 Mon Sep 17 00:00:00 2001 From: joacir Date: Wed, 29 Nov 2023 11:24:39 -0300 Subject: [PATCH 24/30] Cancel a run --- src/OpenAi.php | 14 ++++++++++++++ tests/OpenAiTest.php | 11 +++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/OpenAi.php b/src/OpenAi.php index 6d7865a..7234376 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -775,6 +775,20 @@ public function submitToolOutputs($id, $runId, $outputs) return $this->sendRequest($url, 'POST', $outputs); } + /** + * @param string $id + * @param string $runId + * @return bool|string + */ + public function cancelRun($id, $runId) + { + $this->headers[] = 'OpenAI-Beta: assistants=v1'; + $url = Url::threadsUrl() . '/' . $id . '/runs/' . $runId . '/cancel'; + $this->baseUrl($url); + + return $this->sendRequest($url, 'POST'); + } + /** * @param int $timeout */ diff --git a/tests/OpenAiTest.php b/tests/OpenAiTest.php index e3c27dd..f561edb 100644 --- a/tests/OpenAiTest.php +++ b/tests/OpenAiTest.php @@ -522,3 +522,14 @@ $this->assertStringContainsString('"object": "thread.run"', $run); $this->assertStringContainsString('tools', $run); })->group('working'); + +it('should handle cancel a run of a thread', function () use ($open_ai) { + $threadId = 'thread_JZbzCYpYgpNb79FNeneO3cGI'; + $runId = 'run_xBKYFcD2Jg3gnfrje6fhiyXj'; + + $run = $open_ai->cancelRun($threadId, $runId); + + $this->assertStringContainsString('id', $run); + $this->assertStringContainsString('"object": "thread.run"', $run); + $this->assertStringContainsString('"status": "cancelling"', $run); +})->group('working'); From 5feb6169878c114f5a3540766dbbd9307843c238 Mon Sep 17 00:00:00 2001 From: joacir Date: Wed, 29 Nov 2023 11:31:51 -0300 Subject: [PATCH 25/30] Create a thread and run --- src/OpenAi.php | 13 +++++++++++++ tests/OpenAiTest.php | 20 ++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/OpenAi.php b/src/OpenAi.php index 7234376..df61888 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -789,6 +789,19 @@ public function cancelRun($id, $runId) return $this->sendRequest($url, 'POST'); } + /** + * @param array $thread + * @return bool|string + */ + public function createThreadAndRun($thread) + { + $this->headers[] = 'OpenAI-Beta: assistants=v1'; + $url = Url::threadsUrl() . '/runs'; + $this->baseUrl($url); + + return $this->sendRequest($url, 'POST', $thread); + } + /** * @param int $timeout */ diff --git a/tests/OpenAiTest.php b/tests/OpenAiTest.php index f561edb..bc7f92e 100644 --- a/tests/OpenAiTest.php +++ b/tests/OpenAiTest.php @@ -533,3 +533,23 @@ $this->assertStringContainsString('"object": "thread.run"', $run); $this->assertStringContainsString('"status": "cancelling"', $run); })->group('working'); + +it('should handle create thread and run', function () use ($open_ai) { + $thread = [ + 'assistant_id' => 'asst_zT1LLZ8dWnuFCrMFzqxFOhzz', + 'thread' => [ + 'messages' => [ + [ + 'role' => 'user', + 'content' => 'Hello, what is AI?', + 'file_ids' => [], + ], + ] + ] + ]; + + $run = $open_ai->createThreadAndRun($thread); + + $this->assertStringContainsString('id', $run); + $this->assertStringContainsString('"object": "thread.run"', $run); +})->group('working'); From e7e1bfe25165f4ff9186a2248ada2058cc3047b2 Mon Sep 17 00:00:00 2001 From: joacir Date: Wed, 29 Nov 2023 12:43:33 -0300 Subject: [PATCH 26/30] Create a thread and run --- tests/OpenAiTest.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/OpenAiTest.php b/tests/OpenAiTest.php index bc7f92e..2f16553 100644 --- a/tests/OpenAiTest.php +++ b/tests/OpenAiTest.php @@ -553,3 +553,23 @@ $this->assertStringContainsString('id', $run); $this->assertStringContainsString('"object": "thread.run"', $run); })->group('working'); + +it('should handle list run steps', function () use ($open_ai) { + $thread = [ + 'assistant_id' => 'asst_zT1LLZ8dWnuFCrMFzqxFOhzz', + 'thread' => [ + 'messages' => [ + [ + 'role' => 'user', + 'content' => 'Hello, what is AI?', + 'file_ids' => [], + ], + ] + ] + ]; + + $run = $open_ai->createThreadAndRun($thread); + + $this->assertStringContainsString('id', $run); + $this->assertStringContainsString('"object": "thread.run"', $run); +})->group('working'); From d9794ea3cebbd2a85fde68dc1e3c3e87a02d82a6 Mon Sep 17 00:00:00 2001 From: joacir Date: Wed, 29 Nov 2023 14:48:49 -0300 Subject: [PATCH 27/30] Retrieve and list run steps --- src/OpenAi.php | 29 +++++++++++++++++++++++++++++ tests/OpenAiTest.php | 32 +++++++++++++++++--------------- 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/src/OpenAi.php b/src/OpenAi.php index df61888..5342a22 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -802,6 +802,35 @@ public function createThreadAndRun($thread) return $this->sendRequest($url, 'POST', $thread); } + /** + * @param string $threadId + * @param string $runId + * @param string $stepId + * @return bool|string + */ + public function retrieveRunStep($threadId, $runId, $stepId) + { + $this->headers[] = 'OpenAI-Beta: assistants=v1'; + $url = Url::threadsUrl() . '/' . $threadId . '/runs/' . $runId . '/steps/' . $stepId; + $this->baseUrl($url); + + return $this->sendRequest($url, 'GET'); + } + + /** + * @param string $threadId + * @param string $runId + * @return bool|string + */ + public function listRunSteps($threadId, $runId) + { + $this->headers[] = 'OpenAI-Beta: assistants=v1'; + $url = Url::threadsUrl() . '/' . $threadId . '/runs/' . $runId . '/steps'; + $this->baseUrl($url); + + return $this->sendRequest($url, 'GET'); + } + /** * @param int $timeout */ diff --git a/tests/OpenAiTest.php b/tests/OpenAiTest.php index 2f16553..876fc40 100644 --- a/tests/OpenAiTest.php +++ b/tests/OpenAiTest.php @@ -554,22 +554,24 @@ $this->assertStringContainsString('"object": "thread.run"', $run); })->group('working'); +it('should handle retrieve a run step', function () use ($open_ai) { + $threadId = 'thread_JZbzCYpYgpNb79FNeneO3cGI'; + $runId = 'run_xBKYFcD2Jg3gnfrje6fhiyXj'; + $stepId = 'step_kwLG0vPQjqVyQHVoL7GVK3aG'; + + $step = $open_ai->retrieveRunStep($threadId, $runId, $stepId); + + $this->assertStringContainsString('id', $step); + $this->assertStringContainsString('"object": "thread.run.step"', $step); +})->group('working'); + it('should handle list run steps', function () use ($open_ai) { - $thread = [ - 'assistant_id' => 'asst_zT1LLZ8dWnuFCrMFzqxFOhzz', - 'thread' => [ - 'messages' => [ - [ - 'role' => 'user', - 'content' => 'Hello, what is AI?', - 'file_ids' => [], - ], - ] - ] - ]; + $threadId = 'thread_JZbzCYpYgpNb79FNeneO3cGI'; + $runId = 'run_xBKYFcD2Jg3gnfrje6fhiyXj'; - $run = $open_ai->createThreadAndRun($thread); + $steps = $open_ai->listRunSteps($threadId, $runId); - $this->assertStringContainsString('id', $run); - $this->assertStringContainsString('"object": "thread.run"', $run); + $this->assertStringContainsString('id', $steps); + $this->assertStringContainsString('"object": "list"', $steps); + $this->assertStringContainsString('data', $steps); })->group('working'); From ac3b3bebafaa2a55cd02ab8dc73c6d391f66215e Mon Sep 17 00:00:00 2001 From: joacir Date: Wed, 29 Nov 2023 15:58:04 -0300 Subject: [PATCH 28/30] Refactory naming params and vars --- src/OpenAi.php | 185 ++++++++++++++++++++++--------------------- tests/OpenAiTest.php | 100 ++++++++++++++--------- 2 files changed, 160 insertions(+), 125 deletions(-) diff --git a/src/OpenAi.php b/src/OpenAi.php index 5342a22..002c2f1 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -429,54 +429,54 @@ public function embeddings($opts) } /** - * @param array $opts + * @param array $data * @return bool|string */ - public function createAssistant($opts) + public function createAssistant($data) { - $opts['model'] = $opts['model'] ?? $this->chatModel; + $data['model'] = $data['model'] ?? $this->chatModel; $this->headers[] = 'OpenAI-Beta: assistants=v1'; $url = Url::assistantsUrl(); $this->baseUrl($url); - return $this->sendRequest($url, 'POST', $opts); + return $this->sendRequest($url, 'POST', $data); } /** - * @param string $id + * @param string $assistantId * @return bool|string */ - public function retrieveAssistant($id) + public function retrieveAssistant($assistantId) { $this->headers[] = 'OpenAI-Beta: assistants=v1'; - $url = Url::assistantsUrl() . '/' . $id; + $url = Url::assistantsUrl() . '/' . $assistantId; $this->baseUrl($url); return $this->sendRequest($url, 'GET'); } /** - * @param string $id - * @param array $opts + * @param string $assistantId + * @param array $data * @return bool|string */ - public function modifyAssistant($id, $opts) + public function modifyAssistant($assistantId, $data) { $this->headers[] = 'OpenAI-Beta: assistants=v1'; - $url = Url::assistantsUrl() . '/' . $id; + $url = Url::assistantsUrl() . '/' . $assistantId; $this->baseUrl($url); - return $this->sendRequest($url, 'POST', $opts); + return $this->sendRequest($url, 'POST', $data); } /** - * @param string $id + * @param string $assistantId * @return bool|string */ - public function deleteAssistant($id) + public function deleteAssistant($assistantId) { $this->headers[] = 'OpenAI-Beta: assistants=v1'; - $url = Url::assistantsUrl() . '/' . $id; + $url = Url::assistantsUrl() . '/' . $assistantId; $this->baseUrl($url); return $this->sendRequest($url, 'DELETE'); @@ -499,42 +499,42 @@ public function listAssistants($query = []) } /** - * @param string $id + * @param string $assistantId * @param string $fileId * @return bool|string */ - public function createAssistantFile($id, $fileId) + public function createAssistantFile($assistantId, $fileId) { $this->headers[] = 'OpenAI-Beta: assistants=v1'; - $url = Url::assistantsUrl() . '/' . $id . '/files'; + $url = Url::assistantsUrl() . '/' . $assistantId . '/files'; $this->baseUrl($url); return $this->sendRequest($url, 'POST', ['file_id' => $fileId]); } /** - * @param string $id + * @param string $assistantId * @param string $fileId * @return bool|string */ - public function retrieveAssistantFile($id, $fileId) + public function retrieveAssistantFile($assistantId, $fileId) { $this->headers[] = 'OpenAI-Beta: assistants=v1'; - $url = Url::assistantsUrl() . '/' . $id . '/files/' . $fileId; + $url = Url::assistantsUrl() . '/' . $assistantId . '/files/' . $fileId; $this->baseUrl($url); return $this->sendRequest($url, 'GET'); } /** - * @param string $id + * @param string $assistantId * @param array $query * @return bool|string */ - public function listAssistantFiles($id, $query = []) + public function listAssistantFiles($assistantId, $query = []) { $this->headers[] = 'OpenAI-Beta: assistants=v1'; - $url = Url::assistantsUrl() . '/' . $id . '/files'; + $url = Url::assistantsUrl() . '/' . $assistantId . '/files'; if (count($query) > 0) { $url .= '?' . http_build_query($query); } @@ -544,124 +544,124 @@ public function listAssistantFiles($id, $query = []) } /** - * @param string $id + * @param string $assistantId * @param string $fileId * @return bool|string */ - public function deleteAssistantFile($id, $fileId) + public function deleteAssistantFile($assistantId, $fileId) { $this->headers[] = 'OpenAI-Beta: assistants=v1'; - $url = Url::assistantsUrl() . '/' . $id . '/files/' . $fileId; + $url = Url::assistantsUrl() . '/' . $assistantId . '/files/' . $fileId; $this->baseUrl($url); return $this->sendRequest($url, 'DELETE'); } /** - * @param array $opts + * @param array $data * @return bool|string */ - public function createThread($opts = []) + public function createThread($data = []) { $this->headers[] = 'OpenAI-Beta: assistants=v1'; $url = Url::threadsUrl(); $this->baseUrl($url); - return $this->sendRequest($url, 'POST', $opts); + return $this->sendRequest($url, 'POST', $data); } /** - * @param string $id + * @param string $threadId * @return bool|string */ - public function retrieveThread($id) + public function retrieveThread($threadId) { $this->headers[] = 'OpenAI-Beta: assistants=v1'; - $url = Url::threadsUrl() . '/' . $id; + $url = Url::threadsUrl() . '/' . $threadId; $this->baseUrl($url); return $this->sendRequest($url, 'GET'); } /** - * @param string $id - * @param array $opts + * @param string $threadId + * @param array $data * @return bool|string */ - public function modifyThread($id, $opts) + public function modifyThread($threadId, $data) { $this->headers[] = 'OpenAI-Beta: assistants=v1'; - $url = Url::threadsUrl() . '/' . $id; + $url = Url::threadsUrl() . '/' . $threadId; $this->baseUrl($url); - return $this->sendRequest($url, 'POST', $opts); + return $this->sendRequest($url, 'POST', $data); } /** - * @param string $id + * @param string $threadId * @return bool|string */ - public function deleteThread($id) + public function deleteThread($threadId) { $this->headers[] = 'OpenAI-Beta: assistants=v1'; - $url = Url::threadsUrl() . '/' . $id; + $url = Url::threadsUrl() . '/' . $threadId; $this->baseUrl($url); return $this->sendRequest($url, 'DELETE'); } /** - * @param string $id - * @param array $opts + * @param string $threadId + * @param array $data * @return bool|string */ - public function createThreadMessage($id, $opts) + public function createThreadMessage($threadId, $data) { $this->headers[] = 'OpenAI-Beta: assistants=v1'; - $url = Url::threadsUrl() . '/' . $id . '/messages'; + $url = Url::threadsUrl() . '/' . $threadId . '/messages'; $this->baseUrl($url); - return $this->sendRequest($url, 'POST', $opts); + return $this->sendRequest($url, 'POST', $data); } /** - * @param string $id + * @param string $threadId * @param string $messageId * @return bool|string */ - public function retrieveThreadMessage($id, $messageId) + public function retrieveThreadMessage($threadId, $messageId) { $this->headers[] = 'OpenAI-Beta: assistants=v1'; - $url = Url::threadsUrl() . '/' . $id . '/messages/' . $messageId; + $url = Url::threadsUrl() . '/' . $threadId . '/messages/' . $messageId; $this->baseUrl($url); return $this->sendRequest($url, 'GET'); } /** - * @param string $id + * @param string $threadId * @param string $messageId - * @param array $opts + * @param array $data * @return bool|string */ - public function modifyThreadMessage($id, $messageId, $opts) + public function modifyThreadMessage($threadId, $messageId, $data) { $this->headers[] = 'OpenAI-Beta: assistants=v1'; - $url = Url::threadsUrl() . '/' . $id . '/messages/' . $messageId; + $url = Url::threadsUrl() . '/' . $threadId . '/messages/' . $messageId; $this->baseUrl($url); - return $this->sendRequest($url, 'POST', $opts); + return $this->sendRequest($url, 'POST', $data); } /** - * @param string $id + * @param string $threadId * @param array $query * @return bool|string */ - public function listThreadMessages($id, $query = []) + public function listThreadMessages($threadId, $query = []) { $this->headers[] = 'OpenAI-Beta: assistants=v1'; - $url = Url::threadsUrl() . '/' . $id . '/messages'; + $url = Url::threadsUrl() . '/' . $threadId . '/messages'; if (count($query) > 0) { $url .= '?' . http_build_query($query); } @@ -671,87 +671,90 @@ public function listThreadMessages($id, $query = []) } /** - * @param string $id + * @param string $threadId * @param string $messageId * @param string $fileId * @return bool|string */ - public function retrieveMessageFile($id, $messageId, $fileId) + public function retrieveMessageFile($threadId, $messageId, $fileId) { $this->headers[] = 'OpenAI-Beta: assistants=v1'; - $url = Url::threadsUrl() . '/' . $id . '/messages/' . $messageId . '/files/' . $fileId; + $url = Url::threadsUrl() . '/' . $threadId . '/messages/' . $messageId . '/files/' . $fileId; $this->baseUrl($url); return $this->sendRequest($url, 'GET'); } /** - * @param string $id + * @param string $threadId * @param string $messageId * @param array $query * @return bool|string */ - public function listMessageFiles($id, $messageId) + public function listMessageFiles($threadId, $messageId, $query = []) { $this->headers[] = 'OpenAI-Beta: assistants=v1'; - $url = Url::threadsUrl() . '/' . $id . '/messages/' . $messageId . '/files'; + $url = Url::threadsUrl() . '/' . $threadId . '/messages/' . $messageId . '/files'; + if (count($query) > 0) { + $url .= '?' . http_build_query($query); + } $this->baseUrl($url); return $this->sendRequest($url, 'GET'); } /** - * @param string $id - * @param array $opts + * @param string $threadId + * @param array $data * @return bool|string */ - public function createRun($id, $opts) + public function createRun($threadId, $data) { $this->headers[] = 'OpenAI-Beta: assistants=v1'; - $url = Url::threadsUrl() . '/' . $id . '/runs'; + $url = Url::threadsUrl() . '/' . $threadId . '/runs'; $this->baseUrl($url); - return $this->sendRequest($url, 'POST', $opts); + return $this->sendRequest($url, 'POST', $data); } /** - * @param string $id + * @param string $threadId * @param string $runId * @return bool|string */ - public function retrieveRun($id, $runId) + public function retrieveRun($threadId, $runId) { $this->headers[] = 'OpenAI-Beta: assistants=v1'; - $url = Url::threadsUrl() . '/' . $id . '/runs/' . $runId; + $url = Url::threadsUrl() . '/' . $threadId . '/runs/' . $runId; $this->baseUrl($url); return $this->sendRequest($url, 'GET'); } /** - * @param string $id + * @param string $threadId * @param string $runId - * @param array $opts + * @param array $data * @return bool|string */ - public function modifyRun($id, $runId, $opts) + public function modifyRun($threadId, $runId, $data) { $this->headers[] = 'OpenAI-Beta: assistants=v1'; - $url = Url::threadsUrl() . '/' . $id . '/runs/' . $runId; + $url = Url::threadsUrl() . '/' . $threadId . '/runs/' . $runId; $this->baseUrl($url); - return $this->sendRequest($url, 'POST', $opts); + return $this->sendRequest($url, 'POST', $data); } /** - * @param string $id + * @param string $threadId * @param array $query * @return bool|string */ - public function listRuns($id, $query = []) + public function listRuns($threadId, $query = []) { $this->headers[] = 'OpenAI-Beta: assistants=v1'; - $url = Url::threadsUrl() . '/' . $id . '/runs'; + $url = Url::threadsUrl() . '/' . $threadId . '/runs'; if (count($query) > 0) { $url .= '?' . http_build_query($query); } @@ -761,45 +764,45 @@ public function listRuns($id, $query = []) } /** - * @param string $id + * @param string $threadId * @param string $runId * @param array $outputs * @return bool|string */ - public function submitToolOutputs($id, $runId, $outputs) + public function submitToolOutputs($threadId, $runId, $outputs) { $this->headers[] = 'OpenAI-Beta: assistants=v1'; - $url = Url::threadsUrl() . '/' . $id . '/runs/' . $runId . '/submit_tool_outputs'; + $url = Url::threadsUrl() . '/' . $threadId . '/runs/' . $runId . '/submit_tool_outputs'; $this->baseUrl($url); return $this->sendRequest($url, 'POST', $outputs); } /** - * @param string $id + * @param string $threadId * @param string $runId * @return bool|string */ - public function cancelRun($id, $runId) + public function cancelRun($threadId, $runId) { $this->headers[] = 'OpenAI-Beta: assistants=v1'; - $url = Url::threadsUrl() . '/' . $id . '/runs/' . $runId . '/cancel'; + $url = Url::threadsUrl() . '/' . $threadId . '/runs/' . $runId . '/cancel'; $this->baseUrl($url); return $this->sendRequest($url, 'POST'); } /** - * @param array $thread + * @param array $data * @return bool|string */ - public function createThreadAndRun($thread) + public function createThreadAndRun($data) { $this->headers[] = 'OpenAI-Beta: assistants=v1'; $url = Url::threadsUrl() . '/runs'; $this->baseUrl($url); - return $this->sendRequest($url, 'POST', $thread); + return $this->sendRequest($url, 'POST', $data); } /** @@ -820,12 +823,16 @@ public function retrieveRunStep($threadId, $runId, $stepId) /** * @param string $threadId * @param string $runId + * @param array $query * @return bool|string */ - public function listRunSteps($threadId, $runId) + public function listRunSteps($threadId, $runId, $query = []) { $this->headers[] = 'OpenAI-Beta: assistants=v1'; $url = Url::threadsUrl() . '/' . $threadId . '/runs/' . $runId . '/steps'; + if (count($query) > 0) { + $url .= '?' . http_build_query($query); + } $this->baseUrl($url); return $this->sendRequest($url, 'GET'); diff --git a/tests/OpenAiTest.php b/tests/OpenAiTest.php index 876fc40..cf63b25 100644 --- a/tests/OpenAiTest.php +++ b/tests/OpenAiTest.php @@ -256,14 +256,16 @@ })->group('working'); it('should handle create a assistant', function () use ($open_ai) { - $assistant = $open_ai->createAssistant([ + $data = [ 'model' => 'gpt-3.5-turbo', 'name' => 'my assistant', 'description' => 'my assistant description', 'instructions' => 'you should cordially help me', 'tools' => [], 'file_ids' => [], - ]); + ]; + + $assistant = $open_ai->createAssistant($data); $this->assertStringContainsString('id', $assistant); $this->assertStringContainsString('"object": "assistant"', $assistant); @@ -271,7 +273,9 @@ })->group('working'); it('should handle retrieve a assistant', function () use ($open_ai) { - $assistant = $open_ai->retrieveAssistant('asst_rzJqKRfpQI2JMj1gYGKQOZZo'); + $assistantId = 'asst_rzJqKRfpQI2JMj1gYGKQOZZo'; + + $assistant = $open_ai->retrieveAssistant($assistantId); $this->assertStringContainsString('id', $assistant); $this->assertStringContainsString('"object": "assistant"', $assistant); @@ -279,10 +283,13 @@ })->group('working'); it('should handle modify a assistant', function () use ($open_ai) { - $assistant = $open_ai->modifyAssistant('asst_rzJqKRfpQI2JMj1gYGKQOZZo', [ + $assistantId = 'asst_rzJqKRfpQI2JMj1gYGKQOZZo'; + $data = [ 'name' => 'my modified assistant', 'instructions' => 'you should cordially help me again', - ]); + ]; + + $assistant = $open_ai->modifyAssistant($assistantId, $data); $this->assertStringContainsString('id', $assistant); $this->assertStringContainsString('"object": "assistant"', $assistant); @@ -290,7 +297,9 @@ })->group('working'); it('should handle delete a assistant', function () use ($open_ai) { - $assistant = $open_ai->deleteAssistant('asst_rzJqKRfpQI2JMj1gYGKQOZZo'); + $assistantId = 'asst_rzJqKRfpQI2JMj1gYGKQOZZo'; + + $assistant = $open_ai->deleteAssistant($assistantId); $this->assertStringContainsString('id', $assistant); $this->assertStringContainsString('"deleted": true', $assistant); @@ -298,6 +307,7 @@ it('should handle list assistants', function () use ($open_ai) { $query = ['limit' => 10]; + $assistants = $open_ai->listAssistants($query); $this->assertStringContainsString('"object": "list"', $assistants); @@ -306,12 +316,12 @@ })->group('working'); it('should handle create a assistant file', function () use ($open_ai) { + $assistantId = 'asst_zT1LLZ8dWnuFCrMFzqxFOhzz'; $upload = curl_file_create(__DIR__ . '/../files/assistant-file.txt'); $file = json_decode($open_ai->uploadFile([ 'purpose' => 'assistants', 'file' => $upload, ]), true); - $assistantId = 'asst_zT1LLZ8dWnuFCrMFzqxFOhzz'; $assistantFile = $open_ai->createAssistantFile($assistantId, $file['id']); @@ -351,7 +361,7 @@ })->group('working'); it('should handle create a thread', function () use ($open_ai) { - $thread = $open_ai->createThread([ + $data = [ 'messages' => [ [ 'role' => 'user', @@ -359,32 +369,39 @@ 'file_ids' => [], ], ], - ]); + ]; + + $thread = $open_ai->createThread($data); $this->assertStringContainsString('id', $thread); $this->assertStringContainsString('"object": "thread"', $thread); })->group('working'); it('should handle retrieve a thread', function () use ($open_ai) { - $thread = $open_ai->retrieveThread('thread_GHGU1zEOVD5z2EXs3e8zU55S'); + $threadId = 'thread_GHGU1zEOVD5z2EXs3e8zU55S'; + + $thread = $open_ai->retrieveThread($threadId); $this->assertStringContainsString('id', $thread); $this->assertStringContainsString('"object": "thread"', $thread); })->group('working'); it('should handle modify a thread', function () use ($open_ai) { - $thread = $open_ai->modifyThread('thread_GHGU1zEOVD5z2EXs3e8zU55S', [ - 'metadata' => [ - 'test' => '1234abcd', - ], - ]); + $threadId = 'thread_GHGU1zEOVD5z2EXs3e8zU55S'; + $data = [ + 'metadata' => ['test' => '1234abcd'], + ]; + + $thread = $open_ai->modifyThread($threadId, $data); $this->assertStringContainsString('id', $thread); $this->assertStringContainsString('"object": "thread"', $thread); })->group('working'); it('should handle delete a thread', function () use ($open_ai) { - $thread = $open_ai->deleteThread('thread_GHGU1zEOVD5z2EXs3e8zU55S'); + $threadId = 'thread_GHGU1zEOVD5z2EXs3e8zU55S'; + + $thread = $open_ai->deleteThread($threadId); $this->assertStringContainsString('id', $thread); $this->assertStringContainsString('"deleted": true', $thread); @@ -392,10 +409,12 @@ it('should handle create a message within thread', function () use ($open_ai) { $thread = json_decode($open_ai->createThread(), true); - $message = $open_ai->createThreadMessage($thread['id'], [ + $data = [ 'role' => 'user', 'content' => 'How does AI work? Explain it in simple terms.', - ]); + ]; + + $message = $open_ai->createThreadMessage($thread['id'], $data); $this->assertStringContainsString('id', $message); $this->assertStringContainsString('"object": "thread.message"', $message); @@ -404,6 +423,7 @@ it('should handle retrieve a message within thread', function () use ($open_ai) { $threadId = 'thread_d86alfR2rfF7rASyV4V7hicz'; $messageId = 'msg_d37P5XgREsm6BItOcppnBO1b'; + $message = $open_ai->retrieveThreadMessage($threadId, $messageId); $this->assertStringContainsString('id', $message); @@ -413,11 +433,11 @@ it('should handle modify a message within thread', function () use ($open_ai) { $threadId = 'thread_d86alfR2rfF7rASyV4V7hicz'; $messageId = 'msg_d37P5XgREsm6BItOcppnBO1b'; - $message = $open_ai->modifyThreadMessage($threadId, $messageId, [ - 'metadata' => [ - 'test' => '1234abcd', - ], - ]); + $data = [ + 'metadata' => ['test' => '1234abcd'], + ]; + + $message = $open_ai->modifyThreadMessage($threadId, $messageId, $data); $this->assertStringContainsString('id', $message); $this->assertStringContainsString('"object": "thread.message"', $message); @@ -426,6 +446,7 @@ it('should handle list messages within thread', function () use ($open_ai) { $threadId = 'thread_d86alfR2rfF7rASyV4V7hicz'; $query = ['limit' => 10]; + $messages = $open_ai->listThreadMessages($threadId, $query); $this->assertStringContainsString('id', $messages); @@ -460,17 +481,20 @@ })->group('working'); it('should handle create a run of a thread', function () use ($open_ai) { - $assistantId = 'asst_zT1LLZ8dWnuFCrMFzqxFOhzz'; - $thread = json_decode($open_ai->createThread([ + $threadData = [ 'messages' => [ [ 'role' => 'user', 'content' => 'Hello, what is AI?', 'file_ids' => [], ], - ]]), true); + ], + ]; + $newThread = $open_ai->createThread($threadData); + $thread = json_decode($newThread, true); + $data = ['assistant_id' => 'asst_zT1LLZ8dWnuFCrMFzqxFOhzz']; - $run = $open_ai->createRun($thread['id'], ['assistant_id' => $assistantId]); + $run = $open_ai->createRun($thread['id'], $data); $this->assertStringContainsString('id', $run); $this->assertStringContainsString('"object": "thread.run"', $run); @@ -489,10 +513,11 @@ it('should handle modify a run of a thread', function () use ($open_ai) { $threadId = 'thread_JZbzCYpYgpNb79FNeneO3cGI'; $runId = 'run_xBKYFcD2Jg3gnfrje6fhiyXj'; + $data = [ + 'metadata' => ['test' => 'abcd1234'], + ]; - $run = $open_ai->modifyRun($threadId, $runId, [ - 'metadata' => ['test' => 'abcd1234'] - ]); + $run = $open_ai->modifyRun($threadId, $runId, $data); $this->assertStringContainsString('id', $run); $this->assertStringContainsString('"object": "thread.run"', $run); @@ -512,9 +537,11 @@ it('should handle submit tool outputs within run', function () use ($open_ai) { $threadId = 'thread_JZbzCYpYgpNb79FNeneO3cGI'; $runId = 'run_xBKYFcD2Jg3gnfrje6fhiyXj'; - $outputs = ['tool_outputs' => [ - ['tool_call_id' => 'call_abc123', 'output' => '28C'], - ]]; + $outputs = [ + 'tool_outputs' => [ + ['tool_call_id' => 'call_abc123', 'output' => '28C'], + ], + ]; $run = $open_ai->submitToolOutputs($threadId, $runId, $outputs); @@ -535,7 +562,7 @@ })->group('working'); it('should handle create thread and run', function () use ($open_ai) { - $thread = [ + $data = [ 'assistant_id' => 'asst_zT1LLZ8dWnuFCrMFzqxFOhzz', 'thread' => [ 'messages' => [ @@ -548,7 +575,7 @@ ] ]; - $run = $open_ai->createThreadAndRun($thread); + $run = $open_ai->createThreadAndRun($data); $this->assertStringContainsString('id', $run); $this->assertStringContainsString('"object": "thread.run"', $run); @@ -568,8 +595,9 @@ it('should handle list run steps', function () use ($open_ai) { $threadId = 'thread_JZbzCYpYgpNb79FNeneO3cGI'; $runId = 'run_xBKYFcD2Jg3gnfrje6fhiyXj'; + $query = ['limit' => 10]; - $steps = $open_ai->listRunSteps($threadId, $runId); + $steps = $open_ai->listRunSteps($threadId, $runId, $query); $this->assertStringContainsString('id', $steps); $this->assertStringContainsString('"object": "list"', $steps); From c8c7295310e2cf2feccce3f7340cc20c08ce3c13 Mon Sep 17 00:00:00 2001 From: joacir Date: Wed, 29 Nov 2023 16:18:06 -0300 Subject: [PATCH 29/30] php-cs-fixer fix executed --- src/OpenAi.php | 54 +++++++++++++++++++++++--------------------- tests/OpenAiTest.php | 32 +++++++++++++------------- 2 files changed, 44 insertions(+), 42 deletions(-) diff --git a/src/OpenAi.php b/src/OpenAi.php index 002c2f1..e3ff4c7 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -20,7 +20,7 @@ class OpenAi public function __construct($OPENAI_API_KEY) { $this->contentTypes = [ - "application/json" => "Content-Type: application/json", + "application/json" => "Content-Type: application/json", "multipart/form-data" => "Content-Type: multipart/form-data", ]; @@ -57,7 +57,7 @@ public function listModels() public function retrieveModel($model) { $model = "/$model"; - $url = Url::fineTuneModel().$model; + $url = Url::fineTuneModel().$model; $this->baseUrl($url); return $this->sendRequest($url, 'GET'); @@ -71,7 +71,7 @@ public function retrieveModel($model) public function complete($opts) { $engine = $opts['engine'] ?? $this->engine; - $url = Url::completionURL($engine); + $url = Url::completionURL($engine); unset($opts['engine']); $this->baseUrl($url); @@ -97,7 +97,7 @@ public function completion($opts, $stream = null) } $opts['model'] = $opts['model'] ?? $this->model; - $url = Url::completionsURL(); + $url = Url::completionsURL(); $this->baseUrl($url); return $this->sendRequest($url, 'POST', $opts); @@ -159,7 +159,7 @@ public function createImageVariation($opts) public function search($opts) { $engine = $opts['engine'] ?? $this->engine; - $url = Url::searchURL($engine); + $url = Url::searchURL($engine); unset($opts['engine']); $this->baseUrl($url); @@ -213,7 +213,7 @@ public function moderation($opts) public function chat($opts, $stream = null) { if ($stream != null && array_key_exists('stream', $opts)) { - if (!$opts['stream']) { + if (! $opts['stream']) { throw new Exception( 'Please provide a stream function. Check https://github.com/orhanerday/open-ai#stream-example for an example.' ); @@ -223,7 +223,7 @@ public function chat($opts, $stream = null) } $opts['model'] = $opts['model'] ?? $this->chatModel; - $url = Url::chatUrl(); + $url = Url::chatUrl(); $this->baseUrl($url); return $this->sendRequest($url, 'POST', $opts); @@ -283,7 +283,7 @@ public function listFiles() public function retrieveFile($file_id) { $file_id = "/$file_id"; - $url = Url::filesUrl().$file_id; + $url = Url::filesUrl().$file_id; $this->baseUrl($url); return $this->sendRequest($url, 'GET'); @@ -296,7 +296,7 @@ public function retrieveFile($file_id) public function retrieveFileContent($file_id) { $file_id = "/$file_id/content"; - $url = Url::filesUrl().$file_id; + $url = Url::filesUrl().$file_id; $this->baseUrl($url); return $this->sendRequest($url, 'GET'); @@ -309,7 +309,7 @@ public function retrieveFileContent($file_id) public function deleteFile($file_id) { $file_id = "/$file_id"; - $url = Url::filesUrl().$file_id; + $url = Url::filesUrl().$file_id; $this->baseUrl($url); return $this->sendRequest($url, 'DELETE'); @@ -345,7 +345,7 @@ public function listFineTunes() public function retrieveFineTune($fine_tune_id) { $fine_tune_id = "/$fine_tune_id"; - $url = Url::fineTuneUrl().$fine_tune_id; + $url = Url::fineTuneUrl().$fine_tune_id; $this->baseUrl($url); return $this->sendRequest($url, 'GET'); @@ -358,7 +358,7 @@ public function retrieveFineTune($fine_tune_id) public function cancelFineTune($fine_tune_id) { $fine_tune_id = "/$fine_tune_id/cancel"; - $url = Url::fineTuneUrl().$fine_tune_id; + $url = Url::fineTuneUrl().$fine_tune_id; $this->baseUrl($url); return $this->sendRequest($url, 'POST'); @@ -371,7 +371,7 @@ public function cancelFineTune($fine_tune_id) public function listFineTuneEvents($fine_tune_id) { $fine_tune_id = "/$fine_tune_id/events"; - $url = Url::fineTuneUrl().$fine_tune_id; + $url = Url::fineTuneUrl().$fine_tune_id; $this->baseUrl($url); return $this->sendRequest($url, 'GET'); @@ -384,7 +384,7 @@ public function listFineTuneEvents($fine_tune_id) public function deleteFineTune($fine_tune_id) { $fine_tune_id = "/$fine_tune_id"; - $url = Url::fineTuneModel().$fine_tune_id; + $url = Url::fineTuneModel().$fine_tune_id; $this->baseUrl($url); return $this->sendRequest($url, 'DELETE'); @@ -919,28 +919,28 @@ private function sendRequest(string $url, string $method, array $opts = []) if (array_key_exists('file', $opts) || array_key_exists('image', $opts)) { $this->headers[0] = $this->contentTypes["multipart/form-data"]; - $post_fields = $opts; + $post_fields = $opts; } else { $this->headers[0] = $this->contentTypes["application/json"]; } $curl_info = [ - CURLOPT_URL => $url, + CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, - CURLOPT_ENCODING => '', - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => $this->timeout, + CURLOPT_ENCODING => '', + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => $this->timeout, CURLOPT_FOLLOWLOCATION => true, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, - CURLOPT_CUSTOMREQUEST => $method, - CURLOPT_POSTFIELDS => $post_fields, - CURLOPT_HTTPHEADER => $this->headers, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, + CURLOPT_CUSTOMREQUEST => $method, + CURLOPT_POSTFIELDS => $post_fields, + CURLOPT_HTTPHEADER => $this->headers, ]; if ($opts == []) { unset($curl_info[CURLOPT_POSTFIELDS]); } - if (!empty($this->proxy)) { + if (! empty($this->proxy)) { $curl_info[CURLOPT_PROXY] = $this->proxy; } @@ -953,12 +953,14 @@ private function sendRequest(string $url, string $method, array $opts = []) curl_setopt_array($curl, $curl_info); $response = curl_exec($curl); - $info = curl_getinfo($curl); + $info = curl_getinfo($curl); $this->curlInfo = $info; curl_close($curl); - if (!$response) throw new Exception(curl_error($curl)); + if (! $response) { + throw new Exception(curl_error($curl)); + } return $response; } diff --git a/tests/OpenAiTest.php b/tests/OpenAiTest.php index cf63b25..6b93927 100644 --- a/tests/OpenAiTest.php +++ b/tests/OpenAiTest.php @@ -24,7 +24,7 @@ "max_tokens" => 150, "frequency_penalty" => 0, "presence_penalty" => 0.6, - "stream" => true + "stream" => true, ]))->toThrow(Exception::class, 'Please provide a stream function. Check https://github.com/orhanerday/open-ai#stream-example for an example.'); })->group('working'); @@ -118,19 +118,19 @@ $result = $open_ai->cancelFineTune('file-XGinujblHPwGLSztz8cPS8XY'); $this->assertStringContainsString('training_files', $result); -})->group('requires-missing-file');; +})->group('requires-missing-file'); it('should handle list fine-tune event', function () use ($open_ai) { $result = $open_ai->listFineTuneEvents('file-XGinujblHPwGLSztz8cPS8XY'); $this->assertStringContainsString('data', $result); -})->group('requires-missing-file');; +})->group('requires-missing-file'); it('should handle delete fine-tune model', function () use ($open_ai) { $result = $open_ai->deleteFineTune('curie:ft-acmeco-2021-03-03-21-44-20'); $this->assertStringContainsString('deleted', $result); -})->group('requires-missing-file');; +})->group('requires-missing-file'); it('should handle answers', function () use ($open_ai) { $result = $open_ai->answer([ @@ -243,8 +243,8 @@ 'messages' => [ [ "role" => "user", - "content" => "Hello!" - ] + "content" => "Hello!", + ], ], 'temperature' => 0.9, "max_tokens" => 150, @@ -273,7 +273,7 @@ })->group('working'); it('should handle retrieve a assistant', function () use ($open_ai) { - $assistantId = 'asst_rzJqKRfpQI2JMj1gYGKQOZZo'; + $assistantId = 'asst_zT1LLZ8dWnuFCrMFzqxFOhzz'; $assistant = $open_ai->retrieveAssistant($assistantId); @@ -283,7 +283,7 @@ })->group('working'); it('should handle modify a assistant', function () use ($open_ai) { - $assistantId = 'asst_rzJqKRfpQI2JMj1gYGKQOZZo'; + $assistantId = 'asst_zT1LLZ8dWnuFCrMFzqxFOhzz'; $data = [ 'name' => 'my modified assistant', 'instructions' => 'you should cordially help me again', @@ -297,7 +297,7 @@ })->group('working'); it('should handle delete a assistant', function () use ($open_ai) { - $assistantId = 'asst_rzJqKRfpQI2JMj1gYGKQOZZo'; + $assistantId = 'asst_DgiOnXK7nRfyvqoXWpFlwESc'; $assistant = $open_ai->deleteAssistant($assistantId); @@ -331,7 +331,7 @@ it('should handle retrieve a assistant file', function () use ($open_ai) { $assistantId = 'asst_zT1LLZ8dWnuFCrMFzqxFOhzz'; - $fileId = 'file-CRLcY63DiHphWuBrmDWZVCgA'; + $fileId = 'file-jrNZZZBAPGnhYUKma7CblGoR'; $assistantFile = $open_ai->retrieveAssistantFile($assistantId, $fileId); @@ -352,7 +352,7 @@ it('should handle delete a assistant file', function () use ($open_ai) { $assistantId = 'asst_zT1LLZ8dWnuFCrMFzqxFOhzz'; - $fileId = 'file-CRLcY63DiHphWuBrmDWZVCgA'; + $fileId = 'file-jrNZZZBAPGnhYUKma7CblGoR'; $file = $open_ai->deleteAssistantFile($assistantId, $fileId); @@ -378,7 +378,7 @@ })->group('working'); it('should handle retrieve a thread', function () use ($open_ai) { - $threadId = 'thread_GHGU1zEOVD5z2EXs3e8zU55S'; + $threadId = 'thread_YKDArENVWFDO2Xz3POifFYlp'; $thread = $open_ai->retrieveThread($threadId); @@ -387,7 +387,7 @@ })->group('working'); it('should handle modify a thread', function () use ($open_ai) { - $threadId = 'thread_GHGU1zEOVD5z2EXs3e8zU55S'; + $threadId = 'thread_YKDArENVWFDO2Xz3POifFYlp'; $data = [ 'metadata' => ['test' => '1234abcd'], ]; @@ -399,7 +399,7 @@ })->group('working'); it('should handle delete a thread', function () use ($open_ai) { - $threadId = 'thread_GHGU1zEOVD5z2EXs3e8zU55S'; + $threadId = 'thread_YKDArENVWFDO2Xz3POifFYlp'; $thread = $open_ai->deleteThread($threadId); @@ -571,8 +571,8 @@ 'content' => 'Hello, what is AI?', 'file_ids' => [], ], - ] - ] + ], + ], ]; $run = $open_ai->createThreadAndRun($data); From 01779d356b2ccf5626aff143688bbcf3c830a680 Mon Sep 17 00:00:00 2001 From: joacir Date: Wed, 29 Nov 2023 18:07:20 -0300 Subject: [PATCH 30/30] Documentation updated --- README.md | 376 ++++++++++++++++++++++++++++++++++++++++++- tests/OpenAiTest.php | 42 ++--- 2 files changed, 391 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 914f792..8ec6d3c 100644 --- a/README.md +++ b/README.md @@ -108,7 +108,7 @@ Please visit https://orhanerday.gitbook.io/openai-php-api-1/ # Endpoint Support - Chat - - [x] [ChatGPT API](#chat-as-known-as-chatgpt-api) + - [x] [ChatGPT API](#chat-as-known-as-chatgpt-api) - Models - [x] [List models](https://beta.openai.com/docs/api-reference/models/list) - [x] [Retrieve model](https://beta.openai.com/docs/api-reference/models/retrieve) @@ -143,6 +143,38 @@ Please visit https://orhanerday.gitbook.io/openai-php-api-1/ - ~~Engines~~ *(deprecated)* - ~~[List engines](https://beta.openai.com/docs/api-reference/engines/list)~~ - ~~[Retrieve engine](https://beta.openai.com/docs/api-reference/engines/retrieve)~~ +- Assistants (beta) + - [x] [Create assistant](https://platform.openai.com/docs/api-reference/assistants/createAssistant) + - [x] [Retrieve assistant](https://platform.openai.com/docs/api-reference/assistants/getAssistant) + - [x] [Modify assistant](https://platform.openai.com/docs/api-reference/assistants/modifyAssistant) + - [x] [Delete assistant](https://platform.openai.com/docs/api-reference/assistants/deleteAssistant) + - [x] [Lists assistants](https://platform.openai.com/docs/api-reference/assistants/listAssistants) + - [x] [Create assistant file](https://platform.openai.com/docs/api-reference/assistants/createAssistantFile) + - [x] [Retrieve assistant file](https://platform.openai.com/docs/api-reference/assistants/getAssistantFile) + - [x] [Delete assistant file](https://platform.openai.com/docs/api-reference/assistants/deleteAssistantFile) + - [x] [List assistant files](https://platform.openai.com/docs/api-reference/assistants/listAssistantFiles) +- Threads (beta) + - [x] [Create thread](https://platform.openai.com/docs/api-reference/threads/createThread) + - [x] [Retrieve thread](https://platform.openai.com/docs/api-reference/threads/getThread) + - [x] [Modify thread](https://platform.openai.com/docs/api-reference/threads/modifyThread) + - [x] [Delete thread](https://platform.openai.com/docs/api-reference/threads/deleteThread) +- Messages (beta) + - [x] [Create message](https://platform.openai.com/docs/api-reference/messages/createMessage) + - [x] [Retrieve message](https://platform.openai.com/docs/api-reference/messages/getMessage) + - [x] [Modify message](https://platform.openai.com/docs/api-reference/messages/modifyMessage) + - [x] [Lists messages](https://platform.openai.com/docs/api-reference/messages/listMessages) + - [x] [Retrieve message file](https://platform.openai.com/docs/api-reference/messages/getMessageFile) + - [x] [List message files](https://platform.openai.com/docs/api-reference/messages/listMessageFiles) +- Runs (beta) + - [x] [Create run](https://platform.openai.com/docs/api-reference/runs/createRun) + - [x] [Retrieve run](https://platform.openai.com/docs/api-reference/runs/getRun) + - [x] [Modify run](https://platform.openai.com/docs/api-reference/runs/modifyRun) + - [x] [Lists runs](https://platform.openai.com/docs/api-reference/runs/listRuns) + - [x] [Submit tool outputs](https://platform.openai.com/docs/api-reference/runs/submitToolOutputs) + - [x] [Cancel run](https://platform.openai.com/docs/api-reference/runs/cancelRun) + - [x] [Create thread and run](https://platform.openai.com/docs/api-reference/runs/createThreadAndRun) + - [x] [Retrieve run step](https://platform.openai.com/docs/api-reference/runs/getRunStep) + - [x] [List run steps](https://platform.openai.com/docs/api-reference/runs/listRunSteps) ## Installation @@ -285,7 +317,7 @@ $open_ai->setHeader(["Connection"=>"keep-alive"]); ## Get cURL request info > ### !!! WARNING:Your API key will expose if you add this method to your code, therefore remove the method before deployment. Be careful ! -You can get cURL info after the request. +You can get cURL info after the request. ````php $open_ai = new OpenAi($open_ai_key); @@ -325,11 +357,11 @@ $complete = $open_ai->chat([ ]); ``` -## Accessing the Element +## Accessing the Element ```php completion($opts, function ($curl_info, $data) { Add this part inside `` of the HTML ````php - +
Hello