File size: 3,555 Bytes
e0ed027
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""Generate an OpenSearch request with opensearch-agentic-dsl 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=(",", ":"))
    + "\nDerive price and gender affinity in the generated DSL. "
    "Apply persona only as instructed. Follow 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=1024,
        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)