Skip to content

Commit

Permalink
Merge pull request #52 from outl1ne/Virtual-branch
Browse files Browse the repository at this point in the history
feat: Add support for sending image URLs in chat messages and update message parameter types for system, user, assistant, and tool roles.
  • Loading branch information
allantatter authored Aug 12, 2024
2 parents 497b67f + c680e48 commit c5617f9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/Capabilities/Chat/Parameters/Messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand Down
24 changes: 22 additions & 2 deletions tests/Unit/ChatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit c5617f9

Please sign in to comment.