-
-
Save roadlabs/4cb582b209676899cc479e7355c5c45c to your computer and use it in GitHub Desktop.
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
| /* | |
| # Query the OpenAI API | |
| - Prompts the user for an OPENAI_API_KEY | |
| - Prompts the user for a prompt to send to OpenAI | |
| - Sends the prompt to OpenAI | |
| - Displays the response from OpenAI in the built-in editor | |
| */ | |
| // Name: OpenAI Playground | |
| // Description: Query Open AI's API | |
| import "@johnlindquist/kit" | |
| let { Configuration, OpenAIApi } = await npm("openai") | |
| let configuration = new Configuration({ | |
| apiKey: await env("OPENAI_API_KEY"), | |
| }) | |
| let openai = new OpenAIApi(configuration) | |
| let prompt = await arg("Prompt:") | |
| editor(prompt) | |
| setTimeout(() => { | |
| setLoading(true) | |
| }, 250) | |
| let response = await openai.createCompletion({ | |
| model: "text-davinci-002", | |
| prompt: `${prompt} | |
| `, | |
| temperature: 0.7, | |
| max_tokens: 512, | |
| top_p: 1, | |
| frequency_penalty: 0, | |
| presence_penalty: 0, | |
| }) | |
| setLoading(false) | |
| let text = response?.data?.choices[0]?.text?.trim() | |
| if (text) { | |
| await editor(text) | |
| } else { | |
| dev(response.data) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment