#!/usr/bin/env python3
"""Written for https://paraloncloud.com/resources/semantic-search-open-embeddings-api
Needs: pip install numpy scikit-learn requests; PRL_KEY=prlc_... in the environment;
support_tickets_990.jsonl and queries_300.json from the same /files/guides/ folder.

Generate short search queries with a known intent, in five languages, with the
chat model. These are the queries a support agent or a customer would type into
a search box; the corpus they are run against is the 990 English tickets."""
import json, os, sys, requests
API="https://paraloncloud.com/v1/chat/completions"; KEY=os.environ["PRL_KEY"]
INTENTS={"order_status":"asks where an order is or when it arrives","cancel_order":"wants to cancel an order not yet delivered","return_request":"wants to send an item back",
 "refund_status":"asks about money owed back for a return or cancellation already agreed","damaged_item":"item arrived broken or defective","wrong_item":"received a different item than ordered",
 "payment_failed":"card or payment did not go through","discount_code":"a promo code does not work or how to use one","product_question":"asks about a product before or after buying, not a problem with it",
 "shipping_address_change":"wants to change where an order ships","account_login":"cannot sign in, password, account access","complaint":"expresses dissatisfaction with service or the company as a whole"}
LANGS={"en":"English","ro":"Romanian","de":"German","es":"Spanish","zh":"Simplified Chinese"}
N=5
schema={"type":"object","properties":{"queries":{"type":"array","minItems":N,"maxItems":N,"items":{"type":"string","maxLength":120}}},"required":["queries"]}
out=[]
for intent,definition in INTENTS.items():
    for code,lang in LANGS.items():
        prompt=(f"Write {N} different short search queries, 3 to 12 words each, in {lang}, that a customer-support agent or a customer would type "
                f"to find support tickets about this situation: {definition}. Vary the wording and the products mentioned (electronics, clothing, board games, cookware). "
                f"Do not mention order numbers. Return only the queries.")
        r=requests.post(API,headers={"Authorization":f"Bearer {KEY}"},json={"model":"qwen3.8-27b","messages":[{"role":"user","content":prompt}],
            "response_format":{"type":"json_schema","json_schema":{"name":"q","schema":schema}},"chat_template_kwargs":{"enable_thinking":False},"temperature":0.7,"max_tokens":400},timeout=120)
        r.raise_for_status(); qs=json.loads(r.json()["choices"][0]["message"]["content"])["queries"]
        for q in qs: out.append({"intent":intent,"lang":code,"query":q})
        print(intent,code,qs[0],file=sys.stderr)
json.dump(out,open("queries.json","w"),ensure_ascii=False,indent=1); print(len(out),"queries")
