Add ai.improvePrompt() (#29660)
This commit is contained in:
+60
@@ -0,0 +1,60 @@
|
||||
module.exports = {
|
||||
|
||||
|
||||
friendlyName: 'Improve prompt',
|
||||
|
||||
|
||||
description: '',
|
||||
|
||||
|
||||
sideEffects: 'cacheable',
|
||||
|
||||
|
||||
inputs: {
|
||||
|
||||
prompt: { type: 'string', required: true, example: 'Who is running macOS 15?' },
|
||||
cycles: { type: 'number', defaultsTo: 1 }
|
||||
|
||||
},
|
||||
|
||||
|
||||
exits: {
|
||||
|
||||
success: {
|
||||
outputFriendlyName: 'Improved prompt',
|
||||
outputDescription: 'The new and improved prompt.',
|
||||
outputType: 'string',
|
||||
outputExample: 'Who is running macOS 15?',
|
||||
},
|
||||
|
||||
},
|
||||
|
||||
|
||||
fn: async function ({ prompt: originalPrompt, cycles }) {
|
||||
|
||||
let newPrompt = originalPrompt;
|
||||
for (let i=0; i<cycles; i++) {
|
||||
let improverPrompt = 'Given a prompt, improve it to be a more effective prompt optimized for use with an LLM, and particularly this LLM.';
|
||||
improverPrompt += 'Prompt: ```\n';
|
||||
improverPrompt += `${newPrompt}\n`;
|
||||
improverPrompt += '```\n';
|
||||
improverPrompt += '\n';
|
||||
improverPrompt += 'Respond only with the exact text of the prompt.';
|
||||
improverPrompt += '\n';
|
||||
|
||||
newPrompt = await sails.helpers.flow.build(async ()=>{
|
||||
// FUTURE: Add an option to run multiple times.
|
||||
let parsedPromptResponse = await sails.helpers.ai.prompt.with({
|
||||
baseModel: 'o4-mini-2025-04-16',
|
||||
prompt: improverPrompt,
|
||||
});
|
||||
return parsedPromptResponse;
|
||||
}).retry();
|
||||
}//∞
|
||||
|
||||
return newPrompt;
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
Vendored
-5
@@ -27,11 +27,6 @@ module.exports = {
|
||||
author: 'koo',
|
||||
tweet: 'Sails.js is the best JavaScript framework'
|
||||
},
|
||||
{
|
||||
id: 4,
|
||||
author: 'koo',
|
||||
tweet: 'The 4th annual SailsConf is coming up in May in Abuja!'
|
||||
},
|
||||
];
|
||||
|
||||
let topPosts = [];
|
||||
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
module.exports = {
|
||||
|
||||
|
||||
friendlyName: 'Test ai improve prompt',
|
||||
|
||||
|
||||
description: '',
|
||||
|
||||
|
||||
fn: async function () {
|
||||
|
||||
sails.log('Running custom shell script... (`sails run test-ai-improve-prompt`)');
|
||||
|
||||
let prompt1 = (
|
||||
'Given some data and a set of possible choices, decide which choice most accurately classifies the data. Data: ```\n'+
|
||||
JSON.stringify([
|
||||
{
|
||||
id: 1,
|
||||
author: 'mikermcneil',
|
||||
tweet: 'I fed this one stray cat and now I have 20 stray cats coming to my house',
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
author: 'fancydoilies',
|
||||
tweet: 'My cat is named Rory'
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
author: 'koo',
|
||||
tweet: 'Sails.js is the best JavaScript framework'
|
||||
},
|
||||
])+
|
||||
'```\n'+'\n'+'Choices:\n'+
|
||||
' • A social media post that is both (a) VERY interesting and (b) in reasonably good taste\n'+
|
||||
' • Anything else\n'+
|
||||
'\n'+
|
||||
'Decide based on which choice is the most correct for the given data. Respond only with the exact string value for the choice provided.'
|
||||
);
|
||||
sails.log(await ƒ.improvePrompt(prompt1));
|
||||
|
||||
sails.log(await ƒ.improvePrompt(prompt1),2);
|
||||
|
||||
sails.log(await ƒ.improvePrompt(prompt1),5);
|
||||
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
Vendored
-5
@@ -27,11 +27,6 @@ module.exports = {
|
||||
author: 'koo',
|
||||
tweet: 'Sails.js is the best JavaScript framework'
|
||||
},
|
||||
{
|
||||
id: 4,
|
||||
author: 'koo',
|
||||
tweet: 'The 4th annual SailsConf is coming up in May in Abuja!'
|
||||
},
|
||||
];
|
||||
|
||||
let weighedPosts = await ƒ.simultaneouslyForEach(posts, async (post)=>{
|
||||
|
||||
Reference in New Issue
Block a user