From 796918e07e38ea68110d2683f48aecb4dde1c450 Mon Sep 17 00:00:00 2001 From: "Namhyeon, Go" Date: Mon, 27 Nov 2023 18:03:22 +0900 Subject: [PATCH] ChatGPT + MS Office --- .gitignore | 3 +++ lib/chatgpt.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ officetest.js | 8 ++++++-- 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 lib/chatgpt.js diff --git a/.gitignore b/.gitignore index 069e95c..afc5e34 100644 --- a/.gitignore +++ b/.gitignore @@ -108,3 +108,6 @@ bin/ packages/ config.xml staticip.xml + +# ChatGPT API Key +data/chatgpt-apikey.txt diff --git a/lib/chatgpt.js b/lib/chatgpt.js new file mode 100644 index 0000000..a391acd --- /dev/null +++ b/lib/chatgpt.js @@ -0,0 +1,49 @@ +var FILE = require("lib/file"); +var HTTP = require("lib/http"); + +function loadApiKey() { + var s = FILE.readFile("data/chatgpt-apikey.txt", FILE.CdoCharset.CdoUTF_8); + return s.trim(); +} + +function chat(content) { + var s = ''; + + var apikey = loadApiKey(); + console.log("ChatGPT API KEY:", apikey); + + var response = HTTP.create("MSXML") + .setVariables({ + "OPENAI_API_KEY": apikey + }) + .setHeaders({ + "Content-Type": "application/json", + "Authorization": "Bearer {OPENAI_API_KEY}" + }) + .setRequestBody({ + "model": "gpt-3.5-turbo", + "messages": [ + { + "role": "user", + "content": content + } + ] + }) + .open("post", "https://api.openai.com/v1/chat/completions") + .send() + .responseBody + ; + + if (response.choices.length > 0) { + s += response.choices[0].message.content; + } + + return s; +} + +exports.chat = chat; + +exports.VERSIONINFO = "ChatGPT interface (chatgpt.js) version 0.1"; +exports.AUTHOR = "abuse@catswords.net"; +exports.global = global; +exports.require = global.require; diff --git a/officetest.js b/officetest.js index 935022b..0fb80c2 100644 --- a/officetest.js +++ b/officetest.js @@ -1,13 +1,17 @@ var Office = require("lib/msoffice"); +var ChatGPT = require("lib/chatgpt"); function main(args) { var excel = new Office.Excel(); excel.open(); - excel.getValueByPosition(1, 1, "hello world"); + var message = ChatGPT.chat("Say this is a test!"); + console.log(message); - excel.close(); + excel.setValueByPosition(1, 1, message); + + //excel.close(); } exports.main = main; \ No newline at end of file