from transformers import AutoModelForCausalLM, AutoTokenizer
model_id = "InternScience/Agents-K1"
tok = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype="bfloat16", device_map="auto")
system = (
"You are an expert in information extraction. Given a task instruction "
"with schema definitions and input text, extract the required information.\n\n"
"You should think step by step about the extraction task, then provide "
"your answer in JSON format.\n\n"
"Format your response as:\n"
"<think>\nYour step-by-step reasoning...\n</think>\n"
"<answer>\nYour JSON extraction result here\n</answer>"
)
user = (
"You are an expert in named entity recognition. Please extract entities "
"that match the schema definition from the input. Return an empty list if "
"the entity type does not exist. Please respond in the format of a JSON "
"dictionary.\n\n"
'Entity types to extract: ["person", "organization", "location"]\n\n'
"Input text: Marie Curie worked at the University of Paris.\n\n"
"Please think step by step and respond in the following format:\n"
"<think>\nYour reasoning process...\n</think>\n"
"<answer>\nYour JSON extraction result\n</answer>"
)
messages = [{"role": "system", "content": system},
{"role": "user", "content": user}]
inputs = tok.apply_chat_template(messages, add_generation_prompt=True,
return_tensors="pt").to(model.device)
out = model.generate(inputs, max_new_tokens=512, do_sample=False)
print(tok.decode(out[0][inputs.shape[-1]:], skip_special_tokens=True))