Merge pull request #40 from josedario87/codex/completar-funcionalidad-del-agente
Improve planning prompts
This commit is contained in:
@@ -0,0 +1,22 @@
|
|||||||
|
import { genAI, getMcpTool } from '../llm/gemini';
|
||||||
|
import { FunctionCallingConfigMode } from '@google/genai';
|
||||||
|
|
||||||
|
export async function executeTools(instruction: string): Promise<string> {
|
||||||
|
if (!genAI) throw new Error('LLM not configured');
|
||||||
|
const mcpTool = await getMcpTool();
|
||||||
|
const executionPrompt = `Vas a ejecutar una sola herramienta del MCP seg\xFAn el plan. Explic\xE1 brevemente la acci\xF3n y devuelve s\xF3lo el resultado.`;
|
||||||
|
|
||||||
|
const result = await genAI.models.generateContent({
|
||||||
|
model: 'gemini-pro',
|
||||||
|
contents: [{ role: 'user', parts: [{ text: `${executionPrompt}\n${instruction}` }] }],
|
||||||
|
config: {
|
||||||
|
tools: [mcpTool],
|
||||||
|
toolConfig: {
|
||||||
|
functionCallingConfig: { mode: FunctionCallingConfigMode.ANY },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const text = (result as any).text || '';
|
||||||
|
return text.trim();
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
import { genAI, getMcpTool } from '../llm/gemini';
|
||||||
|
import { FunctionCallingConfigMode } from '@google/genai';
|
||||||
|
import { systemPrompt } from '../systemPrompt';
|
||||||
|
import type { Conversation } from '../types';
|
||||||
|
|
||||||
|
export async function generatePlan(conversation: Conversation, cognitionPrompt: string): Promise<string> {
|
||||||
|
if (!genAI) throw new Error('LLM not configured');
|
||||||
|
|
||||||
|
const mcpTool = await getMcpTool();
|
||||||
|
const planExecutionPrompt = `Est\xE1s generando el plan de acci\xF3n. Solo deb\xE9s consultar el MCP para listar sus capacidades disponibles y luego describir qu\xE9 pasos seguir. Si ya no hay tareas, inclu\xED la frase \"respuesta final\".`;
|
||||||
|
|
||||||
|
const context = conversation.messages
|
||||||
|
.map(m => {
|
||||||
|
const sender = conversation.participants.find(p => p.id === m.from)?.name || m.from;
|
||||||
|
const content = m.text || `[${m.type}]`;
|
||||||
|
return `${sender}: ${content}`;
|
||||||
|
})
|
||||||
|
.join('\n');
|
||||||
|
|
||||||
|
const prompt = `${systemPrompt}\nConversation:\n${context}\n\nCognition:\n${cognitionPrompt}\n\n${planExecutionPrompt}\nPlan:`;
|
||||||
|
const result = await genAI.models.generateContent({
|
||||||
|
model: 'gemini-pro',
|
||||||
|
contents: [{ role: 'user', parts: [{ text: prompt }] }],
|
||||||
|
config: {
|
||||||
|
tools: [mcpTool],
|
||||||
|
toolConfig: {
|
||||||
|
functionCallingConfig: { mode: FunctionCallingConfigMode.ANY },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const text = (result as any).text || '';
|
||||||
|
return text.trim();
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,14 +1,34 @@
|
|||||||
export async function iniciarProcesoCognitivo(){
|
import type { Conversation } from '../types';
|
||||||
|
import { generatePlan } from './generatePlan';
|
||||||
|
import { executeTools } from './executeTools';
|
||||||
|
|
||||||
|
interface CognitionArgs {
|
||||||
|
conversation: Conversation;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function iniciarProcesoCognitivo({ conversation }: CognitionArgs) {
|
||||||
try {
|
try {
|
||||||
console.log("Iniciando proceso cognitivo...");
|
console.log('Iniciando proceso cognitivo...');
|
||||||
|
let cognitionPrompt = '';
|
||||||
|
let loopCount = 0;
|
||||||
|
|
||||||
|
while (loopCount < 5) {
|
||||||
|
const plan = await generatePlan(conversation, cognitionPrompt);
|
||||||
|
cognitionPrompt += `\n## Plan\n${plan}\n`;
|
||||||
|
|
||||||
//
|
if (/respuesta final/i.test(plan)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
const toolResult = await executeTools(plan);
|
||||||
|
cognitionPrompt += `\n## Resultado\n${toolResult}\n`;
|
||||||
|
loopCount += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('Proceso cognitivo completado.');
|
||||||
console.log("Proceso cognitivo completado.");
|
return { text: cognitionPrompt };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error al iniciar el proceso cognitivo:", error);
|
console.error('Error al iniciar el proceso cognitivo:', error);
|
||||||
|
return { text: 'Error en el proceso cognitivo' };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ app.post('/', async (req, res) => {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const contents = `${systemPrompt}\nConversation:\n${context}\n`;
|
const contents = `${systemPrompt}\nConversation:\n${context}\n`;
|
||||||
const result = await iniciarProcesoCognitivo({})
|
const result = await iniciarProcesoCognitivo({ conversation })
|
||||||
const reply = (result.text || '').trim();
|
const reply = (result.text || '').trim();
|
||||||
res.json({ reply });
|
res.json({ reply });
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
|
|||||||
Reference in New Issue
Block a user