-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathtest_input_format.rs
More file actions
280 lines (234 loc) · 7.65 KB
/
test_input_format.rs
File metadata and controls
280 lines (234 loc) · 7.65 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
use dspy_rs::bamltype::compat::ToBamlValue;
use dspy_rs::{BamlType, BamlTypeTrait, BamlValue, ChatAdapter, Signature};
#[derive(Clone, Debug)]
#[BamlType]
struct Document {
text: String,
}
#[derive(Signature, Clone, Debug)]
/// Provide an answer with supporting context.
struct FormatSig {
#[input]
question: String,
#[input]
#[format("yaml")]
context: Vec<Document>,
#[output]
answer: String,
}
#[derive(Signature, Clone, Debug)]
/// Provide an answer with supporting context in JSON.
struct FormatJsonSig {
#[input]
question: String,
#[input]
#[format("json")]
context: Vec<Document>,
#[output]
answer: String,
}
#[derive(Signature, Clone, Debug)]
/// Provide an answer with supporting context in TOON.
struct FormatToonSig {
#[input]
question: String,
#[input]
#[format("toon")]
context: Vec<Document>,
#[output]
answer: String,
}
#[derive(Signature, Clone, Debug)]
/// Provide an answer with supporting context using the default format.
struct DefaultFormatSig {
#[input]
question: String,
#[input]
context: Vec<Document>,
#[output]
answer: String,
}
#[derive(Signature, Clone, Debug)]
/// Render a context field using Jinja.
struct RenderJinjaSig {
#[input]
question: String,
#[input]
#[alias("ctx")]
#[render(
jinja = "{{ this.text }} | {{ input.question }} | {{ input.ctx.text }} | {{ input.context.text }} | {{ field.name }} | {{ field.rust_name }}"
)]
context: Document,
#[output]
answer: String,
}
#[derive(Signature, Clone, Debug)]
/// Render with strict undefined vars.
struct RenderJinjaStrictSig {
#[input]
#[render(jinja = "{{ missing_var }}")]
question: String,
#[output]
answer: String,
}
#[derive(Signature, Clone, Debug)]
/// Render using field metadata and vars context.
struct RenderJinjaFieldMetaSig {
#[input]
#[alias("ctx")]
#[render(
jinja = "{{ field.name }}|{{ field.rust_name }}|{{ field.type }}|{{ vars is defined }}"
)]
context: Document,
#[output]
answer: String,
}
fn extract_field(message: &str, field_name: &str) -> String {
let start_marker = format!("[[ ## {field_name} ## ]]");
let start_pos = message
.find(&start_marker)
.unwrap_or_else(|| panic!("missing marker: {field_name}"));
let after_marker = start_pos + start_marker.len();
let remaining = &message[after_marker..];
let end_pos = remaining.find("[[ ##").unwrap_or(remaining.len());
remaining[..end_pos].trim().to_string()
}
fn extract_baml_field<'a>(value: &'a BamlValue, field_name: &str) -> &'a BamlValue {
match value {
BamlValue::Class(_, fields) | BamlValue::Map(fields) => fields
.get(field_name)
.unwrap_or_else(|| panic!("missing field: {field_name}")),
other => panic!("unexpected input value: {other:?}"),
}
}
#[test]
fn typed_input_format_yaml_renders_field_names() {
let adapter = ChatAdapter;
let input = FormatSigInput {
question: "What is YAML?".to_string(),
context: vec![Document {
text: "Hello".to_string(),
}],
};
let message = adapter.format_user_message_typed::<FormatSig>(&input);
let context_value = extract_field(&message, "context");
let question_value = extract_field(&message, "question");
assert!(context_value.contains("text: Hello"));
assert_eq!(question_value, "What is YAML?");
}
#[test]
fn typed_input_format_json_is_parsable() {
let adapter = ChatAdapter;
let input = FormatJsonSigInput {
question: "What is JSON?".to_string(),
context: vec![Document {
text: "Hello".to_string(),
}],
};
let message = adapter.format_user_message_typed::<FormatJsonSig>(&input);
let context_value = extract_field(&message, "context");
let parsed: serde_json::Value = serde_json::from_str(&context_value).expect("valid JSON");
let first = parsed
.as_array()
.and_then(|items| items.first())
.and_then(|value| value.as_object())
.expect("expected array with object");
assert_eq!(first.get("text").and_then(|v| v.as_str()), Some("Hello"));
}
#[test]
fn typed_input_format_toon_matches_formatter() {
let adapter = ChatAdapter;
let input = FormatToonSigInput {
question: "What is TOON?".to_string(),
context: vec![Document {
text: "Hello".to_string(),
}],
};
let message = adapter.format_user_message_typed::<FormatToonSig>(&input);
let context_value = extract_field(&message, "context");
let baml_value = input.to_baml_value();
let context_baml = extract_baml_field(&baml_value, "context");
let output_format = <FormatToonSigInput as BamlTypeTrait>::baml_output_format();
let expected = dspy_rs::bamltype::internal_baml_jinja::format_baml_value(
context_baml,
output_format,
"toon",
)
.expect("formatting should succeed");
assert_eq!(context_value, expected);
}
#[test]
fn typed_input_default_string_is_raw() {
let adapter = ChatAdapter;
let input = DefaultFormatSigInput {
question: "Raw string".to_string(),
context: vec![Document {
text: "Hello".to_string(),
}],
};
let message = adapter.format_user_message_typed::<DefaultFormatSig>(&input);
let question_value = extract_field(&message, "question");
assert_eq!(question_value, "Raw string");
}
#[test]
fn typed_input_default_non_string_is_json() {
let adapter = ChatAdapter;
let input = DefaultFormatSigInput {
question: "Default JSON".to_string(),
context: vec![Document {
text: "Hello".to_string(),
}],
};
let message = adapter.format_user_message_typed::<DefaultFormatSig>(&input);
let context_value = extract_field(&message, "context");
let parsed: serde_json::Value = serde_json::from_str(&context_value).expect("valid JSON");
let first = parsed
.as_array()
.and_then(|items| items.first())
.and_then(|value| value.as_object())
.expect("expected array with object");
assert_eq!(first.get("text").and_then(|v| v.as_str()), Some("Hello"));
}
#[test]
fn typed_input_render_jinja_uses_context_values() {
let adapter = ChatAdapter;
let input = RenderJinjaSigInput {
question: "Question".to_string(),
context: Document {
text: "Hello".to_string(),
},
};
let message = adapter.format_user_message_typed::<RenderJinjaSig>(&input);
let context_value = extract_field(&message, "ctx");
assert_eq!(
context_value,
"Hello | Question | Hello | Hello | ctx | context"
);
}
#[test]
fn typed_input_render_jinja_strict_undefined_returns_error_sentinel() {
let adapter = ChatAdapter;
let input = RenderJinjaStrictSigInput {
question: "Question".to_string(),
};
let message = adapter.format_user_message_typed::<RenderJinjaStrictSig>(&input);
let question_value = extract_field(&message, "question");
assert_eq!(question_value, "<error>");
}
#[test]
fn typed_input_render_jinja_exposes_field_metadata_and_vars() {
let adapter = ChatAdapter;
let input = RenderJinjaFieldMetaSigInput {
context: Document {
text: "Hello".to_string(),
},
};
let message = adapter.format_user_message_typed::<RenderJinjaFieldMetaSig>(&input);
let context_value = extract_field(&message, "ctx");
let parts: Vec<&str> = context_value.split('|').collect();
assert_eq!(parts.len(), 4);
assert_eq!(parts[0], "ctx");
assert_eq!(parts[1], "context");
assert!(parts[2].contains("Document"));
assert_eq!(parts[3].to_ascii_lowercase(), "true");
}