Any help appreciated - I’m having trouble getting to grips with why the code below fails. I have a rules engine in which the steps must be executed in sequence because each rule may assess the results of previous rules, so these have to be available.
You are not returning any value from the “mycaller” function. Also, since you’re already in an “async” function you can just await the call and get the response, without using “then”.
Thanks so much for your reply. I think I didn’t explain my situation properly though.
I understand that I can call an async version of mycaller() and how this works. My issue is that my mycaller() function in this stripped-down example represents thousands of lines of synchronous code in my “real” application, so what I’m trying to do is add one asynchronous call to chatGPT to these.
To explain a little further, I have a rules engine where each rule may require values that result from earlier rule evaluations. The logical way to code this is to evaluate them in sequence, synchronously. Theoretically it would be possible to code asynchronously with each callback re-building the tree of values but this would be really weird and a nightmare to debug.
I’ve put a “better” example below but it’s quite possible that what I need is simply not possible to do in node/javascript
I am not sure if I totally I understand. Based on your example I see a simple mistake. Your getMiddle function does not have a return statement. You are combining await with .then. I would use one OR the other. For example I would change getMiddle to:
You also don’t need to recreate an instance of the OpenAIApi client inside the rules (such as in getMiddle) you can use one instance in the whole script and reuse.
I’m fairly confident that what you’re trying to do is possible with JavaScript, you just have to structure your code correctly. Always make sure to return a value from each rule function.