Views
No views yet
1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3
4# Load model and tokenizer
5device = "cuda" if torch.cuda.is_available() else "cpu"
6model_name = "oscarwu/Llama-3.2-3B-CLEAR"
7
8tokenizer = AutoTokenizer.from_pretrained(model_name)
9model = AutoModelForCausalLM.from_pretrained(
10 model_name,
11 torch_dtype=torch.float16
12).to(device)
13
14
15
16
17# Example query
18query = "I live in Burwood (Vic) and want details on renewable energy initiatives. Are solar farms planned?"
19
20# Format prompt
21prompt = f"""Below is an instruction that describes a task, paired with an input that provides further context. Your response must be a valid JSON object, strictly following the requested format.
22### Instruction:
23Extract location, topics, and search queries from Australian climate policy questions. Your response must be a valid JSON object with the following structure:
24{{
25 "rag_queries": ["query1", "query2", "query3"], // 1-3 policy search queries
26 "topics": ["topic1", "topic2", "topic3"], // 1-3 climate/environment topics
27 "location": {{
28 "query_suburb": "suburb_name or null",
29 "query_state": "state_code or null",
30 "query_lga": "lga_name or null"
31 }}
32}}
33### Input:
34{query}
35### Response (valid JSON only):
36"""
37
38
39
40
41# Generate response
42inputs = tokenizer(prompt, return_tensors="pt").to(device)
43outputs = model.generate(**inputs, max_new_tokens=220)
44result = tokenizer.decode(outputs[0], skip_special_tokens=True)
45
46```json
47
48{
49 "rag_queries": [
50 "What renewable energy projects are planned for Burwood?",
51 "Are there solar farm initiatives in Burwood Victoria?"
52 ],
53 "topics": [
54 "renewable energy",
55 "solar power"
56 ],
57 "location": {
58 "query_suburb": "Burwood",
59 "query_state": "VIC",
60 "query_lga": null
61 }
62}