@@ -239,6 +239,42 @@ See attribute class [With](src/Chain/JsonSchema/Attribute/With.php) for all avai
239
239
> [ !NOTE]
240
240
> Please be aware, that this is only converted in a JSON Schema for the LLM to respect, but not validated by LLM Chain.
241
241
242
+ #### Third-Party Tools
243
+
244
+ In some cases you might want to use third-party tools, which are not part of your application. Adding the ` #[AsTool] `
245
+ attribute to the class is not possible in those cases, but you can explicitly register the tool in the ` MemoryFactory ` :
246
+
247
+ ``` php
248
+ use PhpLlm\LlmChain\Chain\Toolbox\Toolbox;
249
+ use PhpLlm\LlmChain\Chain\Toolbox\MetadataFactory\MemoryFactory;
250
+ use Symfony\Component\Clock\Clock;
251
+
252
+ $metadataFactory = (new MemoryFactory())
253
+ ->addTool(Clock::class, 'clock', 'Get the current date and time', 'now');
254
+ $toolbox = new Toolbox($metadataFactory, [new Clock()]);
255
+ ```
256
+
257
+ > [ !NOTE]
258
+ > Please be aware that not all return types are supported by the toolbox, so a decorator might still be needed.
259
+
260
+ This can be combined with the ` ChainFactory ` which enables you to use explicitly registered tools and ` #[AsTool] ` tagged
261
+ tools in the same chain - which even enables you to overwrite the pre-existing configuration of a tool:
262
+
263
+ ``` php
264
+ use PhpLlm\LlmChain\Chain\Toolbox\Toolbox;
265
+ use PhpLlm\LlmChain\Chain\Toolbox\MetadataFactory\ChainFactory;
266
+ use PhpLlm\LlmChain\Chain\Toolbox\MetadataFactory\MemoryFactory;
267
+ use PhpLlm\LlmChain\Chain\Toolbox\MetadataFactory\ReflectionFactory;
268
+
269
+ $reflectionFactory = new ReflectionFactory(); // Register tools with #[AsTool] attribute
270
+ $metadataFactory = (new MemoryFactory()) // Register or overwrite tools explicitly
271
+ ->addTool(...);
272
+ $toolbox = new Toolbox(new ChainFactory($metadataFactory, $reflectionFactory), [...]);
273
+ ```
274
+
275
+ > [ !NOTE]
276
+ > The order of the factories in the ` ChainFactory ` matters, as the first factory has the highest priority.
277
+
242
278
#### Fault Tolerance
243
279
244
280
To gracefully handle errors that occur during tool calling, e.g. wrong tool names or runtime errors, you can use the
0 commit comments