How can I use async/await in a nodejs lambda?

0

I have a very simple lambda that uses the OpenAI API to send a prompt to ChatGPT. The lambda handler does not appear to wait for the request to complete. I've set the timeout in the lambda Configuration to 30 seconds. The same code when run from my linux command line with the same version of Node executes successfully (i.e. gets and displays a response) in about 2 seconds.

Any insights very welcome - I've been beating up on this for over a week :(

Thanks,

David

Sample code:

const { Configuration, OpenAIApi } = require("openai");

exports.handler = async function(event, context, callback) {

    var output;

	try {
		output = getResponse(event.prompt);
		console.log('finished');
	}
	catch (e){
		output = e.message;
	}

	let response = {
		statusCode: 200,
		headers: { "Content-type" : "application/json" },
		body: JSON.stringify(output)
	};

	return response;
};

async function getResponse(prompt)
{
    console.log("getResponse('" + prompt + "')");
    
	const configuration = new Configuration({
		apiKey: "my-api-key-here",
	});
	const openai = new OpenAIApi(configuration);

	console.log('make request');
	
	await openai.createCompletion({
			model: "text-davinci-003",
			prompt: prompt,
			temperature: 0.7,
			max_tokens: 500,
			top_p: 1,
			frequency_penalty: 0,
			presence_penalty: 0,
		}).then((res) => {
			var text = res.data.choices[0].text;
			console.log('GPT3 says: ' + text);
		}).catch((error) => {
			console.log('in the catch clause: ' + error);
		});

	console.log('how did we get here?');
}

Input:

{ "prompt": "what did the fox say?" }

Output:

Test Event Name fox_test

Response { "statusCode": 200, "headers": { "Content-type": "application/json" }, "body": "{}" }

Function Logs

2023-04-24T13:58:50.031+01:00	START RequestId: 18ac9c86-d16b-46f2-8cbd-b4f3887429ae Version: $LATEST
2023-04-24T13:58:50.063+01:00	2023-04-24T12:58:50.063Z 18ac9c86-d16b-46f2-8cbd-b4f3887429ae INFO getResponse('what did the fox say?')
2023-04-24T13:58:50.063+01:00	2023-04-24T12:58:50.063Z 18ac9c86-d16b-46f2-8cbd-b4f3887429ae INFO make request
2023-04-24T13:58:50.064+01:00	2023-04-24T12:58:50.064Z 18ac9c86-d16b-46f2-8cbd-b4f3887429ae INFO finished
2023-04-24T13:58:50.124+01:00	END RequestId: 18ac9c86-d16b-46f2-8cbd-b4f3887429ae
2023-04-24T13:58:50.124+01:00	REPORT RequestId: 18ac9c86-d16b-46f2-8cbd-b4f3887429ae Duration: 92.45 ms Billed Duration: 93 ms Memory Size: 128 MB Max Memory Used: 77 MB
dmb0058
gefragt vor einem Jahr752 Aufrufe
1 Antwort
0
Akzeptierte Antwort

Hi,

have you tried to return from method:

return await openai.createCompletion

?

Also you could probably remove await from getResponse method

return openai.createCompletion

and then add await in handler.

output = await getResponse(event.prompt);

Did similar work in this article: https://medium.com/@alatech/build-your-personal-speaker-assistant-with-amplify-and-chatgpt-8b6433fea042

Hope it helps ;)

profile picture
EXPERTE
beantwortet vor einem Jahr
  • Brilliant! Thanks so much ... I almost see how this works and why my original code doesn't but not quite :) I'll experiment until I understand properly but it's so good to have this working.

    David

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen