commit 3edbf5d1101103af54f61fa4f3469cdd80d1e237 Author: ModelHub XC Date: Wed Jul 29 03:59:18 2026 +0800 初始化项目,由ModelHub XC社区提供模型 Model: jarminraws/hotel-llm-search Source: Original Platform 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..d8bf331 --- /dev/null +++ b/README.md @@ -0,0 +1,23 @@ +--- +license: apache-2.0 +base_model: Qwen/Qwen3-1.7B +tags: [hotel-search, entity-extraction, lora-merged, qwen3] +--- + +# Hotel Entity Extractor (Qwen3-1.7B, LoRA merged) + +Fine-tuned to extract structured hotel-search params from natural-language queries +and return strict JSON. Trained filters-free of codes: `filters` is emitted as +human-readable PHRASES (e.g. `"swimming pool, pet friendly"`); a downstream matcher +resolves phrases -> production codes. + +- **Input**: a hotel-search query + `today` date (DDMMYYYY date math). +- **Output**: JSON with destination, locality, hotelName, checkin/checkoutDate, + adult/room/child/childAges/infantCount, sortCriteria, min/maxPrice, filters, + deepSearch, isNearMe, resetAction. +- **Prompt/schema contract**: bundled as `contract.py` in this repo. The serving + layer MUST use this exact prompt (it verifies byte-parity at startup). + +Serve with vLLM (see the entity-extraction-serving repo). Raw model output is +exposed by the API; date validation + filter phrase->code resolution happen in +the calling service. diff --git a/chat_template.jinja b/chat_template.jinja new file mode 100644 index 0000000..01be9b3 --- /dev/null +++ b/chat_template.jinja @@ -0,0 +1,89 @@ +{%- 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 %} +{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %} +{%- for message in messages[::-1] %} + {%- set index = (messages|length - 1) - loop.index0 %} + {%- if ns.multi_step_tool and message.role == "user" and message.content is string and not(message.content.startswith('') and message.content.endswith('')) %} + {%- set ns.multi_step_tool = false %} + {%- set ns.last_query_index = index %} + {%- endif %} +{%- endfor %} +{%- 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" %} + {%- set reasoning_content = '' %} + {%- if message.reasoning_content is string %} + {%- set reasoning_content = message.reasoning_content %} + {%- else %} + {%- if '' in content %} + {%- set reasoning_content = content.split('')[0].rstrip('\n').split('')[-1].lstrip('\n') %} + {%- set content = content.split('')[-1].lstrip('\n') %} + {%- endif %} + {%- endif %} + {%- if loop.index0 > ns.last_query_index %} + {%- if loop.last or (not loop.last and reasoning_content) %} + {{- '<|im_start|>' + message.role + '\n\n' + reasoning_content.strip('\n') + '\n\n\n' + content.lstrip('\n') }} + {%- else %} + {{- '<|im_start|>' + message.role + '\n' + content }} + {%- endif %} + {%- else %} + {{- '<|im_start|>' + message.role + '\n' + content }} + {%- endif %} + {%- 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' }} + {%- if enable_thinking is defined and enable_thinking is false %} + {{- '\n\n\n\n' }} + {%- endif %} +{%- endif %} \ No newline at end of file diff --git a/config.json b/config.json new file mode 100644 index 0000000..b3724a3 --- /dev/null +++ b/config.json @@ -0,0 +1,63 @@ +{ + "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": 2048, + "initializer_range": 0.02, + "intermediate_size": 6144, + "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" + ], + "max_position_embeddings": 40960, + "max_window_layers": 28, + "model_type": "qwen3", + "num_attention_heads": 16, + "num_hidden_layers": 28, + "num_key_value_heads": 8, + "pad_token_id": null, + "rms_norm_eps": 1e-06, + "rope_parameters": { + "rope_theta": 1000000, + "rope_type": "default" + }, + "sliding_window": null, + "tie_word_embeddings": true, + "transformers_version": "5.8.1", + "use_cache": true, + "use_sliding_window": false, + "vocab_size": 151936 +} diff --git a/contract.py b/contract.py new file mode 100644 index 0000000..8d2c5db --- /dev/null +++ b/contract.py @@ -0,0 +1,127 @@ +"""SHARED CONTRACT — the prompt + output schema the model is trained and served on. + +This file is the single source of truth for: + - FIELD_ORDER : canonical key order of the extracted JSON + - build_prompt() : the EXACT prompt string used at train AND inference time + - format_completion() : training target serialization (also used by eval) + - extract_json() : brace-matched JSON extraction from raw model text + - HotelExtraction : Pydantic schema for validation + - validate() : parse+validate -> clean dict (or None) + +It is intentionally DEPENDENCY-LIGHT (only json + pydantic) so both the heavy +training env and the lean serving env can import it. A COPY of this file lives in +BOTH training/ and deployment/; tests/test_contract_parity.py asserts they are +byte-identical, so the prompt can never silently drift between train and serve. + +DO NOT EDIT one copy without the other. Edit the source, re-sync, re-run the test. +""" + +from __future__ import annotations + +import json +from typing import Any, Literal, Optional + +from pydantic import BaseModel, ConfigDict, Field, ValidationError + +DEFAULT_BASE_MODEL = "Qwen/Qwen3-1.7B" + +# `filters` is emitted as a comma-separated string of NATURAL PHRASES +# (e.g. "swimming pool, pet friendly"), NOT production codes. A downstream matcher +# resolves phrases -> codes (FL_HF_29, ...), keeping the model taxonomy-agnostic. +FIELD_ORDER = [ + "destination", "locality", "hotelName", "checkinDate", "checkoutDate", + "adultCount", "roomCount", "childCount", "childAges", "infantCount", + "sortCriteria", "minPrice", "maxPrice", "filters", "deepSearch", "isNearMe", "resetAction", +] + + +def build_prompt(query: str, today: str) -> str: + """The exact user-message content used for BOTH training and inference.""" + return ( + "Extract hotel-search entities.\n" + "Return strict JSON only.\n" + "Schema:\n" + "destination, locality, hotelName, checkinDate, checkoutDate, " + "adultCount, roomCount, childCount, childAges, infantCount, " + "sortCriteria, minPrice, maxPrice, filters, deepSearch, isNearMe, resetAction.\n\n" + "Rules:\n" + "- Dates are DDMMYYYY.\n" + '- deepSearch and isNearMe must be "true" or "false".\n' + "- filters is a comma-separated list of amenity/type phrases (e.g. " + '"swimming pool, pet friendly"); prefix removals with "no ".\n' + "- Omit unknown optional fields.\n\n" + f"today={today}\n" + f"query={query}" + ) + + +def format_completion(expected: dict[str, Any]) -> str: + """Stable-key-order minified JSON — the training target / gold serialization.""" + ordered = {f: expected[f] for f in FIELD_ORDER if f in expected} + for k in expected: + if k not in ordered: + ordered[k] = expected[k] + return json.dumps(ordered, ensure_ascii=False, separators=(",", ":")) + + +def extract_json(text: str) -> dict | None: + """Pull the first balanced {...} object out of raw model text.""" + start = text.find("{") + if start < 0: + return None + depth = 0 + in_str = esc = False + for i in range(start, len(text)): + c = text[i] + if esc: + esc = False + continue + if c == "\\": + esc = True + continue + if c == '"': + in_str = not in_str + continue + if in_str: + continue + if c == "{": + depth += 1 + elif c == "}": + depth -= 1 + if depth == 0: + try: + return json.loads(text[start:i + 1]) + except json.JSONDecodeError: + return None + return None + + +class HotelExtraction(BaseModel): + """Output contract. `filters` is a comma-separated PHRASE string (e.g. + "swimming pool, pet friendly"); a downstream matcher resolves it to codes.""" + model_config = ConfigDict(extra="forbid") + deepSearch: Literal["true", "false"] + isNearMe: Literal["true", "false"] + destination: Optional[str] = Field(default=None, min_length=1) + locality: Optional[str] = Field(default=None, min_length=1) + hotelName: Optional[str] = Field(default=None, min_length=1) + checkinDate: Optional[str] = Field(default=None, pattern=r"^\d{8}$") + checkoutDate: Optional[str] = Field(default=None, pattern=r"^\d{8}$") + adultCount: Optional[int] = Field(default=None, ge=0, le=20) + roomCount: Optional[int] = Field(default=None, ge=0, le=20) + childCount: Optional[int] = Field(default=None, ge=0, le=20) + childAges: Optional[list[int]] = None + infantCount: Optional[int] = Field(default=None, ge=0, le=20) + sortCriteria: Optional[Literal["SC_P_LH", "SC_P_HL", "SC_UR", "SC_P", "SC_DIST"]] = None + minPrice: Optional[int] = Field(default=None, ge=0, le=10_000_000) + maxPrice: Optional[int] = Field(default=None, ge=0, le=10_000_000) + filters: Optional[str] = Field(default=None, min_length=1) + resetAction: Optional[Literal["filters", "guests", "dates", "all"]] = None + + +def validate(d: dict) -> dict | None: + """Validate a parsed dict against the schema; return clean dict or None.""" + try: + return HotelExtraction.model_validate(d).model_dump(exclude_none=True) + except ValidationError: + return None diff --git a/generation_config.json b/generation_config.json new file mode 100644 index 0000000..c472e21 --- /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.6, + "top_k": 20, + "top_p": 0.95, + "transformers_version": "5.8.1" +} diff --git a/model.safetensors b/model.safetensors new file mode 100644 index 0000000..27582fe --- /dev/null +++ b/model.safetensors @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:081680104296e847046cb301f532cbce1f0116105fa07050a0cf48f7f350a98c +size 3441185608 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..5668a4a --- /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": true, + "local_files_only": false, + "model_max_length": 131072, + "pad_token": "<|endoftext|>", + "split_special_tokens": false, + "tokenizer_class": "Qwen2Tokenizer", + "unk_token": null +}