langcode
Version:
A Plugin-Based Framework for Managing and Using LangChain
42 lines (41 loc) • 2.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.retrieverBuilder = retrieverBuilder;
const openai_1 = require("@langchain/openai");
const hf_1 = require("@langchain/community/embeddings/hf");
const faiss_1 = require("@langchain/community/vectorstores/faiss");
const memory_1 = require("langchain/vectorstores/memory");
const ollama_1 = require("@langchain/ollama");
const embeddingFactories = {
openai: ({ apiKey, model }) => new openai_1.OpenAIEmbeddings({ apiKey, model: model || "text-embedding-3-small" }),
ollama: ({ model }) => new ollama_1.OllamaEmbeddings({ model: model || "nomic-embed-text" }),
huggingface: ({ apiKey, model }) => new hf_1.HuggingFaceInferenceEmbeddings({
apiKey: apiKey,
model: model || "sentence-transformers/all-MiniLM-L6-v2",
}),
};
const storeLoaders = {
faiss: async ({ indexPath, documents }, embeddings) => {
if (!indexPath)
throw new Error("indexPath is required for FAISS");
// Eğer documents varsa önce oluştur, kaydet, sonra yükle
if (documents && documents.length > 0) {
const createdStore = await faiss_1.FaissStore.fromDocuments(documents, embeddings);
await createdStore.save(indexPath);
}
return await faiss_1.FaissStore.load(indexPath, embeddings);
},
memory: async (_, embeddings) => new memory_1.MemoryVectorStore(embeddings),
};
async function retrieverBuilder(config) {
var _a;
const embeddingFactory = embeddingFactories[config.embedding.provider];
if (!embeddingFactory)
throw new Error(`Unsupported embedding provider: ${config.embedding.provider}`);
const embeddings = embeddingFactory(config.embedding);
const storeLoader = storeLoaders[config.store.type];
if (!storeLoader)
throw new Error(`Unsupported vector store: ${config.store.type}`);
const store = await storeLoader(config.store, embeddings);
return store.asRetriever({ k: (_a = config.k) !== null && _a !== void 0 ? _a : 4 });
}