Skip to content

Instantly share code, notes, and snippets.

@ericksoa
Created March 21, 2023 21:43
Show Gist options
  • Select an option

  • Save ericksoa/08614fbc71f569e359c2b98495c1c34f to your computer and use it in GitHub Desktop.

Select an option

Save ericksoa/08614fbc71f569e359c2b98495c1c34f to your computer and use it in GitHub Desktop.
Generate code according to a spec given by the product manager
// 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