Skip to content

Commit 55c2882

Browse files
committed
refactor: rename tool param attribute
1 parent b71864f commit 55c2882

File tree

7 files changed

+53
-61
lines changed

7 files changed

+53
-61
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,10 @@ LLM Chain generates a JSON Schema representation for all tools in the `ToolBox`
199199
method arguments and param comments in the doc block. Additionally, JSON Schema support validation rules, which are
200200
partially support by LLMs like GPT.
201201

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

207207
#[AsTool('my_tool', 'Example tool with parameters requirements.')]
208208
final class MyTool
@@ -212,17 +212,17 @@ final class MyTool
212212
* @param int $number The number of an object
213213
*/
214214
public function __invoke(
215-
#[ToolParameter(pattern: '/([a-z0-1]){5}/')]
215+
#[With(pattern: '/([a-z0-1]){5}/')]
216216
string $name,
217-
#[ToolParameter(minimum: 0, maximum: 10)]
217+
#[With(minimum: 0, maximum: 10)]
218218
int $number,
219219
): string {
220220
// ...
221221
}
222222
}
223223
```
224224

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

227227
> [!NOTE]
228228
> Please be aware, that this is only converted in a JSON Schema for the LLM to respect, but not validated by LLM Chain.

src/Chain/ToolBox/Attribute/ToolParameter.php renamed to src/Chain/ToolBox/Attribute/With.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Webmozart\Assert\Assert;
88

99
#[\Attribute(\Attribute::TARGET_PARAMETER)]
10-
final readonly class ToolParameter
10+
final readonly class With
1111
{
1212
/**
1313
* @param list<int|string>|null $enum

src/Chain/ToolBox/ParameterAnalyzer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace PhpLlm\LlmChain\Chain\ToolBox;
66

7-
use PhpLlm\LlmChain\Chain\ToolBox\Attribute\ToolParameter;
7+
use PhpLlm\LlmChain\Chain\ToolBox\Attribute\With;
88
use PhpLlm\LlmChain\Chain\ToolBox\Exception\ToolConfigurationException;
99

1010
/**
@@ -81,7 +81,7 @@ public function getDefinition(string $className, string $methodName): ?array
8181
];
8282

8383
// Check for ToolParameter attributes
84-
$attributes = $parameter->getAttributes(ToolParameter::class);
84+
$attributes = $parameter->getAttributes(With::class);
8585
if (count($attributes) > 0) {
8686
$attributeState = array_filter((array) $attributes[0]->newInstance(), fn ($value) => null !== $value);
8787
$property = array_merge($property, $attributeState);

src/Chain/ToolBox/Tool/OpenMeteo.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace PhpLlm\LlmChain\Chain\ToolBox\Tool;
66

77
use PhpLlm\LlmChain\Chain\ToolBox\Attribute\AsTool;
8-
use PhpLlm\LlmChain\Chain\ToolBox\Attribute\ToolParameter;
8+
use PhpLlm\LlmChain\Chain\ToolBox\Attribute\With;
99
use Symfony\Contracts\HttpClient\HttpClientInterface;
1010

1111
#[AsTool(name: 'weather_current', description: 'get current weather for a location', method: 'current')]
@@ -94,7 +94,7 @@ public function current(float $latitude, float $longitude): array
9494
public function forecast(
9595
float $latitude,
9696
float $longitude,
97-
#[ToolParameter(minimum: 1, maximum: 16)]
97+
#[With(minimum: 1, maximum: 16)]
9898
int $days = 7,
9999
): array {
100100
$response = $this->httpClient->request('GET', 'https://api.open-meteo.com/v1/forecast', [

tests/Chain/ToolBox/Attribute/ToolParameterTest.php

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@
44

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

7-
use PhpLlm\LlmChain\Chain\ToolBox\Attribute\ToolParameter;
7+
use PhpLlm\LlmChain\Chain\ToolBox\Attribute\With;
88
use PHPUnit\Framework\Attributes\CoversClass;
99
use PHPUnit\Framework\Attributes\Test;
1010
use PHPUnit\Framework\TestCase;
1111
use Webmozart\Assert\InvalidArgumentException;
1212

13-
#[CoversClass(ToolParameter::class)]
13+
#[CoversClass(With::class)]
1414
final class ToolParameterTest extends TestCase
1515
{
1616
#[Test]
1717
public function validEnum(): void
1818
{
1919
$enum = ['value1', 'value2'];
20-
$toolParameter = new ToolParameter(enum: $enum);
20+
$toolParameter = new With(enum: $enum);
2121
self::assertSame($enum, $toolParameter->enum);
2222
}
2323

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

3232
#[Test]
3333
public function validConstString(): void
3434
{
3535
$const = 'constant value';
36-
$toolParameter = new ToolParameter(const: $const);
36+
$toolParameter = new With(const: $const);
3737
self::assertSame($const, $toolParameter->const);
3838
}
3939

@@ -42,14 +42,14 @@ public function invalidConstEmptyString(): void
4242
{
4343
$this->expectException(InvalidArgumentException::class);
4444
$const = ' ';
45-
new ToolParameter(const: $const);
45+
new With(const: $const);
4646
}
4747

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

@@ -58,30 +58,30 @@ public function invalidPatternEmptyString(): void
5858
{
5959
$this->expectException(InvalidArgumentException::class);
6060
$pattern = ' ';
61-
new ToolParameter(pattern: $pattern);
61+
new With(pattern: $pattern);
6262
}
6363

6464
#[Test]
6565
public function validMinLength(): void
6666
{
6767
$minLength = 5;
68-
$toolParameter = new ToolParameter(minLength: $minLength);
68+
$toolParameter = new With(minLength: $minLength);
6969
self::assertSame($minLength, $toolParameter->minLength);
7070
}
7171

7272
#[Test]
7373
public function invalidMinLengthNegative(): void
7474
{
7575
$this->expectException(InvalidArgumentException::class);
76-
new ToolParameter(minLength: -1);
76+
new With(minLength: -1);
7777
}
7878

7979
#[Test]
8080
public function validMinLengthAndMaxLength(): void
8181
{
8282
$minLength = 5;
8383
$maxLength = 10;
84-
$toolParameter = new ToolParameter(minLength: $minLength, maxLength: $maxLength);
84+
$toolParameter = new With(minLength: $minLength, maxLength: $maxLength);
8585
self::assertSame($minLength, $toolParameter->minLength);
8686
self::assertSame($maxLength, $toolParameter->maxLength);
8787
}
@@ -90,45 +90,45 @@ public function validMinLengthAndMaxLength(): void
9090
public function invalidMaxLengthLessThanMinLength(): void
9191
{
9292
$this->expectException(InvalidArgumentException::class);
93-
new ToolParameter(minLength: 10, maxLength: 5);
93+
new With(minLength: 10, maxLength: 5);
9494
}
9595

9696
#[Test]
9797
public function validMinimum(): void
9898
{
9999
$minimum = 0;
100-
$toolParameter = new ToolParameter(minimum: $minimum);
100+
$toolParameter = new With(minimum: $minimum);
101101
self::assertSame($minimum, $toolParameter->minimum);
102102
}
103103

104104
#[Test]
105105
public function invalidMinimumNegative(): void
106106
{
107107
$this->expectException(InvalidArgumentException::class);
108-
new ToolParameter(minimum: -1);
108+
new With(minimum: -1);
109109
}
110110

111111
#[Test]
112112
public function validMultipleOf(): void
113113
{
114114
$multipleOf = 5;
115-
$toolParameter = new ToolParameter(multipleOf: $multipleOf);
115+
$toolParameter = new With(multipleOf: $multipleOf);
116116
self::assertSame($multipleOf, $toolParameter->multipleOf);
117117
}
118118

119119
#[Test]
120120
public function invalidMultipleOfNegative(): void
121121
{
122122
$this->expectException(InvalidArgumentException::class);
123-
new ToolParameter(multipleOf: -5);
123+
new With(multipleOf: -5);
124124
}
125125

126126
#[Test]
127127
public function validExclusiveMinimumAndMaximum(): void
128128
{
129129
$exclusiveMinimum = 1;
130130
$exclusiveMaximum = 10;
131-
$toolParameter = new ToolParameter(exclusiveMinimum: $exclusiveMinimum, exclusiveMaximum: $exclusiveMaximum);
131+
$toolParameter = new With(exclusiveMinimum: $exclusiveMinimum, exclusiveMaximum: $exclusiveMaximum);
132132
self::assertSame($exclusiveMinimum, $toolParameter->exclusiveMinimum);
133133
self::assertSame($exclusiveMaximum, $toolParameter->exclusiveMaximum);
134134
}
@@ -137,15 +137,15 @@ public function validExclusiveMinimumAndMaximum(): void
137137
public function invalidExclusiveMaximumLessThanExclusiveMinimum(): void
138138
{
139139
$this->expectException(InvalidArgumentException::class);
140-
new ToolParameter(exclusiveMinimum: 10, exclusiveMaximum: 5);
140+
new With(exclusiveMinimum: 10, exclusiveMaximum: 5);
141141
}
142142

143143
#[Test]
144144
public function validMinItemsAndMaxItems(): void
145145
{
146146
$minItems = 1;
147147
$maxItems = 5;
148-
$toolParameter = new ToolParameter(minItems: $minItems, maxItems: $maxItems);
148+
$toolParameter = new With(minItems: $minItems, maxItems: $maxItems);
149149
self::assertSame($minItems, $toolParameter->minItems);
150150
self::assertSame($maxItems, $toolParameter->maxItems);
151151
}
@@ -154,29 +154,29 @@ public function validMinItemsAndMaxItems(): void
154154
public function invalidMaxItemsLessThanMinItems(): void
155155
{
156156
$this->expectException(InvalidArgumentException::class);
157-
new ToolParameter(minItems: 5, maxItems: 1);
157+
new With(minItems: 5, maxItems: 1);
158158
}
159159

160160
#[Test]
161161
public function validUniqueItemsTrue(): void
162162
{
163-
$toolParameter = new ToolParameter(uniqueItems: true);
163+
$toolParameter = new With(uniqueItems: true);
164164
self::assertTrue($toolParameter->uniqueItems);
165165
}
166166

167167
#[Test]
168168
public function invalidUniqueItemsFalse(): void
169169
{
170170
$this->expectException(InvalidArgumentException::class);
171-
new ToolParameter(uniqueItems: false);
171+
new With(uniqueItems: false);
172172
}
173173

174174
#[Test]
175175
public function validMinContainsAndMaxContains(): void
176176
{
177177
$minContains = 1;
178178
$maxContains = 3;
179-
$toolParameter = new ToolParameter(minContains: $minContains, maxContains: $maxContains);
179+
$toolParameter = new With(minContains: $minContains, maxContains: $maxContains);
180180
self::assertSame($minContains, $toolParameter->minContains);
181181
self::assertSame($maxContains, $toolParameter->maxContains);
182182
}
@@ -185,13 +185,13 @@ public function validMinContainsAndMaxContains(): void
185185
public function invalidMaxContainsLessThanMinContains(): void
186186
{
187187
$this->expectException(InvalidArgumentException::class);
188-
new ToolParameter(minContains: 3, maxContains: 1);
188+
new With(minContains: 3, maxContains: 1);
189189
}
190190

191191
#[Test]
192192
public function validRequired(): void
193193
{
194-
$toolParameter = new ToolParameter(required: true);
194+
$toolParameter = new With(required: true);
195195
self::assertTrue($toolParameter->required);
196196
}
197197

@@ -200,7 +200,7 @@ public function validMinPropertiesAndMaxProperties(): void
200200
{
201201
$minProperties = 1;
202202
$maxProperties = 5;
203-
$toolParameter = new ToolParameter(minProperties: $minProperties, maxProperties: $maxProperties);
203+
$toolParameter = new With(minProperties: $minProperties, maxProperties: $maxProperties);
204204
self::assertSame($minProperties, $toolParameter->minProperties);
205205
self::assertSame($maxProperties, $toolParameter->maxProperties);
206206
}
@@ -209,20 +209,20 @@ public function validMinPropertiesAndMaxProperties(): void
209209
public function invalidMaxPropertiesLessThanMinProperties(): void
210210
{
211211
$this->expectException(InvalidArgumentException::class);
212-
new ToolParameter(minProperties: 5, maxProperties: 1);
212+
new With(minProperties: 5, maxProperties: 1);
213213
}
214214

215215
#[Test]
216216
public function validDependentRequired(): void
217217
{
218-
$toolParameter = new ToolParameter(dependentRequired: true);
218+
$toolParameter = new With(dependentRequired: true);
219219
self::assertTrue($toolParameter->dependentRequired);
220220
}
221221

222222
#[Test]
223223
public function validCombination(): void
224224
{
225-
$toolParameter = new ToolParameter(
225+
$toolParameter = new With(
226226
enum: ['value1', 'value2'],
227227
const: 'constant',
228228
pattern: '/^[a-z]+$/',
@@ -244,13 +244,13 @@ enum: ['value1', 'value2'],
244244
dependentRequired: true
245245
);
246246

247-
self::assertInstanceOf(ToolParameter::class, $toolParameter);
247+
self::assertInstanceOf(With::class, $toolParameter);
248248
}
249249

250250
#[Test]
251251
public function invalidCombination(): void
252252
{
253253
$this->expectException(InvalidArgumentException::class);
254-
new ToolParameter(minLength: -1, maxLength: -2);
254+
new With(minLength: -1, maxLength: -2);
255255
}
256256
}

tests/Chain/ToolBox/ParameterAnalyzerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace PhpLlm\LlmChain\Tests\Chain\ToolBox;
66

77
use PhpLlm\LlmChain\Chain\ToolBox\Attribute\AsTool;
8-
use PhpLlm\LlmChain\Chain\ToolBox\Attribute\ToolParameter;
8+
use PhpLlm\LlmChain\Chain\ToolBox\Attribute\With;
99
use PhpLlm\LlmChain\Chain\ToolBox\Metadata;
1010
use PhpLlm\LlmChain\Chain\ToolBox\ParameterAnalyzer;
1111
use PhpLlm\LlmChain\Tests\Fixture\Tool\ToolNoParams;
@@ -22,7 +22,7 @@
2222
#[UsesClass(AsTool::class)]
2323
#[UsesClass(Metadata::class)]
2424
#[UsesClass(ParameterAnalyzer::class)]
25-
#[UsesClass(ToolParameter::class)]
25+
#[UsesClass(With::class)]
2626
final class ParameterAnalyzerTest extends TestCase
2727
{
2828
private ParameterAnalyzer $analyzer;

0 commit comments

Comments
 (0)