0

I have a simple openai lambda with a layer that gives me what I want when I test it, but when I try to actually use it, I get no body if it depends on an awaited promise. I can pass an explicit value and it returns, but the minute I use the await for my layer it doesn't. Is there something I should be doing in the lambda to make it return the body?

Here is my code:

The actual lambda:

exports.handler = async (event, context) => {
    const { completionModel } = require('/opt/openai-module')
    try {
        const result = await completionModel(event.inputText)
        const response = {
            headers: {
                "Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept, Authorization",
                "Access-Control-Allow-Origin": "*",
                "Access-Control-Allow-Methods": "OPTIONS,POST,GET",
            },
            statusCode: result.statusCode,
            body: result.data
        }
        return response
    } catch (e) {
        return e
    }
};

The layer that's uploaded:

const { Configuration, OpenAIApi } = require('./nodejs/node_modules/openai/dist')
const axios = require('./nodejs/node_modules/axios').default

exports.completionModel = async function(inputText) {
  if (typeof inputText != 'string') return new Error('Input is not a string')
  const openAiConfig = new Configuration({
    apiKey: redacted,
  })

  const openai = new OpenAIApi(openAiConfig)
  const prompt = `prompt - ${inputText}`
  try {
    const response = await openai.createCompletion({
      model: "text-davinci-002",
      prompt: prompt,
      max_tokens: 256,
      temperature: 0.72,
      frequency_penalty: 0,
      presence_penalty: 0,
      suffix: '\n\n'
    })
    return response
  } catch(e) {
    return new Error('There was an error with your request: ', e)
  }
}

The test works perfectly via testing: enter image description here

Joel Hager
  • 2,990
  • 3
  • 15
  • 44

0 Answers0