-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinference.py
More file actions
119 lines (93 loc) · 2.77 KB
/
Copy pathinference.py
File metadata and controls
119 lines (93 loc) · 2.77 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
MODEL_PATH = "/ft-reward-retraining/outputs/sft_grpo/grpo/checkpoint-800"
print("Loading tokenizer...")
tokenizer = AutoTokenizer.from_pretrained(
MODEL_PATH,
trust_remote_code=True,
)
print("Loading model...")
model = AutoModelForCausalLM.from_pretrained(
MODEL_PATH,
torch_dtype=torch.bfloat16,
device_map="auto",
trust_remote_code=True,
)
model.eval()
print("Model loaded.")
print("Device:", next(model.parameters()).device)
def generate(prompt, max_new_tokens=1024):
messages = [
{
"role": "user",
"content": prompt,
}
]
inputs = tokenizer.apply_chat_template(
messages,
tokenize=True,
add_generation_prompt=True,
return_tensors="pt",
return_dict=True,
)
inputs = {
key: value.to(model.device)
for key, value in inputs.items()
}
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=max_new_tokens,
temperature=0.7,
top_p=0.9,
do_sample=True,
pad_token_id=tokenizer.eos_token_id,
)
input_length = inputs["input_ids"].shape[-1]
generated_tokens = outputs[0][input_length:]
response = tokenizer.decode(
generated_tokens,
skip_special_tokens=True,
)
return response
if __name__ == "__main__":
prompt = """
You are a precise function-calling assistant. Given a user query and a list of available tools, respond with a JSON array containing all the tool calls needed to answer the query.
Each element of the array must be a JSON object with exactly two keys:
"name" : the tool name (string)
"arguments" : a dict of argument name → value
Wrap your entire response in a single ```json ... ``` code block.
Do not include any explanation outside the code block.
User query:
Fetch news articles about cricket from India Today and health-related news in the US
Available tools:
[
{
"name": "tag_search",
"description": "Fetches news articles based on the provided tag from the India Today API.",
"parameters": {
"tag": {
"description": "The tag or keyword to search for in the news articles.",
"type": "str",
"default": "India"
}
}
},
{
"name": "health",
"description": "Fetches health-related news from Google News using the provided language region and API key.",
"parameters": {
"lr": {
"description": "Language region for the news, e.g., 'en-US'.",
"type": "str",
"default": "en-US"
}
}
}
]
"""
response = generate(prompt)
print("\n" + "=" * 80)
print("MODEL OUTPUT")
print("=" * 80)
print(response)