Context manipulation arranges relevant information, examples, instructions, and behavioral constraints inside a prompt so an AI model produces more accurate, consistent, and specific results. It creates an environment that keeps the model focused on the task. section 9.5.3
Use it when you need to:
- Enforce a coding style, library choice, function structure, or output tone. section 9.5.3
- Maintain consistency across functions or modules. section 9.5.3
- Simplify complex requests with initial parameters and examples. section 9.5.3
- Match explanations or documentation to an existing structure. section 9.5.3
A constrained pandas task
Start by defining the role and rules: “You are a Python assistant. Always use the pandas library for data manipulation tasks, and structure your code with a function called process_data. Avoid using any other libraries unless specified.” Then request a function that reads a CSV file, filters rows where age exceeds 30, and sorts the result. The resulting structure reads the file with pandas, filters age, sorts by age, and returns the data. section 9.5.3
import pandas as pd def process_data(file_path): df = pd.read_csv(file_path) filtered_df = df[df['age'] > 30] sorted_df = filtered_df.sort_values(by='age') return sorted_df
Inspect the compact pandas implementation used to demonstrate how contextual constraints shape generated code. section 9.5.3
