Text Generation
Transformers
Safetensors
English
qwen2
chat
conversational
text-generation-inference
Instructions to use Qwen/Qwen2-7B-Instruct with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Qwen/Qwen2-7B-Instruct with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="Qwen/Qwen2-7B-Instruct") messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2-7B-Instruct") model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2-7B-Instruct") messages = [ {"role": "user", "content": "Who are you?"}, ] inputs = tokenizer.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=40) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Inference
- Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use Qwen/Qwen2-7B-Instruct with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "Qwen/Qwen2-7B-Instruct" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Qwen/Qwen2-7B-Instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/Qwen/Qwen2-7B-Instruct
- SGLang
How to use Qwen/Qwen2-7B-Instruct with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "Qwen/Qwen2-7B-Instruct" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Qwen/Qwen2-7B-Instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "Qwen/Qwen2-7B-Instruct" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Qwen/Qwen2-7B-Instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use Qwen/Qwen2-7B-Instruct with Docker Model Runner:
docker model run hf.co/Qwen/Qwen2-7B-Instruct
在提示词使用结构化输出,内容会少很多, 甚至会出现幻觉!
#14
by weiminw - opened
你好,再使用Qwen的时候,对于同一个问题,比如使用下面的Pormpt:
"""
根据我提供的历史步骤和我的任务,判断该任务是否完成.
我的任务:
{task}
历史步骤:
{past_steps}
让我们一步一步思考, 返回你的详细分析.
"""
根据我的一些输入得出的结果是任务没有完成,并给我了详细的分析(和事实上人类分析一致)
如果其他都不变,使用下面的Prompt:
"""
根据我提供的历史步骤和我的任务,判断该任务是否完成. 按照下列的JSON格式返回.
回复的格式指令:
{{
"result": str , \\ 你的判断结果,任务完成为True, 任务没有完成为False
"reason": str \\ 你判断的依据,包含详细的推理过程.
}}
我的任务:
{task}
历史步骤:
{past_steps}
让我们一步一步思考, 返回你的详细分析.
"""
输入这些都不变, 我的得到的结果是result=True,并且reason字段的内容也很少。
所有qwen系列都有这个问题,包括之前的qwen1.5