Source: Phi35 Instruct Demo · GitHub · microsoft/generative-ai-for-beginners Authors: Microsoft (microsoft) Licence: MIT — https://spdx.org/licenses/MIT.html
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
torch.random.manual_seed(0)
model = AutoModelForCausalLM.from_pretrained(
"../phi-3-instruct",
device_map="cuda",
torch_dtype="auto",
trust_remote_code=True,
)
tokenizer = AutoTokenizer.from_pretrained("../phi-3-instruct")
# 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|>"
pipe = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
)
generation_args = {
"max_new_tokens": 1024,
"return_full_text": False,
"temperature": 0.3,
"do_sample": False,
}
output = pipe(messages, **generation_args)
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.