|
|
@ -1,9 +1,9 @@ |
|
|
|
const { OpenAI } = require("openai"); |
|
|
|
const {OpenAI} = require("openai"); |
|
|
|
require("dotenv").config({ |
|
|
|
require("dotenv").config({ |
|
|
|
path: require("os").homedir() + "/.config/fabric/.env", |
|
|
|
path: require("os").homedir() + "/.config/fabric/.env", |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
let openaiClient = null; |
|
|
|
let openAIClient = null; |
|
|
|
|
|
|
|
|
|
|
|
// Function to initialize and get the OpenAI client
|
|
|
|
// Function to initialize and get the OpenAI client
|
|
|
|
function getOpenAIClient() { |
|
|
|
function getOpenAIClient() { |
|
|
@ -12,14 +12,17 @@ function getOpenAIClient() { |
|
|
|
"The OPENAI_API_KEY environment variable is missing or empty." |
|
|
|
"The OPENAI_API_KEY environment variable is missing or empty." |
|
|
|
); |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
return new OpenAI({ apiKey: process.env.OPENAI_API_KEY }); |
|
|
|
if(!openAIClient){ |
|
|
|
|
|
|
|
openAIClient = new OpenAI({apiKey: process.env.OPENAI_API_KEY}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return openAIClient |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async function queryOpenAI(system, user, callback) { |
|
|
|
async function queryOpenAI(system, user, callback) { |
|
|
|
const openai = getOpenAIClient(); // Ensure the client is initialized here
|
|
|
|
const openai = getOpenAIClient(); // Ensure the client is initialized here
|
|
|
|
const messages = [ |
|
|
|
const messages = [ |
|
|
|
{ role: "system", content: system }, |
|
|
|
{role: "system", content: system}, |
|
|
|
{ role: "user", content: user }, |
|
|
|
{role: "user", content: user}, |
|
|
|
]; |
|
|
|
]; |
|
|
|
try { |
|
|
|
try { |
|
|
|
const stream = await openai.chat.completions.create({ |
|
|
|
const stream = await openai.chat.completions.create({ |
|
|
@ -42,4 +45,4 @@ async function queryOpenAI(system, user, callback) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
module.exports = { queryOpenAI }; |
|
|
|
module.exports = {queryOpenAI}; |
|
|
|