Skip to content

Tutorial: RAG-Grounded Code Generation

In this tutorial you'll build a working RAG-grounded code generation script. When you finish, you'll have a Python program that initializes RagCodeGenWorkflow, submits a question, and prints a response that cites real attune APIs and workflow names — never invented ones.

What you are building: a short script that runs RagCodeGenWorkflow.execute() end-to-end, so you can see how retrieved documentation context flows into a grounded code answer with provenance.

Prerequisites

  • Python 3.10 or newer
  • The attune package installed in your environment
  • A basic understanding of how retrieval-augmented generation (RAG) works — you don't need to be an expert, but knowing that RAG means "answer from retrieved documents, not from model memory" will help you follow the reasoning below

Why RAG grounding matters here

Standard code-generation prompts let the model invent library names, method signatures, and CLI flags. RagCodeGenWorkflow prevents that by retrieving real attune-help passages first and injecting them as context. The system prompt that ships with the workflow explicitly instructs the model to cite source files and to ignore any instruction-like text it finds inside a retrieved passage — so the grounding is tamper-resistant by design.

Understanding this mechanism is why each step below explains what is happening before showing the code.


Step 1 — Import the workflow class

The workflow lives in workflows.rag_code_gen. Import it directly:

from workflows.rag_code_gen import RagCodeGenWorkflow

Verify: open a Python REPL, paste the import, and confirm no ModuleNotFoundError is raised. If you see one, check that the attune package is on your PYTHONPATH.


Step 2 — Initialize the workflow

RagCodeGenWorkflow.__init__ accepts keyword arguments that control retrieval and generation settings. For this tutorial, start with no arguments so you can see the defaults in action:

workflow = RagCodeGenWorkflow()

No arguments means the workflow uses its built-in defaults — the same citation-forced system prompt and retrieval configuration the attune team ships. You'll override these later once you understand the baseline.

Verify: print(workflow) should not raise an error. You now hold an instance ready to execute.


Step 3 — Execute the workflow with a question

execute() is the single entry point that triggers retrieval, prompt assembly, and model inference. Pass your question as a keyword argument:

result = workflow.execute(query="How do I define a new task template?")

The workflow retrieves relevant attune-help passages, wraps them in <passage>...</passage> tags, and sends them to the model alongside your query. The model is instructed to cite the source file of every pattern it references.

Verify: print(type(result)) should print <class 'workflows.rag_code_gen.WorkflowResult'>. If you see an exception instead, check your API credentials and network access.


Step 4 — Read the grounded answer

execute() returns a WorkflowResult. Inspect its content to see the grounded answer:

print(result)

Look for two things in the output:

  1. Cited source files — the response should reference real attune file paths, not invented ones.
  2. No hallucinated API names — every class, method, or CLI flag mentioned should match something in the attune codebase.

If either condition is violated, the retrieval step may not have found relevant context. Try a more specific query and re-run execute().


What you learned

  • Step 1 showed you that RagCodeGenWorkflow lives at workflows.rag_code_gen and has a clean, importable public API.
  • Step 2 showed you that the workflow ships with a citation-forcing system prompt by default — you don't have to wire up grounding yourself.
  • Step 3 showed you that execute() is the single call that handles retrieval, prompt assembly, and inference in one shot, returning a WorkflowResult.
  • Step 4 showed you how to verify grounding quality: check that source files are cited and that no attune features are invented.

Next step

Read the RagCodeGenWorkflow reference to see every keyword argument accepted by __init__ and execute() — including how to swap in a different retrieval source or adjust the citation requirements.

Unresolved references

Auto-generated by attune-author fact-check. Review and either fix the source code, fix this doc, or add an override.

Location Severity Issue
Line 32 (code fence) error from workflows.rag_code_gen import … — module not importable
Line 45 (python fence) error Name "RagCodeGenWorkflow" is not defined [name-defined]
Line 59 (python fence) error Name "workflow" is not defined [name-defined]
Line 73 (python fence) error Name "result" is not defined [name-defined]