Views
No views yet

1from transformers import GenerationConfig, LlamaForCausalLM, LlamaTokenizer,AutoTokenizer,AutoModelForCausalLM,MistralForCausalLM
2import torch
3
4model_id=Mistral-7B-Instruct-v0.4
5
6tokenizer = AutoTokenizer.from_pretrained(model_id)
7model = AutoModelForCausalLM.from_pretrained(model_id,torch_dtype=torch.bfloat16,device_map="auto",)
8
9chat_template="{{ bos_token }}{% for message in messages %}{% if (message['role'] == 'user') != (loop.index0 % 2 == 0) %}{{ raise_exception('Conversation roles must alternate user/assistant/user/assistant/...') }}{% endif %}{% if message['role'] == 'user' %}{{ '[INST] ' + message['content'] + ' [/INST]' }}{% elif message['role'] == 'assistant' %}{{ message['content'] + eos_token}}{% else %}{{ raise_exception('Only user and assistant roles are supported!') }}{% endif %}{% endfor %}"
10
11def chat_format(conversation:list):
12 system_prompt = "You are a helpful, respectful and honest assistant.Help humman as much as you can."
13
14 id = tokenizer.apply_chat_template(conversation,chat_template=chat_template,tokenize=False)
15
16 return id
17
18user_chat=[{"role":"user","content":"你好,最近在干嘛呢"}]
19text = chat_format(user_chat).rstrip("</s>")
20def predict(content_prompt):
21 inputs = tokenizer(content_prompt,return_tensors="pt",add_special_tokens=True)
22 input_ids = inputs["input_ids"].to("cuda:0")
23 # print(f"input length:{len(input_ids[0])}")
24 with torch.no_grad():
25 generation_output = model.generate(
26 input_ids=input_ids,
27 #generation_config=generation_config,
28 return_dict_in_generate=True,
29 output_scores=True,
30 max_new_tokens=2048,
31 top_p=0.9,
32 num_beams=1,
33 do_sample=True,
34 repetition_penalty=1.0,
35 eos_token_id=tokenizer.eos_token_id,
36 pad_token_id=tokenizer.pad_token_id,
37 )
38 s = generation_output.sequences[0]
39 output = tokenizer.decode(s,skip_special_tokens=True)
40 output1 = output.split("[/INST]")[-1].strip()
41 # print(output1)
42 return output1
43
44predict(text)
45output:你好!作为一个大型语言模型,我一直在学习和提高自己的能力。最近,我一直在努力学习新知识、改进算法,以便更好地回答用户的问题并提供帮助。同时,我也会定期接受人工智能专家的指导和评估,以确保我的表现不断提升。希望这些信息对你有所帮助!1#llama2-chat-template.jinja file is chat-template above
2model_path=Mistral-7B-Instruct-V0.4
3python -m vllm.entrypoints.openai.api_server --model=$model_path \
4 --trust-remote-code --host 0.0.0.0 --port 7777 \
5 --gpu-memory-utilization 0.8 \
6 --max-model-len 8192 --chat-template llama2-chat-template.jinja \
7 --tensor-parallel-size 1 --served-model-name chatbot1from openai import OpenAI
2# Set OpenAI's API key and API base to use vLLM's API server.
3openai_api_key = "EMPTY"
4openai_api_base = "http://localhost:7777/v1"
5
6client = OpenAI(
7 api_key=openai_api_key,
8 base_url=openai_api_base,
9)
10call_args = {
11 'temperature': 0.7,
12 'top_p': 0.9,
13 'top_k': 40,
14 'max_tokens': 2048, # output-len
15 'presence_penalty': 1.0,
16 'frequency_penalty': 0.0,
17 "repetition_penalty":1.0,
18 "stop":["</s>"],
19 }
20chat_response = client.chat.completions.create(
21 model="chatbot",
22 messages=[
23 {"role": "user", "content": "你好"},
24 ],
25 extra_body=call_args
26)
27print("Chat response:", chat_response)
28
29