-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig_schema.py
85 lines (64 loc) · 2.26 KB
/
config_schema.py
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
from pydantic import BaseModel, Field, field_validator
from typing import List, Dict
import toml
class DockerConfig(BaseModel):
hostname: str = "simurgh"
cpu_period: int = 100000
cpu_quota: int = 200000
mem_limit: str = "8g"
cap_add: List[str] = ["NET_RAW"]
@classmethod
def make_default(cls):
return DockerConfig()
class PrefilledMessage(BaseModel):
role: str
content: str
@field_validator("content")
@classmethod
def strip_whitespace(cls, content):
return content.strip()
class Elicitation(BaseModel):
terminal_interop_protocol: str
prefilled_messages: List[PrefilledMessage]
@classmethod
def from_toml(cls, toml_content: str):
return cls.model_validate(toml.loads(toml_content))
def to_normalized_toml(self) -> str:
python_dict = self.model_dump(
by_alias=True,
exclude_none=True,
include={"terminal_interop_protocol", "prefilled_messages"},
)
return toml.dumps(python_dict)
class EnvironmentConfig(BaseModel):
docker: DockerConfig = Field(default_factory=DockerConfig.make_default)
files: Dict[str, str]
files_b64: Dict[str, str] = Field(default_factory=dict)
class RulesetConfig(BaseModel):
max_turns: int
flag: str
class TaskConfig(BaseModel):
# increment when adding breaking changes, or implement conversion
name: str
version: str = Field(pattern=r"0\.3")
tags: Dict[str, str] = Field(default_factory=dict)
ruleset: RulesetConfig
environments: Dict[str, EnvironmentConfig]
elicitations: List[Elicitation]
@classmethod
def from_toml(cls, toml_content: str):
return cls.model_validate(toml.loads(toml_content))
@classmethod
def from_config(cls, config_path: str) -> 'TaskConfig':
with open(config_path, "r") as f:
return TaskConfig.from_toml(f.read())
def to_toml(self) -> str:
python_dict = self.model_dump(by_alias=True, exclude_none=True)
return toml.dumps(python_dict)
def to_normalized_toml(self) -> str:
python_dict = self.model_dump(
by_alias=True,
exclude_none=True,
include={"version", "ruleset", "environments"},
)
return toml.dumps(python_dict)