Skip to content

Commit 3a6d402

Browse files
Merge pull request #51 from WordPress/simplify/48-tools
2 parents 3c12da3 + b25dd7d commit 3a6d402

File tree

4 files changed

+123
-712
lines changed

4 files changed

+123
-712
lines changed

src/Providers/Models/DTO/ModelConfig.php

Lines changed: 75 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
use WordPress\AiClient\Files\Enums\FileTypeEnum;
1010
use WordPress\AiClient\Files\Enums\MediaOrientationEnum;
1111
use WordPress\AiClient\Messages\Enums\ModalityEnum;
12-
use WordPress\AiClient\Tools\DTO\Tool;
12+
use WordPress\AiClient\Tools\DTO\FunctionDeclaration;
13+
use WordPress\AiClient\Tools\DTO\WebSearch;
1314

1415
/**
1516
* Represents configuration for an AI model.
@@ -20,7 +21,8 @@
2021
*
2122
* @since n.e.x.t
2223
*
23-
* @phpstan-import-type ToolArrayShape from Tool
24+
* @phpstan-import-type FunctionDeclarationArrayShape from FunctionDeclaration
25+
* @phpstan-import-type WebSearchArrayShape from WebSearch
2426
*
2527
* @phpstan-type ModelConfigArrayShape array{
2628
* outputModalities?: list<string>,
@@ -35,7 +37,8 @@
3537
* frequencyPenalty?: float,
3638
* logprobs?: bool,
3739
* topLogprobs?: int,
38-
* tools?: list<ToolArrayShape>,
40+
* functionDeclarations?: list<FunctionDeclarationArrayShape>,
41+
* webSearch?: WebSearchArrayShape,
3942
* outputFileType?: string,
4043
* outputMimeType?: string,
4144
* outputSchema?: array<string, mixed>,
@@ -60,7 +63,8 @@ class ModelConfig extends AbstractDataTransferObject
6063
public const KEY_FREQUENCY_PENALTY = 'frequencyPenalty';
6164
public const KEY_LOGPROBS = 'logprobs';
6265
public const KEY_TOP_LOGPROBS = 'topLogprobs';
63-
public const KEY_TOOLS = 'tools';
66+
public const KEY_FUNCTION_DECLARATIONS = 'functionDeclarations';
67+
public const KEY_WEB_SEARCH = 'webSearch';
6468
public const KEY_OUTPUT_FILE_TYPE = 'outputFileType';
6569
public const KEY_OUTPUT_MIME_TYPE = 'outputMimeType';
6670
public const KEY_OUTPUT_SCHEMA = 'outputSchema';
@@ -129,9 +133,14 @@ class ModelConfig extends AbstractDataTransferObject
129133
protected ?int $topLogprobs = null;
130134

131135
/**
132-
* @var list<Tool>|null Tools available to the model.
136+
* @var list<FunctionDeclaration>|null Function declarations available to the model.
133137
*/
134-
protected ?array $tools = null;
138+
protected ?array $functionDeclarations = null;
139+
140+
/**
141+
* @var WebSearch|null Web search configuration for the model.
142+
*/
143+
protected ?WebSearch $webSearch = null;
135144

136145
/**
137146
* @var FileTypeEnum|null Output file type.
@@ -464,33 +473,57 @@ public function getTopLogprobs(): ?int
464473
}
465474

466475
/**
467-
* Sets the tools.
476+
* Sets the function declarations.
468477
*
469478
* @since n.e.x.t
470479
*
471-
* @param list<Tool> $tools The tools.
480+
* @param list<FunctionDeclaration> $function_declarations The function declarations.
472481
*
473482
* @throws InvalidArgumentException If the array is not a list.
474483
*/
475-
public function setTools(array $tools): void
484+
public function setFunctionDeclarations(array $function_declarations): void
476485
{
477-
if (!array_is_list($tools)) {
478-
throw new InvalidArgumentException('Tools must be a list array.');
486+
if (!array_is_list($function_declarations)) {
487+
throw new InvalidArgumentException('Function declarations must be a list array.');
479488
}
480489

481-
$this->tools = $tools;
490+
$this->functionDeclarations = $function_declarations;
491+
}
492+
493+
/**
494+
* Gets the function declarations.
495+
*
496+
* @since n.e.x.t
497+
*
498+
* @return list<FunctionDeclaration>|null The function declarations.
499+
*/
500+
public function getFunctionDeclarations(): ?array
501+
{
502+
return $this->functionDeclarations;
482503
}
483504

484505
/**
485-
* Gets the tools.
506+
* Sets the web search configuration.
486507
*
487508
* @since n.e.x.t
488509
*
489-
* @return list<Tool>|null The tools.
510+
* @param WebSearch $web_search The web search configuration.
490511
*/
491-
public function getTools(): ?array
512+
public function setWebSearch(WebSearch $web_search): void
492513
{
493-
return $this->tools;
514+
$this->webSearch = $web_search;
515+
}
516+
517+
/**
518+
* Gets the web search configuration.
519+
*
520+
* @since n.e.x.t
521+
*
522+
* @return WebSearch|null The web search configuration.
523+
*/
524+
public function getWebSearch(): ?WebSearch
525+
{
526+
return $this->webSearch;
494527
}
495528

496529
/**
@@ -738,11 +771,12 @@ public static function getJsonSchema(): array
738771
'minimum' => 1,
739772
'description' => 'Number of top log probabilities to return.',
740773
],
741-
self::KEY_TOOLS => [
774+
self::KEY_FUNCTION_DECLARATIONS => [
742775
'type' => 'array',
743-
'items' => Tool::getJsonSchema(),
744-
'description' => 'Tools available to the model.',
776+
'items' => FunctionDeclaration::getJsonSchema(),
777+
'description' => 'Function declarations available to the model.',
745778
],
779+
self::KEY_WEB_SEARCH => WebSearch::getJsonSchema(),
746780
self::KEY_OUTPUT_FILE_TYPE => [
747781
'type' => 'string',
748782
'enum' => FileTypeEnum::getValues(),
@@ -841,10 +875,17 @@ static function (ModalityEnum $modality): string {
841875
$data[self::KEY_TOP_LOGPROBS] = $this->topLogprobs;
842876
}
843877

844-
if ($this->tools !== null) {
845-
$data[self::KEY_TOOLS] = array_map(static function (Tool $tool): array {
846-
return $tool->toArray();
847-
}, $this->tools);
878+
if ($this->functionDeclarations !== null) {
879+
$data[self::KEY_FUNCTION_DECLARATIONS] = array_map(
880+
static function (FunctionDeclaration $function_declaration): array {
881+
return $function_declaration->toArray();
882+
},
883+
$this->functionDeclarations
884+
);
885+
}
886+
887+
if ($this->webSearch !== null) {
888+
$data[self::KEY_WEB_SEARCH] = $this->webSearch->toArray();
848889
}
849890

850891
if ($this->outputFileType !== null) {
@@ -932,10 +973,17 @@ public static function fromArray(array $array): self
932973
$config->setTopLogprobs($array[self::KEY_TOP_LOGPROBS]);
933974
}
934975

935-
if (isset($array[self::KEY_TOOLS])) {
936-
$config->setTools(array_map(static function (array $toolData): Tool {
937-
return Tool::fromArray($toolData);
938-
}, $array[self::KEY_TOOLS]));
976+
if (isset($array[self::KEY_FUNCTION_DECLARATIONS])) {
977+
$config->setFunctionDeclarations(array_map(
978+
static function (array $function_declaration_data): FunctionDeclaration {
979+
return FunctionDeclaration::fromArray($function_declaration_data);
980+
},
981+
$array[self::KEY_FUNCTION_DECLARATIONS]
982+
));
983+
}
984+
985+
if (isset($array[self::KEY_WEB_SEARCH])) {
986+
$config->setWebSearch(WebSearch::fromArray($array[self::KEY_WEB_SEARCH]));
939987
}
940988

941989
if (isset($array[self::KEY_OUTPUT_FILE_TYPE])) {

src/Tools/DTO/Tool.php

Lines changed: 0 additions & 193 deletions
This file was deleted.

0 commit comments

Comments
 (0)