Chapter 2: First steps with AI-assisted coding
The chapter introduction recommends starting with generative AI coding tools by choosing one and experimenting.
After this chapter you will be able to
- Getting started by experimenting. Explain why choosing one tool and trying it is presented as an approachable way to begin using generative AI for coding.
- Copilot’s coding assistance. Describe GitHub Copilot as a responsive coding assistant that suggests completions, helps find and fix mistakes, and can generate functions to reduce repetitive work.
- Editor suggestion and feedback flow. Trace the section’s overview of code going from the editor to the Copilot service and model, suggestions returning to the editor, and accepted changes providing feedback.
- Editor context and FIM. Explain what Copilot sends alongside a comment or chat prompt, and how fill-in-the-middle uses code on both sides of the relevant segment.
- Inbound filtering and generation. Trace the prompt from the editor through the proxy and toxicity filters to the LLM, then explain what the model uses to generate suggestions.
- Outbound suggestion checks. Trace a generated suggestion back toward the IDE and describe the checks it passes before the developer sees it.
- Editor suggestions and code generation. Distinguish Copilot’s real-time code completions from generating larger code segments from comments, statements, or partial snippets. Explain how a hint such as `async def` can steer what it generates.
- Editor commands and project-aware chat. Explain how a developer can request documentation or other help through the editor, right-click commands, or Copilot chat, and describe what distinguishes the chat window.
- Diagnosing and understanding code. Describe how to ask an AI coding tool to find errors in selected code or explain unfamiliar code, and when each request is useful.
- Targeted code improvements. Explain how to request a refactor or suggestions for readability and maintainability, including the kinds of improvements the request can target.
- Code and test generation. Describe how to ask for code for a specific task or tests for existing code, and why those requests can be useful.
- Comparing approach tradeoffs. Explain how to ask an AI assistant to compare two options when choosing an approach to a problem.
- Sources of coding context. Explain that IDE-based coding assistants use more than the current line: they draw on the file being edited and other project files, while available context can also include workspace structure, documentation, configuration, and repository information. Describe how product level can affect the depth of context available.
- Fill-in-the-middle completions. Describe how Copilot’s fill-in-the-middle approach uses code before and after the cursor to suggest a completion that fits the surrounding logic.
- Natural language processing purpose. Explain what NLP enables computers to do with human language and give examples of its uses.
- Text tokenization into chunks. Describe how tokenization breaks text into tokens and why that helps an NLP system analyze text.
- Word-frequency application flow. Trace the project from reading a text file through preparing and counting its words to displaying the most frequent ones in a bar chart.
- Normalization for accurate counts. Explain why the project lowercases text and removes punctuation before counting words, using the truck example.
- Project folder and terminal. Describe how to open an application folder in VS Code and create an integrated terminal for development.
- Copilot function creation methods. Describe the two ways the section uses Copilot to create the text-reading function: completion from a named function and Chat feedback on code already written.
- Generated code review. Explain why the author reviews Copilot's text-reading code instead of assuming it is ready to use, including the encoding example.
- Comment-driven cleanup suggestions. Describe how a code comment elicits suggestions for lowercasing text and removing punctuation, and explain why the reader should compare the alternatives rather than expect a fixed result.
- Verification of Copilot code. Explain why the author times two text-cleaning functions and how the result affects the choice of function.
- Words and frequency counts. Describe how the script moves from cleaned text to counts of unique words, and how Copilot is prompted to help with those steps.
- Chart and script assembly. Trace how the author prompts Copilot to display the most frequent words and make the completed script run.
What it builds on
- What is an LLM, and why should I care? (chapter 1) · The earlier introduction to LLMs provides background for a model that generates text and code.
Section by section
Introduction
The chapter introduction recommends starting with generative AI coding tools by choosing one and experimenting.
- Getting started by experimenting: Explain why choosing one tool and trying it is presented as an approachable way to begin using generative AI for coding.
Why it matters: You can begin learning through hands-on use without settling every tool or model choice first.
Key terms: generative AI tools, experiment, learning curve
2.1 What is GitHub Copilot?
This section introduces GitHub Copilot as a coding assistant, explains what it can suggest, and sketches how its trained model and editor service produce suggestions.
- Copilot’s coding assistance: Describe GitHub Copilot as a responsive coding assistant that suggests completions, helps find and fix mistakes, and can generate functions to reduce repetitive work.
Why it matters: This establishes what kind of help Copilot offers while you code. - Training behind Copilot suggestions: Explain that Copilot uses an OpenAI Codex model trained on large amounts of public code to learn patterns and generate similar code.
Why it matters: The training data helps explain where Copilot’s ability to suggest code comes from. - Editor suggestion and feedback flow: Trace the section’s overview of code going from the editor to the Copilot service and model, suggestions returning to the editor, and accepted changes providing feedback.
Why it matters: This connects the code Copilot can see to the suggestions you receive and the feedback from accepting them. - Copilot availability and cost: State that Copilot is available in several editors and IDEs, normally requires a monthly fee, and is free for the specified eligible groups.
Why it matters: These practical limits help a reader tell whether they can use Copilot in their environment and what access may cost.
Key terms: GitHub Copilot, code suggestions, rubber duck, OpenAI Codex model, public source code, training, Copilot service, context, suggestions, feedback
2.1.1 How GitHub Copilot works
This section traces how GitHub Copilot turns editor context into code suggestions and filters those suggestions on their way back to the IDE.
- Editor context and FIM: Explain what Copilot sends alongside a comment or chat prompt, and how fill-in-the-middle uses code on both sides of the relevant segment.
Why it matters: The context available in your editor helps shape the suggestions you receive. - Inbound filtering and generation: Trace the prompt from the editor through the proxy and toxicity filters to the LLM, then explain what the model uses to generate suggestions.
Why it matters: It shows both why a suggestion can reflect your project and where incoming content is screened. - Outbound suggestion checks: Trace a generated suggestion back toward the IDE and describe the checks it passes before the developer sees it.
Why it matters: A generated suggestion is screened before it appears back in the editor. - Continuous IDE suggestion loop: Explain why the section describes Copilot’s suggestion process as ongoing and how its context and filtering distinguish it from ChatGPT for code suggestions.
Why it matters: It connects the two flows to the experience of receiving suggestions while coding.
Key terms: prompt, context, fill-in-the-middle (FIM), inbound flow, proxy server, toxicity filter, LLM model, outbound flow, code quality, security
2.1.2 Interacting with GitHub Copilot
This section shows how developers can use GitHub Copilot in the editor and chat window to generate, transform, debug, and document code.
- Editor suggestions and code generation: Distinguish Copilot’s real-time code completions from generating larger code segments from comments, statements, or partial snippets. Explain how a hint such as `async def` can steer what it generates.
Why it matters: These are the basic ways to get useful code suggestions while you work in the editor. - Selected code translation and debugging: Describe how highlighting code lets a developer ask Copilot to translate it into another language or suggest fixes for a problematic section.
Why it matters: Copilot can help with existing code, not just code you are writing from scratch. - Editor commands and project-aware chat: Explain how a developer can request documentation or other help through the editor, right-click commands, or Copilot chat, and describe what distinguishes the chat window.
Why it matters: Choosing between an editor command and chat makes it easier to request help and continue the conversation when needed.
Key terms: code completion, code generation, hints, async def, translation, debugging, documentation generation, right-click, chat window, project
2.2 Common patterns
This section gives reusable chat requests for diagnosing, understanding, improving, generating, and evaluating code with AI coding tools.
- Diagnosing and understanding code: Describe how to ask an AI coding tool to find errors in selected code or explain unfamiliar code, and when each request is useful.
Why it matters: These requests help you investigate a problem or understand code before changing it. - Targeted code improvements: Explain how to request a refactor or suggestions for readability and maintainability, including the kinds of improvements the request can target.
Why it matters: Naming the improvement you want gives the tool a useful direction for its suggestions. - Code and test generation: Describe how to ask for code for a specific task or tests for existing code, and why those requests can be useful.
Why it matters: These requests can reduce repetitive work and help uncover missing test cases. - Comparing approach tradeoffs: Explain how to ask an AI assistant to compare two options when choosing an approach to a problem.
Why it matters: Comparing options can help you reconsider an initial approach.
Key terms: selected code, diagnose, legacy code, refactor, security, speed, resource usage, readability, maintainability, specific task
2.3 Context is everything
This section explains how coding assistants use project context, including code around the cursor, to make suggestions that fit existing code.
- Sources of coding context: Explain that IDE-based coding assistants use more than the current line: they draw on the file being edited and other project files, while available context can also include workspace structure, documentation, configuration, and repository information. Describe how product level can affect the depth of context available.
Why it matters: It explains why suggestions can reflect the code you are already working on. - Fill-in-the-middle completions: Describe how Copilot’s fill-in-the-middle approach uses code before and after the cursor to suggest a completion that fits the surrounding logic.
Why it matters: It shows why a completion inserted into existing code can fit better than a generic suggestion.
Key terms: context, workspace, repository-level context, IDE integration, fill-in-the-middle, FIM, cursor
2.4 What is NLP?
The section introduces natural language processing, then explains how tokenization and word-pattern analysis help computers work with human language.
- Natural language processing purpose: Explain what NLP enables computers to do with human language and give examples of its uses.
Why it matters: This gives you a way to describe what NLP tools are trying to accomplish. - Text tokenization into chunks: Describe how tokenization breaks text into tokens and why that helps an NLP system analyze text.
Why it matters: Breaking text into tokens gives the system smaller pieces to analyze. - Patterns in tokenized text: Describe examples of word patterns NLP systems examine and what those patterns can help reveal or classify.
Why it matters: These examples show what a system can examine after text has been broken into pieces.
Key terms: natural language processing, NLP, sentiment analysis, machine translation, text summarization, tokenization, tokens, word-based tokenization, word frequency, word types
2.5 A simple Python project
This section defines a Copilot-assisted Python project that finds the most frequent words in a text file and displays them in a bar chart.
- Word-frequency application flow: Trace the project from reading a text file through preparing and counting its words to displaying the most frequent ones in a bar chart.
Why it matters: This flow gives you a way to describe what the sample program is meant to do. - Normalization for accurate counts: Explain why the project lowercases text and removes punctuation before counting words, using the truck example.
Why it matters: Without this preparation, the frequency ranking would scatter counts for the same word. - Tokens in word counting: Describe tokenization as splitting text into tokens, and distinguish the word tokens the project counts from punctuation tokens it excludes.
Why it matters: The distinction explains why splitting text into tokens is not the same as counting every token.
Key terms: word frequency, top N, bar chart, lowercase, punctuation, word count, tokenization, token
2.5.1 Preparing your development environment
This section sets up a folder in VS Code, identifies the workspace areas used during development, and reviews ways to interact with Copilot.
- Project folder and terminal: Describe how to open an application folder in VS Code and create an integrated terminal for development.
Why it matters: These steps establish the workspace used for the Python application. - VS Code workspace areas: Distinguish the side bar, activity bar, editor window, and terminal window by their roles in this workspace.
Why it matters: Recognizing these areas makes the section’s later workspace directions easier to follow. - Copilot interaction options: Describe the three Copilot interactions reviewed here: comments, code completion, and Copilot Chat.
Why it matters: These are the ways the reader can ask Copilot for help while working in the prepared environment.
Key terms: Open Folder, New Terminal, terminal, side bar, activity bar, Copilot Chat, terminal window, editor window, comment, code completion
2.5.2 Creating the application
The section sets up an isolated Python project and demonstrates ways to use, compare, and review Copilot suggestions while building functions to read and clean a text file.
- Project virtual environment: Explain why the application uses a Python virtual environment and describe how the section checks that it is active.
Why it matters: Isolation helps keep this application's dependencies from interfering with other Python projects. - Copilot function creation methods: Describe the two ways the section uses Copilot to create the text-reading function: completion from a named function and Chat feedback on code already written.
Why it matters: These are two practical ways to get help when starting a function or improving a draft. - Generated code review: Explain why the author reviews Copilot's text-reading code instead of assuming it is ready to use, including the encoding example.
Why it matters: Review catches problems that a plausible-looking suggestion may conceal. - Comment-driven cleanup suggestions: Describe how a code comment elicits suggestions for lowercasing text and removing punctuation, and explain why the reader should compare the alternatives rather than expect a fixed result.
Why it matters: Comparing suggestions helps you choose an understandable approach without mistaking a performance guess for a measured result.
Key terms: Python virtual environment, packages, libraries, code completion, Copilot Chat, refactor, generated code, encoding, manual intervention, comment
2.5.3 Side quest: Testing the function speed
The section tests Copilot-generated text-cleaning functions, then completes a word-frequency script that displays its results as a bar chart.
- Verification of Copilot code: Explain why the author times two text-cleaning functions and how the result affects the choice of function.
Why it matters: A useful suggestion still needs verification before it becomes part of your application. - Words and frequency counts: Describe how the script moves from cleaned text to counts of unique words, and how Copilot is prompted to help with those steps.
Why it matters: Splitting and counting provide the data the chart needs, while leaving room to review Copilot’s implementation. - Chart and script assembly: Trace how the author prompts Copilot to display the most frequent words and make the completed script run.
Why it matters: Generating individual functions is not enough: they must be connected, run, and display their result.
Key terms: Copilot, testing, profiling, clean_text2, tokenize, unique word, frequency, comment prompt, Defaultdict(), Matplotlib
2.6 Summary
Where this is used later
- Using Copilot for test generation (chapter 8) · Understanding the code helps when requesting tests for it.
- Refactoring our Questions class (chapter 5) · Applies requests for code improvements during refactoring.
- Writing unit tests with generative AI (chapter 8) · Develops the use of generative AI to write tests.
- Digging deeper (chapter 3) · Supports considering alternative approaches during design.
- A design document created with ChatGPT (chapter 3) · The importance of context when working with an AI coding assistant.
- Technical stack (section 5) (chapter 3) · The importance of context when working with an AI coding tool.
Concepts explained in this chapter
- GitHub CopilotCopilot Chat is a conversational generative AI tool that answers questions and assists with software-related tasks across web, desktop, and mobile interfaces.
- autocompleteA programming assistance feature that suggests code according to predefined rules or patterns rather than generative prediction.
- code generationCode generation is the automated production of source code from natural-language instructions, existing code, or other structured input.
- promptA prompt is natural-language input that instructs a generative artificial-intelligence system what response or result to produce.
- contextContext is the surrounding conversation history, instructions, assumptions, and other information that determines the meaning and intent of an input.
- PythonPython is a general-purpose, dynamically typed programming language commonly used for application development, automation, and data science.
- virtual environmentA Python virtual environment is an isolated Python runtime environment with its own interpreter and installed packages, separate from other projects and the system installation.
- MatplotlibA Python library for creating static, animated, and interactive data visualizations.
- bar chartA bar chart is a data visualization that represents values with rectangular bars whose lengths correspond to those values.
- performanceThe degree to which software executes efficiently in terms of time and computational-resource use.
- profilingThe measurement and analysis of a program's execution behavior, such as runtime and resource usage, to identify performance characteristics.
- tkinterPython's standard interface to the Tk graphical user-interface toolkit.
- tokenizationTokenization is the process of breaking text into smaller units such as words, punctuation marks, or phrases.
- natural language processingNatural language processing is the computational analysis and generation of human language, including interpreting text and producing meaningful language-related outputs.
- debuggingDebugging is the process of locating, diagnosing, and correcting defects in software.
- documentation generationDocumentation generation is the automatic production of software documentation from source code, comments, or related project information.
- context-aware coding assistanceProgramming assistance that incorporates the surrounding code, project information, and current editing context when generating or suggesting results.
- feedbackUser feedback is information, opinions, or evaluations provided by users about a product, service, or interaction.
- language modelA large language model is a neural language model trained on extensive data to understand and generate language, including programming-language text.
- training datasetTraining data is the collection of examples analyzed by a machine-learning model during training.
- boilerplate codeBoilerplate code is repetitive, standard code required for common implementation tasks but contributing little distinctive application behavior.
- refactoringCode refactoring is the restructuring of existing source code to improve its internal organization without changing its externally observable behavior.
- testA test case is a defined input, execution condition, and expected result used to evaluate software behavior.
- prompt engineeringPrompt engineering is the systematic design and refinement of instructions and context provided to an AI system to elicit desired outputs.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
