-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
487 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Providers\OpenAI; | ||
|
||
use EchoLabs\Prism\Providers\OpenAI\MessageMap; | ||
use EchoLabs\Prism\ValueObjects\Messages\AssistantMessage; | ||
use EchoLabs\Prism\ValueObjects\Messages\Support\Image; | ||
use EchoLabs\Prism\ValueObjects\Messages\ToolResultMessage; | ||
use EchoLabs\Prism\ValueObjects\Messages\UserMessage; | ||
use EchoLabs\Prism\ValueObjects\ToolCall; | ||
use EchoLabs\Prism\ValueObjects\ToolResult; | ||
|
||
it('maps user messages', function (): void { | ||
$messageMap = new MessageMap( | ||
messages: [ | ||
new UserMessage('Who are you?'), | ||
], | ||
systemPrompt: '' | ||
); | ||
|
||
expect($messageMap())->toBe([[ | ||
'role' => 'user', | ||
'content' => [ | ||
['type' => 'text', 'text' => 'Who are you?'], | ||
], | ||
]]); | ||
}); | ||
|
||
it('maps user messages with images', function (): void { | ||
$messageMap = new MessageMap( | ||
messages: [ | ||
new UserMessage('Who are you?', [ | ||
Image::fromPath('tests/Fixtures/test-image.png'), | ||
]), | ||
], | ||
systemPrompt: '' | ||
); | ||
|
||
$mappedMessage = $messageMap(); | ||
|
||
expect(data_get($mappedMessage, '0.content.1.type')) | ||
->toBe('image_url'); | ||
expect(data_get($mappedMessage, '0.content.1.image_url.url')) | ||
->toStartWith('data:image/png;base64,'); | ||
expect(data_get($mappedMessage, '0.content.1.image_url.url')) | ||
->toContain(Image::fromPath('tests/Fixtures/test-image.png')->image); | ||
}); | ||
|
||
it('maps assistant message', function (): void { | ||
$messageMap = new MessageMap( | ||
messages: [ | ||
new AssistantMessage('I am Nyx'), | ||
], | ||
systemPrompt: '' | ||
); | ||
|
||
expect($messageMap())->toContain([ | ||
'role' => 'assistant', | ||
'content' => 'I am Nyx', | ||
]); | ||
}); | ||
|
||
it('maps assistant message with tool calls', function (): void { | ||
$messageMap = new MessageMap( | ||
messages: [ | ||
new AssistantMessage('I am Nyx', [ | ||
new ToolCall( | ||
'tool_1234', | ||
'search', | ||
[ | ||
'query' => 'Laravel collection methods', | ||
] | ||
), | ||
]), | ||
], | ||
systemPrompt: '' | ||
); | ||
|
||
expect($messageMap())->toBe([[ | ||
'role' => 'assistant', | ||
'content' => 'I am Nyx', | ||
'tool_calls' => [[ | ||
'id' => 'tool_1234', | ||
'type' => 'function', | ||
'function' => [ | ||
'name' => 'search', | ||
'arguments' => json_encode([ | ||
'query' => 'Laravel collection methods', | ||
]), | ||
], | ||
]], | ||
]]); | ||
}); | ||
|
||
it('maps tool result messages', function (): void { | ||
$messageMap = new MessageMap( | ||
messages: [ | ||
new ToolResultMessage([ | ||
new ToolResult( | ||
'tool_1234', | ||
'search', | ||
[ | ||
'query' => 'Laravel collection methods', | ||
], | ||
'[search results]' | ||
), | ||
]), | ||
], | ||
systemPrompt: '' | ||
); | ||
|
||
expect($messageMap())->toBe([[ | ||
'role' => 'tool', | ||
'tool_call_id' => 'tool_1234', | ||
'content' => '[search results]', | ||
]]); | ||
}); | ||
|
||
it('maps system prompt', function (): void { | ||
$messageMap = new MessageMap( | ||
messages: [], | ||
systemPrompt: 'MODEL ADOPTS ROLE of [PERSONA: Nyx the Cthulhu]' | ||
); | ||
|
||
expect($messageMap())->toContain([ | ||
'role' => 'system', | ||
'content' => 'MODEL ADOPTS ROLE of [PERSONA: Nyx the Cthulhu]', | ||
]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Providers\OpenAI; | ||
|
||
use EchoLabs\Prism\Providers\OpenAI\Tool as OpenAITool; | ||
use EchoLabs\Prism\Tool; | ||
|
||
it('maps tools', function (): void { | ||
$tool = (new Tool) | ||
->as('search') | ||
->for('Searching the web') | ||
->withStringParameter('query', 'the detailed search query') | ||
->using(fn (): string => '[Search results]'); | ||
|
||
expect(OpenAITool::toArray($tool))->toBe([ | ||
'type' => 'function', | ||
'function' => [ | ||
'name' => $tool->name(), | ||
'description' => $tool->description(), | ||
'parameters' => [ | ||
'type' => 'object', | ||
'properties' => $tool->parameters(), | ||
'required' => $tool->requiredParameters(), | ||
], | ||
], | ||
]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Providers\OpenAI; | ||
|
||
use EchoLabs\Prism\Providers\OpenAI\MessageMap; | ||
use EchoLabs\Prism\ValueObjects\Messages\AssistantMessage; | ||
use EchoLabs\Prism\ValueObjects\Messages\Support\Image; | ||
use EchoLabs\Prism\ValueObjects\Messages\ToolResultMessage; | ||
use EchoLabs\Prism\ValueObjects\Messages\UserMessage; | ||
use EchoLabs\Prism\ValueObjects\ToolCall; | ||
use EchoLabs\Prism\ValueObjects\ToolResult; | ||
|
||
it('maps user messages', function (): void { | ||
$messageMap = new MessageMap( | ||
messages: [ | ||
new UserMessage('Who are you?'), | ||
], | ||
systemPrompt: '' | ||
); | ||
|
||
expect($messageMap())->toBe([[ | ||
'role' => 'user', | ||
'content' => [ | ||
['type' => 'text', 'text' => 'Who are you?'], | ||
], | ||
]]); | ||
}); | ||
|
||
it('maps user messages with images', function (): void { | ||
$messageMap = new MessageMap( | ||
messages: [ | ||
new UserMessage('Who are you?', [ | ||
Image::fromPath('tests/Fixtures/test-image.png'), | ||
]), | ||
], | ||
systemPrompt: '' | ||
); | ||
|
||
$mappedMessage = $messageMap(); | ||
|
||
expect(data_get($mappedMessage, '0.content.1.type')) | ||
->toBe('image_url'); | ||
expect(data_get($mappedMessage, '0.content.1.image_url.url')) | ||
->toStartWith('data:image/png;base64,'); | ||
expect(data_get($mappedMessage, '0.content.1.image_url.url')) | ||
->toContain(Image::fromPath('tests/Fixtures/test-image.png')->image); | ||
}); | ||
|
||
it('maps assistant message', function (): void { | ||
$messageMap = new MessageMap( | ||
messages: [ | ||
new AssistantMessage('I am Nyx'), | ||
], | ||
systemPrompt: '' | ||
); | ||
|
||
expect($messageMap())->toContain([ | ||
'role' => 'assistant', | ||
'content' => 'I am Nyx', | ||
]); | ||
}); | ||
|
||
it('maps assistant message with tool calls', function (): void { | ||
$messageMap = new MessageMap( | ||
messages: [ | ||
new AssistantMessage('I am Nyx', [ | ||
new ToolCall( | ||
'tool_1234', | ||
'search', | ||
[ | ||
'query' => 'Laravel collection methods', | ||
] | ||
), | ||
]), | ||
], | ||
systemPrompt: '' | ||
); | ||
|
||
expect($messageMap())->toBe([[ | ||
'role' => 'assistant', | ||
'content' => 'I am Nyx', | ||
'tool_calls' => [[ | ||
'id' => 'tool_1234', | ||
'type' => 'function', | ||
'function' => [ | ||
'name' => 'search', | ||
'arguments' => json_encode([ | ||
'query' => 'Laravel collection methods', | ||
]), | ||
], | ||
]], | ||
]]); | ||
}); | ||
|
||
it('maps tool result messages', function (): void { | ||
$messageMap = new MessageMap( | ||
messages: [ | ||
new ToolResultMessage([ | ||
new ToolResult( | ||
'tool_1234', | ||
'search', | ||
[ | ||
'query' => 'Laravel collection methods', | ||
], | ||
'[search results]' | ||
), | ||
]), | ||
], | ||
systemPrompt: '' | ||
); | ||
|
||
expect($messageMap())->toBe([[ | ||
'role' => 'tool', | ||
'tool_call_id' => 'tool_1234', | ||
'content' => '[search results]', | ||
]]); | ||
}); | ||
|
||
it('maps system prompt', function (): void { | ||
$messageMap = new MessageMap( | ||
messages: [], | ||
systemPrompt: 'MODEL ADOPTS ROLE of [PERSONA: Nyx the Cthulhu]' | ||
); | ||
|
||
expect($messageMap())->toContain([ | ||
'role' => 'system', | ||
'content' => 'MODEL ADOPTS ROLE of [PERSONA: Nyx the Cthulhu]', | ||
]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Providers\OpenAI; | ||
|
||
use EchoLabs\Prism\Providers\OpenAI\Tool as OpenAITool; | ||
use EchoLabs\Prism\Tool; | ||
|
||
it('maps tools', function (): void { | ||
$tool = (new Tool) | ||
->as('search') | ||
->for('Searching the web') | ||
->withStringParameter('query', 'the detailed search query') | ||
->using(fn (): string => '[Search results]'); | ||
|
||
expect(OpenAITool::toArray($tool))->toBe([ | ||
'type' => 'function', | ||
'function' => [ | ||
'name' => $tool->name(), | ||
'description' => $tool->description(), | ||
'parameters' => [ | ||
'type' => 'object', | ||
'properties' => $tool->parameters(), | ||
'required' => $tool->requiredParameters(), | ||
], | ||
], | ||
]); | ||
}); |
Oops, something went wrong.