Skip to content

Commit 60aeade

Browse files
authored
Merge pull request #780 from mohammadanang/master
Provide CLI Project For Assisting You
2 parents f882d79 + 6a13d56 commit 60aeade

File tree

6 files changed

+397
-0
lines changed

6 files changed

+397
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# ChatGPT(OPENAI) Secret Key
2+
OPENAI_SECRET_KEY=

AIAssistant/mohammadanang/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# AI Assitant
2+
3+
This project using Open AI plaform, ChatGPT. You must be register [here](https://platform.openai.com/) to get the `Secret Key`.
4+
5+
## Getting Started
6+
7+
- Open terminal.
8+
- Run `npm install`. (I am using node v18)
9+
- Copy the **secret key** from your OpenAI account page. [here](https://platform.openai.com/account/api-keys)
10+
- Run `cp .env.example`.
11+
- Paste the **secret key** into the `.env` file.
12+
- Run `npm start`.
13+
14+
## Preview Results
15+
16+
![preview_results](captured.png)
286 KB
Loading

AIAssistant/mohammadanang/index.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import OpenAI from 'openai';
2+
import readlineSync from 'readline-sync';
3+
import chalk from 'chalk';
4+
import { config } from 'dotenv';
5+
6+
config();
7+
8+
// chalk with customer color: orange matte
9+
const orange = chalk.hex('#ED944D');
10+
const redMatte = chalk.hex('#FF1A1A');
11+
12+
const exec = async () => {
13+
const openai = new OpenAI({
14+
apiKey: process.env.OPENAI_SECRET_KEY,
15+
});
16+
17+
const chatHistory = [];
18+
19+
do {
20+
const user_input = readlineSync.question(`${chalk.blueBright('Enter your input')}: `);
21+
const messageList = chatHistory.map((c) => ({
22+
role: c.role,
23+
content: c.content
24+
}));
25+
messageList.push(
26+
{ role: "user", content: user_input }
27+
);
28+
29+
try {
30+
const GPTOutput = await openai.chat.completions.create({
31+
model: "gpt-3.5-turbo",
32+
messages: messageList,
33+
});
34+
35+
const output_text = GPTOutput.choices[0].message;
36+
const reply = `😎 ${redMatte("Jaruis")}(${orange(output_text.role)}):\n${chalk.green(output_text.content)}`;
37+
console.log(`\n${reply}`);
38+
39+
chatHistory.push(output_text);
40+
} catch (err) {
41+
if (err.response) {
42+
console.log(err.response.status);
43+
console.log(err.response.data);
44+
} else {
45+
console.log(err.message);
46+
}
47+
}
48+
} while (readlineSync.question("\nDo you want to response? (Y/N)").toUpperCase() === "Y");
49+
};
50+
51+
exec();

AIAssistant/mohammadanang/package-lock.json

Lines changed: 306 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)