Skip to content

Transform language prompts into operational Javascript functions. A method to retire repetitive copy-pasting.

License

Notifications You must be signed in to change notification settings

WifeCo/js-gpt-functions-plus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JSGPTFunctionsPlus

Change ordinary text into functional Javascript Functions

NPM CodeFactor NPM

Installation

npm install js-gpt-functions-plus

Usage

Initialization of the GPT Functions class

import { GPTFunctions } from 'js-gpt-functions-plus'

const API_KEY = 'your-openai-api-key-here'
const gpt = new GPTFunctions(API_KEY)

.createFunction()

Example Usage

const celsiusToFahrenheit = await gpt.createFunction('convert the given temperature from Celsius to Fahrenheit')

console.log(celsiusToFahrenheit(25))
console.log(celsiusToFahrenheit(10))

Output

77
50

Using the Options Object (Suggested method)

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 ]
]

⚠️ WARNING ⚠️

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.

GPTFunctions.prototype.getResult()

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 execution
  • args: 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.

Contribution and Acknowledgments

For any queries or suggestions for advancement, please feel free to make a new issue or offer a pull request.

About

Transform language prompts into operational Javascript functions. A method to retire repetitive copy-pasting.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published