npm install js-gpt-functions-plus
import { GPTFunctions } from 'js-gpt-functions-plus'
const API_KEY = 'your-openai-api-key-here'
const gpt = new GPTFunctions(API_KEY)
const celsiusToFahrenheit = await gpt.createFunction('convert the given temperature from Celsius to Fahrenheit')
console.log(celsiusToFahrenheit(25))
console.log(celsiusToFahrenheit(10))
Output
77
50
const permutations = await gpt.createFunction({
func: '(array) => array',
desc: 'Return all permutations of the passed array'
})
console.log(permutations([1,2,3]))
Output
[
[ 1, 2, 3 ],
[ 1, 3, 2 ],
[ 2, 1, 3 ],
[ 2, 3, 1 ],
[ 3, 1, 2 ],
[ 3, 2, 1 ]
]
NEVER PASS RAW USER INPUT WITHOUT VALIDATING IT FIRST. JSGPTFUNCTIONSPLUS UTILIZES THE JS FUNCTION CONSTRUCTOR, WHICH HAS THE POTENTIAL TO CARRY OUT ARBITRARY CODE. A MALICIOUS USER COULD EMPLOY THIS TO EXECUTE DAMAGING CODE ON YOUR SYSTEM. IT IS VITAL TO ASSESS USER INPUT AND SANITIZE IT BEFORE USAGE.
.createFunction() is a method that takes a string as the function description or an object with the following attributes as its parameter:
func: a string that represents the type of the function
desc: a string that delineates the code's function
model: the name of the OpenAI model utilised to administer the code
evaluate: a function that translates the string into a functional code `Default: Function Constructor`
The .createFunction() method returns a function that can be stirred with arguments to enact the code contained in the func
property.
It is important to note that the .createFunction() function does not carry out the code promptly, but instead offers a function that could be engaged to execute the code at a later time.
const result = await gpt.getResult({
func: '(array, array) => array',
args: [['a', 'b', 'c'], ['x', 'y', 'z']],
desc: 'Forms an array of arrays, clustering the elements of each input array centered on their index.'
})
console.log(result)
Output
[ [ 'a', 'x' ], [ 'b', 'y' ], [ 'c', 'z' ] ]
GPTFunctions.prototype.getResult() is a function that takes an object with the succeeding properties as its parameter:
func
: a string that represents the code ready for executionargs
: an array of arrays, each containing the arguments to relay to the func.desc
: a string that outlines the code's purpose.model
: the label of the OpenAI model selected to run the code.postProcess
: a function to parse the API reply
The getResult()
method returns a Promise that settles to the result of executing the code.
For any queries or suggestions for advancement, please feel free to make a new issue or offer a pull request.