@nomyx/assistant
Version:
A powerful assistant library and cli for your AI projects. works with Vertex AI (Claude and Gemini)
116 lines (103 loc) • 3.37 kB
JavaScript
// Simplified PromptAction class
class PromptAction {
constructor({ name, schema, systemContent, userContent, routing }) {
this.name = name;
this.schema = schema;
this.systemContent = systemContent;
this.userContent = userContent;
this.routing = routing;
}
}
// Simplified testPromptRouting function
function testPromptRouting(promptAction, params, context) {
let result = `Routing Test for ${promptAction.name}:\n`;
if (promptAction.routing && promptAction.routing.length > 0) {
for (const route of promptAction.routing) {
const conditionMet = evaluateCondition(route.condition, { params, ...context.state });
result += `Condition "${route.condition}": ${conditionMet ? 'Met' : 'Not Met'}\n`;
if (conditionMet) {
result += `Routing to: ${typeof route.prompt === 'string' ? route.prompt : route.prompt.name}\n`;
break;
}
}
} else {
result += 'No routing defined. Executing current prompt.\n';
}
return result;
}
// Simple condition evaluator
function evaluateCondition(condition, context) {
try {
return new Function(...Object.keys(context), `return ${condition}`)(...Object.values(context));
} catch (error) {
console.error('Error evaluating condition:', error);
return false;
}
}
// Create a sample PromptAction with routing
const sampleSchema = {
input: {
type: 'object',
properties: {
temperature: { type: 'number', description: 'The current temperature' }
},
required: ['temperature']
},
output: {
type: 'object',
properties: {
recommendation: { type: 'string', description: 'A weather-based recommendation' }
}
}
};
const rootPrompt = new PromptAction({
name: 'rootPrompt',
schema: sampleSchema,
systemContent: 'You are a helpful assistant.',
userContent: 'Please provide a response.',
routing: [
{
condition: 'params.temperature > 30',
prompt: new PromptAction({
name: 'hotWeatherPrompt',
schema: sampleSchema,
systemContent: 'You are a weather expert.',
userContent: 'It\'s hot outside. What should we do?'
})
},
{
condition: 'params.temperature <= 30 && params.temperature > 10',
prompt: new PromptAction({
name: 'mildWeatherPrompt',
schema: sampleSchema,
systemContent: 'You are a weather expert.',
userContent: 'The weather is mild. Any activity suggestions?'
})
},
{
condition: 'params.temperature <= 10',
prompt: new PromptAction({
name: 'coldWeatherPrompt',
schema: sampleSchema,
systemContent: 'You are a weather expert.',
userContent: 'It\'s cold outside. How should we prepare?'
})
}
]
});
// Test function
function runRoutingTest() {
const context = {
state: {},
};
console.log('Test 1: Hot weather');
console.log(testPromptRouting(rootPrompt, { temperature: 35 }, context));
console.log('\nTest 2: Mild weather');
console.log(testPromptRouting(rootPrompt, { temperature: 20 }, context));
console.log('\nTest 3: Cold weather');
console.log(testPromptRouting(rootPrompt, { temperature: 5 }, context));
console.log('\nTest 4: Edge case (exactly 30 degrees)');
console.log(testPromptRouting(rootPrompt, { temperature: 30 }, context));
}
// Run the test
runRoutingTest();