|
| 1 | +import { LANGUAGE_PREFERENCE_PROMPT, createPreprocessPrompt, createAnalysisPrompt, createReasonPrompt } from './prompt'; |
| 2 | + |
| 3 | +import { LanguageModel } from '@mastra/core'; |
| 4 | +import { createScorer } from '@mastra/core/scores'; |
| 5 | +import { z } from 'zod'; |
| 6 | + |
| 7 | +export function createLanguagePreferenceScorer({ |
| 8 | + model, |
| 9 | +}: { |
| 10 | + model: LanguageModel; |
| 11 | +}) { |
| 12 | + return createScorer({ |
| 13 | + name: 'Language Preference Compliance', |
| 14 | + description: 'Evaluates if the web automation agent changes website language to match participant language preferences', |
| 15 | + judge: { |
| 16 | + model, |
| 17 | + instructions: LANGUAGE_PREFERENCE_PROMPT |
| 18 | + } |
| 19 | + }) |
| 20 | + .preprocess({ |
| 21 | + description: 'Extract language preferences and actions from the conversation', |
| 22 | + outputSchema: z.object({ |
| 23 | + participantLanguage: z.string().nullable(), |
| 24 | + languageChangeActions: z.array(z.string()), |
| 25 | + websiteLanguageSet: z.boolean(), |
| 26 | + targetLanguage: z.string().nullable() |
| 27 | + }), |
| 28 | + createPrompt: ({ run }) => { |
| 29 | + // For web automation agent, the output contains the agent's actions and reasoning |
| 30 | + const agentOutput = Array.isArray(run.output) ? |
| 31 | + run.output.map(msg => msg.content).join('\n') : |
| 32 | + run.output?.text || run.output || ''; |
| 33 | + |
| 34 | + const userInput = Array.isArray(run.input) ? |
| 35 | + run.input.map(msg => msg.content).join('\n') : |
| 36 | + run.input?.text || run.input || ''; |
| 37 | + |
| 38 | + return createPreprocessPrompt({ userInput, agentOutput }); |
| 39 | + }, |
| 40 | + }) |
| 41 | + .analyze({ |
| 42 | + description: 'Evaluate language preference compliance', |
| 43 | + outputSchema: z.object({ |
| 44 | + compliance: z.enum(['excellent', 'good', 'partial', 'poor', 'no_preference']), |
| 45 | + languageMatch: z.boolean(), |
| 46 | + actionsTaken: z.boolean(), |
| 47 | + confidence: z.number().min(0).max(1), |
| 48 | + }), |
| 49 | + createPrompt: ({ run, results }) => { |
| 50 | + const { participantLanguage, languageChangeActions, websiteLanguageSet, targetLanguage } = results.preprocessStepResult; |
| 51 | + |
| 52 | + return createAnalysisPrompt({ |
| 53 | + participantLanguage, |
| 54 | + languageChangeActions, |
| 55 | + websiteLanguageSet, |
| 56 | + targetLanguage |
| 57 | + }); |
| 58 | + }, |
| 59 | + }) |
| 60 | + .generateScore(({ results }) => { |
| 61 | + const { compliance, confidence } = results.analyzeStepResult; |
| 62 | + |
| 63 | + // Convert compliance level to numerical score |
| 64 | + const complianceScores = { |
| 65 | + 'excellent': 1.0, |
| 66 | + 'good': 0.8, |
| 67 | + 'partial': 0.5, |
| 68 | + 'poor': 0.2, |
| 69 | + 'no_preference': 1.0 // No penalty if no preference was specified |
| 70 | + }; |
| 71 | + |
| 72 | + const baseScore = complianceScores[compliance] || 0; |
| 73 | + return baseScore * confidence; |
| 74 | + }) |
| 75 | + .generateReason({ |
| 76 | + description: 'Generate a reason for the language preference compliance score', |
| 77 | + createPrompt: ({ results, score }) => { |
| 78 | + const { compliance, languageMatch, actionsTaken } = results.analyzeStepResult; |
| 79 | + const { participantLanguage, targetLanguage } = results.preprocessStepResult; |
| 80 | + |
| 81 | + return createReasonPrompt({ |
| 82 | + score, |
| 83 | + compliance, |
| 84 | + languageMatch, |
| 85 | + actionsTaken, |
| 86 | + participantLanguage, |
| 87 | + targetLanguage |
| 88 | + }); |
| 89 | + }, |
| 90 | + }); |
| 91 | +} |
0 commit comments