2024-06-29 00:32:30 +02:00
|
|
|
/**
|
|
|
|
* @typedef {Object} BaseLLMProvider - A basic llm provider object
|
|
|
|
* @property {Function} streamingEnabled - Checks if streaming is enabled for chat completions.
|
|
|
|
* @property {Function} promptWindowLimit - Returns the token limit for the current model.
|
|
|
|
* @property {Function} isValidChatCompletionModel - Validates if the provided model is suitable for chat completion.
|
|
|
|
* @property {Function} constructPrompt - Constructs a formatted prompt for the chat completion request.
|
|
|
|
* @property {Function} getChatCompletion - Gets a chat completion response from OpenAI.
|
|
|
|
* @property {Function} streamGetChatCompletion - Streams a chat completion response from OpenAI.
|
|
|
|
* @property {Function} handleStream - Handles the streaming response.
|
|
|
|
* @property {Function} embedTextInput - Embeds the provided text input using the specified embedder.
|
|
|
|
* @property {Function} embedChunks - Embeds multiple chunks of text using the specified embedder.
|
|
|
|
* @property {Function} compressMessages - Compresses chat messages to fit within the token limit.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} BaseVectorDatabaseProvider
|
|
|
|
* @property {string} name - The name of the Vector Database instance.
|
|
|
|
* @property {Function} connect - Connects to the Vector Database client.
|
|
|
|
* @property {Function} totalVectors - Returns the total number of vectors in the database.
|
|
|
|
* @property {Function} namespaceCount - Returns the count of vectors in a given namespace.
|
|
|
|
* @property {Function} similarityResponse - Performs a similarity search on a given namespace.
|
|
|
|
* @property {Function} namespace - Retrieves the specified namespace collection.
|
|
|
|
* @property {Function} hasNamespace - Checks if a namespace exists.
|
|
|
|
* @property {Function} namespaceExists - Verifies if a namespace exists in the client.
|
|
|
|
* @property {Function} deleteVectorsInNamespace - Deletes all vectors in a specified namespace.
|
|
|
|
* @property {Function} deleteDocumentFromNamespace - Deletes a document from a specified namespace.
|
|
|
|
* @property {Function} addDocumentToNamespace - Adds a document to a specified namespace.
|
|
|
|
* @property {Function} performSimilaritySearch - Performs a similarity search in the namespace.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} BaseEmbedderProvider
|
|
|
|
* @property {string} model - The model used for embedding.
|
|
|
|
* @property {number} maxConcurrentChunks - The maximum number of chunks processed concurrently.
|
|
|
|
* @property {number} embeddingMaxChunkLength - The maximum length of each chunk for embedding.
|
|
|
|
* @property {Function} embedTextInput - Embeds a single text input.
|
|
|
|
* @property {Function} embedChunks - Embeds multiple chunks of text.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the systems current vector database provider.
|
|
|
|
* @returns { BaseVectorDatabaseProvider}
|
|
|
|
*/
|
2023-06-08 06:31:35 +02:00
|
|
|
function getVectorDbClass() {
|
2024-05-13 21:22:53 +02:00
|
|
|
const vectorSelection = process.env.VECTOR_DB || "lancedb";
|
2023-06-08 06:31:35 +02:00
|
|
|
switch (vectorSelection) {
|
|
|
|
case "pinecone":
|
2023-08-04 23:56:27 +02:00
|
|
|
const { Pinecone } = require("../vectorDbProviders/pinecone");
|
2023-06-08 06:31:35 +02:00
|
|
|
return Pinecone;
|
|
|
|
case "chroma":
|
2023-08-04 23:56:27 +02:00
|
|
|
const { Chroma } = require("../vectorDbProviders/chroma");
|
2023-06-08 06:31:35 +02:00
|
|
|
return Chroma;
|
2023-06-09 03:40:29 +02:00
|
|
|
case "lancedb":
|
2023-08-04 23:56:27 +02:00
|
|
|
const { LanceDb } = require("../vectorDbProviders/lance");
|
2023-06-09 03:40:29 +02:00
|
|
|
return LanceDb;
|
2023-08-09 03:02:30 +02:00
|
|
|
case "weaviate":
|
|
|
|
const { Weaviate } = require("../vectorDbProviders/weaviate");
|
|
|
|
return Weaviate;
|
2023-08-16 00:26:44 +02:00
|
|
|
case "qdrant":
|
|
|
|
const { QDrant } = require("../vectorDbProviders/qdrant");
|
|
|
|
return QDrant;
|
2024-01-12 22:23:57 +01:00
|
|
|
case "milvus":
|
|
|
|
const { Milvus } = require("../vectorDbProviders/milvus");
|
|
|
|
return Milvus;
|
2024-01-18 03:00:54 +01:00
|
|
|
case "zilliz":
|
|
|
|
const { Zilliz } = require("../vectorDbProviders/zilliz");
|
|
|
|
return Zilliz;
|
2024-01-26 22:07:53 +01:00
|
|
|
case "astra":
|
|
|
|
const { AstraDB } = require("../vectorDbProviders/astra");
|
|
|
|
return AstraDB;
|
2023-06-08 06:31:35 +02:00
|
|
|
default:
|
2023-06-08 22:13:48 +02:00
|
|
|
throw new Error("ENV: No VECTOR_DB value found in environment!");
|
2023-06-08 06:31:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-29 00:32:30 +02:00
|
|
|
/**
|
|
|
|
* Returns the LLMProvider with its embedder attached via system or via defined provider.
|
|
|
|
* @param {{provider: string | null, model: string | null} | null} params - Initialize params for LLMs provider
|
|
|
|
* @returns {BaseLLMProvider}
|
|
|
|
*/
|
2024-04-05 19:58:36 +02:00
|
|
|
function getLLMProvider({ provider = null, model = null } = {}) {
|
|
|
|
const LLMSelection = provider ?? process.env.LLM_PROVIDER ?? "openai";
|
2023-11-17 00:19:49 +01:00
|
|
|
const embedder = getEmbeddingEngineSelection();
|
2024-04-05 19:58:36 +02:00
|
|
|
|
|
|
|
switch (LLMSelection) {
|
2023-08-04 23:56:27 +02:00
|
|
|
case "openai":
|
2023-10-30 23:44:03 +01:00
|
|
|
const { OpenAiLLM } = require("../AiProviders/openAi");
|
2024-04-05 19:58:36 +02:00
|
|
|
return new OpenAiLLM(embedder, model);
|
2023-08-04 23:56:27 +02:00
|
|
|
case "azure":
|
2023-10-30 23:44:03 +01:00
|
|
|
const { AzureOpenAiLLM } = require("../AiProviders/azureOpenAi");
|
2024-04-05 19:58:36 +02:00
|
|
|
return new AzureOpenAiLLM(embedder, model);
|
2023-10-30 23:44:03 +01:00
|
|
|
case "anthropic":
|
|
|
|
const { AnthropicLLM } = require("../AiProviders/anthropic");
|
2024-04-05 19:58:36 +02:00
|
|
|
return new AnthropicLLM(embedder, model);
|
2023-12-28 02:08:03 +01:00
|
|
|
case "gemini":
|
|
|
|
const { GeminiLLM } = require("../AiProviders/gemini");
|
2024-04-05 19:58:36 +02:00
|
|
|
return new GeminiLLM(embedder, model);
|
2023-11-09 21:33:21 +01:00
|
|
|
case "lmstudio":
|
|
|
|
const { LMStudioLLM } = require("../AiProviders/lmStudio");
|
2024-04-05 19:58:36 +02:00
|
|
|
return new LMStudioLLM(embedder, model);
|
2023-11-14 21:31:44 +01:00
|
|
|
case "localai":
|
|
|
|
const { LocalAiLLM } = require("../AiProviders/localAi");
|
2024-04-05 19:58:36 +02:00
|
|
|
return new LocalAiLLM(embedder, model);
|
2023-12-28 02:21:47 +01:00
|
|
|
case "ollama":
|
|
|
|
const { OllamaAILLM } = require("../AiProviders/ollama");
|
2024-04-05 19:58:36 +02:00
|
|
|
return new OllamaAILLM(embedder, model);
|
2024-01-10 21:35:30 +01:00
|
|
|
case "togetherai":
|
|
|
|
const { TogetherAiLLM } = require("../AiProviders/togetherAi");
|
2024-04-05 19:58:36 +02:00
|
|
|
return new TogetherAiLLM(embedder, model);
|
2024-02-22 21:48:57 +01:00
|
|
|
case "perplexity":
|
|
|
|
const { PerplexityLLM } = require("../AiProviders/perplexity");
|
2024-04-05 19:58:36 +02:00
|
|
|
return new PerplexityLLM(embedder, model);
|
2024-02-24 02:18:58 +01:00
|
|
|
case "openrouter":
|
|
|
|
const { OpenRouterLLM } = require("../AiProviders/openRouter");
|
2024-04-05 19:58:36 +02:00
|
|
|
return new OpenRouterLLM(embedder, model);
|
2024-01-17 23:42:05 +01:00
|
|
|
case "mistral":
|
|
|
|
const { MistralLLM } = require("../AiProviders/mistral");
|
2024-04-05 19:58:36 +02:00
|
|
|
return new MistralLLM(embedder, model);
|
2023-12-07 23:48:27 +01:00
|
|
|
case "native":
|
|
|
|
const { NativeLLM } = require("../AiProviders/native");
|
2024-04-05 19:58:36 +02:00
|
|
|
return new NativeLLM(embedder, model);
|
2024-02-06 18:17:51 +01:00
|
|
|
case "huggingface":
|
|
|
|
const { HuggingFaceLLM } = require("../AiProviders/huggingface");
|
2024-04-05 19:58:36 +02:00
|
|
|
return new HuggingFaceLLM(embedder, model);
|
2024-03-06 23:48:38 +01:00
|
|
|
case "groq":
|
|
|
|
const { GroqLLM } = require("../AiProviders/groq");
|
2024-04-05 19:58:36 +02:00
|
|
|
return new GroqLLM(embedder, model);
|
2024-05-02 21:12:44 +02:00
|
|
|
case "koboldcpp":
|
|
|
|
const { KoboldCPPLLM } = require("../AiProviders/koboldCPP");
|
|
|
|
return new KoboldCPPLLM(embedder, model);
|
2024-05-08 20:56:30 +02:00
|
|
|
case "textgenwebui":
|
|
|
|
const { TextGenWebUILLM } = require("../AiProviders/textGenWebUI");
|
|
|
|
return new TextGenWebUILLM(embedder, model);
|
2024-05-02 19:35:50 +02:00
|
|
|
case "cohere":
|
|
|
|
const { CohereLLM } = require("../AiProviders/cohere");
|
|
|
|
return new CohereLLM(embedder, model);
|
2024-05-16 22:56:28 +02:00
|
|
|
case "litellm":
|
|
|
|
const { LiteLLM } = require("../AiProviders/liteLLM");
|
|
|
|
return new LiteLLM(embedder, model);
|
2024-04-23 22:06:07 +02:00
|
|
|
case "generic-openai":
|
|
|
|
const { GenericOpenAiLLM } = require("../AiProviders/genericOpenAi");
|
|
|
|
return new GenericOpenAiLLM(embedder, model);
|
2023-08-04 23:56:27 +02:00
|
|
|
default:
|
2024-04-23 22:06:07 +02:00
|
|
|
throw new Error(
|
|
|
|
`ENV: No valid LLM_PROVIDER value found in environment! Using ${process.env.LLM_PROVIDER}`
|
|
|
|
);
|
2023-08-04 23:56:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-29 00:32:30 +02:00
|
|
|
/**
|
|
|
|
* Returns the EmbedderProvider by itself to whatever is currently in the system settings.
|
|
|
|
* @returns {BaseEmbedderProvider}
|
|
|
|
*/
|
2023-10-30 23:44:03 +01:00
|
|
|
function getEmbeddingEngineSelection() {
|
2024-05-17 02:51:04 +02:00
|
|
|
const { NativeEmbedder } = require("../EmbeddingEngines/native");
|
2023-10-30 23:44:03 +01:00
|
|
|
const engineSelection = process.env.EMBEDDING_ENGINE;
|
|
|
|
switch (engineSelection) {
|
|
|
|
case "openai":
|
2023-10-30 23:49:29 +01:00
|
|
|
const { OpenAiEmbedder } = require("../EmbeddingEngines/openAi");
|
2023-10-30 23:44:03 +01:00
|
|
|
return new OpenAiEmbedder();
|
|
|
|
case "azure":
|
|
|
|
const {
|
|
|
|
AzureOpenAiEmbedder,
|
2023-10-30 23:49:29 +01:00
|
|
|
} = require("../EmbeddingEngines/azureOpenAi");
|
2023-10-30 23:44:03 +01:00
|
|
|
return new AzureOpenAiEmbedder();
|
2023-11-14 22:49:31 +01:00
|
|
|
case "localai":
|
|
|
|
const { LocalAiEmbedder } = require("../EmbeddingEngines/localAi");
|
|
|
|
return new LocalAiEmbedder();
|
2024-02-27 01:12:20 +01:00
|
|
|
case "ollama":
|
|
|
|
const { OllamaEmbedder } = require("../EmbeddingEngines/ollama");
|
|
|
|
return new OllamaEmbedder();
|
2023-12-06 19:36:22 +01:00
|
|
|
case "native":
|
|
|
|
return new NativeEmbedder();
|
2024-04-20 00:36:07 +02:00
|
|
|
case "lmstudio":
|
|
|
|
const { LMStudioEmbedder } = require("../EmbeddingEngines/lmstudio");
|
|
|
|
return new LMStudioEmbedder();
|
2024-05-02 19:35:50 +02:00
|
|
|
case "cohere":
|
|
|
|
const { CohereEmbedder } = require("../EmbeddingEngines/cohere");
|
|
|
|
return new CohereEmbedder();
|
2024-05-19 20:20:23 +02:00
|
|
|
case "voyageai":
|
|
|
|
const { VoyageAiEmbedder } = require("../EmbeddingEngines/voyageAi");
|
|
|
|
return new VoyageAiEmbedder();
|
2024-06-06 21:43:34 +02:00
|
|
|
case "litellm":
|
|
|
|
const { LiteLLMEmbedder } = require("../EmbeddingEngines/liteLLM");
|
|
|
|
return new LiteLLMEmbedder();
|
2024-06-22 01:27:02 +02:00
|
|
|
case "generic-openai":
|
|
|
|
const {
|
|
|
|
GenericOpenAiEmbedder,
|
|
|
|
} = require("../EmbeddingEngines/genericOpenAi");
|
|
|
|
return new GenericOpenAiEmbedder();
|
2023-10-30 23:44:03 +01:00
|
|
|
default:
|
2024-05-17 02:51:04 +02:00
|
|
|
return new NativeEmbedder();
|
2023-10-30 23:44:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-08 01:27:36 +01:00
|
|
|
// Some models have lower restrictions on chars that can be encoded in a single pass
|
|
|
|
// and by default we assume it can handle 1,000 chars, but some models use work with smaller
|
|
|
|
// chars so here we can override that value when embedding information.
|
|
|
|
function maximumChunkLength() {
|
|
|
|
if (
|
|
|
|
!!process.env.EMBEDDING_MODEL_MAX_CHUNK_LENGTH &&
|
|
|
|
!isNaN(process.env.EMBEDDING_MODEL_MAX_CHUNK_LENGTH) &&
|
|
|
|
Number(process.env.EMBEDDING_MODEL_MAX_CHUNK_LENGTH) > 1
|
|
|
|
)
|
|
|
|
return Number(process.env.EMBEDDING_MODEL_MAX_CHUNK_LENGTH);
|
|
|
|
|
|
|
|
return 1_000;
|
|
|
|
}
|
|
|
|
|
2023-06-08 22:13:48 +02:00
|
|
|
function toChunks(arr, size) {
|
|
|
|
return Array.from({ length: Math.ceil(arr.length / size) }, (_v, i) =>
|
|
|
|
arr.slice(i * size, i * size + size)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-06-08 06:31:35 +02:00
|
|
|
module.exports = {
|
2023-10-30 23:44:03 +01:00
|
|
|
getEmbeddingEngineSelection,
|
2023-12-08 01:27:36 +01:00
|
|
|
maximumChunkLength,
|
2023-06-08 06:31:35 +02:00
|
|
|
getVectorDbClass,
|
2023-08-04 23:56:27 +02:00
|
|
|
getLLMProvider,
|
2023-06-08 22:13:48 +02:00
|
|
|
toChunks,
|
2023-06-08 06:31:35 +02:00
|
|
|
};
|