https://promptfoo.dev/ logo
Map test case variables to corresponding values
# ❓|questions
p
I have a test file in JSON, in the variable "weekdays", I want to map each day with each corresponding value(1 to 7) for the {{day_index}} in the assert value. For example, if the variable is "tuesday", the asserted value will be "interval=1w download=true | tuesday=count(_time[day]=2) | top(file_type)" Could anyone please take a look. Thank you. Here is the test:
Copy code
[
  {
      “description”:“Query downloaded files on specific day?“,
      “vars”:{
         “prompt”:[
            “How many files were downloaded on {{weekdays}}?“,
            “Show me the file types downloaded every {{weekdays}}.”
         ],
         “weekdays”:[
            “sunday”,
            “monday”,
            “tuesday”,
            “wednesday”,
            “thursday”,
            “friday”,
            “saturday”
         ]
      },
      “assert”:[
         {
            “type”:“contains-any”,
            “value”:[
               “interval=1w download=true | {{weekdays}}=count(_time[day]={{day_index}}) | top(file_type)”
            ]
         }
      ]
   }
]
m
this is a great use case for transformVars
Copy code
defaultTest:
  options:
    transformVars: |
      // Map weekdays to their corresponding numeric indices (1-7)
      // Note: In many systems, Sunday is 0, Monday is 1, etc.
      // But according to the user's requirement, we're using Sunday as 1, Monday as 2, etc.
      const weekdayMap = {
        'sunday': 1,
        'monday': 2,
        'tuesday': 3,
        'wednesday': 4,
        'thursday': 5,
        'friday': 6,
        'saturday': 7
      };
      
      // In promptfoo, when iterating through an array variable like 'weekdays',
      // each iteration will have a single weekday value in 'vars.weekdays'
      if (vars.weekdays && typeof vars.weekdays === 'string') {
        return { ...vars, day_index: weekdayMap[vars.weekdays.toLowerCase()] || '' };
      }
      
      return vars;
https://www.promptfoo.dev/docs/configuration/guide/#transforming-input-variables Note that this also supports file reference: file://path_to_transform.js:func_name which is my preferred way of writing it. You can also use a javascript or python test case loader and do the transform there. See https://www.promptfoo.dev/docs/configuration/parameters/#import-from-javascript-or-typescript Let us know if you get stuck or need more help! Happy to jump on a call.
p
Thanks a lot for your solution, but seem like the vars.weekdays can not be assigned properly into the weekdayMap[]. I tried your solution but it doesn’t has day_index in the test result, also tried different ways, but it only returned “none” at day_index. But when I tried with weekdayMap[“tuesday”], it returned the index properly. Here is mine: transformVars: | const weekdayMap = { "sunday": "1", "monday": "2", "tuesday": "3", "wednesday": "4", "thursday": "5", "friday": "6", "saturday": "7" }; return { ...vars, day_index: weekdayMap["tuesday"] || 'none' };
I think something went wrong with the vars.weekdays, with this way it did not work either: let/const weekday = vars.weekdays day_index: weekdayMap[weekday]
m
@pgduc1511 Could you send me a minimal config example that reproduces your issue? Feel free to email it to michael@promptfoo.dev and I’ll take a closer look. Thanks!
p
Sorry, I was too busy to get back to you on this one. I understand how the transformVars works now. I appreciated your support, and thank you for this open source.