qwen_agent/promptfoo/csv_to_yaml.py
2025-12-09 20:17:21 +08:00

46 lines
1.5 KiB
Python

#!/usr/bin/env python3
import csv
import yaml
import sys
def csv_to_yaml(csv_file, yaml_file):
"""Convert CSV file to YAML format for promptfoo tests"""
tests = []
with open(csv_file, 'r', encoding='utf-8-sig') as f:
reader = csv.DictReader(f)
for row in reader:
if row['question']:
test_case = {
'vars': {
'question': row['question'].strip(),
'use_history': True if row['use_history'] == "1" else False,
},
'assert':[]
}
if row['regex'] and row['regex'].strip():
test_case['assert'].append({
'type': 'regex',
'value': row['regex'].strip()
})
# Add llm-rubric if present
if row['llm-rubric'] and row['llm-rubric'].strip():
test_case['assert'].append({
'type': 'llm-rubric',
'value': row['llm-rubric'].strip()
})
tests.append(test_case)
with open(yaml_file, 'w', encoding='utf-8') as f:
yaml.dump(tests, f, default_flow_style=False, allow_unicode=True, sort_keys=False)
print(f"Converted {len(tests)} test cases from {csv_file} to {yaml_file}")
if __name__ == '__main__':
csv_to_yaml("novare/novare.csv", "novare/tests.yaml")