Skip to content

Instantly share code, notes, and snippets.

@greenido
Created March 20, 2023 18:10
Show Gist options
  • Save greenido/8e840227c42ee0f896e52ee767da0764 to your computer and use it in GitHub Desktop.
Save greenido/8e840227c42ee0f896e52ee767da0764 to your computer and use it in GitHub Desktop.
OpenAI simple text call
const axios = require('axios');
// You should use dotenv - but for the easy of use I put it here.
const API_KEY = 'your_api_key_here';
const API_URL = 'https://api.openai.com/v1/';
const prompt = 'how can I bake a good bread?';
async function generateText(prompt) {
  const response = await axios.post(`${API_URL}engines/davinci-codex/completions`, {
    prompt,
    max_tokens: 50,
    n: 1,
    stop: '\n',
  }, {
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${API_KEY}`,
    },
  });
  const text = response.data.choices[0].text.trim();
// let's just log it for now
  console.log(text);
}
generateText(prompt);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment