-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_model.py
More file actions
66 lines (54 loc) · 1.71 KB
/
test_model.py
File metadata and controls
66 lines (54 loc) · 1.71 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
from transformers.utils import get_json_schema
# --- Tool Definitions (Same as training for consistency) ---
def search_knowledge_base(query: str) -> str:
"""
Search internal company documents, policies and project data.
Args:
query: query string
"""
return "Internal Result"
def search_google(query: str) -> str:
"""
Search public information.
Args:
query: query string
"""
return "Public Result"
TOOLS = [get_json_schema(search_knowledge_base), get_json_schema(search_google)]
# Load the fine-tuned model
# Note: Ensure this matches the output_dir in train.py
checkpoint_dir = "functiongemma-270m-it-simple-tool-calling"
print(f"Loading model from {checkpoint_dir}...")
try:
tokenizer = AutoTokenizer.from_pretrained(checkpoint_dir)
model = AutoModelForCausalLM.from_pretrained(
checkpoint_dir,
device_map="auto",
torch_dtype="auto"
)
except OSError:
print(f"Error: Could not find model in {checkpoint_dir}. Make sure you have run train.py first.")
exit(1)
# Test input
user_query = "What is the reimbursement limit for travel meals?"
print(f"User Query: {user_query}")
# Prepare the prompt
messages = [
{"role": "user", "content": user_query}
]
# Apply template
inputs = tokenizer.apply_chat_template(
messages,
tools=TOOLS,
return_tensors="pt",
add_generation_prompt=True
).to(model.device)
# Generate response
print("Generating response...")
outputs = model.generate(inputs, max_new_tokens=128)
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
print("\n--- Model Output ---")
print(result)
print("--------------------")