Certifications
Gen AI Advanced
Create interface
Setup OpenAI API

Setup OpenAI API

In this step you will set up the function calls needed to get and send data to OpenAI.

Tasks:

  • Create the handler functions that will make your requests to the OpenAI API.
  • Include your OpenAI API key in your function, a plus if you think about how to handle the key in a safe manner.
  • Follow best practises and add tests for above functionality.

See example of how this can look:

// Example using the 'openai' package
const { OpenAIAPI } = require('openai')
const openai = new OpenAIAPI('YOUR_API_KEY')
 
async function getOpenAIResponse(input) {
  const response = await openai.complete({
    engine: 'text-davinci-003', // Choose the appropriate engine
    prompt: input,
    max_tokens: 100,
  })
 
  return response.choices[0]?.text.trim()
}