sydrawat01 / gpt-3

A simple conversational bot in ReactJS that interacts with the custom OpenAI API using the GPT-3 text-davinci-003 model
0 stars 0 forks source link

⛵ Change API endpoint from custom API to Azure OpenAI API #5

Open sydrawat01 opened 1 year ago

sydrawat01 commented 1 year ago

Use the openai NodeJS package to use the Azure OpenAI API to provide responses to our bot instead of our custom built OpenAI API.

sydrawat01 commented 1 year ago

Demo code:

const { Configuration, OpenAIApi } = require('openai')

const basePath = 'https://azureopenaiproject.openai.azure.com/openai/deployments/'
const model = 'azure-openai-model-name'

const azureOpenAiChatGPT = async (model) => {
  const configuration = new Configuration({
    basePath: basePath + model,
    apiKey: 'abcdxyz',
  })
  const openai = new OpenAIApi(configuration)
  const completion = await openai.createCompletion(
    {
      model,
      prompt: 'what is jardiance?',
      max_tokens: 100,
      temperature: 1,
      frequency_penalty: 0,
      presence_penalty: 0,
      top_p: 0.5,
      best_of: 1,
      stop: null,
    },
    {
      headers: {
        'api-key': 'abcdxyz',
      },
      params: { 'api-version': '2022-12-01' },
    }
  )
  return completion
}

azureOpenAiChatGPT(model)
  .then((result) => {
    console.log(result.data.choices[0].text)
    return 'ok'
  })
  .catch((err) => {
    console.log(err)
    return 'not ok'
  })
sydrawat01 commented 1 year ago

Have a message stream that the API can accept, which can then be rendered on the UI.