Views
No views yet
transformers library. Below is a straightforward example of how to deploy the shellwork/ChatParts-qwen2.5-14b model using transformers.transformers version >= 4.43.0 installed. You can update your installation using:pip install --upgrade transformers1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3
4# Load the tokenizer and model
5model_name = "shellwork/ChatParts-qwen2.5-14b"
6model = AutoModelForCausalLM.from_pretrained(
7 model_name,
8 torch_dtype="auto",
9 device_map="auto"
10)
11tokenizer = AutoTokenizer.from_pretrained(model_name)
12
13# Define the prompt and messages
14prompt = "Give me a short introduction to synthetic biology."
15messages = [
16 {"role": "system", "content": "You are ChatParts, a model specialized in synthetic biology created by XJTLU-Software."},
17 {"role": "user", "content": prompt}
18]
19
20# Apply chat template
21text = tokenizer.apply_chat_template(
22 messages,
23 tokenize=False,
24 add_generation_prompt=True
25)
26
27# Tokenize the input
28model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
29
30# Generate the response
31generated_ids = model.generate(
32 **model_inputs,
33 max_new_tokens=512
34)
35
36# Extract the generated tokens
37generated_ids = [
38 output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
39]
40
41# Decode the response
42response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
43print(response)torch, modelscope, and transformers.AutoModelForCausalLM and AutoTokenizer from modelscope to load the pre-trained model and tokenizer.apply_chat_template method to format the messages appropriately for the model.generate method to produce a response with a specified maximum number of new tokens.