2023-10-30 23:44:03 +01:00
|
|
|
const { AzureOpenAiEmbedder } = require("../../EmbeddingEngines/azureOpenAi");
|
2023-11-06 22:13:53 +01:00
|
|
|
const { chatPrompt } = require("../../chats");
|
2023-08-22 19:23:29 +02:00
|
|
|
|
2023-11-17 00:19:49 +01:00
|
|
|
class AzureOpenAiLLM {
|
|
|
|
constructor(embedder = null) {
|
2023-08-04 23:56:27 +02:00
|
|
|
const { OpenAIClient, AzureKeyCredential } = require("@azure/openai");
|
2023-10-30 23:44:03 +01:00
|
|
|
if (!process.env.AZURE_OPENAI_ENDPOINT)
|
|
|
|
throw new Error("No Azure API endpoint was set.");
|
|
|
|
if (!process.env.AZURE_OPENAI_KEY)
|
|
|
|
throw new Error("No Azure API key was set.");
|
|
|
|
|
|
|
|
this.openai = new OpenAIClient(
|
2023-08-04 23:56:27 +02:00
|
|
|
process.env.AZURE_OPENAI_ENDPOINT,
|
|
|
|
new AzureKeyCredential(process.env.AZURE_OPENAI_KEY)
|
|
|
|
);
|
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,
|
|
|
|
};
|
2023-11-17 00:19:49 +01:00
|
|
|
|
|
|
|
if (!embedder)
|
|
|
|
console.warn(
|
|
|
|
"No embedding provider defined for AzureOpenAiLLM - falling back to AzureOpenAiEmbedder for embedding!"
|
|
|
|
);
|
|
|
|
this.embedder = !embedder ? new AzureOpenAiEmbedder() : embedder;
|
2023-11-06 22:13:53 +01:00
|
|
|
}
|
|
|
|
|
2023-12-28 23:42:34 +01:00
|
|
|
#appendContext(contextTexts = []) {
|
|
|
|
if (!contextTexts || !contextTexts.length) return "";
|
|
|
|
return (
|
|
|
|
"\nContext:\n" +
|
|
|
|
contextTexts
|
|
|
|
.map((text, i) => {
|
|
|
|
return `[CONTEXT ${i}]:\n${text}\n[END CONTEXT ${i}]\n\n`;
|
|
|
|
})
|
|
|
|
.join("")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-11-14 00:07:30 +01:00
|
|
|
streamingEnabled() {
|
|
|
|
return "streamChat" in this && "streamGetChatCompletion" in this;
|
|
|
|
}
|
|
|
|
|
2023-11-06 22:13:53 +01:00
|
|
|
// Sure the user selected a proper value for the token limit
|
|
|
|
// could be any of these https://learn.microsoft.com/en-us/azure/ai-services/openai/concepts/models#gpt-4-models
|
|
|
|
// and if undefined - assume it is the lowest end.
|
|
|
|
promptWindowLimit() {
|
|
|
|
return !!process.env.AZURE_OPENAI_TOKEN_LIMIT
|
|
|
|
? Number(process.env.AZURE_OPENAI_TOKEN_LIMIT)
|
|
|
|
: 4096;
|
2023-08-04 23:56:27 +02:00
|
|
|
}
|
|
|
|
|
2023-11-06 22:13:53 +01:00
|
|
|
isValidChatCompletionModel(_modelName = "") {
|
2023-08-04 23:56:27 +02:00
|
|
|
// The Azure user names their "models" as deployments and they can be any name
|
|
|
|
// so we rely on the user to put in the correct deployment as only they would
|
|
|
|
// know it.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-10-30 23:44:03 +01:00
|
|
|
constructPrompt({
|
|
|
|
systemPrompt = "",
|
|
|
|
contextTexts = [],
|
|
|
|
chatHistory = [],
|
|
|
|
userPrompt = "",
|
|
|
|
}) {
|
|
|
|
const prompt = {
|
|
|
|
role: "system",
|
2023-12-28 23:42:34 +01:00
|
|
|
content: `${systemPrompt}${this.#appendContext(contextTexts)}`,
|
2023-10-30 23:44:03 +01:00
|
|
|
};
|
|
|
|
return [prompt, ...chatHistory, { role: "user", content: userPrompt }];
|
|
|
|
}
|
|
|
|
|
2023-08-04 23:56:27 +02:00
|
|
|
async isSafe(_input = "") {
|
|
|
|
// Not implemented by Azure OpenAI so must be stubbed
|
|
|
|
return { safe: true, reasons: [] };
|
|
|
|
}
|
|
|
|
|
2023-11-06 22:13:53 +01:00
|
|
|
async sendChat(chatHistory = [], prompt, workspace = {}, rawHistory = []) {
|
|
|
|
if (!this.model)
|
2023-08-04 23:56:27 +02:00
|
|
|
throw new Error(
|
|
|
|
"No OPEN_MODEL_PREF ENV defined. This must the name of a deployment on your Azure account for an LLM chat model like GPT-3.5."
|
|
|
|
);
|
|
|
|
|
2023-11-06 22:13:53 +01:00
|
|
|
const messages = await this.compressMessages(
|
|
|
|
{
|
|
|
|
systemPrompt: chatPrompt(workspace),
|
|
|
|
userPrompt: prompt,
|
|
|
|
chatHistory,
|
|
|
|
},
|
|
|
|
rawHistory
|
|
|
|
);
|
2023-08-04 23:56:27 +02:00
|
|
|
const textResponse = await this.openai
|
2023-11-06 22:13:53 +01:00
|
|
|
.getChatCompletions(this.model, messages, {
|
|
|
|
temperature: Number(workspace?.openAiTemp ?? 0.7),
|
|
|
|
n: 1,
|
|
|
|
})
|
2023-08-04 23:56:27 +02:00
|
|
|
.then((res) => {
|
|
|
|
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;
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.log(error);
|
|
|
|
throw new Error(
|
|
|
|
`AzureOpenAI::getChatCompletions failed with: ${error.message}`
|
|
|
|
);
|
|
|
|
});
|
|
|
|
return textResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getChatCompletion(messages = [], { temperature = 0.7 }) {
|
2023-11-06 22:13:53 +01:00
|
|
|
if (!this.model)
|
2023-08-04 23:56:27 +02:00
|
|
|
throw new Error(
|
|
|
|
"No OPEN_MODEL_PREF ENV defined. This must the name of a deployment on your Azure account for an LLM chat model like GPT-3.5."
|
|
|
|
);
|
|
|
|
|
2023-11-06 22:13:53 +01:00
|
|
|
const data = await this.openai.getChatCompletions(this.model, messages, {
|
2023-08-04 23:56:27 +02:00
|
|
|
temperature,
|
|
|
|
});
|
|
|
|
if (!data.hasOwnProperty("choices")) return null;
|
|
|
|
return data.choices[0].message.content;
|
|
|
|
}
|
2023-11-06 22:13:53 +01:00
|
|
|
|
2023-11-17 00:19:49 +01:00
|
|
|
// Simple wrapper for dynamic embedder & normalize interface for all LLM implementations
|
|
|
|
async embedTextInput(textInput) {
|
|
|
|
return await this.embedder.embedTextInput(textInput);
|
|
|
|
}
|
|
|
|
async embedChunks(textChunks = []) {
|
|
|
|
return await this.embedder.embedChunks(textChunks);
|
|
|
|
}
|
|
|
|
|
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-08-04 23:56:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2023-10-30 23:44:03 +01:00
|
|
|
AzureOpenAiLLM,
|
2023-08-04 23:56:27 +02:00
|
|
|
};
|