assistant.php
2 years ago
base.php
2 years ago
embed.php
2 years ago
feedback.php
2 years ago
function.php
2 years ago
image.php
2 years ago
parameter.php
2 years ago
text.php
2 years ago
transcribe.php
2 years ago
function.php
104 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_Query_Function { |
| 4 | public string $name; |
| 5 | public string $description; |
| 6 | public array $parameters; |
| 7 | public string $type; |
| 8 | public string $id; |
| 9 | |
| 10 | public function __construct( string $name, string $description, |
| 11 | array $parameters = [], string $type = 'PHP', string $id = null ) { |
| 12 | // $name: The name of the function to be called. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64. |
| 13 | if ( !preg_match( '/^[a-zA-Z0-9_-]{1,64}$/', $name ) ) { |
| 14 | throw new InvalidArgumentException( "AI Engine: Invalid function name ($name) for Meow_MWAI_Query_Function." ); |
| 15 | } |
| 16 | |
| 17 | foreach ( $parameters as $parameter ) { |
| 18 | if ( !( $parameter instanceof Meow_MWAI_Query_Parameter ) ) { |
| 19 | throw new InvalidArgumentException( "AI Engine: Invalid parameter for Meow_MWAI_Query_Function." ); |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | $this->name = $name; |
| 24 | $this->description = $description; |
| 25 | $this->parameters = $parameters; |
| 26 | $this->type = $type; |
| 27 | $this->id = $id; |
| 28 | } |
| 29 | |
| 30 | public function serializeForOpenAI() { |
| 31 | // Initialize the base structure with name and description |
| 32 | $json = [ 'name' => $this->name, 'description' => $this->description ]; |
| 33 | |
| 34 | // Check if parameters are set and not empty |
| 35 | if ( !empty( $this->parameters ) ) { |
| 36 | $properties = []; |
| 37 | $required = []; |
| 38 | |
| 39 | // Loop through each parameter to construct the properties object |
| 40 | foreach ( $this->parameters as $parameter ) { |
| 41 | $properties[$parameter->name] = [ |
| 42 | 'type' => $parameter->type, // Assuming each parameter has a 'type' attribute |
| 43 | 'description' => $parameter->description, // Assuming each parameter has a 'description' attribute |
| 44 | ]; |
| 45 | |
| 46 | // If an enum is set for the parameter, include it |
| 47 | if ( isset($parameter->enum) ) { |
| 48 | $properties[$parameter->name]['enum'] = $parameter->enum; |
| 49 | } |
| 50 | |
| 51 | // If the parameter is required, add its name to the required array |
| 52 | if ( $parameter->required ) { |
| 53 | $required[] = $parameter->name; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // Assemble the parameters part of the JSON |
| 58 | $json['parameters'] = [ |
| 59 | 'type' => 'object', |
| 60 | 'properties' => $properties, |
| 61 | 'required' => $required, |
| 62 | ]; |
| 63 | } |
| 64 | |
| 65 | return $json; |
| 66 | } |
| 67 | |
| 68 | public function serializeForAnthropic() |
| 69 | { |
| 70 | $json = [ |
| 71 | 'name' => $this->name, |
| 72 | 'description' => $this->description, |
| 73 | 'input_schema' => [ |
| 74 | 'type' => 'object', |
| 75 | 'properties' => new stdClass() |
| 76 | ], |
| 77 | ]; |
| 78 | |
| 79 | if ( !empty( $this->parameters ) ) { |
| 80 | $properties = []; |
| 81 | $required = []; |
| 82 | foreach ( $this->parameters as $parameter ) |
| 83 | { |
| 84 | $properties[$parameter->name] = [ |
| 85 | 'type' => $parameter->type, |
| 86 | 'description' => $parameter->description, |
| 87 | ]; |
| 88 | if ( isset( $parameter->enum ) ) { |
| 89 | $properties[$parameter->name]['enum'] = $parameter->enum; |
| 90 | } |
| 91 | if ( $parameter->required ) { |
| 92 | $required[] = $parameter->name; |
| 93 | } |
| 94 | } |
| 95 | $json['input_schema']['properties'] = empty( $properties ) ? new stdClass() : $properties; |
| 96 | if ( !empty( $required ) ) { |
| 97 | $json['input_schema']['required'] = $required; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | return $json; |
| 102 | } |
| 103 | } |
| 104 |