*/ private array $tools; /** * @param array|null $tools */ public function __construct(?array $tools = null) { $tools = $tools ?? [ new GetTimeTool(), ]; $this->tools = []; foreach ($tools as $tool) { $this->tools[$tool->name()] = $tool; } } public function get(string $name): ?Tool { return $this->tools[$name] ?? null; } /** * @return array */ public function all(): array { return array_values($this->tools); } /** * 返回 OpenAI-compatible tools 描述。 * * @return array> */ public function openAiToolsSpec(): array { return array_map(function (Tool $tool) { return [ 'type' => 'function', 'function' => [ 'name' => $tool->name(), 'description' => $tool->description(), 'parameters' => $tool->parameters(), ], ]; }, $this->all()); } }