From 641dcfad733857429376ff1e25d317267f543e4b Mon Sep 17 00:00:00 2001 From: Daniel Avila Date: Sat, 4 Feb 2023 20:49:01 -0500 Subject: [PATCH] chat methods defined, also test file for library testing --- app/chatgpt.js | 14 ++++++++------ app/test.js | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 app/test.js diff --git a/app/chatgpt.js b/app/chatgpt.js index 8e1dbcf450..48f7e7165c 100644 --- a/app/chatgpt.js +++ b/app/chatgpt.js @@ -1,10 +1,12 @@ require('dotenv').config(); -(async () => { + +const ask = async (question) => { const { ChatGPTAPI } = await import('chatgpt'); - const api = new ChatGPTAPI({ apiKey: process.env.OPENAI_KEY }); + const res = await api.sendMessage(question, { + onProgress: (partialRes) => console.log(partialRes.text) + }); + return res; +}; - // send a message and wait for the response - let res = await api.sendMessage('What is OpenAI?'); - console.log(res); -})(); +module.exports = { ask }; \ No newline at end of file diff --git a/app/test.js b/app/test.js new file mode 100644 index 0000000000..abcefb8657 --- /dev/null +++ b/app/test.js @@ -0,0 +1,27 @@ +require('dotenv').config(); +// docs https://github.com/transitive-bullshit/chatgpt-api + +(async () => { + const { ChatGPTAPI } = await import('chatgpt'); + + const api = new ChatGPTAPI({ apiKey: process.env.OPENAI_KEY }); + + // send a message and wait for the response + let res = await api.sendMessage('What is OpenAI?'); + console.log(res); +})(); + +// If you want to track the conversation, you'll need to pass the parentMessageid and conversationid: +// See example in models/Message.js + +/* +// You can add streaming via the onProgress handler: +// timeout after 2 minutes (which will also abort the underlying HTTP request) +const res = await api.sendMessage('Write a 500 word essay on frogs.', { + // print the partial response as the AI is "typing" + onProgress: (partialResponse) => console.log(partialResponse.text) +}) + +// print the full text at the end +console.log(res.text) +*/ \ No newline at end of file