GithubHelp home page GithubHelp logo

alenvelocity / gpt-functions Goto Github PK

View Code? Open in Web Editor NEW
14.0 1.0 2.0 84 KB

Convert prompts into working Javascript Functions. Never CTRL+V again

Home Page: https://www.npmjs.com/package/gpt-functions

License: MIT License

TypeScript 100.00%
ai gpt gpt4 no-code openai

gpt-functions's Introduction

GPTFunctions

Convert raw text into actual Javascript Functions

NPM CodeFactor NPM

Installation

npm install gpt-functions

Usage

Initialiaze the GPT Functions class

import { GPTFunctions } from 'gpt-functions'

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 (Recommended way)

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. GPTFUNCTIONS USES THE JS FUNCTION CONSTRUCTOR, WHICH CAN EXECUTE ARBITRARY CODE. AN ATTACKER COULD EXPLOIT THIS TO RUN MALICIOUS CODE ON YOUR SYSTEM. ALWAYS VALIDATE USER INPUT AND SANITIZE IT BEFORE PASSING IT

.createFunction() is a method that takes a string as the functio description or an object with the following properties as its parameter:

func: a string that represents the type of the fucntion
desc: a string that describes what the code does
model: the name of the OpenAI model you want to use to execute the code
evaulate: a function evaluates the string to a an actual function `Default: Function Constructor`

The createFunction() method returns a function that can be called with arguments to execute the code provided in the func property.

Note that the createFunction() function does not execute the code immediately, but instead returns a function that can be used to execute the code later

GPTFunctions.prototype.getResult()

const result = await gpt.getResult({
    func: '(array, array) => array',
    args: [['a', 'b', 'c'], ['x', 'y', 'z']],
    desc: 'Creates an array of arrays, grouping the elements of each input array based 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 following properties as its parameter:

  • func: a string that represents the code you want to execute
  • args: an array of arrays containing the arguments to pass to the func.
  • desc: a string that describes what the code does.
  • model: the name of the OpenAI model you want to use to execute the code.
  • postProcess: a function to parse the API response

The getResult() method returns a Promise that resolves to the result of executing the code.

Contribution and Acknowledgments

If you find any issues or have any suggestions for improvement, please feel free to open an issue or a pull request.

gpt-functions's People

Contributors

alenvelocity avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

aliaryantech

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.