leejunyoung5819handmade-dataset-creator

공개

고품질 LLM 데이터셋 수제작을 위한 생산성 도구

109줄4.7 KB
1use handmade_dataset_creator::model::Entry;
2use handmade_dataset_creator::tools::*;
3use serde_json::{Value, json};
4
5fn weather() -> Value {
6 json!({"type":"function","function":{"name":"get_weather","description":"날씨 조회","parameters":{
7 "type":"object",
8 "properties":{
9 "city":{"type":"string","description":"도시"},
10 "unit":{"type":"string","enum":["c","f"]},
11 "days":{"type":"integer"},
12 "tags":{"type":"array","items":{"type":"string"}},
13 "filter":{"type":"object","properties":{"x":{"type":"number"}}}
14 },
15 "required":["city","filter"],
16 "additionalProperties":false
17 },"strict":true}})
18}
19
20#[test]
21fn form_roundtrip_preserves_schema() {
22 let def = ToolDef::from_value(&weather()).unwrap();
23 let kinds: Vec<ParamType> = def.params.iter().map(|p| p.ty).collect();
24 assert_eq!(
25 kinds,
26 [ParamType::String, ParamType::String, ParamType::Integer, ParamType::StringArray, ParamType::Raw]
27 );
28 assert_eq!(def.params[1].enum_values, ["c", "f"]);
29 assert!(def.params[0].required && !def.params[1].required && def.params[4].required);
30 assert_eq!(def.to_value(), weather());
31}
32
33#[test]
34fn accepts_anthropic_format() {
35 let v = json!({"name":"search","description":"d","input_schema":{"type":"object","properties":{"q":{"type":"string"}},"required":["q"]}});
36 let def = ToolDef::from_value(&v).unwrap();
37 assert_eq!(def.name, "search");
38 assert_eq!(def.to_value()["function"]["parameters"]["required"][0], "q");
39}
40
41#[test]
42fn skeleton_and_validation() {
43 let def = ToolDef::from_value(&weather()).unwrap();
44 let sk: Value = serde_json::from_str(&def.skeleton_args()).unwrap();
45 assert_eq!(sk, json!({"city":"", "filter":{}}));
46 assert!(def.validate_args(&def.skeleton_args()).is_empty());
47
48 let w = def.validate_args(r#"{"unit":"k","days":1.5,"oops":1}"#);
49 assert!(w.iter().any(|s| s.contains("'city'")), "{w:?}");
50 assert!(w.iter().any(|s| s.contains("'unit'")), "{w:?}");
51 assert!(w.iter().any(|s| s.contains("'days'")), "{w:?}");
52 assert!(w.iter().any(|s| s.contains("'oops'")), "{w:?}");
53
54 // 빠진 필수 인자만 채우고 기존 값은 유지
55 let filled: Value = serde_json::from_str(&fill_missing_required(&def, r#"{"city":"Seoul"}"#).unwrap()).unwrap();
56 assert_eq!(filled, json!({"city":"Seoul","filter":{}}));
57}
58
59#[test]
60fn type_switch_keeps_content() {
61 let mut p = Param { name: "a".into(), description: "설명".into(), ..Default::default() };
62 p.set_type(ParamType::Raw);
63 assert!(p.raw.contains("설명"));
64 p.set_type(ParamType::Integer);
65 assert_eq!(p.description, "설명");
66}
67
68#[test]
69fn collect_from_dataset_dedupes() {
70 let tools = serde_json::to_string(&json!([weather()])).unwrap();
71 let line1 = format!(r#"{{"messages":[{{"role":"tools","content":{tools}}}]}}"#);
72 let line2 = format!(r#"{{"messages":[{{"role":"tools","content":{}}}]}}"#, serde_json::to_string(&tools).unwrap());
73 let line3 = r#"{"messages":[{"role":"user","content":"hi"}],"tools":[{"type":"function","function":{"name":"other"}}]}"#;
74 let entries = [Entry::from_raw(line1), Entry::from_raw(line2), Entry::from_raw(line3.into())];
75 let found = collect_tools(&entries);
76 let names: Vec<&str> = found.iter().map(|t| t.name.as_str()).collect();
77 assert_eq!(names, ["get_weather", "other"]);
78
79 let mut lib = ToolLibrary::default();
80 assert_eq!(lib.merge(found.clone()), 2);
81 assert_eq!(lib.merge(found), 0);
82 assert!(lib.dirty);
83}
84
85#[test]
86fn form_args_escape_and_order() {
87 let def = ToolDef::from_value(&weather()).unwrap();
88 let mut obj = args_object("").unwrap();
89 // 폼에서 입력한 순서와 상관없이 정의 순서로, 따옴표/줄바꿈은 자동 이스케이프
90 obj.insert("extra".into(), json!(1));
91 obj.insert("unit".into(), json!("c"));
92 obj.insert("city".into(), json!("서울 \"강남구\"\n2줄"));
93 let text = args_to_text(&def, &obj);
94 let back: Value = serde_json::from_str(&text).unwrap();
95 assert_eq!(back["city"], "서울 \"강남구\"\n2줄");
96 let keys: Vec<&String> = back.as_object().unwrap().keys().collect();
97 assert_eq!(keys, ["city", "unit", "extra"]);
98 assert!(args_object("[1]").is_err());
99}
100
101#[test]
102fn array_lines_roundtrip() {
103 let v = lines_to_array(ParamType::StringArray, "a \"q\"\n\nb\n").unwrap();
104 assert_eq!(v, json!(["a \"q\"", "b"]));
105 assert_eq!(array_to_lines(Some(&v)), "a \"q\"\nb");
106 assert_eq!(lines_to_array(ParamType::IntegerArray, "1\n 2 ").unwrap(), json!([1, 2]));
107 assert!(lines_to_array(ParamType::IntegerArray, "1.5").is_err());
108 assert_eq!(lines_to_array(ParamType::BooleanArray, "true\nfalse").unwrap(), json!([true, false]));
109}