From 90a4358ba5ac1110a514af509e7fac8a86617ade Mon Sep 17 00:00:00 2001 From: ModelHub XC Date: Fri, 17 Jul 2026 19:18:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=EF=BC=8C=E7=94=B1ModelHub=20XC=E7=A4=BE=E5=8C=BA=E6=8F=90?= =?UTF-8?q?=E4=BE=9B=E6=A8=A1=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Model: joshelu/qwen3-4b-eventspec-martech-merged Source: Original Platform --- .gitattributes | 36 +++++++++ README.md | 167 +++++++++++++++++++++++++++++++++++++++++ chat_template.jinja | 61 +++++++++++++++ config.json | 71 ++++++++++++++++++ generation_config.json | 13 ++++ model.safetensors | 3 + tokenizer.json | 3 + tokenizer_config.json | 30 ++++++++ 8 files changed, 384 insertions(+) create mode 100644 .gitattributes create mode 100644 README.md create mode 100644 chat_template.jinja create mode 100644 config.json create mode 100644 generation_config.json create mode 100644 model.safetensors create mode 100644 tokenizer.json create mode 100644 tokenizer_config.json diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..52373fe --- /dev/null +++ b/.gitattributes @@ -0,0 +1,36 @@ +*.7z filter=lfs diff=lfs merge=lfs -text +*.arrow filter=lfs diff=lfs merge=lfs -text +*.bin filter=lfs diff=lfs merge=lfs -text +*.bz2 filter=lfs diff=lfs merge=lfs -text +*.ckpt filter=lfs diff=lfs merge=lfs -text +*.ftz filter=lfs diff=lfs merge=lfs -text +*.gz filter=lfs diff=lfs merge=lfs -text +*.h5 filter=lfs diff=lfs merge=lfs -text +*.joblib filter=lfs diff=lfs merge=lfs -text +*.lfs.* filter=lfs diff=lfs merge=lfs -text +*.mlmodel filter=lfs diff=lfs merge=lfs -text +*.model filter=lfs diff=lfs merge=lfs -text +*.msgpack filter=lfs diff=lfs merge=lfs -text +*.npy filter=lfs diff=lfs merge=lfs -text +*.npz filter=lfs diff=lfs merge=lfs -text +*.onnx filter=lfs diff=lfs merge=lfs -text +*.ot filter=lfs diff=lfs merge=lfs -text +*.parquet filter=lfs diff=lfs merge=lfs -text +*.pb filter=lfs diff=lfs merge=lfs -text +*.pickle filter=lfs diff=lfs merge=lfs -text +*.pkl filter=lfs diff=lfs merge=lfs -text +*.pt filter=lfs diff=lfs merge=lfs -text +*.pth filter=lfs diff=lfs merge=lfs -text +*.rar filter=lfs diff=lfs merge=lfs -text +*.safetensors filter=lfs diff=lfs merge=lfs -text +saved_model/**/* filter=lfs diff=lfs merge=lfs -text +*.tar.* filter=lfs diff=lfs merge=lfs -text +*.tar filter=lfs diff=lfs merge=lfs -text +*.tflite filter=lfs diff=lfs merge=lfs -text +*.tgz filter=lfs diff=lfs merge=lfs -text +*.wasm filter=lfs diff=lfs merge=lfs -text +*.xz filter=lfs diff=lfs merge=lfs -text +*.zip filter=lfs diff=lfs merge=lfs -text +*.zst filter=lfs diff=lfs merge=lfs -text +*tfevents* filter=lfs diff=lfs merge=lfs -text +tokenizer.json filter=lfs diff=lfs merge=lfs -text diff --git a/README.md b/README.md new file mode 100644 index 0000000..8b8fb8a --- /dev/null +++ b/README.md @@ -0,0 +1,167 @@ +--- +license: apache-2.0 +base_model: Qwen/Qwen3-4B-Instruct-2507 +tags: +- martech +- analytics +- event-taxonomy +- json +- structured-output +- sft +- lora +library_name: transformers +pipeline_tag: text-generation +--- + +# Qwen3-4B EventSpec — MarTech Event Taxonomy Generator (merged) + +Fine-tuned **Qwen/Qwen3-4B-Instruct-2507** that converts free-form marketing tracking +requests into clean, implementation-ready **analytics event specifications** as strict JSON. + +This repository contains the **fully merged weights** (LoRA adapter merged into the base +model), so it loads like any standard model — no PEFT/adapter step required. + +- **Base model:** [Qwen/Qwen3-4B-Instruct-2507](https://huggingface.co/Qwen/Qwen3-4B-Instruct-2507) +- **LoRA adapter (pre-merge):** [joshelu/qwen3-4b-Instruct-eventspec-martech-sft](https://huggingface.co/joshelu/qwen3-4b-Instruct-eventspec-martech-sft) +- **Method:** Supervised Fine-Tuning (SFT) + LoRA, then merged +- **Task:** marketing tracking request → strict-JSON EventSpec + +## Intended use + +Give the model a plain-English marketing/analytics tracking request; it returns a single +JSON object specifying the events to implement (event names, parameters, triggers, consent +and deduplication requirements, QA criteria, risk flags, etc.). Useful for MarTech / analytics +engineering, GA4 / GTM instrumentation planning, and taxonomy standardization. + +## ⚠️ Prompt format (required for correct output) + +The model was trained with a specific chat format. **You must reproduce it** or output +quality degrades sharply. + +**System prompt (use verbatim):** + +``` +You are EventSpec, an expert MarTech analytics engineer. You convert free-form marketing tracking requests into clean, implementation-ready analytics event specifications. + +Given a marketing tracking request, respond with a SINGLE valid JSON object and nothing else: no prose, no markdown, no code fences. The JSON must be strictly parseable. + +The specification captures: a concise `request_summary`; the `business_goal`; the `tracking_scope` (platforms, page_or_screen, user_action, conversion_type); and a list of `recommended_events`. Each recommended event defines `event_name` (snake_case), `event_description`, `platform`, `event_type`, `required_parameters`, `optional_parameters`, `trigger_condition`, `consent_requirements`, and `deduplication_requirements`. + +Use consistent snake_case event and parameter names, follow analytics best practices (GA4/GTM conventions where relevant), and respect privacy/consent requirements. Output only the JSON object. +``` + +**User message format:** + +``` +Convert this marketing tracking request into a clean analytics event specification. + +Request: + +``` + +## Usage (transformers) + +```python +import torch +from transformers import AutoModelForCausalLM, AutoTokenizer + +repo = "joshelu/qwen3-4b-eventspec-martech-merged" +tok = AutoTokenizer.from_pretrained(repo) +model = AutoModelForCausalLM.from_pretrained(repo, dtype=torch.bfloat16, device_map="auto") + +SYSTEM_PROMPT = ( + "You are EventSpec, an expert MarTech analytics engineer. You convert free-form " + "marketing tracking requests into clean, implementation-ready analytics event " + "specifications.\n\n" + "Given a marketing tracking request, respond with a SINGLE valid JSON object and " + "nothing else: no prose, no markdown, no code fences. The JSON must be strictly " + "parseable.\n\n" + "The specification captures: a concise `request_summary`; the `business_goal`; the " + "`tracking_scope` (platforms, page_or_screen, user_action, conversion_type); and a " + "list of `recommended_events`. Each recommended event defines `event_name` " + "(snake_case), `event_description`, `platform`, `event_type`, `required_parameters`, " + "`optional_parameters`, `trigger_condition`, `consent_requirements`, and " + "`deduplication_requirements`.\n\n" + "Use consistent snake_case event and parameter names, follow analytics best " + "practices (GA4/GTM conventions where relevant), and respect privacy/consent " + "requirements. Output only the JSON object." +) +USER_PREFIX = "Convert this marketing tracking request into a clean analytics event specification." + +request = ("A pharmacy app wants to track refill reminders, refill started, refill submitted, " + "refill ready, and pickup completed, but no medication names or prescription numbers " + "should be sent.") + +messages = [ + {"role": "system", "content": SYSTEM_PROMPT}, + {"role": "user", "content": f"{USER_PREFIX}\n\nRequest:\n{request}"}, +] +prompt = tok.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) +inputs = tok(prompt, return_tensors="pt").to(model.device) + +out = model.generate(**inputs, max_new_tokens=2048, do_sample=False, + pad_token_id=tok.eos_token_id) +print(tok.decode(out[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)) +``` + +## Usage (Inference Endpoints / chat_completion) + +```python +from huggingface_hub import InferenceClient +client = InferenceClient("https://YOUR-ENDPOINT.endpoints.huggingface.cloud", token="hf_...") +resp = client.chat_completion( + messages=[ + {"role": "system", "content": SYSTEM_PROMPT}, + {"role": "user", "content": f"{USER_PREFIX}\n\nRequest:\n{request}"}, + ], + max_tokens=2048, + temperature=0, +) +print(resp.choices[0].message.content) +``` + +## Recommended generation settings + +- `temperature = 0` (greedy) — best for stable, strictly parseable JSON. +- `max_new_tokens >= 2048` — specs are long; a lower limit will truncate the JSON mid-string. +- Parse the output with `json.loads`; retry with a higher token limit if parsing fails. + +## Output schema + +The model produces a single JSON object. Core keys: + +- `request_summary` — one-line summary of the request. +- `business_goal` — the measurement objective. +- `tracking_scope` — `{ platforms, page_or_screen, user_action, conversion_type }`. +- `recommended_events` — list of events, each with `event_name` (snake_case), + `event_description`, `platform`, `event_type`, `required_parameters`, + `optional_parameters`, `trigger_condition`, `consent_requirements`, + `deduplication_requirements`. + +Richer examples in the training data also include `implementation_notes`, `qa_criteria`, +`open_questions`, and `risk_flags`, which the model produces as appropriate. + +## Training + +- **Dataset:** `joshelu/martech-event-taxonomy-mapper-training-data` (private). + - The 100-row `train` split, **minus** every `id` appearing in the `validation` or `test` + split → **80 training rows** (held-out eval stays honest). + - Eval during training used the `validation` split (10 rows). + - Assistant targets are compact, strict JSON (`json.dumps(output, separators=(",", ":"))`), + validated as parseable before training. +- **Method:** SFT + LoRA, then merged. + - LoRA: `r=16`, `alpha=32`, `dropout=0.05`, + target modules `q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj`. + - 3 epochs, effective batch size 4 (per-device 1 × grad-accum 4), lr `2e-4`, + warmup ratio `0.05`, cosine schedule, max length 2048, bf16, gradient checkpointing. + - Hardware: a10g-small (HF Jobs). +- **Metrics (final):** eval loss ≈ `0.320`, eval mean token accuracy ≈ `0.924`. + +## Limitations + +- Trained on a small (80-row) dataset — coverage is limited to the taxonomy and styles seen + in training; unusual domains may produce weaker specs. +- Output is a strong **draft**, not a substitute for review by an analytics engineer, + especially for consent/privacy and PII handling. +- With very low `max_new_tokens`, long specs will be truncated and fail JSON parsing — keep + the limit high and validate the parse. diff --git a/chat_template.jinja b/chat_template.jinja new file mode 100644 index 0000000..70adff8 --- /dev/null +++ b/chat_template.jinja @@ -0,0 +1,61 @@ +{%- if tools %} + {{- '<|im_start|>system\n' }} + {%- if messages[0].role == 'system' %} + {{- messages[0].content + '\n\n' }} + {%- endif %} + {{- "# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within XML tags:\n" }} + {%- for tool in tools %} + {{- "\n" }} + {{- tool | tojson }} + {%- endfor %} + {{- "\n\n\nFor each function call, return a json object with function name and arguments within XML tags:\n\n{\"name\": , \"arguments\": }\n<|im_end|>\n" }} +{%- else %} + {%- if messages[0].role == 'system' %} + {{- '<|im_start|>system\n' + messages[0].content + '<|im_end|>\n' }} + {%- endif %} +{%- endif %} +{%- for message in messages %} + {%- if message.content is string %} + {%- set content = message.content %} + {%- else %} + {%- set content = '' %} + {%- endif %} + {%- if (message.role == "user") or (message.role == "system" and not loop.first) %} + {{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }} + {%- elif message.role == "assistant" %} + {{- '<|im_start|>' + message.role + '\n' + content }} + {%- if message.tool_calls %} + {%- for tool_call in message.tool_calls %} + {%- if (loop.first and content) or (not loop.first) %} + {{- '\n' }} + {%- endif %} + {%- if tool_call.function %} + {%- set tool_call = tool_call.function %} + {%- endif %} + {{- '\n{"name": "' }} + {{- tool_call.name }} + {{- '", "arguments": ' }} + {%- if tool_call.arguments is string %} + {{- tool_call.arguments }} + {%- else %} + {{- tool_call.arguments | tojson }} + {%- endif %} + {{- '}\n' }} + {%- endfor %} + {%- endif %} + {{- '<|im_end|>\n' }} + {%- elif message.role == "tool" %} + {%- if loop.first or (messages[loop.index0 - 1].role != "tool") %} + {{- '<|im_start|>user' }} + {%- endif %} + {{- '\n\n' }} + {{- content }} + {{- '\n' }} + {%- if loop.last or (messages[loop.index0 + 1].role != "tool") %} + {{- '<|im_end|>\n' }} + {%- endif %} + {%- endif %} +{%- endfor %} +{%- if add_generation_prompt %} + {{- '<|im_start|>assistant\n' }} +{%- endif %} \ No newline at end of file diff --git a/config.json b/config.json new file mode 100644 index 0000000..637c2d8 --- /dev/null +++ b/config.json @@ -0,0 +1,71 @@ +{ + "architectures": [ + "Qwen3ForCausalLM" + ], + "attention_bias": false, + "attention_dropout": 0.0, + "bos_token_id": 151643, + "dtype": "bfloat16", + "eos_token_id": 151645, + "head_dim": 128, + "hidden_act": "silu", + "hidden_size": 2560, + "initializer_range": 0.02, + "intermediate_size": 9728, + "layer_types": [ + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention" + ], + "max_position_embeddings": 262144, + "max_window_layers": 36, + "model_type": "qwen3", + "num_attention_heads": 32, + "num_hidden_layers": 36, + "num_key_value_heads": 8, + "pad_token_id": null, + "rms_norm_eps": 1e-06, + "rope_parameters": { + "rope_theta": 5000000, + "rope_type": "default" + }, + "sliding_window": null, + "tie_word_embeddings": true, + "transformers_version": "5.13.0", + "use_cache": true, + "use_sliding_window": false, + "vocab_size": 151936 +} diff --git a/generation_config.json b/generation_config.json new file mode 100644 index 0000000..69d962b --- /dev/null +++ b/generation_config.json @@ -0,0 +1,13 @@ +{ + "bos_token_id": 151643, + "do_sample": true, + "eos_token_id": [ + 151645, + 151643 + ], + "pad_token_id": 151643, + "temperature": 0.7, + "top_k": 20, + "top_p": 0.8, + "transformers_version": "5.13.0" +} diff --git a/model.safetensors b/model.safetensors new file mode 100644 index 0000000..4a36337 --- /dev/null +++ b/model.safetensors @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:910066f6343ad97dd3b84352ad1fa1e902aa2a6aaa257e272ac651d166426c80 +size 8044982080 diff --git a/tokenizer.json b/tokenizer.json new file mode 100644 index 0000000..c7afbed --- /dev/null +++ b/tokenizer.json @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:be75606093db2094d7cd20f3c2f385c212750648bd6ea4fb2bf507a6a4c55506 +size 11422650 diff --git a/tokenizer_config.json b/tokenizer_config.json new file mode 100644 index 0000000..cb87962 --- /dev/null +++ b/tokenizer_config.json @@ -0,0 +1,30 @@ +{ + "add_prefix_space": false, + "backend": "tokenizers", + "bos_token": null, + "clean_up_tokenization_spaces": false, + "eos_token": "<|im_end|>", + "errors": "replace", + "extra_special_tokens": [ + "<|im_start|>", + "<|im_end|>", + "<|object_ref_start|>", + "<|object_ref_end|>", + "<|box_start|>", + "<|box_end|>", + "<|quad_start|>", + "<|quad_end|>", + "<|vision_start|>", + "<|vision_end|>", + "<|vision_pad|>", + "<|image_pad|>", + "<|video_pad|>" + ], + "is_local": false, + "local_files_only": false, + "model_max_length": 1010000, + "pad_token": "<|endoftext|>", + "split_special_tokens": false, + "tokenizer_class": "Qwen2Tokenizer", + "unk_token": null +}