Skip to content

Commit

Permalink
Apply refactors to groq
Browse files Browse the repository at this point in the history
  • Loading branch information
sixlive committed Oct 27, 2024
1 parent a0e98cc commit bd889aa
Show file tree
Hide file tree
Showing 7 changed files with 487 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Tests\Providers;
namespace Tests\Providers\Groq;

use EchoLabs\Prism\Enums\Provider;
use EchoLabs\Prism\Facades\Tool;
Expand All @@ -23,7 +23,8 @@

$response = Prism::text()
->using('groq', 'llama3-8b-8192')
->withPrompt('Who are you?')();
->withPrompt('Who are you?')
->generate();

expect($response->usage->promptTokens)->toBe(13);
expect($response->usage->completionTokens)->toBe(208);
Expand All @@ -40,7 +41,8 @@
$response = Prism::text()
->using('groq', 'llama3-8b-8192')
->withSystemPrompt('MODEL ADOPTS ROLE of [PERSONA: Nyx the Cthulhu]!')
->withPrompt('Who are you?')();
->withPrompt('Who are you?')
->generate();

expect($response->usage->promptTokens)->toBe(37);
expect($response->usage->completionTokens)->toBe(273);
Expand Down Expand Up @@ -69,7 +71,8 @@
->using('groq', 'llama3-groq-70b-8192-tool-use-preview')
->withTools($tools)
->withMaxSteps(3)
->withPrompt('What time is the tigers game today in Detroit and should I wear a coat?')();
->withPrompt('What time is the tigers game today in Detroit and should I wear a coat?')
->generate();

// Assert tool calls in the first step
$firstStep = $response->steps[0];
Expand Down
131 changes: 131 additions & 0 deletions tests/Providers/Groq/MessageMapTest.php
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]',
]);
});
29 changes: 29 additions & 0 deletions tests/Providers/Groq/ToolTest.php
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(),
],
],
]);
});
131 changes: 131 additions & 0 deletions tests/Providers/Mistral/MessageMapTest.php
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]',
]);
});
29 changes: 29 additions & 0 deletions tests/Providers/Mistral/ToolTest.php
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(),
],
],
]);
});
Loading

0 comments on commit bd889aa

Please sign in to comment.