Views
No views yet
codellama/CodeLlama-7b-Instruct-hf designed specifically to translate natural language project proposals into concrete, hierarchical directory structures.<TREE_START> / <TREE_END>: Bounds the entire project structure.<DIR_START> / <DIR_END>: Bounds a directory/folder.<FILE>: Indicates a file.1pip install transformers torch
21import torch
2import re
3from transformers import AutoTokenizer, AutoModelForCausalLM
4
5model_id = "your_hf_username/CodeLlama-7b-NL2PPlanner"
6
7# Load tokenizer and model
8tokenizer = AutoTokenizer.from_pretrained(model_id)
9model = AutoModelForCausalLM.from_pretrained(
10 model_id,
11 torch_dtype=torch.bfloat16,
12 device_map="auto"
13)
14
15# Function to parse generated tokens back to a dictionary tree
16def regex_parser(text):
17 tree = {}
18 stack = [tree]
19 token_pattern = re.compile(r"(<FILE>|<DIR_START>|<DIR_END>)\s+([^\s<]+)?")
20 matches = token_pattern.findall(text)
21
22 for tag, name in matches:
23 if tag == "<FILE>" and name:
24 stack[-1][name] = "file"
25 elif tag == "<DIR_START>" and name:
26 new_dir = {}
27 stack[-1][name] = new_dir
28 stack.append(new_dir)
29 elif tag == "<DIR_END>" and len(stack) > 1:
30 stack.pop()
31 return tree
32
33# System & User Prompt formatting
34system_prompt = """You are a Principal Software Architect. Design the directory structure.
35[GRAMMAR RULES]
361. Start with <TREE_START> and end with <TREE_END>.
372. Folders: <DIR_START> name ... <DIR_END>
383. Files: <FILE> name
394. NO JSON. ONLY TOKENS."""
40
41user_input = """Design structure for: "E-Commerce Backend"
42[CONTEXT]
43- Domain: E-commerce (Web API)
44- Desc: A robust backend for handling users, products, and orders.
45- Stack: Python, PostgreSQL, Docker
46[ARCH]
47 - **AuthModule**: `/src/auth` (Handles JWT authentication).
48 - **OrderModule**: `/src/orders` (Processes checkout logic).
49[ENTRIES]
50['/src/main.py']
51[COMMAND]
52Generate Linearized Token Sequence."""
53
54formatted_prompt = f"<s>[INST] <<SYS>>\n{system_prompt}\n<</SYS>>\n\n<|user|>{user_input}<|end|> [/INST]"
55
56# Generate
57inputs = tokenizer(formatted_prompt, return_tensors="pt").to("cuda")
58with torch.no_grad():
59 outputs = model.generate(
60 **inputs,
61 max_new_tokens=1024,
62 do_sample=False
63 )
64
65output_text = tokenizer.decode(outputs[0], skip_special_tokens=False)
66generated_sequence = output_text.split("[/INST]")[1]
67
68# Parse to JSON
69directory_tree = regex_parser(generated_sequence)
70print(directory_tree)
71