-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathphi4.py
More file actions
52 lines (40 loc) · 1.23 KB
/
phi4.py
File metadata and controls
52 lines (40 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
torch.manual_seed(0)
model_path = "microsoft/Phi-4-mini-instruct"
# 모델 및 토크나이저 로딩
model = AutoModelForCausalLM.from_pretrained(
model_path,
device_map="auto",
torch_dtype="auto",
trust_remote_code=True,
)
tokenizer = AutoTokenizer.from_pretrained(model_path)
# 파이프라인 생성
pipe = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
)
# 대화 히스토리 초기화
messages = [
{"role": "system", "content": "You are a helpful AI assistant."}
]
generation_args = {
"max_new_tokens": 256,
"return_full_text": False,
"temperature": 0.7,
"do_sample": False,
}
print("💬 대화를 시작하세요. 종료하려면 'exit' 입력")
while True:
user_input = input("👤 You: ")
if user_input.strip().lower() in ['exit', 'quit']:
print("👋 대화를 종료합니다.")
break
messages.append({"role": "user", "content": user_input})
# 모델 호출
output = pipe(messages, **generation_args)
reply = output[0]["generated_text"]
print(f"🤖 AI: {reply.strip()}")
messages.append({"role": "assistant", "content": reply.strip()})