Skip to content

Instantly share code, notes, and snippets.

@BenQoder
Created March 13, 2023 23:15
Show Gist options
  • Select an option

  • Save BenQoder/098086dc9e17e2a259a2568b262b52b4 to your computer and use it in GitHub Desktop.

Select an option

Save BenQoder/098086dc9e17e2a259a2568b262b52b4 to your computer and use it in GitHub Desktop.
Generate Typescript Type Using GPT-3 API
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
class GenerateTypes extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'generate:type {uuid}';
/**
* The console command description.
*
* @var string
*/
protected $description = "Generate types from a telescope request entry using OpenAI's GPT-3 API";
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$uuid = $this->argument("uuid") ?? $this->ask("What is the request endpoint telescope UUID?");
$telescope = DB::table('telescope_entries')
->where('uuid', $uuid)
->first();
if (!$telescope || $telescope->type !== 'request') {
$this->error("No telescope entry found for UUID: {$uuid}");
return Command::FAILURE;
}
$response = json_decode($telescope->content, true)['response'];
$this->info("Generating types for {$uuid}");
$http = Http::withHeaders([
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Authorization' => "Bearer " . env("OPENAI_API_KEY"),
])->post(
"https://api.openai.com/v1/chat/completions",
[
"model" => "gpt-3.5-turbo",
"messages" => [[
"role" => "user",
"content" => "convert json " . json_encode($response) . " to typescript interface, do not include explanation, do not include examples, just the interface",
]]
]
);
$this->info($http->json("choices")[0]["message"]["content"]);
return Command::SUCCESS;
}
}
@BenQoder

BenQoder commented Mar 13, 2023 •

Copy link
Copy Markdown
Author
  1. Make sure you have installed Laravel telescope correctly
  2. Set your OpenAI API Key on .env using OPENAI_API_KEY as key
  3. Collect the request uuid from the link on the address bar when viewing the request
  4. Run php artisan generate:type {uuid collected from address bar}

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