chat methods defined, also test file for library testing

This commit is contained in:
Daniel Avila 2023-02-04 20:49:01 -05:00
parent 7c4e7ab07a
commit 641dcfad73
2 changed files with 35 additions and 6 deletions

View file

@ -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 };

27
app/test.js Normal file
View file

@ -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)
*/