Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,44 @@ handler.prototype.exit = function(args, fn) {
this.emit("close");
};

handler.prototype.gpt = function(args, fn) {
let result = {
lines: [
"--- ChatGPT API Ready ---",
`> ${args[0]}`
],
time: moment()
};

core.display(result);

const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: util.openai_api_key
});

const openai = new OpenAIApi(configuration);

(async () => {
const completion = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: [{ role: "user", content: args[0] }],
});

let responseLines = ["--- ChatGPT API Response ---", ...completion.data.choices[0].message.content.split(/\n/)];

let result = {
lines: responseLines,
time: moment()
};

core.display(result);

})();

fn(null, args);
};

(new cli(new handler())).run();

module.exports = core;
5 changes: 5 additions & 0 deletions lib/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ let initialize = () => {
utility.buffer = {};
utility.theme = {};
utility.twitter = {};
utility.openai_api_token = "";
};

initialize();
Expand Down Expand Up @@ -159,6 +160,10 @@ utility.parseSettingFile = (path) => {
utility.twitter = settings.twitter;
}
}

if (settings.openai_api_key) {
utility.openai_api_key = settings.openai_api_key;
}
};

utility.decolateText = (message) => {
Expand Down
60 changes: 60 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"moment": "^2.29.4",
"nconf": "^0.12.0",
"node-emoji": "^1.11.0",
"openai": "^3.2.1",
"shell": "^0.8.5",
"sprintf-js": "^1.1.2",
"twitter": "^1.7.1",
Expand Down
2 changes: 1 addition & 1 deletion test/settings_sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ twitter:
consumer_secret: CONSUMER_SECRET_SAMPLE
access_token_key: ACCESS_TOKEN_KEY_SAMPLE
access_token_secret: ACCESS_TOKEN_SECRET_SAMPLE

openai_api_key: sk-xxxxxyyyyyzzzzzz
5 changes: 5 additions & 0 deletions test/util_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ describe("設定ファイル読み込みのテスト", () => {
assert.equal(util.theme.date, "red", "日付の表示用色指定が読み込めている");
});

it("OpenAI API用の設定が読めていること", () => {
util.parseSettingFile('./test/settings_sample.yaml');
assert.equal(util.openai_api_key, "sk-xxxxxyyyyyzzzzzz", "OpenAI API用のキーが読み取れている");
});

describe("twitter設定読み込みのテスト", () => {
it("twitterの設定が無いYAMLファイルの場合、util.twitterのプロパティがfalseになっていること", () => {
util.parseSettingFile('./test/settings_sample_without_twitter.yaml');
Expand Down