File size: 1,413 Bytes
868e04b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

from pathlib import Path
from typing import Any

import httpx

from harness.adapters import Adapter

PROMPTS = Path(__file__).resolve().parent / "prompts"
MAX_TOKENS = 512
TRACE_LIMIT = 1200


def _trim(text: str, limit: int) -> str:
    return text if len(text) <= limit else text[-limit:]


def repair(
    task: dict[str, Any],
    draft: Any,
    trace: str,
    errors: str,
    *,
    adapter: Adapter,
    relay_url: str,
    ticket: str,
    model: str,
) -> Any:
    system = adapter.render(PROMPTS, "system", task)
    user = adapter.render(
        PROMPTS,
        "repair",
        task,
        draft=adapter.as_text(draft),
        errors=_trim(errors or "no verifier output", 400),
        trace=_trim(trace or "", TRACE_LIMIT),
    )
    body = {
        "model": model,
        "max_tokens": MAX_TOKENS,
        "messages": [{"role": "system", "content": system}, {"role": "user", "content": user}],
    }
    with httpx.Client(timeout=180.0) as http:
        response = http.post(
            relay_url.rstrip("/") + "/v1/chat/completions",
            json=body,
            headers={"x-mt-ticket": ticket},
        )
    if response.status_code != 200:
        return draft
    choices = response.json().get("choices") or [{}]
    text = str((choices[0].get("message") or {}).get("content") or "")
    fixed = adapter.clean(text)
    return fixed or draft