Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
OskarStark committed Sep 22, 2024
1 parent bdd0dd7 commit 8a2e688
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 43 deletions.
14 changes: 8 additions & 6 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;
use Rector\PHPUnit\Set\PHPUnitSetList;

return RectorConfig::configure()
->withPaths([
__DIR__ . '/examples',
__DIR__ . '/src',
__DIR__ . '/tests',
__DIR__.'/examples',
__DIR__.'/src',
__DIR__.'/tests',
])
->withPhpSets(php82: true)
->withSets([
PHPUnitSetList::PHPUNIT_90,
PHPUnitSetList::PHPUNIT_110,
PHPUnitSetList::ANNOTATIONS_TO_ATTRIBUTES,
])
->withImportNames(importNames: true, importShortClasses: false)
->withSkip([
ClosureToArrowFunctionRector::class
ClosureToArrowFunctionRector::class,
])
->withTypeCoverageLevel(0);
1 change: 1 addition & 0 deletions src/ToolBox/Tool/Clock.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public function __construct(
private ClockInterface $clock,
) {
}

public function __invoke(): string
{
return sprintf(
Expand Down
2 changes: 2 additions & 0 deletions src/ToolBox/Tool/SerpApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function __construct(
private string $apiKey,
) {
}

/**
* @param string $query The search query to use
*/
Expand All @@ -29,6 +30,7 @@ public function __invoke(string $query): string

return sprintf('Results for "%s" are "%s".', $query, $this->extractBestResponse($response->toArray()));
}

/**
* @param array<string, mixed> $results
*/
Expand Down
3 changes: 3 additions & 0 deletions src/ToolBox/Tool/Wikipedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function __construct(
private string $locale = 'en',
) {
}

/**
* @param string $query The query to search for on Wikipedia
*/
Expand All @@ -32,6 +33,7 @@ public function search(string $query): string

return 'On Wikipedia, I found the following articles: '.implode(', ', $titles).'.';
}

/**
* @param string $title The title of the article to retrieve from Wikipedia
*/
Expand All @@ -47,6 +49,7 @@ public function getArticle(string $title): string

return current($result['query']['pages'])['extract'];
}

/**
* @param array<string, mixed> $query
*
Expand Down
19 changes: 13 additions & 6 deletions tests/Message/MessageBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpLlm\LlmChain\Message\MessageBag;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;

Expand All @@ -16,7 +17,8 @@
#[Small]
final class MessageBagTest extends TestCase
{
public function testGetSystemMessage(): void
#[Test]
public function getSystemMessage(): void
{
$messageBag = new MessageBag(
Message::forSystem('My amazing system prompt.'),
Expand All @@ -29,7 +31,8 @@ public function testGetSystemMessage(): void
self::assertSame('My amazing system prompt.', $systemMessage->content);
}

public function testGetSystemMessageWithoutSystemMessage(): void
#[Test]
public function getSystemMessageWithoutSystemMessage(): void
{
$messageBag = new MessageBag(
Message::ofAssistant('It is time to sleep.'),
Expand All @@ -39,7 +42,8 @@ public function testGetSystemMessageWithoutSystemMessage(): void
self::assertNull($messageBag->getSystemMessage());
}

public function testWith(): void
#[Test]
public function with(): void
{
$messageBag = new MessageBag(
Message::forSystem('My amazing system prompt.'),
Expand All @@ -55,7 +59,8 @@ public function testWith(): void
self::assertSame('It is time to wake up.', $newMessageBag[3]->content);
}

public function testWithoutSystemMessage(): void
#[Test]
public function withoutSystemMessage(): void
{
$messageBag = new MessageBag(
Message::forSystem('My amazing system prompt.'),
Expand All @@ -70,7 +75,8 @@ public function testWithoutSystemMessage(): void
self::assertSame('It is time to sleep.', $newMessageBag[0]->content);
}

public function testPrepend(): void
#[Test]
public function prepend(): void
{
$messageBag = new MessageBag(
Message::ofAssistant('It is time to sleep.'),
Expand All @@ -85,7 +91,8 @@ public function testPrepend(): void
self::assertSame('My amazing system prompt.', $newMessageBag[0]->content);
}

public function testJsonSerialize(): void
#[Test]
public function jsonSerialize(): void
{
$messageBag = new MessageBag(
Message::forSystem('My amazing system prompt.'),
Expand Down
19 changes: 13 additions & 6 deletions tests/Message/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;

Expand All @@ -17,7 +18,8 @@
#[Small]
final class MessageTest extends TestCase
{
public function testCreateSystemMessage(): void
#[Test]
public function createSystemMessage(): void
{
$message = Message::forSystem('My amazing system prompt.');

Expand All @@ -29,7 +31,8 @@ public function testCreateSystemMessage(): void
self::assertFalse($message->hasToolCalls());
}

public function testCreateAssistantMessage(): void
#[Test]
public function createAssistantMessage(): void
{
$message = Message::ofAssistant('It is time to sleep.');

Expand All @@ -41,7 +44,8 @@ public function testCreateAssistantMessage(): void
self::assertFalse($message->hasToolCalls());
}

public function testCreateAssistantMessageWithToolCalls(): void
#[Test]
public function createAssistantMessageWithToolCalls(): void
{
$toolCalls = [
new ToolCall('call_123456', 'my_tool', ['foo' => 'bar']),
Expand All @@ -57,7 +61,8 @@ public function testCreateAssistantMessageWithToolCalls(): void
self::assertTrue($message->hasToolCalls());
}

public function testCreateUserMessage(): void
#[Test]
public function createUserMessage(): void
{
$message = Message::ofUser('Hi, my name is John.');

Expand All @@ -69,7 +74,8 @@ public function testCreateUserMessage(): void
self::assertFalse($message->hasToolCalls());
}

public function testCreateToolCallMessage(): void
#[Test]
public function createToolCallMessage(): void
{
$toolCall = new ToolCall('call_123456', 'my_tool', ['foo' => 'bar']);
$message = Message::ofToolCall($toolCall, 'Foo bar.');
Expand All @@ -84,7 +90,8 @@ public function testCreateToolCallMessage(): void
}

#[DataProvider('provideJsonScenarios')]
public function testJsonSerialize(Message $message, array $expected): void
#[Test]
public function jsonSerialize(Message $message, array $expected): void
{
self::assertSame($expected, $message->jsonSerialize());
}
Expand Down
13 changes: 9 additions & 4 deletions tests/Response/ChoiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpLlm\LlmChain\Response\ToolCall;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;

Expand All @@ -16,7 +17,8 @@
#[Small]
final class ChoiceTest extends TestCase
{
public function testChoiceEmpty(): void
#[Test]
public function choiceEmpty(): void
{
$choice = new Choice();
self::assertFalse($choice->hasContent());
Expand All @@ -25,7 +27,8 @@ public function testChoiceEmpty(): void
self::assertCount(0, $choice->getToolCalls());
}

public function testChoiceWithContent(): void
#[Test]
public function choiceWithContent(): void
{
$choice = new Choice('content');
self::assertTrue($choice->hasContent());
Expand All @@ -34,7 +37,8 @@ public function testChoiceWithContent(): void
self::assertCount(0, $choice->getToolCalls());
}

public function testChoiceWithToolCall(): void
#[Test]
public function choiceWithToolCall(): void
{
$choice = new Choice(null, [new ToolCall('name', 'arguments')]);
self::assertFalse($choice->hasContent());
Expand All @@ -43,7 +47,8 @@ public function testChoiceWithToolCall(): void
self::assertCount(1, $choice->getToolCalls());
}

public function testChoiceWithContentAndToolCall(): void
#[Test]
public function choiceWithContentAndToolCall(): void
{
$choice = new Choice('content', [new ToolCall('name', 'arguments')]);
self::assertTrue($choice->hasContent());
Expand Down
25 changes: 17 additions & 8 deletions tests/Response/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpLlm\LlmChain\Response\ToolCall;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;

Expand All @@ -18,7 +19,8 @@
#[Small]
final class ResponseTest extends TestCase
{
public function testGetChoices(): void
#[Test]
public function getChoices(): void
{
$response = new Response(
new Choice('content', [new ToolCall('call_123456', 'name', ['foo' => 'bar'])]),
Expand All @@ -28,15 +30,17 @@ public function testGetChoices(): void
self::assertCount(2, $response->getChoices());
}

public function testConstructorThrowsException(): void
#[Test]
public function constructorThrowsException(): void
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Response must have at least one choice');

$response = new Response();
}

public function testGetContent(): void
#[Test]
public function getContent(): void
{
$response = new Response(
new Choice('content', [new ToolCall('call_123456', 'name', ['foo' => 'bar'])]),
Expand All @@ -45,7 +49,8 @@ public function testGetContent(): void
self::assertSame('content', $response->getContent());
}

public function testGetContentThrowsException(): void
#[Test]
public function getContentThrowsException(): void
{
$response = new Response(
new Choice('content', [new ToolCall('call_123456', 'name', ['foo' => 'bar'])]),
Expand All @@ -58,7 +63,8 @@ public function testGetContentThrowsException(): void
$response->getContent();
}

public function testGetToolCalls(): void
#[Test]
public function getToolCalls(): void
{
$response = new Response(
new Choice('content', [new ToolCall('call_123456', 'name', ['foo' => 'bar'])]),
Expand All @@ -67,7 +73,8 @@ public function testGetToolCalls(): void
self::assertCount(1, $response->getToolCalls());
}

public function testGetToolCallsThrowsException(): void
#[Test]
public function getToolCallsThrowsException(): void
{
$response = new Response(
new Choice('content', [new ToolCall('call_123456', 'name', ['foo' => 'bar'])]),
Expand All @@ -80,7 +87,8 @@ public function testGetToolCallsThrowsException(): void
$response->getToolCalls();
}

public function testHasToolCalls(): void
#[Test]
public function hasToolCalls(): void
{
$response = new Response(
new Choice('content', [new ToolCall('call_123456', 'name', ['foo' => 'bar'])]),
Expand All @@ -89,7 +97,8 @@ public function testHasToolCalls(): void
self::assertTrue($response->hasToolCalls());
}

public function testHasToolCallsReturnsFalse(): void
#[Test]
public function hasToolCallsReturnsFalse(): void
{
$response = new Response(new Choice('content'));

Expand Down
7 changes: 5 additions & 2 deletions tests/Response/ToolCallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,24 @@
use PhpLlm\LlmChain\Response\ToolCall;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

#[CoversClass(ToolCall::class)]
#[Small]
final class ToolCallTest extends TestCase
{
public function testToolCall(): void
#[Test]
public function toolCall(): void
{
$toolCall = new ToolCall('id', 'name', ['foo' => 'bar']);
self::assertSame('id', $toolCall->id);
self::assertSame('name', $toolCall->name);
self::assertSame(['foo' => 'bar'], $toolCall->arguments);
}

public function testToolCallJsonSerialize(): void
#[Test]
public function toolCallJsonSerialize(): void
{
$toolCall = new ToolCall('id', 'name', ['foo' => 'bar']);
self::assertSame(
Expand Down
Loading

0 comments on commit 8a2e688

Please sign in to comment.