- 新しい順
- 投票が多い順
- コメントが多い順
I found the answer ...
In case it helps anyone, the code below mixes synchronous and async function calls in sequence, waiting for the async one to complete before continuing to the next function.
const { Configuration, OpenAIApi } = require("openai");
exports.handler = async function(event, context, callback) {
var output;
try {
const rule1 = getStart(); // call the first (sync) function
const rule2 = await getMiddle(event.prompt).then(res => { // call the second (async) function and wait
return res.data.choices[0].text;
}).catch(error => {
return error.message;
});
output = getEnd(rule1 + ' ' + rule2); // call the third (sync) function
console.log('result: ' + output);
}
catch (e){
output = e.message;
}
let response = {
statusCode: 200,
headers: { "Content-type" : "application/json" },
body: JSON.stringify(output)
};
return response;
};
function getStart()
{
return '<sentence>Hi there,';
}
async function getMiddle(prompt)
{
console.log("getMiddle('" + prompt + "')");
const configuration = new Configuration({
apiKey: "my-api-key-here",
});
const openai = new OpenAIApi(configuration);
return openai.createCompletion({
model: "text-davinci-003",
prompt: prompt,
temperature: 0.7,
max_tokens: 500,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
});
}
function getEnd(resp)
{
return resp + '</sentence>';
}
Response
{
"statusCode": 200,
"headers": {
"Content-type": "application/json"
},
"body": "\"<sentence>Hi there, \\n\\nThe fox usually makes a sound similar to \\\"Skrr! Skrr!\\\"</sentence>\""
}
Function Logs
START RequestId: 41cc46f8-9a9b-4500-854e-574739511e2e Version: $LATEST
2023-04-27T10:55:22.820Z 41cc46f8-9a9b-4500-854e-574739511e2e INFO getMiddle('what did the fox say?')
2023-04-27T10:55:25.040Z 41cc46f8-9a9b-4500-854e-574739511e2e INFO result: <sentence>Hi there,
The fox usually makes a sound similar to "Skrr! Skrr!"</sentence>
END RequestId: 41cc46f8-9a9b-4500-854e-574739511e2e
Hi,
I think your issue is due to that in getMiddle function, if you use await, you do not need to chain or with a then statement.
You could just say
Const res = await openai.createCompletion(…)
See below as example: https://levelup.gitconnected.com/build-your-personal-speaker-assistant-with-amplify-and-chatgpt-8b6433fea042
Hope it helps ;)
Hmm, I wondered about that, but it doesn't seem to make a difference.
I read your excellent article on Amplify and ChatGPT, and I thought I understood most of it - I'm still studying and learning :) The example that you gave to my related question worked perfectly, with no synchronous calls in the chain. However when I tried to integrate async and sync functions the async ones behave synchronously (actually I think they just return a Promise and the caller continues regardless).
I don't understand (mea culpa) why the call to getMiddle() in makeSentence() doesn't seem to wait, it sends the request and then goes straight to the getClosing() call ... I suspect the problem is something to do with how async functions are off-boarded to the Node environment, but I'm just guessing here.