We're going to be exploring this amazing NodeJS library called chatgpt-prompts. This library allows you to use over 140+ amazing community built prompts compiled over at awesome-chatgpt-prompts instantly.
🚀 Quick Start!
If you are impatient person (like me!), here's a quick start on what the code would look like for a basic example but I recommend you follow along just to ensure that your (hopefully typescript) project gets set up correctly as this package is ESM-only.
import { ChatGPTAPI } from "chatgpt";
import { createChatGPTPrompt } from "chatgpt-prompts";
const run = async () => {
const instance = new ChatGPTAPI({
apiKey: "OPEN_AI_API_KEY",
});
const prompt = createChatGPTPrompt(instance);
let res = await prompt.linuxTerminal("touch hello.txt");
console.log(res.text);
res = await prompt.linuxTerminal("echo hello world > hello.txt");
console.log(res.text);
res = await prompt.linuxTerminal("cat hello.txt");
console.log(res.text);
};
run().catch((err) => console.log("Something went wrong"));
💻 Project Setup
This section details how we would set up our typescript & NodeJS project with the chatgpt
and chatgpt-prompts
package.
To start setting up our project let's create a directory to house all of the code for our project
mkdir chatgpt-app
cd chatgpt-app
Now we have a directory, let's create a package.json
file by running
npm init -y
Now let's install all of our dependencies and dev dependencies
npm install --save-dev nodemon ts-node typescript
npm install chatgpt chatgpt-prompts
Your package.json
file should look something like this
{
"name": "chatgpt-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"nodemon": "^2.0.20",
"ts-node": "^10.9.1",
"typescript": "^4.9.5"
}
}
We're going to be making some absolutely important changes to our package.json
file since chatgpt
and chatgpt-prompts
are ESM-only packages.
Modify your package.json so that it looks something like this
{
"name": "chatgpt-app",
"version": "1.0.0",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"type": "module",
"scripts": {
"start": "nodemon --ext js,ts,json,env --exec 'node --experimental-specifier-resolution=node --loader ts-node/esm' src/index.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"nodemon": "^2.0.20",
"ts-node": "^10.9.1",
"typescript": "^4.9.5"
},
"dependencies": {
"chatgpt": "^4.1.1",
"chatgpt-prompts": "^1.1.1"
}
}
Here were the changes that we've made for extra clarity:
- We've changed the
main
field fromindex.js
todist/index.js
- We've added the
types
field with a value ofdist/index.d.ts
- We've added the
type
field with a value ofmodule
; and finally - We've modified the
start
script to be really long.
After this is completed, we're going to set up our lengthy but absolutely necessary tsconfig.json
file. Feel free to copy the one below
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "Node",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"importHelpers": true,
"resolveJsonModule": true,
"sourceMap": true,
"declaration": true,
"alwaysStrict": true,
"forceConsistentCasingInFileNames": true,
"removeComments": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noImplicitThis": true,
"noUnusedLocals": false,
"noUnusedParameters": false,
"noImplicitReturns": true,
"strict": true,
"noFallthroughCasesInSwitch": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"composite": true,
"skipLibCheck": true,
"noEmitHelpers": false,
"outDir": "dist",
"rootDir": "src",
"baseUrl": "."
},
"include": ["src"],
"exclude": ["dist", "node_modules"]
}
Awesome! After that is set up, we're now going to be writing some actual code. Please run the commands below to create a src
directory and an index.ts
file.
mkdir src
cd src
touch index.ts
Now we have created our index.ts
file, let's actually write and run our code. Here's a code snippet to get you started
import { ChatGPTAPI } from "chatgpt";
import { createChatGPTPrompt } from "chatgpt-prompts";
const run = async () => {
const instance = new ChatGPTAPI({
apiKey: "OPEN_AI_API_KEY",
});
const prompt = createChatGPTPrompt(instance);
let res = await prompt.linuxTerminal("touch hello.txt");
console.log(res.text);
res = await prompt.linuxTerminal("echo hello world > hello.txt");
console.log(res.text);
res = await prompt.linuxTerminal("cat hello.txt");
console.log(res.text);
};
run().catch((err) => console.log("Something went wrong"));
You should have an output similar to that one below
💯 Congratulations
You now have access to over 140+ amazing prompts in NodeJS. I'd also like to credit the following people for the awesome work they've done:
- A big thank you to Travis Fischer for making an amazing NodeJS Client of the ChatGPT API.
- All of the prompts featured in this package comes from awesome-chatgpt-prompts maintained by Fatih Kadir Akın
Top comments (0)