From c680e48413b0485051cc9396bda8145353632273 Mon Sep 17 00:00:00 2001 From: Allan Tatter Date: Mon, 12 Aug 2024 15:10:59 +0300 Subject: [PATCH] feat: Add support for sending image URLs in chat messages and update message parameter types for system, user, assistant, and tool roles. --- src/Capabilities/Chat/Parameters/Messages.php | 8 +++---- tests/Unit/ChatTest.php | 24 +++++++++++++++++-- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/Capabilities/Chat/Parameters/Messages.php b/src/Capabilities/Chat/Parameters/Messages.php index 4f21ff4..85e776a 100644 --- a/src/Capabilities/Chat/Parameters/Messages.php +++ b/src/Capabilities/Chat/Parameters/Messages.php @@ -6,7 +6,7 @@ class Messages { public array $messages = []; - public function system(string $content, ?string $name = null): self + public function system(string|array $content, ?string $name = null): self { $message = [ 'role' => 'system', @@ -20,7 +20,7 @@ public function system(string $content, ?string $name = null): self return $this; } - public function user(string $content, ?string $name = null): self + public function user(string|array $content, ?string $name = null): self { $message = [ 'role' => 'user', @@ -34,7 +34,7 @@ public function user(string $content, ?string $name = null): self return $this; } - public function assistant(?string $content = null, ?string $name = null, ?array $toolCalls): self + public function assistant(string|array|null $content = null, ?string $name = null, ?array $toolCalls): self { $message = [ 'role' => 'assistant', @@ -49,7 +49,7 @@ public function assistant(?string $content = null, ?string $name = null, ?array return $this; } - public function tool(string $content, string $toolCallId): self + public function tool(string|array $content, string $toolCallId): self { $message = [ 'role' => 'system', diff --git a/tests/Unit/ChatTest.php b/tests/Unit/ChatTest.php index 8cda1b8..f2893df 100644 --- a/tests/Unit/ChatTest.php +++ b/tests/Unit/ChatTest.php @@ -44,12 +44,32 @@ public function test_chat_json_response(): void public function test_chat_stream(): void { - $response = OpenAI::chat()->stream(function (string $newChunk, string $message) { - })->create( + $response = OpenAI::chat()->stream(function (string $newChunk, string $message) {})->create( model: 'gpt-4o-mini', messages: (new Messages)->system('You are a helpful assistant.')->user('Hello!'), ); $this->assertTrue($response instanceof StreamedChatResponse); $this->assertIsArray($response->choices); } + + public function test_chat_image_url(): void + { + $response = OpenAI::chat()->create( + model: 'gpt-4o-mini', + messages: (new Messages)->system('You are a helpful assistant.')->user([ + [ + 'type' => 'text', + 'text' => 'Describe what\'s on the attached photo', + ], + [ + 'type' => 'image_url', + 'image_url' => [ + 'url' => 'https://img-cdn.pixlr.com/image-generator/history/65bb506dcb310754719cf81f/ede935de-1138-4f66-8ed7-44bd16efc709/medium.webp', + ], + ], + ]), + ); + $this->assertTrue($response instanceof ChatResponse); + $this->assertIsArray($response->choices); + } }