Skip to content

Add support for namespaces in function calling - #28

Open
thekid wants to merge 25 commits into
mainfrom
feature/namespaces
Open

thekid wants to merge 25 commits into
mainfrom
feature/namespaces

Conversation

@thekid

@thekid thekid commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

Example 1

Registering a weather function

use com\openai\tools\{Namespaced, Param};
use webservices\rest\Endpoint;

class Weather {
  private $endpoint;

  public function __construct(string $base= 'https://wttr.in/') {
    $this->endpoint= new Endpoint($base);
  }

  /** Fetches the weather for a given city */
  public function in(#[Param] string $city): string {
    return $this->endpoint->resource('/{0}?0mT', [$city])->get()->content(); 
  }
}

// wttr::in()
$functions= new Namespaced(['wttr' => new Weather()]);

Example 2

Defining shell commands with tool search:

use com\openai\tools\{Definition, Definitions};
use lang\reflect\Invocable;

class ShellCommands extends Definitions {

  public function __construct(private array $commands) { }

  public function schema(): iterable {
    $input= [
      'type'       => 'object',
      'properties' => ['args' => ['type' => 'string', 'description' => 'Command line arguments']],
      'required'   => ['args'],
    ];
    foreach ($this->commands as $command => $description) {
      yield new Definition($command, $description, $input);
    }
  }

  public function invocable(string $name, ?string $namespace= null) {
    if (null === $namespace && ($command= $this->commands[$name] ?? null)) {
      return new Invocable(fn($args) => shell_exec($name.' '.$args));
    }
    return null;
  }
}

$coreutils= new ShellCommands([
  'ls'   => 'Builtin ls: list directory contents',
  'find' => 'Coreutils find: search for files in a directory hierarchy',
  'grep' => 'Coreutils grep: print lines matching a pattern',
]);
$functions= new Namespaced()->add('coreutils', $coreutils, defer: true);
$tools[]= ['type' => 'tool_search'];

See also

@thekid thekid added the enhancement New feature or request label Nov 8, 2026
@thekid

thekid commented Nov 15, 2026

Copy link
Copy Markdown
Member Author

We're missing a way to set defer_loading: true on tools and namespaces!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant