Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@ LLM Chain generates a JSON Schema representation for all tools in the `ToolBox`
method arguments and param comments in the doc block. Additionally, JSON Schema support validation rules, which are
partially support by LLMs like GPT.

To leverage this, configure the `#[ToolParameter]` attribute on the method arguments of your tool:
To leverage this, configure the `#[With]` attribute on the method arguments of your tool:
```php
use PhpLlm\LlmChain\ToolBox\Attribute\AsTool;
use PhpLlm\LlmChain\ToolBox\Attribute\ToolParameter;
use PhpLlm\LlmChain\Chain\ToolBox\Attribute\AsTool;
use PhpLlm\LlmChain\Chain\ToolBox\Attribute\ToolParameter;

#[AsTool('my_tool', 'Example tool with parameters requirements.')]
final class MyTool
Expand All @@ -212,17 +212,17 @@ final class MyTool
* @param int $number The number of an object
*/
public function __invoke(
#[ToolParameter(pattern: '/([a-z0-1]){5}/')]
#[With(pattern: '/([a-z0-1]){5}/')]
string $name,
#[ToolParameter(minimum: 0, maximum: 10)]
#[With(minimum: 0, maximum: 10)]
int $number,
): string {
// ...
}
}
```

See attribute class [ToolParameter](src/Chain/ToolBox/Attribute/ToolParameter.php) for all available options.
See attribute class [With](src/Chain/ToolBox/Attribute/With.php) for all available options.

> [!NOTE]
> Please be aware, that this is only converted in a JSON Schema for the LLM to respect, but not validated by LLM Chain.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Webmozart\Assert\Assert;

#[\Attribute(\Attribute::TARGET_PARAMETER)]
final readonly class ToolParameter
final readonly class With
{
/**
* @param list<int|string>|null $enum
Expand Down
4 changes: 2 additions & 2 deletions src/Chain/ToolBox/ParameterAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace PhpLlm\LlmChain\Chain\ToolBox;

use PhpLlm\LlmChain\Chain\ToolBox\Attribute\ToolParameter;
use PhpLlm\LlmChain\Chain\ToolBox\Attribute\With;
use PhpLlm\LlmChain\Chain\ToolBox\Exception\ToolConfigurationException;

/**
Expand Down Expand Up @@ -81,7 +81,7 @@ public function getDefinition(string $className, string $methodName): ?array
];

// Check for ToolParameter attributes
$attributes = $parameter->getAttributes(ToolParameter::class);
$attributes = $parameter->getAttributes(With::class);
if (count($attributes) > 0) {
$attributeState = array_filter((array) $attributes[0]->newInstance(), fn ($value) => null !== $value);
$property = array_merge($property, $attributeState);
Expand Down
4 changes: 2 additions & 2 deletions src/Chain/ToolBox/Tool/OpenMeteo.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace PhpLlm\LlmChain\Chain\ToolBox\Tool;

use PhpLlm\LlmChain\Chain\ToolBox\Attribute\AsTool;
use PhpLlm\LlmChain\Chain\ToolBox\Attribute\ToolParameter;
use PhpLlm\LlmChain\Chain\ToolBox\Attribute\With;
use Symfony\Contracts\HttpClient\HttpClientInterface;

#[AsTool(name: 'weather_current', description: 'get current weather for a location', method: 'current')]
Expand Down Expand Up @@ -94,7 +94,7 @@ public function current(float $latitude, float $longitude): array
public function forecast(
float $latitude,
float $longitude,
#[ToolParameter(minimum: 1, maximum: 16)]
#[With(minimum: 1, maximum: 16)]
int $days = 7,
): array {
$response = $this->httpClient->request('GET', 'https://api.open-meteo.com/v1/forecast', [
Expand Down
62 changes: 31 additions & 31 deletions tests/Chain/ToolBox/Attribute/ToolParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@

namespace PhpLlm\LlmChain\Tests\Chain\ToolBox\Attribute;

use PhpLlm\LlmChain\Chain\ToolBox\Attribute\ToolParameter;
use PhpLlm\LlmChain\Chain\ToolBox\Attribute\With;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Webmozart\Assert\InvalidArgumentException;

#[CoversClass(ToolParameter::class)]
#[CoversClass(With::class)]
final class ToolParameterTest extends TestCase
{
#[Test]
public function validEnum(): void
{
$enum = ['value1', 'value2'];
$toolParameter = new ToolParameter(enum: $enum);
$toolParameter = new With(enum: $enum);
self::assertSame($enum, $toolParameter->enum);
}

Expand All @@ -26,14 +26,14 @@ public function invalidEnumContainsNonString(): void
{
$this->expectException(InvalidArgumentException::class);
$enum = ['value1', 2];
new ToolParameter(enum: $enum);
new With(enum: $enum);
}

#[Test]
public function validConstString(): void
{
$const = 'constant value';
$toolParameter = new ToolParameter(const: $const);
$toolParameter = new With(const: $const);
self::assertSame($const, $toolParameter->const);
}

Expand All @@ -42,14 +42,14 @@ public function invalidConstEmptyString(): void
{
$this->expectException(InvalidArgumentException::class);
$const = ' ';
new ToolParameter(const: $const);
new With(const: $const);
}

#[Test]
public function validPattern(): void
{
$pattern = '/^[a-z]+$/';
$toolParameter = new ToolParameter(pattern: $pattern);
$toolParameter = new With(pattern: $pattern);
self::assertSame($pattern, $toolParameter->pattern);
}

Expand All @@ -58,30 +58,30 @@ public function invalidPatternEmptyString(): void
{
$this->expectException(InvalidArgumentException::class);
$pattern = ' ';
new ToolParameter(pattern: $pattern);
new With(pattern: $pattern);
}

#[Test]
public function validMinLength(): void
{
$minLength = 5;
$toolParameter = new ToolParameter(minLength: $minLength);
$toolParameter = new With(minLength: $minLength);
self::assertSame($minLength, $toolParameter->minLength);
}

#[Test]
public function invalidMinLengthNegative(): void
{
$this->expectException(InvalidArgumentException::class);
new ToolParameter(minLength: -1);
new With(minLength: -1);
}

#[Test]
public function validMinLengthAndMaxLength(): void
{
$minLength = 5;
$maxLength = 10;
$toolParameter = new ToolParameter(minLength: $minLength, maxLength: $maxLength);
$toolParameter = new With(minLength: $minLength, maxLength: $maxLength);
self::assertSame($minLength, $toolParameter->minLength);
self::assertSame($maxLength, $toolParameter->maxLength);
}
Expand All @@ -90,45 +90,45 @@ public function validMinLengthAndMaxLength(): void
public function invalidMaxLengthLessThanMinLength(): void
{
$this->expectException(InvalidArgumentException::class);
new ToolParameter(minLength: 10, maxLength: 5);
new With(minLength: 10, maxLength: 5);
}

#[Test]
public function validMinimum(): void
{
$minimum = 0;
$toolParameter = new ToolParameter(minimum: $minimum);
$toolParameter = new With(minimum: $minimum);
self::assertSame($minimum, $toolParameter->minimum);
}

#[Test]
public function invalidMinimumNegative(): void
{
$this->expectException(InvalidArgumentException::class);
new ToolParameter(minimum: -1);
new With(minimum: -1);
}

#[Test]
public function validMultipleOf(): void
{
$multipleOf = 5;
$toolParameter = new ToolParameter(multipleOf: $multipleOf);
$toolParameter = new With(multipleOf: $multipleOf);
self::assertSame($multipleOf, $toolParameter->multipleOf);
}

#[Test]
public function invalidMultipleOfNegative(): void
{
$this->expectException(InvalidArgumentException::class);
new ToolParameter(multipleOf: -5);
new With(multipleOf: -5);
}

#[Test]
public function validExclusiveMinimumAndMaximum(): void
{
$exclusiveMinimum = 1;
$exclusiveMaximum = 10;
$toolParameter = new ToolParameter(exclusiveMinimum: $exclusiveMinimum, exclusiveMaximum: $exclusiveMaximum);
$toolParameter = new With(exclusiveMinimum: $exclusiveMinimum, exclusiveMaximum: $exclusiveMaximum);
self::assertSame($exclusiveMinimum, $toolParameter->exclusiveMinimum);
self::assertSame($exclusiveMaximum, $toolParameter->exclusiveMaximum);
}
Expand All @@ -137,15 +137,15 @@ public function validExclusiveMinimumAndMaximum(): void
public function invalidExclusiveMaximumLessThanExclusiveMinimum(): void
{
$this->expectException(InvalidArgumentException::class);
new ToolParameter(exclusiveMinimum: 10, exclusiveMaximum: 5);
new With(exclusiveMinimum: 10, exclusiveMaximum: 5);
}

#[Test]
public function validMinItemsAndMaxItems(): void
{
$minItems = 1;
$maxItems = 5;
$toolParameter = new ToolParameter(minItems: $minItems, maxItems: $maxItems);
$toolParameter = new With(minItems: $minItems, maxItems: $maxItems);
self::assertSame($minItems, $toolParameter->minItems);
self::assertSame($maxItems, $toolParameter->maxItems);
}
Expand All @@ -154,29 +154,29 @@ public function validMinItemsAndMaxItems(): void
public function invalidMaxItemsLessThanMinItems(): void
{
$this->expectException(InvalidArgumentException::class);
new ToolParameter(minItems: 5, maxItems: 1);
new With(minItems: 5, maxItems: 1);
}

#[Test]
public function validUniqueItemsTrue(): void
{
$toolParameter = new ToolParameter(uniqueItems: true);
$toolParameter = new With(uniqueItems: true);
self::assertTrue($toolParameter->uniqueItems);
}

#[Test]
public function invalidUniqueItemsFalse(): void
{
$this->expectException(InvalidArgumentException::class);
new ToolParameter(uniqueItems: false);
new With(uniqueItems: false);
}

#[Test]
public function validMinContainsAndMaxContains(): void
{
$minContains = 1;
$maxContains = 3;
$toolParameter = new ToolParameter(minContains: $minContains, maxContains: $maxContains);
$toolParameter = new With(minContains: $minContains, maxContains: $maxContains);
self::assertSame($minContains, $toolParameter->minContains);
self::assertSame($maxContains, $toolParameter->maxContains);
}
Expand All @@ -185,13 +185,13 @@ public function validMinContainsAndMaxContains(): void
public function invalidMaxContainsLessThanMinContains(): void
{
$this->expectException(InvalidArgumentException::class);
new ToolParameter(minContains: 3, maxContains: 1);
new With(minContains: 3, maxContains: 1);
}

#[Test]
public function validRequired(): void
{
$toolParameter = new ToolParameter(required: true);
$toolParameter = new With(required: true);
self::assertTrue($toolParameter->required);
}

Expand All @@ -200,7 +200,7 @@ public function validMinPropertiesAndMaxProperties(): void
{
$minProperties = 1;
$maxProperties = 5;
$toolParameter = new ToolParameter(minProperties: $minProperties, maxProperties: $maxProperties);
$toolParameter = new With(minProperties: $minProperties, maxProperties: $maxProperties);
self::assertSame($minProperties, $toolParameter->minProperties);
self::assertSame($maxProperties, $toolParameter->maxProperties);
}
Expand All @@ -209,20 +209,20 @@ public function validMinPropertiesAndMaxProperties(): void
public function invalidMaxPropertiesLessThanMinProperties(): void
{
$this->expectException(InvalidArgumentException::class);
new ToolParameter(minProperties: 5, maxProperties: 1);
new With(minProperties: 5, maxProperties: 1);
}

#[Test]
public function validDependentRequired(): void
{
$toolParameter = new ToolParameter(dependentRequired: true);
$toolParameter = new With(dependentRequired: true);
self::assertTrue($toolParameter->dependentRequired);
}

#[Test]
public function validCombination(): void
{
$toolParameter = new ToolParameter(
$toolParameter = new With(
enum: ['value1', 'value2'],
const: 'constant',
pattern: '/^[a-z]+$/',
Expand All @@ -244,13 +244,13 @@ enum: ['value1', 'value2'],
dependentRequired: true
);

self::assertInstanceOf(ToolParameter::class, $toolParameter);
self::assertInstanceOf(With::class, $toolParameter);
}

#[Test]
public function invalidCombination(): void
{
$this->expectException(InvalidArgumentException::class);
new ToolParameter(minLength: -1, maxLength: -2);
new With(minLength: -1, maxLength: -2);
}
}
4 changes: 2 additions & 2 deletions tests/Chain/ToolBox/ParameterAnalyzerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace PhpLlm\LlmChain\Tests\Chain\ToolBox;

use PhpLlm\LlmChain\Chain\ToolBox\Attribute\AsTool;
use PhpLlm\LlmChain\Chain\ToolBox\Attribute\ToolParameter;
use PhpLlm\LlmChain\Chain\ToolBox\Attribute\With;
use PhpLlm\LlmChain\Chain\ToolBox\Metadata;
use PhpLlm\LlmChain\Chain\ToolBox\ParameterAnalyzer;
use PhpLlm\LlmChain\Tests\Fixture\Tool\ToolNoParams;
Expand All @@ -22,7 +22,7 @@
#[UsesClass(AsTool::class)]
#[UsesClass(Metadata::class)]
#[UsesClass(ParameterAnalyzer::class)]
#[UsesClass(ToolParameter::class)]
#[UsesClass(With::class)]
final class ParameterAnalyzerTest extends TestCase
{
private ParameterAnalyzer $analyzer;
Expand Down
Loading