import os
import subprocess
from datetime import datetime
# --- Configuration ---
MODEL = "llama3" # Replace with the Ollama model you want
# MODEL = "deepseek-coder"
OUTPUT_DIR = "generated_code"
ESP_TARGETS = ["Arduino", "ESP-IDF"]
CHIPS = ["ESP32", "ESP8266", "ESP32-C3"]
PROMPT_TEMPLATE = """
Generate {count} different working code examples for {chip} using {framework}.
Task: {task}.
Each example should be complete and compilable.
"""
def generate_code(task, count=5):
os.makedirs(OUTPUT_DIR, exist_ok=True)
for chip in CHIPS:
for framework in ESP_TARGETS:
prompt = PROMPT_TEMPLATE.format(
count=count, chip=chip, framework=framework, task=task
)
print(f"[+] Requesting {chip} / {framework} code...")
# Run Ollama CLI (could also use subprocess.run + pipe JSON)
result = subprocess.run(
["ollama", "run", MODEL],
input=prompt.encode(),
capture_output=True,
)
output = result.stdout.decode().strip()
# Save output
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"{OUTPUT_DIR}/{chip}_{framework}_{timestamp}.txt"
with open(filename, "w") as f:
f.write(output)
print(f"[✓] Saved: {filename}")
def run_tasks(taskfile="tasks.txt", count=3):
with open(taskfile, "r") as f:
tasks = [line.strip() for line in f if line.strip()]
for task in tasks:
print(f"\n=== Generating code for: {task} ===")
generate_code(task=task, count=count)
if __name__ == "__main__":
run_tasks("tasks.txt", count=5)