0.1.5 โ€ข Published 1 year ago

ai-function-js v0.1.5

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

AI Function JS

Heavily inspired by https://github.com/Torantulino/AI-Functions

No need to write functions ๐Ÿ‘จ๐Ÿปโ€๐Ÿ’ป, just describe it ๐Ÿ—ฃ and use it. Let GPT ๐Ÿค– to figure out the rest!

af.run('sum', 'Add all numbers together', [1,2,3,4,5]);
// Output: 15

af.run('checkEmail', 'Check if it is a valid email address', 'liyangguang@gmail.com');
// Output: true

af.run('slugify', 'Make a string into underline slugify case', 'Hi, my name is Yangguang');
// Output: hi_my_name_is_yangguang

af.run('fib', 'Generate first 10 digits in fibonacci sequence');
// Output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

af.run('chineseDate', 'Translate date notation from U.S. to Chinese', 'Apr 8, 2023');
// Output: 2023ๅนด4ๆœˆ8ๆ—ฅ

This package is mostly for fun. DO NOT recommend using it in production code. ๐Ÿ˜œ The response if not always reliable, as you can expect from a GPT bot.

How to use

npm i ai-function-js@latest
import AiFunction from 'ai-function-js';

const af = new AiFunction(process.env.OPENAI_API_KEY);

af.run('sum', 'Add all numbers together', [1,2,3,4,5]).then((result) => {
  console.log(result);
})

API reference

  • Constructor: new AiFunction(apiKey: string, model?: Model)
    • apiKey: OpenAI API key.
    • model: Optional. Default to gpt-3.5-turbo. Only support gpt-3.5-turbo and gpt-4. gpt-4 is smarter, but the cost is higher. Refer to OpenAI API pricing for details.
    • Return value: an instance of AiFunction.
  • Method: af.run(name: string, description: string, args: any): Promise<string>
    • name: function name you want to give. A better name gives a better result.
    • description: what does your imaginary function do. A better description gives a better result.
    • args: Arguments you pass to it. Use array for multiple arguments.
    • Return value: Promise of the result string. (Convert to other types afterwards if needed)

Run examples

To run the example code:

npm i
npm run build
OPENAI_API_KEY=sk-... node dist/example.js
0.1.5

1 year ago

0.1.4

1 year ago

0.1.3

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.0.1

1 year ago