Created
March 21, 2023 21:43
-
-
Save ericksoa/08614fbc71f569e359c2b98495c1c34f to your computer and use it in GitHub Desktop.
Generate code according to a spec given by the product manager
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
| // openAIClient.ts | |
| import { OpenAIApi } from '@openai/api'; | |
| export class OpenAIClient { | |
| private openai: OpenAIApi; | |
| constructor(apiKey: string) { | |
| this.openai = new OpenAIApi({ | |
| apiKey: apiKey, | |
| }); | |
| } | |
| async generateCode(prompt: string): Promise<string> { | |
| const response = await this.openai.Completions.create({ | |
| engine: 'text-davinci-002', // You can choose another engine if you prefer | |
| prompt: `Generate TypeScript code for the following task:\n${prompt}\n---\nCode:`, | |
| max_tokens: 100, | |
| n: 1, | |
| stop: null, | |
| temperature: 0.7, | |
| }); | |
| const generatedCode = response.choices[0]?.text.trim(); | |
| if (!generatedCode) { | |
| throw new Error('No code generated by OpenAI API.'); | |
| } | |
| return generatedCode; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment