mirror of
https://github.com/Mintplex-Labs/anything-llm.git
synced 2025-03-25 23:24:43 +00:00
* Using OpenAI API locally * Infinite prompt input and compression implementation (#332) * WIP on continuous prompt window summary * wip * Move chat out of VDB simplify chat interface normalize LLM model interface have compression abstraction Cleanup compressor TODO: Anthropic stuff * Implement compression for Anythropic Fix lancedb sources * cleanup vectorDBs and check that lance, chroma, and pinecone are returning valid metadata sources * Resolve Weaviate citation sources not working with schema * comment cleanup * disable import on hosted instances (#339) * disable import on hosted instances * Update UI on disabled import/export --------- Co-authored-by: timothycarambat <rambat1010@gmail.com> * Add support for gpt-4-turbo 128K model (#340) resolves #336 Add support for gpt-4-turbo 128K model * 315 show citations based on relevancy score (#316) * settings for similarity score threshold and prisma schema updated * prisma schema migration for adding similarityScore setting * WIP * Min score default change * added similarityThreshold checking for all vectordb providers * linting --------- Co-authored-by: shatfield4 <seanhatfield5@gmail.com> * rename localai to lmstudio * forgot files that were renamed * normalize model interface * add model and context window limits * update LMStudio tagline * Fully working LMStudio integration --------- Co-authored-by: Francisco Bischoff <984592+franzbischoff@users.noreply.github.com> Co-authored-by: Timothy Carambat <rambat1010@gmail.com> Co-authored-by: Sean Hatfield <seanhatfield5@gmail.com>
139 lines
4.2 KiB
JavaScript
139 lines
4.2 KiB
JavaScript
const { chatPrompt } = require("../../chats");
|
|
|
|
// hybrid of openAi LLM chat completion for LMStudio
|
|
class LMStudioLLM {
|
|
constructor(embedder = null) {
|
|
if (!process.env.LMSTUDIO_BASE_PATH)
|
|
throw new Error("No LMStudio API Base Path was set.");
|
|
|
|
const { Configuration, OpenAIApi } = require("openai");
|
|
const config = new Configuration({
|
|
basePath: process.env.LMSTUDIO_BASE_PATH?.replace(/\/+$/, ""), // here is the URL to your LMStudio instance
|
|
});
|
|
this.lmstudio = new OpenAIApi(config);
|
|
// When using LMStudios inference server - the model param is not required so
|
|
// we can stub it here.
|
|
this.model = "model-placeholder";
|
|
this.limits = {
|
|
history: this.promptWindowLimit() * 0.15,
|
|
system: this.promptWindowLimit() * 0.15,
|
|
user: this.promptWindowLimit() * 0.7,
|
|
};
|
|
|
|
if (!embedder)
|
|
throw new Error(
|
|
"INVALID LM STUDIO SETUP. No embedding engine has been set. Go to instance settings and set up an embedding interface to use LMStudio as your LLM."
|
|
);
|
|
this.embedder = embedder;
|
|
}
|
|
|
|
// Ensure the user set a value for the token limit
|
|
// and if undefined - assume 4096 window.
|
|
promptWindowLimit() {
|
|
const limit = process.env.LMSTUDIO_MODEL_TOKEN_LIMIT || 4096;
|
|
if (!limit || isNaN(Number(limit)))
|
|
throw new Error("No LMStudio token context limit was set.");
|
|
return Number(limit);
|
|
}
|
|
|
|
async isValidChatCompletionModel(_ = "") {
|
|
// LMStudio may be anything. The user must do it correctly.
|
|
// See comment about this.model declaration in constructor
|
|
return true;
|
|
}
|
|
|
|
constructPrompt({
|
|
systemPrompt = "",
|
|
contextTexts = [],
|
|
chatHistory = [],
|
|
userPrompt = "",
|
|
}) {
|
|
const prompt = {
|
|
role: "system",
|
|
content: `${systemPrompt}
|
|
Context:
|
|
${contextTexts
|
|
.map((text, i) => {
|
|
return `[CONTEXT ${i}]:\n${text}\n[END CONTEXT ${i}]\n\n`;
|
|
})
|
|
.join("")}`,
|
|
};
|
|
return [prompt, ...chatHistory, { role: "user", content: userPrompt }];
|
|
}
|
|
|
|
async isSafe(_input = "") {
|
|
// Not implemented so must be stubbed
|
|
return { safe: true, reasons: [] };
|
|
}
|
|
|
|
async sendChat(chatHistory = [], prompt, workspace = {}, rawHistory = []) {
|
|
if (!this.model)
|
|
throw new Error(
|
|
`LMStudio chat: ${model} is not valid or defined for chat completion!`
|
|
);
|
|
|
|
const textResponse = await this.lmstudio
|
|
.createChatCompletion({
|
|
model: this.model,
|
|
temperature: Number(workspace?.openAiTemp ?? 0.7),
|
|
n: 1,
|
|
messages: await this.compressMessages(
|
|
{
|
|
systemPrompt: chatPrompt(workspace),
|
|
userPrompt: prompt,
|
|
chatHistory,
|
|
},
|
|
rawHistory
|
|
),
|
|
})
|
|
.then((json) => {
|
|
const res = json.data;
|
|
if (!res.hasOwnProperty("choices"))
|
|
throw new Error("LMStudio chat: No results!");
|
|
if (res.choices.length === 0)
|
|
throw new Error("LMStudio chat: No results length!");
|
|
return res.choices[0].message.content;
|
|
})
|
|
.catch((error) => {
|
|
throw new Error(
|
|
`LMStudio::createChatCompletion failed with: ${error.message}`
|
|
);
|
|
});
|
|
|
|
return textResponse;
|
|
}
|
|
|
|
async getChatCompletion(messages = null, { temperature = 0.7 }) {
|
|
if (!this.model)
|
|
throw new Error(
|
|
`LMStudio chat: ${this.model} is not valid or defined model for chat completion!`
|
|
);
|
|
|
|
const { data } = await this.lmstudio.createChatCompletion({
|
|
model: this.model,
|
|
messages,
|
|
temperature,
|
|
});
|
|
|
|
if (!data.hasOwnProperty("choices")) return null;
|
|
return data.choices[0].message.content;
|
|
}
|
|
|
|
// 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);
|
|
}
|
|
|
|
async compressMessages(promptArgs = {}, rawHistory = []) {
|
|
const { messageArrayCompressor } = require("../../helpers/chat");
|
|
const messageArray = this.constructPrompt(promptArgs);
|
|
return await messageArrayCompressor(this, messageArray, rawHistory);
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
LMStudioLLM,
|
|
};
|