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
질문됨 일 년 전752회 조회
1개 답변
0
수락된 답변

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
전문가
답변함 일 년 전
  • 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

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠