Instructions to use sharp-pixel/opensearch-compact-variables-v2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use sharp-pixel/opensearch-compact-variables-v2 with PEFT:
from peft import PeftModel from transformers import AutoModelForCausalLM base_model = AutoModelForCausalLM.from_pretrained("mistralai/Ministral-3-8B-Instruct-2512-BF16") model = PeftModel.from_pretrained(base_model, "sharp-pixel/opensearch-compact-variables-v2") - Notebooks
- Google Colab
- Kaggle
Download example.py from sharp-pixel/opensearch-compact-variables-v2: direct link, hf CLI and curl.
- Browser
- Download file 3.7 kB
-
https://huggingface.co/sharp-pixel/opensearch-compact-variables-v2/resolve/main/example.py
- Command line
-
hf download hf://sharp-pixel/opensearch-compact-variables-v2/example.py
-
curl -L -o example.py https://huggingface.co/sharp-pixel/opensearch-compact-variables-v2/resolve/main/example.py
3.7 kB
| """Generate compact planner variables with the v2 adapter on a CUDA GPU.""" | |
| import json | |
| import os | |
| from pathlib import Path | |
| import torch | |
| from peft import PeftModel | |
| from transformers import AutoModelForImageTextToText, AutoTokenizer | |
| BASE_MODEL = "mistralai/Ministral-3-8B-Instruct-2512-BF16" | |
| BASE_REVISION = "06cc81bfd6e45321d8fc8f816576c5b6ac67ec22" | |
| ASSET_DIR = Path(__file__).resolve().parent | |
| ADAPTER_ID = os.environ.get("ADAPTER_ID", str(ASSET_DIR)) | |
| SYSTEM_PROMPT = (ASSET_DIR / "prompts/system.txt").read_text(encoding="utf-8").strip() | |
| USER_PROMPT_TEMPLATE = (ASSET_DIR / "prompts/user.txt").read_text(encoding="utf-8").strip() | |
| context = { | |
| "required_filters": [{"term": {"availability": "active"}}], | |
| "ui_max_price": 8000, | |
| "size": 24, | |
| "track_total_hits": 10000, | |
| "sort_mode": "recommended", | |
| "persona": {"id": "anonymous", "version": 1, "mode": "unprofiled"}, | |
| } | |
| question = ( | |
| "Shopper request: dress watch under 5000\n" | |
| "Trusted planner context: " | |
| + json.dumps(context, ensure_ascii=False, separators=(",", ":")) | |
| + "\nGender only from Shopper words, never persona. Dress/formal/suit watch neutral. Derive price; sort_mode." | |
| ) | |
| if len(question) > 1000: | |
| raise ValueError("Native planner question exceeds 1,000 characters") | |
| index_mapping = { | |
| "_doc": { | |
| "dynamic": "false", | |
| "properties": { | |
| "title": {"type": "text"}, | |
| "brand": {"type": "text"}, | |
| "canonical_text": {"type": "text"}, | |
| "description": {"type": "text"}, | |
| "availability": {"type": "keyword"}, | |
| "category": {"type": "keyword"}, | |
| "price": {"type": "integer"}, | |
| "quality_score": {"type": "rank_feature"}, | |
| "freshness_score": {"type": "rank_feature"}, | |
| "seller_score": {"type": "rank_feature"}, | |
| }, | |
| } | |
| } | |
| query_fields = list(index_mapping["_doc"]["properties"]) | |
| def native_string_parameter(value): | |
| compact = json.dumps(value, ensure_ascii=False, separators=(",", ":")) | |
| return json.dumps(compact, ensure_ascii=False) | |
| user_prompt = USER_PROMPT_TEMPLATE | |
| for placeholder, value in { | |
| "${parameters.question}": question, | |
| "${parameters.index_mapping:-}": native_string_parameter(index_mapping), | |
| "${parameters.query_fields:-}": native_string_parameter(query_fields), | |
| }.items(): | |
| user_prompt = user_prompt.replace(placeholder, value) | |
| messages = [ | |
| {"role": "system", "content": SYSTEM_PROMPT}, | |
| {"role": "user", "content": user_prompt}, | |
| ] | |
| tokenizer = AutoTokenizer.from_pretrained( | |
| BASE_MODEL, revision=BASE_REVISION, trust_remote_code=False | |
| ) | |
| tokenizer.pad_token = tokenizer.eos_token | |
| base_model = AutoModelForImageTextToText.from_pretrained( | |
| BASE_MODEL, | |
| revision=BASE_REVISION, | |
| dtype=torch.bfloat16, | |
| device_map={"": 0}, | |
| trust_remote_code=False, | |
| ) | |
| model = PeftModel.from_pretrained(base_model, ADAPTER_ID) | |
| model.eval() | |
| input_ids = tokenizer.apply_chat_template( | |
| messages, tokenize=True, add_generation_prompt=True, return_dict=False | |
| ) | |
| inputs = { | |
| "input_ids": torch.tensor([input_ids], device=model.device), | |
| "attention_mask": torch.ones((1, len(input_ids)), dtype=torch.long, device=model.device), | |
| } | |
| with torch.inference_mode(): | |
| generated = model.generate( | |
| **inputs, | |
| do_sample=False, | |
| max_new_tokens=512, | |
| eos_token_id=tokenizer.eos_token_id, | |
| pad_token_id=tokenizer.pad_token_id, | |
| ) | |
| completion = tokenizer.decode( | |
| generated[0, len(input_ids):], skip_special_tokens=True | |
| ) | |
| print(completion) | |
| # Treat the output as untrusted. The application must validate it, combine it | |
| # with trusted controls, render the included templates, and validate the DSL. | |