File size: 1,050 Bytes
0d1866c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from __future__ import annotations

import json
import re
from typing import Any


def _stringify(value: Any) -> str:
    if isinstance(value, str):
        return value
    return json.dumps(value, ensure_ascii=True)


def render_template(template: str, values: dict[str, Any]) -> str:
    def replace(match: re.Match[str]) -> str:
        key = match.group(1).strip()
        return _stringify(values.get(key, ""))

    return re.sub(r"\{\{\s*([^}]+)\s*\}\}", replace, template)


def build_messages(card: dict, test_input: dict[str, Any]) -> list[dict[str, str]]:
    prompt = card["prompt"]
    return [
        {"role": "system", "content": prompt["system"]},
        {"role": "user", "content": render_template(prompt["user"], test_input)},
    ]


def build_prompt_text(card: dict, test_input: dict[str, Any]) -> str:
    prompt = card["prompt"]
    user_prompt = render_template(prompt["user"], test_input)
    return (
        f"TASK:\n{prompt['task']}\n\n"
        f"SYSTEM:\n{prompt['system']}\n\n"
        f"USER:\n{user_prompt}\n"
    )