Prompt Engineering Guide
In the era of AI-assisted programming, Prompt Engineering is a core skill. Writing high-quality prompts helps OpenCodex better understand your intent and generate code that meets your requirements.
What is Prompt Engineering?
Prompt Engineering is the process of designing and optimizing the text input (the "prompt") given to an AI model to guide it towards generating specific outputs. In OpenCodex, this means how you describe your programming tasks in natural language.
Basic Principles
1. Be Clear
Avoid ambiguous descriptions.
- ❌ "Write a function to handle data."
- ✅ "Write a Python function that takes a CSV file path, reads the data, and returns a list containing all rows."
2. Context is Key
Tell the AI what project you are working on and what frameworks you are using.
- ✅ "This is a React and TypeScript project. Please create a component..."
3. Specify Output
If you need a specific format, state it clearly.
- ✅ "Please list the pros and cons in a Markdown table."
- ✅ "Return only the code, no explanation needed."
Common Scenarios
Code Generation
"Implement a counter component using Vue 3 Composition API, including increment, decrement, and reset functionality."
Code Explanation
"Explain what this code does, specifically the logic within the
useEffecthook."
Code Refactoring
"Refactor this function to make it more readable and extract repeated logic into helper functions."
Bug Fixing
"I encountered a
NullPointerException. Here is the relevant code snippet, please analyze the cause and fix it."
Advanced Tips
- Step-by-Step Instructions: Break down complex tasks into multiple simple steps.
- Role Playing: Tell the AI "You are a senior frontend architect".
- Few-Shot Prompting: Provide a few examples of input and output for the AI to mimic.
Master these skills to use OpenCodex more efficiently!