![[multistep-prompting-for-programming.png]]
## Brief
LLMs can be used in a multi stage process to plan and construct a software project. Instead of going directly to code, outline the idea, execution and steps in a master plan and refer back to this plan in prompts for actual code generation.
## Steps
### Step 1: Setting the Context
#### 1.1 Define the Project Scope
In this initial phase, you need to clearly outline what the project aims to achieve. This involves:
- **Identifying Objectives**: Clearly state the goals and objectives of the project. What problem is the project aiming to solve? What are the expected outcomes?
- **Determining Requirements**: List down the specific requirements of the project. This could include functional requirements, performance requirements, etc.
- **Choosing Technology Stack**: Decide on the technology stack that will be used in the project. This includes selecting the appropriate programming languages, frameworks, and tools.
- **Selecting Architectural Patterns**: Choose the architectural patterns (like MVVM) that will guide the structure of your project. This helps in organizing the code in a way that separates concerns and makes the codebase maintainable.
#### 1.2 Initial Interaction with ChatGPT
Once the project scope is defined, the next step is to initiate interaction with ChatGPT to leverage its capabilities in the project. This involves:
- **Developing Initial Prompt**: Create an initial prompt that succinctly outlines the project's context and requirements. This prompt serves as a guide for ChatGPT to understand the project's needs and generate relevant responses.
- **Setting Guidelines for ChatGPT**: Establish guidelines for how ChatGPT should assist in the project. This could involve setting rules for the kind of responses you expect from ChatGPT, the format of the code to be generated, etc.
- **Test Interactions**: Before diving deep, conduct some test interactions with ChatGPT to understand its capabilities and limitations better. This helps in setting realistic expectations and planning the project accordingly.
By meticulously setting the context in Step 1, you create a solid foundation that guides the subsequent steps in the project, ensuring a smoother and more efficient development process.
### Step 2: Generating a Master Plan
#### 2.1 Detailing the Components and Layers
- Outline the components, layers, and other elements necessary for the project.
- Specify the implementation strategies and patterns to be followed.
- Break down the task into separate elements to maintain a coherent chain of thought.
#### 2.2 Planning for Code Generation
- Utilize ChatGPT to provide example tests and implementation plans for the outlined components.
- Focus on a test-first approach to ensure the quality and functionality of the code.
### Step 3: Code Generation
- Generate initial code snippets based on the master plan using ChatGPT.
- Ensure the generated code aligns with the project's architectural guidelines and requirements.
- Review the generated code and make necessary adjustments to align with the team's test idioms and project requirements.
### Notes
- Refine the prompt based on the responses from ChatGPT At each step.
- Collaborate with ChatGPT as a "junior partner", guiding it and tweaking its outputs as necessary.
## Definitions
### Generated Knowledge
#### Definition
Generated Knowledge refers to the process of using AI, to generate a detailed plan before moving on to the actual code generation. It involves iteratively refining the prompt based on the responses from ChatGPT to generate code in the desired style and structure.
#### Importance
- **Efficiency**: Helps in creating a blueprint of the code structure, which can streamline the coding process.
- **Quality Assurance**: By focusing on generating a detailed plan first, it ensures that the final code is well-structured and meets the specified requirements.
- **Collaboration**: Facilitates collaboration between the AI and the developer, where the AI can contribute to the planning phase, bringing in diverse perspectives and solutions.
### Chain of Thought
#### Definition
The Chain of Thought refers to maintaining a coherent line of thought or context throughout the interaction with ChatGPT. It involves breaking down the task into separate elements and guiding ChatGPT to generate cohesive code across separate sessions, even when the context window or token limit is reached.
#### Importance
- **Context Preservation**: Helps in preserving the context across separate sessions, ensuring that the generated code is cohesive and aligned with the initial plan.
- **Overcoming Limitations**: Addresses the limitation of ChatGPT's context window, allowing for a seamless interaction even with large amounts of context.
- **Guided Development**: Enables the developer to guide ChatGPT in a structured manner, ensuring that the AI remains aligned with the project's architectural guidelines and requirements.
#### Challenges and Solutions
- **Loss of Context**: ChatGPT may lose context as the conversation progresses, especially when the token limit is reached. This can be mitigated by resetting the context and maintaining a coherent chain of thought.
- **Large Prompt Size**: If the prompt size is too large, it may result in a fatal error. This can be addressed by breaking down the task into smaller, manageable sections and maintaining a coherent chain of thought across separate sessions.
## Sources
- https://martinfowler.com/articles/2023-chatgpt-xu-hao.html - The main article leading to this note
- [Learnprompting.org](https://learnprompting.org/) - A collaborative space to describe prompt engineering.
- [Liu et al](https://arxiv.org/abs/2110.08387) - For more on generated knowledge prompting.
- [Wei et al](https://openreview.net/forum?id=_VjQlMeSB_J) - For more on chain of thought prompting.