├── .gitattributes ├── .gitignore ├── tsconfig.json ├── .prettierrc.js ├── LICENSE ├── src └── index.ts ├── package.json └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | * text eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.log 2 | .DS_Store 3 | node_modules 4 | dist 5 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "@tsconfig/recommended/tsconfig.json", 3 | "include": ["src", "types"], 4 | "compilerOptions": { 5 | "module": "esnext", 6 | "moduleResolution": "node" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /.prettierrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | semi: false, 3 | singleQuote: true, 4 | printWidth: 120, 5 | tabWidth: 4, 6 | trailingComma: 'es5', 7 | useTabs: true, 8 | bracketSpacing: true, 9 | arrowParens: 'avoid', 10 | } 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Miguel Piedrafita 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import dotenv from 'dotenv' 2 | dotenv.config() 3 | 4 | const apiToken = process.env.OPENAI_API_KEY 5 | if (!apiToken) throw new Error('Missing OPENAI_API_KEY') 6 | 7 | const AutoCode = new Proxy( 8 | {}, 9 | { 10 | get: (_, name: string) => { 11 | return async (...args: unknown[]) => { 12 | const response = await fetch('https://api.openai.com/v1/completions', { 13 | method: 'POST', 14 | headers: { 15 | 'Content-Type': 'application/json', 16 | Authorization: `Bearer ${apiToken}`, 17 | }, 18 | body: JSON.stringify({ 19 | suffix: '\n}', 20 | temperature: 0, 21 | max_tokens: 512, 22 | model: 'code-davinci-002', 23 | prompt: `// Path: src/index.js\n\n${args.map( 24 | (arg, i) => `// typeof arg${i + 1} == "${typeof arg}"\n` 25 | )}function ${name}(${args.map((_, i) => `arg${i + 1},`)}) {\n`, 26 | }), 27 | }).then(res => res.json() as unknown as { choices: [{ text: string }] }) 28 | 29 | return new Function(...args.map((_, i) => `arg${i + 1}`), response.choices[0].text)(...args) 30 | } 31 | }, 32 | } 33 | ) as Record 34 | 35 | export default AutoCode 36 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ai-code", 3 | "version": "0.1.1", 4 | "license": "MIT", 5 | "author": "Miguel Piedrafita", 6 | "main": "dist/index.js", 7 | "repository": "https://github.com/m1guelpf/ai-code", 8 | "module": "dist/ai-code.esm.js", 9 | "typings": "dist/index.d.ts", 10 | "files": [ 11 | "dist", 12 | "src" 13 | ], 14 | "scripts": { 15 | "lint": "dts lint", 16 | "build": "dts build", 17 | "size": "size-limit", 18 | "start": "dts watch", 19 | "prepare": "dts build", 20 | "analyze": "size-limit --why" 21 | }, 22 | "husky": { 23 | "hooks": { 24 | "pre-commit": "dts lint" 25 | } 26 | }, 27 | "size-limit": [ 28 | { 29 | "path": "dist/auto-code.cjs.production.min.js", 30 | "limit": "10 KB" 31 | }, 32 | { 33 | "path": "dist/auto-code.esm.js", 34 | "limit": "10 KB" 35 | } 36 | ], 37 | "devDependencies": { 38 | "@size-limit/preset-small-lib": "^8.1.0", 39 | "@tsconfig/recommended": "^1.0.1", 40 | "@types/node": "^18.11.8", 41 | "dts-cli": "^1.6.0", 42 | "husky": "^8.0.1", 43 | "size-limit": "^8.1.0", 44 | "tslib": "^2.4.1", 45 | "typescript": "^4.8.4" 46 | }, 47 | "dependencies": { 48 | "dotenv": "^16.0.3" 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![banner](https://user-images.githubusercontent.com/23558090/199871945-c142a172-fe8a-4a87-baf8-50b8a25e99db.jpg) 2 | 3 | 4 | # Automagically-generated functions for any occasion 5 | 6 | This package uses [OpenAI's Codex](https://openai.com/blog/openai-codex/) to generate and run Javascript code from a function name and argument types. 7 | 8 | > **Warning** plz don't use in prod lmeow 9 | 10 | ## Installation 11 | 12 | You can install `ai-code` by running `npm install ai-code` on your terminal. 13 | 14 | You'll also need to set the `OPENAI_API_KEY` environment variable to an [OpenAI API key](https://beta.openai.com/) with access to the [Codex private beta](http://beta.openai.com/codex-waitlist) (API calls will fail otherwise). 15 | 16 | ## Usage 17 | 18 | Import the package and call any function, passing any number of arguments. The package will call Codex's API to try to figure out what you're trying to do, generate the code for it, and run it. 19 | 20 | ```js 21 | import AI from 'ai-code' 22 | 23 | await AI.randomNumberBetween(1, 10) // 4 24 | await AI.slugify('My Article') // my-article 25 | await AI.hasProfanityRegex('f*ck this lol') // false 26 | await AI.shortenETHAddress(ethAddress) // 0xE340...b39D 27 | await AI.extractHashtags('this is #really cool! #ai #code') // ['#really', '#ai', '#code'] 28 | await AI.getProgrammerJoke() // Why do programmers always mix up Halloween and Christmas? Because Oct 31 == Dec 25 29 | ``` 30 | 31 | ## License 32 | 33 | This project is open-sourced under the MIT license. See [the License file](LICENSE) for more information. 34 | --------------------------------------------------------------------------------