Instructions to use microsoft/Phi-3.5-MoE-instruct with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use microsoft/Phi-3.5-MoE-instruct with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="microsoft/Phi-3.5-MoE-instruct", trust_remote_code=True) messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-3.5-MoE-instruct", trust_remote_code=True) model = AutoModelForCausalLM.from_pretrained("microsoft/Phi-3.5-MoE-instruct", trust_remote_code=True) 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]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use microsoft/Phi-3.5-MoE-instruct with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "microsoft/Phi-3.5-MoE-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": "microsoft/Phi-3.5-MoE-instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/microsoft/Phi-3.5-MoE-instruct
- SGLang
How to use microsoft/Phi-3.5-MoE-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 "microsoft/Phi-3.5-MoE-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": "microsoft/Phi-3.5-MoE-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 "microsoft/Phi-3.5-MoE-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": "microsoft/Phi-3.5-MoE-instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use microsoft/Phi-3.5-MoE-instruct with Docker Model Runner:
docker model run hf.co/microsoft/Phi-3.5-MoE-instruct
Model consistently gets into a loop to repeat itself if there is too much in the context window
I've found that the model will consistently get into a loop and repeat itself, which is unfortunate because it's otherwise excellent. This happens when the input gets too long, although I haven't tried to see where exactly the errors start to happen. I am running the model using a FastAPI endpoint, but I doubt that is the case. Here is the function I have been using. Note that the generation parameters and the model loading are all per the document, but the max_length is passed in as a parameter during a call to the function via the web request. It doesn't seem to matter if I use a shorter max_length, though. The problem is when the prompt itself gets too long.
async def generate_text(request: RequestModel):
print(request)
generation_args = {
"max_new_tokens": request.max_length,
"return_full_text": False,
"temperature": 0.0,
"do_sample": False,
}
#inputs = tokenizer.encode(request.prompt, return_tensors="pt").to(device)
try:
messages = [{"role":"user","content":request.prompt}]
outputs = pipe(messages, **generation_args)
print(outputs[0]["generated_text"])
response_text = outputs[0]["generated_text"]#tokenizer.decode(outputs[0], skip_special_tokens=True)
print(response_text)
# Strip the prompt from the generated text
#if response_text.startswith(request.prompt):
# response_text = response_text[len(request.prompt):].strip()
return ResponseModel(response=response_text)
except Exception as e:
print(e)
raise HTTPException(status_code=500, detail=str(e))
I am having the same issue in LM Studio. I suspect it's a Prompt template issue.
I see. Do you imagine the issue will go away if I were to avoid using the pipeline?
hi there,
I'm experiencing the same issue. I haven't found the sweet spot either, but Phi seems to start generating gibberish once the input exceeds 4k tokens.
I must admit I've given up on Phi for this reason. Qwen 32B is smaller in size, better at reasoning, and doesn't have this issue, plus I haven't found it to be any slower.