-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathstructured_tasks.py
More file actions
57 lines (43 loc) · 1.74 KB
/
Copy pathstructured_tasks.py
File metadata and controls
57 lines (43 loc) · 1.74 KB
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
"""Central registry for structured tasks and their JSON schemas."""
from __future__ import annotations
import copy
from dataclasses import dataclass
from typing import Dict, List
@dataclass(frozen=True)
class StructuredTaskDefinition:
"""Describe one structured task that expects schema-constrained JSON output."""
task_name: str
schema_name: str
schema: Dict[str, object]
description: str = ""
_STRUCTURED_TASKS: Dict[str, StructuredTaskDefinition] = {}
def register_structured_task(
*,
task_name: str,
schema_name: str,
schema: Dict[str, object],
description: str = "",
) -> StructuredTaskDefinition:
"""Register a structured task definition."""
definition = StructuredTaskDefinition(
task_name=str(task_name).strip(),
schema_name=str(schema_name).strip(),
schema=copy.deepcopy(dict(schema)),
description=str(description).strip(),
)
_STRUCTURED_TASKS[definition.task_name] = definition
return definition
def get_structured_task(task_name: str) -> StructuredTaskDefinition:
"""Return a structured task definition by task name."""
normalized = str(task_name).strip()
if normalized not in _STRUCTURED_TASKS:
raise KeyError("structured task not found: %s" % normalized)
return StructuredTaskDefinition(
task_name=_STRUCTURED_TASKS[normalized].task_name,
schema_name=_STRUCTURED_TASKS[normalized].schema_name,
schema=copy.deepcopy(_STRUCTURED_TASKS[normalized].schema),
description=_STRUCTURED_TASKS[normalized].description,
)
def list_structured_tasks() -> List[StructuredTaskDefinition]:
"""Return all structured task definitions."""
return [get_structured_task(name) for name in sorted(_STRUCTURED_TASKS)]