Search code examples
node.jsvue.jsopenai-api

Did I somehow break the api?


I have just a simple prompt asking the AI how it is doing

async generateArticles() {
      console.log('article')
      const response = await fetch('https://api.openai.com/v1/engines/davinci-codex/completions', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'Authorization': `Bearer ${this.apiKey}`
        },
        body: JSON.stringify({
          prompt: `Hey, how are you?`,
          max_tokens: 2048,
          n: 1,
          temperature: 0.7
        })
      });
      console.log(response)
      const data = await response.json();
      console.log(data)
      const generatedText = data.choices[0].text.trim();
      console.log(generatedText)
    }

But the respones I am getting seem to be parts of the code Open AI is using like:

'positive': [
    'That\'s great!',
    'Nice!',
    'Good!',
    'Alright!',
    'Cool!',
    'Yeah!',
],
'negative': [
    'That\'s too bad',
    'Too bad',
    'I\'m sorry',
],
'current_user': [
    'My name is {}',
    'My name is {}. Nice to meet you',
    'The name\'s {}',
    'The name\'s {}. Pleased to meet you',
],

or stuff like this

# def greet():
#     print("Hey, how are you?")
#     print("I hope you are fine!")
#     print("Bye!")
#
# def say_bye():
#     print("Bye!")
#
# def greet_bye():
#     greet()
#     say_bye()
#
# print("I am not in the function")
# greet_bye()

It is weird, once even something in Russian. So is it broken, or am I missing something that I am getting such weird responses.


Solution

  • You haven't broken the API endpoint, but you are using the Codex endpoint which is designed for Code completions. Only use this endpoint to generate code.

    For general purpose Natural Language Processing you need to use the OpenAI completions endpoint:

    https://api.openai.com/v1/completions

    The other change you will need to make is in the body where you need to add the model property:

    model: 'text-davinci-003'

    I rewrote your code:

    const response = await fetch('https://api.openai.com/v1/completions', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${this.apiKey}`,
      },
      body: JSON.stringify({
        prompt: `Hey, how are you?`,
        model: 'text-davinci-003',
        max_tokens: 2048,
        n: 1,
        temperature: 0.7
      })
    });
    console.log(response)
    const data = await response.json();
    console.log(data)
    const generatedText = data.choices[0].text.trim();
    console.log(generatedText)
    

    I recieved the following response from GPT3:

    I'm doing well, thank you. How about you?