2023-10-30 23:44:03 +01:00
|
|
|
const { OpenAiEmbedder } = require("../../EmbeddingEngines/openAi");
|
2023-11-06 22:13:53 +01:00
|
|
|
const { chatPrompt } = require("../../chats");
|
2023-10-26 19:57:37 +02:00
|
|
|
|
2023-10-30 23:44:03 +01:00
|
|
|
class OpenAiLLM extends OpenAiEmbedder {
|
2023-06-04 04:28:07 +02:00
|
|
|
constructor() {
|
2023-10-30 23:44:03 +01:00
|
|
|
super();
|
2023-08-04 23:56:27 +02:00
|
|
|
const { Configuration, OpenAIApi } = require("openai");
|
2023-10-30 23:44:03 +01:00
|
|
|
if (!process.env.OPEN_AI_KEY) throw new Error("No OpenAI API key was set.");
|
|
|
|
|
2023-06-08 06:31:35 +02:00
|
|
|
const config = new Configuration({
|
|
|
|
apiKey: process.env.OPEN_AI_KEY,
|
|
|
|
});
|
2023-10-30 23:44:03 +01:00
|
|
|
this.openai = new OpenAIApi(config);
|
2023-11-06 22:13:53 +01:00
|
|
|
this.model = process.env.OPEN_MODEL_PREF;
|
|
|
|
this.limits = {
|
|
|
|
history: this.promptWindowLimit() * 0.15,
|
|
|
|
system: this.promptWindowLimit() * 0.15,
|
|
|
|
user: this.promptWindowLimit() * 0.7,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
promptWindowLimit() {
|
|
|
|
switch (this.model) {
|
|
|
|
case "gpt-3.5-turbo":
|
|
|
|
return 4096;
|
|
|
|
case "gpt-4":
|
|
|
|
return 8192;
|
|
|
|
default:
|
|
|
|
return 4096; // assume a fine-tune 3.5
|
|
|
|
}
|
2023-06-04 04:28:07 +02:00
|
|
|
}
|
2023-07-28 21:05:38 +02:00
|
|
|
|
2023-10-31 19:38:28 +01:00
|
|
|
async isValidChatCompletionModel(modelName = "") {
|
2023-06-08 06:31:35 +02:00
|
|
|
const validModels = ["gpt-4", "gpt-3.5-turbo"];
|
2023-10-31 19:38:28 +01:00
|
|
|
const isPreset = validModels.some((model) => modelName === model);
|
|
|
|
if (isPreset) return true;
|
|
|
|
|
|
|
|
const model = await this.openai
|
|
|
|
.retrieveModel(modelName)
|
|
|
|
.then((res) => res.data)
|
|
|
|
.catch(() => null);
|
|
|
|
return !!model;
|
2023-06-04 04:28:07 +02:00
|
|
|
}
|
|
|
|
|
2023-10-30 23:44:03 +01:00
|
|
|
constructPrompt({
|
|
|
|
systemPrompt = "",
|
|
|
|
contextTexts = [],
|
|
|
|
chatHistory = [],
|
|
|
|
userPrompt = "",
|
|
|
|
}) {
|
|
|
|
const prompt = {
|
|
|
|
role: "system",
|
|
|
|
content: `${systemPrompt}
|
2023-11-06 22:13:53 +01:00
|
|
|
Context:
|
2023-10-30 23:44:03 +01:00
|
|
|
${contextTexts
|
|
|
|
.map((text, i) => {
|
|
|
|
return `[CONTEXT ${i}]:\n${text}\n[END CONTEXT ${i}]\n\n`;
|
|
|
|
})
|
|
|
|
.join("")}`,
|
|
|
|
};
|
|
|
|
return [prompt, ...chatHistory, { role: "user", content: userPrompt }];
|
|
|
|
}
|
|
|
|
|
2023-06-08 06:31:35 +02:00
|
|
|
async isSafe(input = "") {
|
|
|
|
const { flagged = false, categories = {} } = await this.openai
|
|
|
|
.createModeration({ input })
|
2023-06-04 04:28:07 +02:00
|
|
|
.then((json) => {
|
|
|
|
const res = json.data;
|
2023-06-08 06:31:35 +02:00
|
|
|
if (!res.hasOwnProperty("results"))
|
|
|
|
throw new Error("OpenAI moderation: No results!");
|
|
|
|
if (res.results.length === 0)
|
|
|
|
throw new Error("OpenAI moderation: No results length!");
|
|
|
|
return res.results[0];
|
2023-06-27 02:54:55 +02:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
throw new Error(
|
|
|
|
`OpenAI::CreateModeration failed with: ${error.message}`
|
|
|
|
);
|
2023-06-08 06:31:35 +02:00
|
|
|
});
|
2023-06-04 04:28:07 +02:00
|
|
|
|
|
|
|
if (!flagged) return { safe: true, reasons: [] };
|
2023-06-08 06:31:35 +02:00
|
|
|
const reasons = Object.keys(categories)
|
|
|
|
.map((category) => {
|
|
|
|
const value = categories[category];
|
|
|
|
if (value === true) {
|
|
|
|
return category.replace("/", " or ");
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.filter((reason) => !!reason);
|
2023-06-04 04:28:07 +02:00
|
|
|
|
2023-06-08 06:31:35 +02:00
|
|
|
return { safe: false, reasons };
|
2023-06-04 04:28:07 +02:00
|
|
|
}
|
|
|
|
|
2023-11-06 22:13:53 +01:00
|
|
|
async sendChat(chatHistory = [], prompt, workspace = {}, rawHistory = []) {
|
2023-06-08 06:31:35 +02:00
|
|
|
const model = process.env.OPEN_MODEL_PREF;
|
2023-10-31 19:38:28 +01:00
|
|
|
if (!(await this.isValidChatCompletionModel(model)))
|
2023-06-08 06:31:35 +02:00
|
|
|
throw new Error(
|
|
|
|
`OpenAI chat: ${model} is not valid for chat completion!`
|
|
|
|
);
|
2023-06-04 04:28:07 +02:00
|
|
|
|
2023-06-08 06:31:35 +02:00
|
|
|
const textResponse = await this.openai
|
|
|
|
.createChatCompletion({
|
|
|
|
model,
|
2023-06-15 08:12:59 +02:00
|
|
|
temperature: Number(workspace?.openAiTemp ?? 0.7),
|
2023-06-08 06:31:35 +02:00
|
|
|
n: 1,
|
2023-11-06 22:13:53 +01:00
|
|
|
messages: await this.compressMessages(
|
|
|
|
{
|
|
|
|
systemPrompt: chatPrompt(workspace),
|
|
|
|
userPrompt: prompt,
|
|
|
|
chatHistory,
|
|
|
|
},
|
|
|
|
rawHistory
|
|
|
|
),
|
2023-06-04 04:28:07 +02:00
|
|
|
})
|
2023-06-08 06:31:35 +02:00
|
|
|
.then((json) => {
|
|
|
|
const res = json.data;
|
|
|
|
if (!res.hasOwnProperty("choices"))
|
|
|
|
throw new Error("OpenAI chat: No results!");
|
|
|
|
if (res.choices.length === 0)
|
|
|
|
throw new Error("OpenAI chat: No results length!");
|
|
|
|
return res.choices[0].message.content;
|
2023-06-27 02:54:55 +02:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
throw new Error(
|
|
|
|
`OpenAI::createChatCompletion failed with: ${error.message}`
|
|
|
|
);
|
2023-06-08 06:31:35 +02:00
|
|
|
});
|
2023-06-04 04:28:07 +02:00
|
|
|
|
2023-06-08 06:31:35 +02:00
|
|
|
return textResponse;
|
2023-06-04 04:28:07 +02:00
|
|
|
}
|
2023-07-28 21:05:38 +02:00
|
|
|
|
2023-10-31 19:38:28 +01:00
|
|
|
async getChatCompletion(messages = null, { temperature = 0.7 }) {
|
2023-11-06 22:13:53 +01:00
|
|
|
if (!(await this.isValidChatCompletionModel(this.model)))
|
2023-10-31 19:38:28 +01:00
|
|
|
throw new Error(
|
2023-11-06 22:13:53 +01:00
|
|
|
`OpenAI chat: ${this.model} is not valid for chat completion!`
|
2023-10-31 19:38:28 +01:00
|
|
|
);
|
|
|
|
|
2023-07-28 21:05:38 +02:00
|
|
|
const { data } = await this.openai.createChatCompletion({
|
2023-11-06 22:13:53 +01:00
|
|
|
model: this.model,
|
2023-07-28 21:05:38 +02:00
|
|
|
messages,
|
|
|
|
temperature,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!data.hasOwnProperty("choices")) return null;
|
|
|
|
return data.choices[0].message.content;
|
|
|
|
}
|
2023-11-06 22:13:53 +01:00
|
|
|
|
|
|
|
async compressMessages(promptArgs = {}, rawHistory = []) {
|
|
|
|
const { messageArrayCompressor } = require("../../helpers/chat");
|
|
|
|
const messageArray = this.constructPrompt(promptArgs);
|
|
|
|
return await messageArrayCompressor(this, messageArray, rawHistory);
|
|
|
|
}
|
2023-06-04 04:28:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2023-10-30 23:44:03 +01:00
|
|
|
OpenAiLLM,
|
2023-06-04 04:28:07 +02:00
|
|
|
};
|