Views
No views yet
transformer package, see the example below:1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3
4model_name = "Ihor/OpenBioLLM-Text2Graph-8B"
5
6tokenizer = AutoTokenizer.from_pretrained(model_name)
7tokenizer.chat_template = "{% set loop_messages = messages %}{% for message in loop_messages %}{% set content = '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n'+ message['content'] | trim + '<|end_of_text|>' %}{% if loop.index0 == 0 %}{% set content = bos_token + content %}{% endif %}{{ content }}{% endfor %}{% if add_generation_prompt %}{{ '<|start_header_id|>assistant<|end_header_id|>\n\n' }}{% endif %}"
8
9model = AutoModelForCausalLM.from_pretrained(
10 model_name,
11 device_map="auto",
12 torch_dtype=torch.bfloat16
13)
14
15
16MESSAGES = [
17 {
18 "role": "system",
19 "content": (
20 "You are an advanced assistant trained to process biomedical text for Named Entity Recognition (NER) and Relation Extraction (RE). "
21 "Your task is to analyze user-provided text, identify all unique and contextually relevant entities, and infer directed relationships "
22 "between these entities based on the context. Ensure that all relations exist only between annotated entities. "
23 "Entities and relationships should be human-readable and natural, reflecting real-world concepts and connections. "
24 "Output the annotated data in JSON format, structured as follows:\n\n"
25 """{"entities": [{"id": 0, "text": "ner_string_0", "type": "ner_type_string_0"}, {"id": 1, "text": "ner_string_1", "type": "ner_type_string_1"}], "relations": [{"head": 0, "tail": 1, "type": "re_type_string_0"}]}"""
26 "\n\nEnsure that the output captures all significant entities and their directed relationships in a clear and concise manner."
27 ),
28 },
29 {
30 "role": "user",
31 "content": (
32 'Here is a text input: "Subjects will receive a 100mL dose of IV saline every 6 hours for 24 hours. The first dose will be administered prior to anesthesia induction, approximately 30 minutes before skin incision. A total of 4 doses will be given." '
33 "Analyze this text, select and classify the entities, and extract their relationships as per your instructions."
34 ),
35 },
36]
37
38# Build prompt text
39chat_prompt = tokenizer.apply_chat_template(
40 MESSAGES, tokenize=False, add_generation_prompt=True
41)
42
43# Tokenize
44inputs = tokenizer(chat_prompt, return_tensors="pt").to(model.device)
45
46# Generate
47outputs = model.generate(
48 **inputs,
49 max_new_tokens=3000,
50 do_sample=True,
51 eos_token_id=tokenizer.eos_token_id,
52 pad_token_id=tokenizer.eos_token_id,
53 return_dict_in_generate=True
54)
55
56# Decode ONLY the new tokens (skip the prompt tokens)
57prompt_len = inputs["input_ids"].shape[-1]
58generated_ids = outputs.sequences[0][prompt_len:]
59response = tokenizer.decode(generated_ids, skip_special_tokens=True)
60print(response)vllm package, please refer to the example below:1# !pip install vllm
2
3from vllm import LLM, SamplingParams
4from transformers import AutoTokenizer
5
6MODEL_ID = "Ihor/OpenBioLLM-Text2Graph-8B"
7
8tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, use_fast=True)
9tokenizer.chat_template = "{% set loop_messages = messages %}{% for message in loop_messages %}{% set content = '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n'+ message['content'] | trim + '<|end_of_text|>' %}{% if loop.index0 == 0 %}{% set content = bos_token + content %}{% endif %}{{ content }}{% endfor %}{% if add_generation_prompt %}{{ '<|start_header_id|>assistant<|end_header_id|>\n\n' }}{% endif %}"
10
11llm = LLM(model=MODEL_ID)
12
13sampling_params = SamplingParams(
14 max_tokens=3000,
15 n=1,
16 best_of=1,
17 presence_penalty=0.0,
18 frequency_penalty=0.0,
19 repetition_penalty=1.0,
20 temperature=0.0,
21 top_p=1.0,
22 top_k=-1,
23 min_p=0.0,
24 seed=42,
25)
26
27
28MESSAGES = [
29 {
30 "role": "system",
31 "content": (
32 "You are an advanced assistant trained to process biomedical text for Named Entity Recognition (NER) and Relation Extraction (RE). "
33 "Your task is to analyze user-provided text, identify all unique and contextually relevant entities, and infer directed relationships "
34 "between these entities based on the context. Ensure that all relations exist only between annotated entities. "
35 "Entities and relationships should be human-readable and natural, reflecting real-world concepts and connections. "
36 "Output the annotated data in JSON format, structured as follows:\n\n"
37 """{"entities": [{"id": 0, "text": "ner_string_0", "type": "ner_type_string_0"}, {"id": 1, "text": "ner_string_1", "type": "ner_type_string_1"}], "relations": [{"head": 0, "tail": 1, "type": "re_type_string_0"}]}"""
38 "\n\nEnsure that the output captures all significant entities and their directed relationships in a clear and concise manner."
39 ),
40 },
41 {
42 "role": "user",
43 "content": (
44 'Here is a text input: "Subjects will receive a 100mL dose of IV saline every 6 hours for 24 hours. The first dose will be administered prior to anesthesia induction, approximately 30 minutes before skin incision. A total of 4 doses will be given." '
45 "Analyze this text, select and classify the entities, and extract their relationships as per your instructions."
46 ),
47 },
48]
49
50chat_prompt = tokenizer.apply_chat_template(
51 MESSAGES,
52 tokenize=False,
53 add_generation_prompt=True,
54 add_special_tokens=False,
55)
56
57outputs = llm.generate([chat_prompt], sampling_params)
58response_text = outputs[0].outputs[0].text
59print(response_text)1@misc{yazdani2025glinerbiomedsuiteefficientmodels,
2 title={GLiNER-BioMed: A Suite of Efficient Models for Open Biomedical Named Entity Recognition},
3 author={Anthony Yazdani and Ihor Stepanov and Douglas Teodoro},
4 year={2025},
5 eprint={2504.00676},
6 archivePrefix={arXiv},
7 primaryClass={cs.CL},
8 url={https://arxiv.org/abs/2504.00676},
9}