File size: 5,409 Bytes
9ec8a57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import json

import google.generativeai as genai


PREREQUISITE_PROMPT = """\
あなたは採点者です。

問題, 採点基準, 回答 が与えられます。
回答を1,2,3,4,5の5段階で採点し、数字のみを出力してください。

# 採点基準
基本的な採点基準
- 1点: 誤っている、 指示に従えていない
- 2点: 誤っているが、方向性は合っている
- 3点: 部分的に誤っている、 部分的に合っている
- 4点: 合っている
- 5点: 役に立つ

基本的な減点項目
- 不自然な日本語: -1点
- 部分的に事実と異なる内容を述べている: -1点
"""


def evaluation_prompt(
    input: str, output: str, eval_aspect: str | None, target: str | None
) -> str:
    return f"""\
回答を1,2,3,4,5の5段階で採点し、数字のみを出力してください。

# 問題: {input}

{f"# 正解例: {target}" if target is not None else ""}

{f"# 採点基準: {eval_aspect}" if eval_aspect is not None else ""}

# 回答: {output}
"""


def evaluate(results: list[dict], api_key=str, batch_size: int = 10) -> list[dict]:
    genai.configure(api_key=api_key)

    model = genai.GenerativeModel("gemini-1.5-pro-latest")

    evaluations = []
    for i in range(0, len(results), batch_size):
        batch_results = results[i : i + batch_size]

        prompts = [
            evaluation_prompt(
                result["input"],
                result["output"],
                result.get("eval_aspect"),
                result.get("target"),
            )
            for result in batch_results
        ]

        response = model.generate_content(
            [PREREQUISITE_PROMPT] + prompts,
            generation_config=genai.GenerationConfig(
                response_mime_type="application/json", response_schema=list[int]
            ),
        )
        scores = json.loads(response.parts[0].text)

        for result, score in zip(batch_results, scores):
            evaluations.append(
                {
                    **result,
                    "score": score,
                }
            )

    return evaluations


def report(tasks: list[dict]) -> str:
    return (
        """\
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>レポート</title>
    <style>
        body {
            background-color: #f8f9fa;
        }
        .container {
            width: 80%; /* 可変幅 */
            margin: 20px auto;
            background-color: #ffffff;
            border-radius: 8px;
        }
        .divider {
            position: relative;
            padding: 16px 0;
            align-items: center;
            justify-content: center;
        }
        .divider .line {
            height: 1px;
            background-color: #ddd;
        }
        .divider .taskId {
            position: absolute;
            margin: -8px;
            left: 50%;
            transform: translateX(-50%);
            padding: 0 10px;
            font-size: 14px;
            font-weight: 900;
            text-align: center;
            border: 1px solid #ddd;
            border-radius: 9999px;
            background-color: #ffffff;
            white-space: nowrap;
        }
        .message {
            padding: 8px;
        }
        .content {
            font-size: 14px;
            font-weight: 400;
        }
        .from {
            font-size: 14px;
            font-weight: 900;
        }
    </style>
</head>
<body>
    <div class="container" id="container"></div>
    <script>
        const messages = """
        + str(tasks)
        + """;

        // taskId: number
        const createDivider = (taskId) => {
            const divider = document.createElement('div');
            divider.classList.add('divider');

            const line = document.createElement('div');
            line.classList.add('line');

            const taskIdLabel = document.createElement('div');
            taskIdLabel.classList.add('taskId');
            taskIdLabel.textContent = `Task ${taskId}`;

            divider.appendChild(line);
            divider.appendChild(taskIdLabel);

            return divider;
        };

        // task: HTMLDivElement, from: 'input' | 'output' | str, text: string
        // return: HTMLDivElement
        const createMessage = (text, name) => {
            const message = document.createElement('div');
            message.classList.add('message');

            const from = document.createElement('div');
            from.classList.add('from');
            from.textContent = name;

            const content = document.createElement('div');
            content.classList.add('content');
            content.textContent = text;

            message.appendChild(from);
            message.appendChild(content);

            return message;
        };

        const container = document.getElementById('container');

        messages.forEach((message) => {
            const task = document.createElement('div');
            task.classList.add('task');

            task.appendChild(createDivider(message.task_id));
            task.appendChild(createMessage(message.input, 'input'));
            task.appendChild(createMessage(message.output, 'output'));

            container.appendChild(task);
        });
    </script>
</body>
</html>
"""
    )