Talk

6 Best Practices for LLM Code Generation

From Wikiprompt, the free prompt encyclopedia

宝玉
Contributed by宝玉XSource

Jan 20, 2024

6 Best Practices for LLM Code Generation A comprehensive list of six prompt engineering best practices for using LLMs in code generation, covering YAML output, bullet analysis, modular code, double validation, delayed decisions, and flow-oriented supervision.

Prompt ContentSave

🌐
1. Use YAML structured output instead of JSON format - YAML has higher error tolerance, JSON is prone to format errors that cannot be parsed - YAML content consumes fewer tokens 2. Bullet points analysis - When asking LLM to analyze problems, requesting output in bullet points yields better results - Bullets promote deep understanding and logical semantic segmentation - Example: self-reflection questions with bullets for general description, goals and rules, input structure, output structure 3. LLMs excel at generating modular code - Instruct: 'Split the generated code into multiple small sub-function modules and give them meaningful names' - Results in fewer errors and higher success in iterative fixes 4. Flexible decision-making and double validation - After initial output, ask the model to generate the same output again and correct if necessary - Example: feed the model its own AI tests as input, regenerate tests, and correct errors - More effective than asking 'Is this test correct?' yes/no questions 5. Delay decision-making, avoid direct questions, give exploration space - Start with simplest task: self-reflection and reasoning about public test cases - Then generate additional AI tests and possible solutions - Only after model answers, proceed to actual code generation and iterative fixing - Evaluate and rank multiple possible solutions, prioritize top-ranked ones for initial coding 6. Flow-oriented supervision - Design a scientific flow, accumulate data step by step, add new data at each stage - Example for CodeContests: two-phase flow with steps for reflection, public test analysis, solution generation - Validate and select data at each step, with iterative correction if errors occur

Sign in to see the full prompt

Continue with:

By logging in, you agree to our Terms of Use and Privacy Policy

Usage

This prompt is designed for use with coding. Copy the prompt content above and paste it into your preferred AI tool.

For best results, you may customize the placeholders (indicated by square brackets or capital letters) with your specific requirements.

References

Categories:coding| twitter| llm| code-generation

Talk