import torchfrom transformers import AutoTokenizer, AutoModelForCausalLM
model_id = "microsoft/Sentiment-Analysis-7B"
tokenizer = AutoTokenizer.from_pretrained(model_id)model = AutoModelForCausalLM.from_pretrained( model_id, torch_dtype=torch.bfloat16, device_map="auto")
system_prompt = """Return sentiment of user message as Positive/Neutral/Negative and explanation. Ex: Negative, the user expresses blame (as "you lie"), which conveys a negative tone.Definition of "Target": the current service, product, or agent the user is interacting with (including its website, app, API, or support).Rules of sentiment:* Questions/statements clearly unrelated to Target → Neutral* Otherwise, if related to Target: * Questions containing complaint/frustration/blame → Negative. Ex, What is the point of writing here if technical support does not respond? * Indirect incidents → Neutral. Ex, submit helpdesk ticket: mobile app crashes on startup * Statement of any inability/failure → Negative. Ex, I'm having trouble logging in. * Failure statement + polite/help request → Neutral. Ex, Login button is not working. Can you help?"""
sample = "I'm having trouble logging in to my account."
messages = [ {"role": "system", "content": system_prompt}, {"role": "user", "content": sample},]inputs = tokenizer.apply_chat_template( messages, tokenize=True, return_dict=True, return_tensors="pt", add_generation_prompt=True,).to(model.device)
# Label onlyoutput = model.generate(**inputs, max_new_tokens=3, do_sample=False)print(tokenizer.decode(output[0, inputs["input_ids"].shape[-1]:], skip_special_tokens=True))