-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathtest_serialization.py
179 lines (167 loc) · 5.68 KB
/
test_serialization.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
import pytest
import os
@pytest.mark.vcr
def test_agent_is_serializable():
agent_data = {
"environment": {
"type": "AsyncKafkaEnvironment",
"kafka_bootstrap_servers": "",
"kafka_input_topic": "",
"kafka_output_topic": "",
"timeout_ms": 1,
},
"skills": [
{
"type": "ClassificationSkill",
"name": "text_classifier",
"instructions": "Always return the answer 'Feature Lack'.",
"input_template": "{text}",
"output_template": "{output}",
"labels": [
"Feature Lack",
"Price",
"Integration Issues",
"Usability Concerns",
"Competitor Advantage",
],
}
],
"runtimes": {
"default": {
"type": "AsyncLiteLLMChatRuntime",
"model": "gpt-4o-mini",
"max_tokens": 200,
"temperature": 0,
"batch_size": 100,
"timeout": 10,
"verbose": False,
}
},
}
from adala.agents import Agent # type: ignore
agent = Agent(**agent_data)
assert agent.skills["text_classifier"].response_model is not None
serialized_agent = agent.model_dump()
assert serialized_agent == {
"environment": {
"type": "AsyncKafkaEnvironment",
"kafka_bootstrap_servers": "",
"kafka_input_topic": "",
"kafka_output_topic": "",
"timeout_ms": 1,
"consumer": None,
"producer": None,
},
"skills": {
"skills": {
"text_classifier": {
"type": "ClassificationSkill",
"name": "text_classifier",
"instructions": "Always return the answer 'Feature Lack'.",
"input_template": "{text}",
"output_template": "{output}",
"description": "",
"field_schema": {
"output": {
"type": "string",
"description": "The classification label",
"enum": [
"Feature Lack",
"Price",
"Integration Issues",
"Usability Concerns",
"Competitor Advantage",
],
}
},
"instructions_first": True,
"frozen": False,
"response_model": None,
"labels": [
"Feature Lack",
"Price",
"Integration Issues",
"Usability Concerns",
"Competitor Advantage",
],
}
},
"skill_sequence": ["text_classifier"],
},
"memory": None,
"runtimes": {
"default": {
"type": "AsyncLiteLLMChatRuntime",
"verbose": False,
"batch_size": 100,
"concurrency": 1,
"instructor_mode": "tool_call",
"model": "gpt-4o-mini",
"max_tokens": 200,
"temperature": 0.0,
"seed": 47,
"timeout": 10,
"provider": None,
"api_key": None,
"base_url": None,
}
},
"default_runtime": "default",
"teacher_runtimes": {"default": None},
"default_teacher_runtime": "default",
}
agent.model_dump_json()
import pickle
pickled_agent = pickle.dumps(agent)
unpickled_agent = pickle.loads(pickled_agent)
assert unpickled_agent.skills["text_classifier"].response_model is not None
assert serialized_agent == unpickled_agent.model_dump()
@pytest.mark.vcr
def test_agent_is_pickleable():
from adala.agents import Agent
import pickle
agent_json = {
"runtimes": {
"default": {
"type": "AsyncLiteLLMChatRuntime",
"model": "gpt-4o-mini",
"api_key": os.getenv("OPENAI_API_KEY"),
"max_tokens": 200,
"temperature": 0,
"batch_size": 100,
"timeout": 10,
"verbose": False,
}
},
"environment": {
"type": "AsyncKafkaEnvironment",
"kafka_bootstrap_servers": "localhost:9092",
"kafka_input_topic": "input_topic",
"kafka_output_topic": "output_topic",
"timeout_ms": 1000,
},
"skills": [
{
"name": "label_studio_skill",
"type": "LabelStudioSkill",
"input_template": "Classify sentiment of the input text: {input}",
"label_config": """
<View>
<Text name="text" value="$text" />
<Choices name="output" toName="text">
<Choice value="positive" />
<Choice value="negative" />
<Choice value="neutral" />
</Choices>
</View>
""",
}
],
}
agent = Agent(**agent_json)
agent_pickle = pickle.dumps(agent)
agent_roundtrip = pickle.loads(agent_pickle)
assert (
agent_json["skills"][0]["input_template"]
== agent_roundtrip.skills["label_studio_skill"].input_template
)