Skip to content

Commit 21a772c

Browse files
authored
Provider meta on tools & OpenAI Strict Tool Schema (#51)
1 parent 9b6bd23 commit 21a772c

File tree

6 files changed

+71
-32
lines changed

6 files changed

+71
-32
lines changed

Diff for: src/Concerns/BuildsTextRequests.php

+18
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace EchoLabs\Prism\Concerns;
66

77
use EchoLabs\Prism\Contracts\Message;
8+
use EchoLabs\Prism\Enums\Provider;
89
use EchoLabs\Prism\Enums\ToolChoice;
910
use EchoLabs\Prism\Exceptions\PrismException;
1011
use EchoLabs\Prism\Requests\TextRequest;
@@ -37,6 +38,23 @@ trait BuildsTextRequests
3738

3839
protected string|ToolChoice|null $toolChoice = null;
3940

41+
protected string $provider;
42+
43+
protected string $model;
44+
45+
public function using(string|Provider $provider, string $model): self
46+
{
47+
$this->provider = is_string($provider) ? $provider : $provider->value;
48+
$this->model = $model;
49+
50+
return $this;
51+
}
52+
53+
public function provider(): string
54+
{
55+
return $this->provider;
56+
}
57+
4058
public function withPrompt(string|View $prompt): self
4159
{
4260
if ($this->messages) {

Diff for: src/Concerns/HasProvider.php

-27
This file was deleted.

Diff for: src/Generators/TextGenerator.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use EchoLabs\Prism\Concerns\BuildsTextRequests;
88
use EchoLabs\Prism\Concerns\HandlesToolCalls;
9-
use EchoLabs\Prism\Concerns\HasProvider;
109
use EchoLabs\Prism\Enums\FinishReason;
1110
use EchoLabs\Prism\PrismManager;
1211
use EchoLabs\Prism\Providers\ProviderResponse;
@@ -20,7 +19,7 @@
2019

2120
class TextGenerator
2221
{
23-
use BuildsTextRequests, HandlesToolCalls, HasProvider;
22+
use BuildsTextRequests, HandlesToolCalls;
2423

2524
protected TextState $state;
2625

Diff for: src/Providers/OpenAI/Tool.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace EchoLabs\Prism\Providers\OpenAI;
66

7+
use EchoLabs\Prism\Enums\Provider;
78
use EchoLabs\Prism\Providers\ProviderTool;
89
use EchoLabs\Prism\Tool as PrismTool;
910

@@ -12,7 +13,7 @@ class Tool extends ProviderTool
1213
#[\Override]
1314
public static function toArray(PrismTool $tool): array
1415
{
15-
return [
16+
return array_filter([
1617
'type' => 'function',
1718
'function' => [
1819
'name' => $tool->name(),
@@ -23,6 +24,7 @@ public static function toArray(PrismTool $tool): array
2324
'required' => $tool->requiredParameters(),
2425
],
2526
],
26-
];
27+
'strict' => data_get($tool->providerMeta(Provider::OpenAI), 'strict', null),
28+
]);
2729
}
2830
}

Diff for: src/Tool.php

+22-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use ArgumentCountError;
88
use Closure;
99
use EchoLabs\Prism\Contracts\Schema;
10+
use EchoLabs\Prism\Enums\Provider;
1011
use EchoLabs\Prism\Exceptions\PrismException;
1112
use EchoLabs\Prism\Schema\ArraySchema;
1213
use EchoLabs\Prism\Schema\BooleanSchema;
@@ -33,6 +34,9 @@ class Tool
3334
/** @var Closure():string|callable():string */
3435
protected $fn;
3536

37+
/** @var array<string, array<string, mixed>> */
38+
protected $providerMeta = [];
39+
3640
public function as(string $name): self
3741
{
3842
$this->name = $name;
@@ -47,7 +51,6 @@ public function for(string $description): self
4751
return $this;
4852
}
4953

50-
/** @param Closure():string|callable():string $fn */
5154
public function using(Closure|callable $fn): self
5255
{
5356
$this->fn = $fn;
@@ -136,6 +139,24 @@ public function withEnumParameter(
136139
return $this;
137140
}
138141

142+
/**
143+
* @param array<string, mixed> $meta
144+
*/
145+
public function withProviderMeta(Provider $provider, array $meta): self
146+
{
147+
$this->providerMeta[$provider->value] = $meta;
148+
149+
return $this;
150+
}
151+
152+
/**
153+
* @return array<string, mixed>> $meta
154+
*/
155+
public function providerMeta(Provider $provider): array
156+
{
157+
return data_get($this->providerMeta, $provider->value, []);
158+
}
159+
139160
/** @return array<int, string> */
140161
public function requiredParameters(): array
141162
{

Diff for: tests/Providers/OpenAI/ToolTest.php

+26
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Tests\Providers\OpenAI;
66

7+
use EchoLabs\Prism\Enums\Provider;
78
use EchoLabs\Prism\Providers\OpenAI\Tool as OpenAITool;
89
use EchoLabs\Prism\Tool;
910

@@ -27,3 +28,28 @@
2728
],
2829
]);
2930
});
31+
32+
it('maps tools with strict mode', function (): void {
33+
$tool = (new Tool)
34+
->as('search')
35+
->for('Searching the web')
36+
->withStringParameter('query', 'the detailed search query')
37+
->using(fn (): string => '[Search results]')
38+
->withProviderMeta(Provider::OpenAI, [
39+
'strict' => true,
40+
]);
41+
42+
expect(OpenAITool::toArray($tool))->toBe([
43+
'type' => 'function',
44+
'function' => [
45+
'name' => $tool->name(),
46+
'description' => $tool->description(),
47+
'parameters' => [
48+
'type' => 'object',
49+
'properties' => $tool->parameters(),
50+
'required' => $tool->requiredParameters(),
51+
],
52+
],
53+
'strict' => true,
54+
]);
55+
});

0 commit comments

Comments
 (0)