Upload folder using huggingface_hub
Browse filesFiles changed (10)
- README.mdAdded+68
- config.jsonAdded+29
- generation_config.jsonAdded+6
- handler.pyAdded+99
- model.safetensorsAdded+3
- requirements.txtAdded+2
- special_tokens_map.jsonAdded+30
- tokenizer.jsonAdded
- tokenizer.modelAdded+3
- tokenizer_config.jsonAdded+45
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768---language:- entags:- causal-lm- llama- instruction-tuned- point-in-time- dated- lookahead-bias-freepipeline_tag: text-generation---
# DatedGPT-2020-Instruct
**DatedGPT** is a family of point-in-time language models: each vintage istrained only on data available up to its cutoff date, making it suitable forlookahead-bias-free prediction and point-in-time analysis.
This is the **instruction-tuned chat model** with data up to **2020**.For the base (pretrained) model, see[datedgpt/datedgpt-2020-base](https://huggingface.co/datedgpt/datedgpt-2020-base).
| Property | Value ||----------|-------|| Architecture | LlamaForCausalLM || Parameters | ~1.3 B || Context length | 2048 || Vocab | 32,000 (SentencePiece) || Precision | bfloat16 || Data vintage | 2020 |
## Chat template
The Llama-2-style chat template ships in `tokenizer_config.json` — apply itwith the tokenizer. The BOS token must come from the tokenizer, **not** as aliteral `"<s>"` string in your prompt text.
```pythonimport torchfrom transformers import AutoTokenizer, AutoModelForCausalLM
repo_id = "datedgpt/datedgpt-2020-instruct"tokenizer = AutoTokenizer.from_pretrained(repo_id)model = AutoModelForCausalLM.from_pretrained(repo_id, torch_dtype=torch.bfloat16, device_map="auto")
prompt = tokenizer.apply_chat_template( [{"role": "user", "content": "What is the capital of France?"}], tokenize=False,)inputs = tokenizer(prompt, return_tensors="pt").to(model.device)output = model.generate(**inputs, max_new_tokens=128, do_sample=True, temperature=0.7, top_p=0.95, use_cache=True, eos_token_id=tokenizer.eos_token_id, pad_token_id=tokenizer.eos_token_id)print(tokenizer.decode(output[0, inputs["input_ids"].shape[-1]:], skip_special_tokens=True))```
## Serving on HF Inference Endpoints
This repo ships a `handler.py` that applies the chat template server-side —clients send plain text (or a messages list for multi-turn). Deploy with theDefault container on a bf16-capable GPU (A10G or better).
## Limitations
- Knowledge limited to the 2020 data vintage.- No RLHF or safety tuning; outputs can be confidently wrong.1234567891011121314151617181920212223242526272829{ "architectures": [ "LlamaForCausalLM" ], "attention_bias": false, "attention_dropout": 0.0, "bos_token_id": 1, "eos_token_id": 2, "head_dim": 128, "hidden_act": "silu", "hidden_size": 2048, "initializer_range": 0.02, "intermediate_size": 5504, "max_position_embeddings": 2048, "mlp_bias": false, "model_type": "llama", "num_attention_heads": 16, "num_hidden_layers": 24, "num_key_value_heads": 16, "pretraining_tp": 1, "rms_norm_eps": 1e-05, "rope_scaling": null, "rope_theta": 10000.0, "tie_word_embeddings": false, "torch_dtype": "bfloat16", "transformers_version": "4.51.0", "use_cache": false, "vocab_size": 32000}123456{ "_from_model_config": true, "bos_token_id": 1, "eos_token_id": 2, "transformers_version": "4.51.0"}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899import osimport torchfrom transformers import AutoModelForCausalLM, AutoTokenizer
# Keep only the last round (1 user+assistant pair) + the current user messageMAX_HISTORY_MESSAGES = 3
class EndpointHandler: """ Hugging Face Inference Endpoints custom handler.
Expects input like: - {"inputs": "hello"} -> single-turn, auto-wrapped with chat template - {"inputs": [{"role":"user","content":"hello"}, ...]} -> multi-turn chat (messages list) - {"inputs": "hello", "parameters": {"raw": true}} -> sent as-is (no template)
Optional: - {"parameters": {"max_new_tokens": 512, "temperature": 0.7, ...}} """
def __init__(self, path: str = ""): model_dir = path or os.getenv("HF_MODEL_DIR", ".") self.device = "cuda" if torch.cuda.is_available() else "cpu"
self.tokenizer = AutoTokenizer.from_pretrained(model_dir, trust_remote_code=True)
# Ensure pad token exists (common for causal LMs) if self.tokenizer.pad_token is None: self.tokenizer.pad_token = self.tokenizer.eos_token self.tokenizer.pad_token_id = self.tokenizer.eos_token_id
self.model = AutoModelForCausalLM.from_pretrained( model_dir, torch_dtype="auto", device_map="auto" if torch.cuda.is_available() else None, trust_remote_code=True, ) self.model.eval()
def __call__(self, data: dict) -> dict: inputs = data.get("inputs", data) params = data.get("parameters", {}) or {}
raw = bool(params.pop("raw", False))
if raw: if not isinstance(inputs, str): raise ValueError("raw mode requires inputs to be a string.") prompt = inputs elif isinstance(inputs, list): # Multi-turn: inputs is a list of {"role": ..., "content": ...} inputs = inputs[-MAX_HISTORY_MESSAGES:] prompt = self.tokenizer.apply_chat_template( inputs, tokenize=False, ) elif isinstance(inputs, str): # Single-turn: wrap in a one-message list prompt = self.tokenizer.apply_chat_template( [{"role": "user", "content": inputs}], tokenize=False, ) else: raise ValueError("inputs must be a string or a list of messages.")
enc = self.tokenizer( prompt, return_tensors="pt", padding=False, truncation=True, ) input_ids = enc["input_ids"].to(self.model.device) attention_mask = enc.get("attention_mask", torch.ones_like(input_ids)).to(self.model.device)
gen_kwargs = { "max_new_tokens": min(int(params.pop("max_new_tokens", 512)), 512), "do_sample": bool(params.pop("do_sample", True)), "temperature": float(params.pop("temperature", 0.7)), "top_p": float(params.pop("top_p", 0.95)), "repetition_penalty": float(params.pop("repetition_penalty", 1.2)), "eos_token_id": self.tokenizer.eos_token_id, "pad_token_id": self.tokenizer.pad_token_id, } gen_kwargs.update(params)
with torch.no_grad(): out = self.model.generate( input_ids=input_ids, attention_mask=attention_mask, **gen_kwargs, )
# Return only newly generated tokens (your current behavior) new_tokens = out[0, input_ids.shape[-1]:] text = self.tokenizer.decode(new_tokens, skip_special_tokens=True)
return {"generated_text": text}
123version https://git-lfs.github.com/spec/v1oid sha256:daca60d86c720e795482f7073de98759814b6c714ee8a11bff1f24e3bc5bb8c6size 269087197612acceleratesafetensors123456789101112131415161718192021222324252627282930{ "bos_token": { "content": "<s>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false }, "eos_token": { "content": "</s>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false }, "pad_token": { "content": "</s>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false }, "unk_token": { "content": "<unk>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false }}123version https://git-lfs.github.com/spec/v1oid sha256:9e556afd44213b6bd1be2b850ebbbd98f5481437a8021afaf58ee7fb1818d347size 499723123456789101112131415161718192021222324252627282930313233343536373839404142434445{ "add_bos_token": true, "add_eos_token": false, "add_prefix_space": null, "added_tokens_decoder": { "0": { "content": "<unk>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "1": { "content": "<s>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "2": { "content": "</s>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true } }, "bos_token": "<s>", "chat_template": "{% if messages[0]['role'] == 'system' %}{% set loop_messages = messages[1:] %}{% set system_message = messages[0]['content'] %}{% else %}{% set loop_messages = messages %}{% endif %}{% for message in loop_messages %}{% if loop.index0 == 0 and system_message is defined %}{% set content = '<<SYS>>\n' + system_message + '\n<</SYS>>\n\n' + message['content'] %}{% else %}{% set content = message['content'] %}{% endif %}{% if message['role'] == 'user' %}{{ '<s>' + '[INST] ' + content + ' [/INST]' }}{% elif message['role'] == 'assistant' %}{{ content + '</s>' }}{% endif %}{% endfor %}", "clean_up_tokenization_spaces": false, "eos_token": "</s>", "extra_special_tokens": {}, "legacy": false, "model_max_length": 2048, "pad_token": "</s>", "padding_side": "right", "sp_model_kwargs": {}, "split_special_tokens": false, "tokenizer_class": "LlamaTokenizer", "unk_token": "<unk>", "use_default_system_prompt": false}README.mdAdded+68
config.jsonAdded+29
generation_config.jsonAdded+6
handler.pyAdded+99
model.safetensorsAdded+3
requirements.txtAdded+2
special_tokens_map.jsonAdded+30
tokenizer.jsonAdded
tokenizer.modelAdded+3
tokenizer_config.jsonAdded+45