UNPKG

taskforce-aiagent

Version:

TaskForce is a modular, open-source, production-ready TypeScript agent framework for orchestrating AI agents, LLM-powered autonomous agents, task pipelines, dynamic toolchains, RAG workflows and memory/retrieval systems.

86 lines (85 loc) 3.46 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.retrieveAndEnrichPrompt = retrieveAndEnrichPrompt; exports.conversationalRetrievalChain = conversationalRetrievalChain; const chalk_1 = __importDefault(require("chalk")); const log_helper_js_1 = require("../../helpers/log.helper.js"); const systemAi_helper_js_1 = require("../../helpers/systemAi.helper.js"); async function retrieveAndEnrichPrompt(query, retriever, memoryProvider, modelName, verbose = false, contextFilter) { let records = []; if (retriever) { const raw = await retriever.retrieve(query); if (Array.isArray(raw)) { if (raw.length === 0) { records = []; } else if (typeof raw[0] === "string") { records = raw.map((s) => ({ output: s, summary: s, taskId: "retriever", input: "", metadata: {}, })); } else if (typeof raw[0] === "object" && raw[0] !== null && "output" in raw[0]) { records = raw; } else { // Hiçbiri değilse records = []; } } } else if (memoryProvider) { records = await memoryProvider.loadRelevantMemory(query, 3, contextFilter); } const validRecords = records.filter((r) => r.summary?.trim() || r.output?.trim()); if (validRecords.length === 0) return ""; const texts = validRecords.map((r) => r.summary?.trim() ?? r.output?.trim() ?? ""); const fullText = texts.join("\n\n"); if (fullText.length < 1000) { if (verbose) (0, log_helper_js_1.TFLog)("[Retriever] Injecting full memory text", chalk_1.default.yellow); return fullText; } else { if (verbose) (0, log_helper_js_1.TFLog)("[Retriever] Summarizing memory text due to length", chalk_1.default.yellow); const summary = await (0, systemAi_helper_js_1.callOpenAIFunction)({ model: modelName || process.env.DEFAULT_AI_MODEL, system: "Please provide a concise, focused summary of the following text, preserving all important details relevant to the task. Do not omit critical information.", user: fullText, temperature: 0.3, }); return summary; } } async function conversationalRetrievalChain(query, retriever, modelName, chatHistory = [], verbose = false) { const docs = await retriever.retrieve(query); const context = docs.join("\n\n"); let prompt = ""; if (context.length > 0) { prompt += `Related knowledge:\n${context}\n\n`; } if (chatHistory.length > 0) { prompt += `Chat history:\n${chatHistory.join("\n")}\n\n`; } prompt += `Question: ${query}`; if (verbose) (0, log_helper_js_1.TFLog)("[ConversationalRetrievalChain] Prompt:\n" + prompt, chalk_1.default.blue); // LLM çağrısı const response = await (0, systemAi_helper_js_1.callOpenAIFunction)({ model: modelName, system: "You are a helpful assistant with access to relevant knowledge.", user: prompt, temperature: 0.3, }); return response; }