import torchfrom transformers import AutoTokenizer, AutoModelForCausalLM model_id = "ThakiCloud/SKILLRET-Reranker-0.6B"tokenizer = AutoTokenizer.from_pretrained(model_id, padding_side="left")model = AutoModelForCausalLM.from_pretrained(model_id, dtype=torch.bfloat16).eval() token_yes = tokenizer.convert_tokens_to_ids("yes")token_no = tokenizer.convert_tokens_to_ids("no") instruction = ( "Given a skill search query, judge whether the skill document " "is relevant and useful for the query")prefix = ( "<|im_start|>system\nJudge whether the Document meets the requirements based on the " 'Query and the Instruct provided. Note that the answer can only be "yes" or "no".' "<|im_end|>\n<|im_start|>user\n")suffix = "<|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\n"prefix_ids = tokenizer.encode(prefix, add_special_tokens=False)suffix_ids = tokenizer.encode(suffix, add_special_tokens=False)max_length = 8192 def format_pair(query: str, doc: str) -> str: return f"<Instruct>: {instruction}\n<Query>: {query}\n<Document>: {doc}" @torch.no_grad()def score(query: str, docs: list[str]) -> list[float]: pairs = [format_pair(query, d) for d in docs] enc = tokenizer( pairs, padding=False, truncation="longest_first", return_attention_mask=False, max_length=max_length - len(prefix_ids) - len(suffix_ids), ) enc["input_ids"] = [prefix_ids + ids + suffix_ids for ids in enc["input_ids"]] enc = tokenizer.pad(enc, padding=True, return_tensors="pt").to(model.device) logits = model(**enc).logits[:, -1, :] stacked = torch.stack([logits[:, token_no], logits[:, token_yes]], dim=1) return torch.nn.functional.log_softmax(stacked, dim=1)[:, 1].exp().tolist() query = "Help me set up a CI/CD pipeline for my Python project"skills = [ "ci-cd-setup | Configure continuous integration and deployment pipelines ...", "python-debugging | Debug Python applications using pdb and logging ...",]print(score(query, skills)) # higher = more relevant