OER·harvester

← Back to the library
GitHub NOTEBOOK resource

Phi35 Instruct Demo

Repository 21 Lessons, Get Started Building with Generative AI

Licence
OPEN MIT
Authors
Microsoft (microsoft)
Updated
2025-08-25 · GitHub
Language
en
Length
114 words
Open original ↗
{ }
Jupyter notebook Converted to a read-only view · code is not executed

Source: Phi35 Instruct Demo · GitHub · microsoft/generative-ai-for-beginners Authors: Microsoft (microsoft) Licence: MIT — https://spdx.org/licenses/MIT.html

In [1]
import torch 
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
In [2]
torch.random.manual_seed(0) 
model = AutoModelForCausalLM.from_pretrained( 
    "../phi-3-instruct",  
    device_map="cuda",  
    torch_dtype="auto",  
    trust_remote_code=True,  
)
In [3]
tokenizer = AutoTokenizer.from_pretrained("../phi-3-instruct")
In [4]
# messages = [ 
#     {"role": "system", "content": "你是我的人工智能助手,协助我用中文解答问题"}, 
#     {"role": "user", "content": "你能否介绍一下如何提升学习效率吗?"}, 
# ] 

# <|system|>
# You are a helpful assistant.<|end|>
# <|user|>
# Question?<|end|>
# <|assistant|>

messages = "<|system|>\n 你是我的人工智能助手,协助我用中文解答问题.\n<|end|><|user|>\n 你知道长沙吗?? \n<|end|><|assistant|>"
In [5]
pipe = pipeline( 
    "text-generation", 
    model=model, 
    tokenizer=tokenizer, 
)
In [6]
generation_args = { 
    "max_new_tokens": 1024, 
    "return_full_text": False, 
    "temperature": 0.3, 
    "do_sample": False, 
}
In [7]
output = pipe(messages, **generation_args)
In [8]
output[0]['generated_text']

Disclaimer: This document has been translated using the AI translation service Co-op Translator. While we strive for accuracy, please be aware that automated translations may contain errors or inaccuracies. The original document in its native language should be considered the authoritative source. For critical information, professional human translation is recommended. We are not liable for any misunderstandings or misinterpretations arising from the use of this translation.