Refactoring with chain-of-thought
In Chapter 9, we introduced chain-of-thought (CoT) as a key prompt engineering technique for working with GenAI to generate code. This method involves writing a high-level function as the prompt, while leaving the implementation details for the GenAI application to complete.
When refactoring code, we want to include the old implementation as an additional context for the model. This can help it better understand the intended functionality. For instance, in the case of the calculate_distance
function, the previous implementation can clarify which parameters need to be extracted from the JSON request: a
, b
, and dist_type
.
Refactoring for a better structure
When reviewing the implementation of the calculate_distance
function, we can break it down into three main steps:
- Extracting the request parameters
- Deciding whether to compute L1 (Manhattan) or L2 (Euclidean) distance
- Calculating the distance
A CoT prompt...