Skip to content

Instantly share code, notes, and snippets.

@eslof
Last active July 16, 2023 16:13
Show Gist options
  • Save eslof/cde1677b6e72dc19a517347c3c4c115e to your computer and use it in GitHub Desktop.
Save eslof/cde1677b6e72dc19a517347c3c4c115e to your computer and use it in GitHub Desktop.
build gpt 0613 typescript function definition from openai spec php array
function generateTypeScript($dummy): string {
$output = "// " . $dummy['description'] . "\n";
$output .= "type " . $dummy['name'] . " = (_: {\n";
$output .= generateProperties($dummy['parameters']['properties'], $dummy['parameters']['required'], false);
$output .= "}) => any;\n";
return $output;
}
function generateProperties($properties, $required, $indent): string {
$output = '';
$indentation = $indent ? ' ' : '';
foreach ($properties as $name => $property) {
if (isset($property['description'])) {
$output .= $indentation . "// " . $property['description'] . "\n";
}
$output .= $indentation . $name . (in_array($name, $required) ? ": " : "?: ");
$output .= generateType($property, $indent) . ',';
if (isset($property['default'])) {
if (is_array($property['default'])) {
$default = "['" . implode("', '", $property['default']) . "']";
} else if (is_bool($property['default']) || is_null($property['default'])) {
$default = json_encode($property['default']);
} else {
$default = $property['default'];
}
$output .= " // default: " . $default;
}
$output .= "\n";
}
return $output;
}
function generateType($property, $indent) {
switch ($property['type']) {
case 'object':
$output = "{\n";
$output .= generateProperties($property['properties'], $property['required'] ?? [], $indent + 1);
$output .= '}';
return $output;
case 'array':
$isItemsEnum = isset($property['items']['enum']);
switch ($property['items']['type']) {
case 'object':
$output = "{\n";
$output .= generateProperties($property['items']['properties'], $property['items']['required'] ?? [], $indent + 1);
$output .= "}[]";
break;
case 'array':
$output = generateType($property['items'], $indent + 1) . "[]";
break;
case 'string':
if ($isItemsEnum) {
$output = "\"" . implode("\" | \"", $property['items']['enum']) . "\"[]";
break;
}
$output = 'string[]';
break;
case 'number':
if ($isItemsEnum) {
$output = implode(" | ", $property['items']['enum']) . "[]";
break;
}
$output = 'number[]';
break;
default:
$output = $property['items']['type'] . '[]';
}
return $output;
case 'string':
if (isset($property['enum'])) {
return "\"" . implode("\" | \"", $property['enum'] ?? []) . "\"";
}
return 'string';
case 'number':
if (isset($property['enum'])) {
return implode(" | ", $property['enum']) . "[]";
}
return 'number';
default:
return $property['type'];
}
}
@eslof
Copy link
Author

eslof commented Jul 16, 2023

// Function description
type function_name = (_: {
property_zero?: {
  zero_one: 1 | 2 | 3[],
  zero_two: {
  zero_two_one?: "one" | "two" | "three",
  zero_two_two?: 1 | 2 | 3[],
}[][],
}[],
property_one: string, // default: default value
property_one_b?: integer[][],
// Property two description
property_two?: "one" | "two" | "three"[], // default: ['one', 'two']
// Property three object description
property_three?: {
  // Property three's first property description
  property_three_inner_one?: string,
  // Property three's second object property description
  property_three_inner_two?: {
  // Property three's second object property's first property description
  property_three_inner_two_inner_one: string,
  // Property three's second object property's second property description
  property_three_inner_two_inner_two?: "hi" | "ho" | "hum"[], // default: ['ho']
},
},
}) => any;
function_name(
{
"property_zero": [
  {
    "zero_one": [1, 2, 3],
    "zero_two": [
      [
        {
          "zero_two_one": "one",
          "zero_two_two": [1, 2, 3]
        }
      ]
    ]
  }
],
"property_one": "default value",
"property_one_b": [[1, 2, 3]],
"property_two": ["one", "two"],
"property_three": {
  "property_three_inner_one": "string",
  "property_three_inner_two": {
    "property_three_inner_two_inner_one": "string",
    "property_three_inner_two_inner_two": ["ho"]
  }
}
})

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