-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_gen.py
More file actions
77 lines (61 loc) · 2.25 KB
/
code_gen.py
File metadata and controls
77 lines (61 loc) · 2.25 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
# v0.1.0
# { "Depends": "py-genlayer:latest" }
from genlayer import *
import json
import typing
class CodeGen(gl.Contract):
"""
Converts natural language intents into Python code snippets.
Uses Functional Consensus to accept code that works the same way
even if variable names or formatting differ.
"""
# Stores: Intent -> Python Code
snippets: TreeMap[str, str]
def __init__(self):
pass
@gl.public.write
def generate_python(self, intent: str) -> None:
"""
Generates Python code based on the user's intent.
Returns NONE to avoid simulator serialization crashes.
"""
def generate_nondet() -> str:
task = f"""
Act as a Python Developer.
Task: Write a simple Python code block for this intent:
"{intent}"
Instructions:
1. Use standard, readable variable names.
2. Keep logic simple and direct.
3. Do NOT include comments or explanations.
4. Return ONLY the code.
"""
result_raw = gl.nondet.exec_prompt(task)
# Clean up Markdown
cleaned = result_raw.replace("```python", "").replace("```", "").strip()
return cleaned
# Consensus: Functional Equivalence
# We ask the LLM to judge if two code snippets do the same thing.
comparison_criteria = """
Compare the two Python code snippets.
Logic:
1. Ignore variable naming differences (e.g., 'x' vs 'val').
2. Ignore whitespace, indentation style, or comments.
3. Check if the LOGIC and FLOW are identical.
4. If both snippets implement the exact same intent, they are EQUAL.
"""
consensus_code = gl.eq_principle.prompt_comparative(
generate_nondet,
comparison_criteria
)
# Store the result
self.snippets[intent] = consensus_code
return None
@gl.public.view
def get_code(self, intent: str) -> str:
"""
Returns the generated Python code.
"""
if intent in self.snippets:
return self.snippets[intent]
return "# No code generated"