Created
March 13, 2023 23:15
-
-
Save BenQoder/098086dc9e17e2a259a2568b262b52b4 to your computer and use it in GitHub Desktop.
Generate Typescript Type Using GPT-3 API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.