-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpydantic_structure_definitions.py
202 lines (151 loc) · 6.75 KB
/
pydantic_structure_definitions.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
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
199
200
201
202
from pydantic_xml.element import SearchMode
from pydantic_xml import BaseXmlModel, element, attr
from typing import Dict, Optional
import copyreg
import pickle
__all__ = [
"generate_xml_classes",
"DynamicPXUnpickler",
"ArticleResponse1XML",
"ArticleResponse1nointXML",
"ArticleResponse2XML",
"ArticleResponse2XMLalt",
"ArticleResponse3XML",
"ArticleResponse4XML",
"ArticleResponse4XMLalt",
"ListofStrXML",
"ListofHistoricalEventXML",
"HistoricalEventXML",
]
DEFAULT_SEARCH_MODE = SearchMode.STRICT
## START: XML schema definitions
class ArticleResponse1XML(BaseXmlModel, tag="article", search_mode=DEFAULT_SEARCH_MODE):
"""Structured article for publication answering a reader's question"""
title: str = element(description="Title of the article")
answer: str = element(
description="Provide a detailed description of historical events to answer the question"
)
number: int = element(description="A number that is most relevant to the question.")
class ArticleResponse1nointXML(
BaseXmlModel, tag="article", search_mode=DEFAULT_SEARCH_MODE
):
"""Structured article for publication answering a reader's question"""
title: str = element(description="Title of the article")
answer: str = element(
description="Provide a detailed description of historical events to answer the question"
)
number: str = element(description="A number that is most relevant to the question.")
# Lists of simple types
class ArticleResponse2XML(BaseXmlModel, tag="article", search_mode=DEFAULT_SEARCH_MODE):
"""Structured article for publication answering a reader's question"""
title: str = element(description="Title of the article")
answer: str = element(description="Answer the writer's question")
further_questions: list[str] = element(
tag="further_question",
description="A list of related questions that may be of interest to the readers.",
)
class ListofStrXML(BaseXmlModel):
"""A list of related questions of interest to the readers"""
further_questions: list[str] = element(
tag="further_question",
description="A related question of interest to readers",
)
# Lists of simple types (encapsulated list)
class ArticleResponse2XMLalt(
BaseXmlModel, tag="article", search_mode=DEFAULT_SEARCH_MODE
):
"""Structured article for publication answering a reader's question"""
title: str = element(description="Title of the article")
answer: str = element(description="Answer the writer's question")
further_questions: ListofStrXML = element(
tag="further_questions",
description="A list of related questions of interest to the readers",
)
# Nested types
class HistoricalEventXML(BaseXmlModel, search_mode=DEFAULT_SEARCH_MODE):
"""The year and explanation of a historical event."""
year: str = element(description="The year of the historical event")
event: str = element(
description="A clear and concise explanation of what happened in this event"
)
class ArticleResponse3XML(BaseXmlModel, tag="article", search_mode=DEFAULT_SEARCH_MODE):
"""Structured article for publication answering a reader's question"""
title: str = element(description="[Title of the article]")
historical_event_1: HistoricalEventXML = element(
description="A first historical event relevant to the question"
)
historical_event_2: HistoricalEventXML = element(
description="A second historical event relevant to the question"
)
# Lists of custom types
class ArticleResponse4XML(BaseXmlModel, tag="article", search_mode=DEFAULT_SEARCH_MODE):
"""Structured article for publication answering a reader's question"""
title: str = element(description="Title of the article")
historical_timeline: list[HistoricalEventXML] = element(
description="A list of historical events relevant to the question"
)
class ListofHistoricalEventXML(BaseXmlModel):
"""A list of historical events relevant to the question"""
historical_event: list[HistoricalEventXML] = element(
tag="historical_event",
description="A relevant historical event",
)
# Lists of custom types (encapsulated list)
class ArticleResponse4XMLalt(
BaseXmlModel, tag="article", search_mode=DEFAULT_SEARCH_MODE
):
"""Structured article for publication answering a reader's question"""
title: str = element(description="Title of the article")
historical_timeline: ListofHistoricalEventXML = element(
description="A list of historical events relevant to the question"
)
## END: Experiment schema definitions
class DynamicPXUnpickler(pickle.Unpickler):
def __init__(self, *args, **kwargs):
self._search_mode = kwargs.pop("search_mode", None)
self._classes = generate_xml_classes(self._search_mode)
return super().__init__(*args, **kwargs)
def find_class(self, module, name):
if module == "pydantic_structure_definitions":
return self._classes.get(name, globals().get(name))
return super().find_class(module, name)
def generate_xml_classes(search_mode: Optional[str] = None):
# Create new class variants with specified search_mode
class ArticleResponse1XML_Dynamic(ArticleResponse1XML, search_mode=search_mode):
pass
class ArticleResponse1nointXML_Dynamic(
ArticleResponse1nointXML, search_mode=search_mode
):
pass
class ArticleResponse2XML_Dynamic(ArticleResponse2XML, search_mode=search_mode):
pass
class ArticleResponse3XML_Dynamic(ArticleResponse3XML, search_mode=search_mode):
pass
class ArticleResponse4XML_Dynamic(ArticleResponse4XML, search_mode=search_mode):
pass
class ArticleResponse2XMLalt_Dynamic(
ArticleResponse2XMLalt, search_mode=search_mode
):
pass
class ArticleResponse4XMLalt_Dynamic(
ArticleResponse4XMLalt, search_mode=search_mode
):
pass
class HistoricalEventXML_Dynamic(HistoricalEventXML, search_mode=search_mode):
pass
classes = {}
for dname, value in locals().items():
if dname.endswith("Dynamic"):
new_name = dname.removesuffix("_Dynamic")
value.__name__ = new_name # Need to update name as it's used in code to identify the class
classes[new_name] = value
return classes
class DynamicPXUnpickler(pickle.Unpickler):
def __init__(self, *args, **kwargs):
self._search_mode = kwargs.pop("search_mode", None)
super().__init__(*args, **kwargs)
self._classes = generate_xml_classes(self._search_mode)
def find_class(self, module, name):
if module == "pydantic_structure_definitions":
return self._classes.get(name, globals().get(name))
return super().find_class(module, name)