Discover millions of audiobooks, ebooks, and so much more with a free trial

From $11.99/month after trial. Cancel anytime.

AI-Assisted Programming for Web and Machine Learning: Improve your development workflow with ChatGPT and GitHub Copilot
AI-Assisted Programming for Web and Machine Learning: Improve your development workflow with ChatGPT and GitHub Copilot
AI-Assisted Programming for Web and Machine Learning: Improve your development workflow with ChatGPT and GitHub Copilot
Ebook1,552 pages7 hours

AI-Assisted Programming for Web and Machine Learning: Improve your development workflow with ChatGPT and GitHub Copilot

Rating: 0 out of 5 stars

()

Read preview
LanguageEnglish
Release dateAug 30, 2024
ISBN9781835083895
AI-Assisted Programming for Web and Machine Learning: Improve your development workflow with ChatGPT and GitHub Copilot

Related to AI-Assisted Programming for Web and Machine Learning

Related ebooks

Software Development & Engineering For You

View More

Reviews for AI-Assisted Programming for Web and Machine Learning

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    AI-Assisted Programming for Web and Machine Learning - Christoffer Noring

    Cover of AI-Assisted Programming for Web and Machine Learning by Christoffer Noring | Anjali Jain | Marina Fernandez | Ayşe Mutlu | Ajit Jaokar

    AI-Assisted Programming for Web and Machine Learning

    Improve your development workflow with ChatGPT and GitHub Copilot

    Christoffer Noring

    Anjali Jain

    Marina Fernandez

    Ayşe Mutlu

    Ajit Jaokar

    AI-Assisted Programming for Web and Machine Learning

    Copyright © 2024 Packt Publishing

    All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

    Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

    Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

    Senior Publishing Product Manager: Bhavesh Amin

    Acquisition Editor – Peer Reviews: Gaurav Gavas

    Project Editor: Meenakshi Vijay

    Content Development Editor: Deepayan Bhattacharjee

    Copy Editor: Safis Editing

    Technical Editor: Tejas Mhasvekar

    Proofreader: Safis Editing

    Indexer: Tejal Soni

    Presentation Designer: Rajesh Shirsath

    Developer Relations Marketing Executive: Sohini Ghosh

    First published: August 2024

    Production reference: 1270824

    Published by Packt Publishing Ltd.

    Grosvenor House

    11 St Paul’s Square

    Birmingham

    B3 1RB, UK.

    ISBN 978-1-83508-605-6

    www.packt.com

    Contributors

    About the authors

    Christoffer Noring works as a Senior Advocate at Microsoft and focuses on application development and AI. He’s a Google Developer Expert and a public speaker on 100+ presentations across the world. Additionally, he’s a tutor at the University of Oxford on cloud patterns and AI. Chris is also a published author on Angular, NGRX, and programming with Go.

    Anjali Jain is a London-based AI and ML professional with a career spanning over two decades. Currently working as a data architect for Metrobank, she brings her expertise in AI, data, architecture, data governance, and software development to the financial sector. Anjali holds a bachelor’s degree in electrical engineering and boasts certifications, including TOGAF 9.1 and ITIL 2011 Foundation. In her role as Senior AI and ML tutor at Oxford, she shares cutting-edge knowledge on various technologies.

    Marina Fernandez is a data science and Databricks consultant with expertise in financial risk management. She contributes to the academic team at the University of Oxford, where she holds the positions of senior AI and ML tutor and guest lecturer. Throughout her 20-year career, Marina has worked on the development of large-scale enterprise systems for various business domains. Her experience encompasses e-commerce, e-learning, software security, commodity trading, commodity trading and risk management systems, and regulatory reporting. Marina obtained her MSc in Software Engineering from the University of Oxford. Additionally, she has earned professional certifications, including Microsoft Certified Professional and Certified Scrum Master.

    Ayşe Mutlu is a data scientist working on Azure AI and DevOps technologies. Based in London, Ayşe’s work involves building and deploying Machine Learning and Deep Learning models using the Microsoft Azure framework (Azure DevOps and Azure Pipelines). She enjoys coding in Python and contributing to open-source initiatives in Python.

    Ajit Jaokar is a data scientist for Feynlabs, building AI prototypes for complex applications. He is also a course director for AI at the University of Oxford. Besides this, Ajit is a visiting fellow in Engineering Sciences at the University of Oxford and conducts AI courses at the London School of Economics, Universidad Politécnica de Madrid, and the Harvard Kennedy School of Government as part of The Future Society. His work at Oxford and his company is based on interdisciplinary aspects of AI, including AI with digital twins, quantum computing, metaverse, Agtech, and life sciences. His teaching is based on a methodology for AI and cyber-physical systems, which he is developing as part of his research.

    About the reviewers

    Maxim Salnikov is a tech and cloud community enthusiast based in Oslo. With over two decades of experience as a web developer, he shares his extensive knowledge of the web platform, cloud computing, and AI by speaking at and providing training for developer events worldwide. By day, Maxim plays a crucial role in supporting the development of cloud and AI solutions within European companies, serving as the leader of developer productivity business at Microsoft. During evenings, he can be found running events for Norway’s largest web and cloud development communities. Maxim is passionate about exploring and experimenting with Generative AI possibilities, including AI-assisted development. To share his insights and connect with like-minded professionals globally, he founded and organized the inaugural Prompt Engineering Conference, the first of its kind on a global scale.

    Şaban Kara is an AI and ML software engineer who graduated from Gebze Technical University Electronics Engineering. Throughout his career, Şaban has developed several NLP projects and worked on various probabilistic statistics-based ML algorithms. Şaban is especially known for his interest in LLM and LangChain models. His work on these models focuses on improving spontaneous learning abilities. He started his career working at TUBITAK. Currently, he is developing ML algorithms on LLM models in a private company and making a self-learning AI.

    I would like to thank my family, friends, and colleagues for their contributions to the preparation of this book. Their support played an important role in the success of this project.

    Join our community on Discord

    Join our community’s Discord space for discussions with the author and other readers:

    https://packt.link/aicode

    Contents

    Preface

    Who this book is for

    What this book covers

    To get the most out of this book

    Get in touch

    It’s a New World, One with AI Assistants, and You’re Invited

    Introduction

    How ChatGPT came to be, from NLP to LLMs

    The rise of LLMs

    GPT models

    How LLMs are better

    The new paradigm, programming with natural language

    Challenges and limitations

    About this book

    Who this book is for

    Evolution of programming languages

    Looking ahead

    How to use this book

    Prompt Strategy

    Introduction

    Where you are

    Guidelines for how to prompt efficiently

    Prompt techniques

    Task-Action-Guideline prompt pattern (TAG)

    Persona-Instruction-Context prompt pattern (PIC)

    Exploratory prompt pattern

    Learn-Improvise-Feedback-Evaluate prompt pattern (LIFE)

    Which pattern to choose?

    Prompt strategy for web development

    Break down the problem: web system for inventory management

    Further breakdown of the frontend into features

    Generate prompts for each feature

    Identify some basic principles for web development, a prompt strategy

    Prompt strategy for data science

    Problem breakdown: predict sales

    Further breakdown into features/steps for data science

    Generate prompts for each step

    Identify some basic principles for data science, a prompt strategy for data science

    Validate the solution

    Verification via prompts

    Classical verification

    Summary

    Tools of the Trade: Introducing Our AI Assistants

    Introduction

    Understanding Copilot

    How Copilot knows what to generate

    Copilot capabilities and limits

    Setup and installation

    Installing Copilot

    Getting started with Copilot

    Assignment: improve the code

    Solution

    Challenge

    References

    Understanding ChatGPT

    How does ChatGPT work?

    ChatGPT capabilities and limits

    Setup and installation

    Getting started with ChatGPT

    Prompting

    Summary

    Build the Appearance of Our App with HTML and Copilot

    Introduction

    Business problem: e-commerce

    Problem domain

    Problem breakdown: identify the features

    Prompt strategy

    Page structure

    Add AI assistance to our page structure

    Your first prompt, simple prompting, and aiding your AI assistant

    Your second prompt: adding more context

    Your third prompt: accept prompt suggestions

    Challenge: vary the prompt

    Use case: build a front for an e-commerce

    Login page

    Product list page

    Remaining pages

    Assignment

    Challenge

    Quiz

    Summary

    Style the App with CSS and Copilot

    Introduction

    Business problem: e-commerce

    Problem and data domain

    Breaking the problem down into features

    Prompting strategy

    CSS, or Cascading Style Sheets

    First CSS

    CSS by name

    Assignment

    Solution

    Use case: style the e-commerce app

    Basket page

    Challenge

    Quiz

    Summary

    Add Behavior with JavaScript

    Introduction

    Business problem: e-commerce

    Problem and data domain

    Breaking the problem down into features

    Prompting strategy

    Adding JavaScript

    The role of JavaScript

    Adding JavaScript to a page

    A second example: adding a JavaScript library/framework

    Challenge

    Use case: adding behavior

    Improving the output

    Adding Bootstrap

    Adding Vue.js

    Assignment

    Solution

    Summary

    Support Multiple Viewports Using Responsive Web Layouts

    Introduction

    Business problem: e-commerce

    Problem and data domain

    Breaking the problem down into features

    Prompting strategy

    Viewports

    Media queries

    When to adjust to different viewports and make it responsive

    Use case: make our product gallery responsive

    Assignment

    Solution

    Challenge

    Summary

    Build a Backend with Web APIs

    Introduction

    Business domain: e-commerce

    Problem and data domain

    Feature breakdown

    Prompt strategy

    Web APIs

    What language and framework should you pick?

    Planning the Web API

    Creating a Web API with Python and Flask

    Step 1: Create a new project

    Step 2: Install Flask

    Step 3: Create an entry point

    Step 4: Create a Flask app

    Use case: a Web API for an e-commerce site

    Step 1: Create a Web API for an e-commerce site

    Step 2: Return JSON instead of text

    Step 3: Add code to read and write to a database

    Step 4: Improve the code

    Run the code

    Refactor the code

    Step 5: Document the API

    Assignment

    Solution

    Challenge

    Summary

    Augment Web Apps with AI Services

    Introduction

    Business domain, e-commerce

    Problem and data domain

    Feature breakdown

    Prompt strategy

    Creating a model

    Coming up with a plan

    Importing libraries

    Reading the CSV file

    Creating test and training datasets

    Creating a model

    How good is the model?

    Predict

    Saving the model to a .pkl file

    Creating a REST API in Python

    Converting the model to ONNX

    Creating a model in ONNX format

    Loading the ONNX model in JavaScript

    Installing onnxruntime in JavaScript

    Loading the ONNX model in JavaScript

    Assignment: Build a REST API in JavaScript that consumes the model

    Solution

    Quiz

    Summary

    Maintaining Existing Codebases

    Introduction

    Prompt strategy

    Different types of maintenance

    The maintenance process

    Addressing a bug

    1. Identify the problem

    2. Implement the change

    Adding a new feature

    1. Identify a problem and find the function/s to change

    2. Implement change, and add a new feature and tests

    Improving performance

    Big O notation calculation

    Measuring performance

    Improving maintainability

    1. Identify the problems. What problems do you see?

    2. Add tests and de-risk change

    3. Implement change and improve maintainability

    Challenge

    Updating an existing e-commerce site

    Assignment

    Knowledge check

    Summary

    Data Exploration with ChatGPT

    Introduction

    Business problem

    Problem and data domain

    Dataset overview

    Feature breakdown

    Prompting strategy

    Strategy 1: Task-Actions-Guidelines (TAG) prompt strategy

    Strategy 2: Persona-Instructions-Context (PIC) prompt strategy

    Strategy 3: Learn-Improvise-Feedback-Evaluate (LIFE) prompt strategy

    Data exploration of the Amazon review dataset using the free version of ChatGPT

    Feature 1: Loading the dataset

    Feature 2: Inspecting the data

    Feature 3: Summary statistics

    Feature 4: Exploring categorical variables

    Feature 5: Rating distribution

    Feature 6: Temporal trends

    Feature 7: Review length analysis

    Feature 8: Correlation study

    Data exploration of the Amazon review dataset using ChatGPT-4o

    Assignment

    Challenge

    Summary

    Building a Classification Model with ChatGPT

    Introduction

    Business problem

    Problem and data domain

    Dataset overview

    Breaking the problem down into features

    Prompting strategy

    Strategy 1: Task-Actions-Guidelines (TAG) prompt strategy

    Strategy 2: Persona-Instructions-Context (PIC) prompt strategy

    Strategy 3: Learn-Improvise-Feedback-Evaluate (LIFE) prompt strategy

    Building a sentiment analysis model to accurately classify Amazon reviews using the free version of ChatGPT

    Feature 1: Data preprocessing and feature engineering

    Feature 2: Model selection and baseline training

    Feature 3: Model evaluation and interpretation

    Feature 4: Handling imbalanced data

    Feature 5: Hyperparameter tuning

    Feature 6: Experimenting with feature representation

    Building a sentiment analysis model to accurately classify Amazon reviews using ChatGPT-4 or ChatGPT Plus

    Feature 1: Data preprocessing and feature engineering

    Feature 2: Model selection and baseline training

    Feature 3: Model evaluation and interpretation

    Feature 4: Handling data imbalance

    Feature 5: Hyperparameter tuning

    Feature 6: Experimenting with feature representation

    Assignment

    Challenge

    Summary

    Building a Regression Model for Customer Spend with ChatGPT

    Introduction

    Business problem

    Problem and data domain

    Dataset overview

    Breaking the problem down into features

    Prompting strategy

    Strategy 1: Task-Actions-Guidelines (TAG) prompt strategy

    Strategy 2: Persona-Instructions-Context (PIC) prompt strategy

    Strategy 3: Learn-Improvise-Feedback-Evaluate (LIFE) prompt strategy

    Building a simple linear regression model to predict the Yearly Amount Spent by customers using the free version of ChatGPT

    Feature 1: Building the model step by step

    Feature 2: Applying regularization techniques

    Feature 3: Generating a synthetic dataset to add complexity

    Feature 4: Generating code to develop a model in a single step for a synthetic dataset

    Learning simple linear regression using ChatGPT Plus

    Feature 1: Building a simple linear regression model step by step

    Feature 2: Applying regularization techniques

    Feature 3: Generating a synthetic dataset to add complexity

    Feature 4: Generating code to develop a model in a single step for a synthetic dataset

    Assignment

    Challenge

    Summary

    Building an MLP Model for Fashion-MNIST with ChatGPT

    Introduction

    Business problem

    Problem and data domain

    Dataset overview

    Breaking the problem down into features

    Prompting strategy

    Strategy 1: Task-Actions-Guidelines (TAG) prompt strategy

    Strategy 2: Persona-Instructions-Context (PIC) prompt strategy

    Strategy 3: Learn-Improvise-Feedback-Evaluate (LIFE) prompt strategy

    Building an MLP model to accurately classify the Fashion-MNIST images using the free version of ChatGPT

    Feature 1: Building the baseline model

    Feature 2: Adding layers to the model

    Feature 3: Experimenting with batch sizes

    Feature 4: Experimenting with the number of neurons

    Feature 5: Trying different optimizers

    Assignment

    Challenge

    Summary

    Building a CNN Model for CIFAR-10 with ChatGPT

    Introduction

    Business problem

    Problem and data domain

    Dataset overview

    Breaking the problem down into features

    Prompting strategy

    Strategy 1: Task-Actions-Guidelines (TAG) prompt strategy

    Strategy 2: Persona-Instructions-Context (PIC) prompt strategy

    Strategy 3: Learn-Improvise-Feedback-Evaluate (LIFE) prompt strategy

    Building a CNN model to accurately classify the CIFAR-10 images using the free version of ChatGPT

    Feature 1: Building the baseline CNN model with a single convolutional layer

    Feature 2: Experimenting with the addition of convolutional layers

    Feature 3: Incorporating dropout regularization

    Feature 4: Implementing batch normalization

    Feature 5: Optimizing with different optimizers

    Feature 6: Applying the DavidNet architecture

    Assignment

    Challenge

    Summary

    Unsupervised Learning: Clustering and PCA

    Introduction

    Breaking the problem down into features

    Prompt strategy

    Customer segmentation

    The dataset

    Adding AI assistance to the unsupervised learning model development process

    Load the dataset

    Inspect the data

    Summary statistics

    Preprocessing the data

    Feature engineering

    Checking for outliers

    Removing outliers

    Data scaling using standardization

    Deciding on the number of clusters

    Creating a clustering model

    Visualize clustering results

    Final thoughts on clustering and the prompting process

    Product clustering for an e-commerce project

    Your initial prompt: Set context

    Load and preprocess the data

    Feature engineering and text data preprocessing

    Feature engineering

    Choose clustering algorithm

    Feature scaling

    Apply clustering algorithm

    Interpret clusters and visualize results

    Interpreting cluster

    Visualizing clusters

    Creating a word cloud

    Assigning categories to products and evaluating and refining

    Evaluate and refine

    Reflection on prompts for this use case

    Assignment

    Solution

    Summary

    Machine Learning with Copilot

    Introduction

    GitHub Copilot Chat in your IDE

    How it works

    Dataset overview

    Steps for data exploration

    Prompt strategy

    Your initial data exploration prompt: Prompt 1, setting the high-level context

    Step 1: Load the dataset

    Running the code for loading data

    Step 2: Inspect the data

    Step 3: Summary statistics

    Step 4: Explore categorical variables

    Step 5: Distribution of ratings

    Step 6: Temporal analysis

    Step 7: Review length analysis

    Step 8: Correlation analysis

    Step 9: Additional exploratory analysis

    Step 10: Text Preprocessing

    Step 11: Word Frequency Analysis

    Step 12: Sentiment Score Calculation

    Text preprocessing

    Word frequency analysis

    Sentiment score calculation

    Step 13: Visualize the Distribution of Sentiment Scores

    Step 14: Analyze the Relationship Between Sentiment Score and Other Variables

    Visualize the distribution of sentiment scores

    Analyze the relationship between sentiment score and other variables

    Assignment

    Solution

    Summary

    Regression with Copilot Chat

    Introduction

    Regression

    Dataset overview

    Explore the dataset

    Prompt strategy

    Your initial prompt

    Exploratory data analysis

    Data splitting

    Build a regression model

    Evaluate the model

    Evaluation metrics

    Assignment

    Summary

    Regression with Copilot Suggestions

    Introduction

    Dataset overview

    Prompt strategy

    Start coding with Copilot’s help

    Step 1: Import libraries with Copilot’s assistance

    Step 2: Load and explore the dataset

    Get types and columns

    Shape of the dataset

    Addressing the column types

    Statistical summary

    Check for missing values

    Check for duplicates

    Scale numerical features

    Visualization

    Step 3: Split data into training and testing sets

    Asking questions

    Step 4: Build a regression problem

    Step 5: Train the model

    Step 6: Evaluate model performance

    Assignment

    Summary

    Increasing Efficiency with GitHub Copilot

    Introduction

    Code generation and automation

    Copilot’s active editor

    Copilot Chat

    Copilot commands

    Creating a Notebook

    Creating a project

    Debugging and troubleshooting

    Code review and optimization techniques

    Workspace

    Visual Studio Code lookup

    Terminal

    Assignment

    Challenge

    Quiz

    Summary

    Agents in Software Development

    Introduction

    What are agents?

    How do agents work?

    Simpler agents versus agents using AI

    Simpler agents

    A simple agent is not a great conversationalist

    Improved conversation with tool calling and large language models (LLMs)

    The anatomy of a conversational agent

    More on tool calling in LLMs

    Adding capabilities to GPT using tools

    Advanced conversations

    Modeling advanced conversations

    Pseudo code for advanced conversations

    Autonomous agents

    Assignment

    Challenge

    Quiz

    Summary

    References

    Conclusion

    Recap of the book

    Major conclusions

    What’s next

    At last

    Other Books You May Enjoy

    Index

    Landmarks

    Cover

    Index

    Preface

    Who this book is for

    The target audience for this book is professionals in the web development, machine learning, and data science fields. You should be a professional with at least 1-3 years of experience. This book means to empower you by showcasing how AI assistants can be leveraged in different problem domains. It describes overall features but also gives recommendations on effective prompting for best results.

    What this book covers

    Chapter 1, It’s a New World, One with AI Assistants, and You’re Invited, looks at how we started using large language models and how it constitutes a paradigm shift for many, not just IT workers.

    Chapter 2, Prompt Strategy, explains the strategy used throughout the book in terms of breaking down a problem and some guiding principles on how to effectively prompt your chosen AI tool.

    Chapter 3, Tools of the Trade: Introducing Our AI Assistants, is where we explain how to work with our two chosen AI assistants, GitHub Copilot and ChatGPT, covering everything from installation to how to get started using them.

    Chapter 4, Build the Appearance of Our App with HTML and Copilot, focuses on building the frontend for our e-commerce app (a narrative you will see featured throughout the book).

    Chapter 5, Style the App with CSS and Copilot, is where we keep working on our e-commerce app but now focus specifically on CSS and ensuring the appearance is appealing.

    Chapter 6, Add Behaviour with JavaScript, is where we add behavior to our e-commerce app using JavaScript.

    Chapter 7, Support Multiple Viewports Using Responsive Web Layouts, is where we address the fact that an app needs to work for different device types, whether it’s a smaller mobile screen, a tablet, or a desktop screen. Therefore, this chapter focuses on responsive design.

    Chapter 8, Build a Backend with Web APIs, looks at how, for the app to actually work, it needs to have a backend, consisting of code that’s able to read and write data and persist it. This chapter therefore focuses on building a Web API for our e-commerce app.

    Chapter 9, Augment Web apps with AI Services, covers training a machine learning model and how to expose it via a Web API for consumption by anyone with a browser or other type of client capable of using the HTTP protocol.

    Chapter 10, Maintaining Existing Codebases, covers how most developers work on existing code and maintain existing codebases rather than creating new projects. Therefore, this chapter focuses on various aspects of maintaining code, like dealing with bugs, performance, working with tests, and more.

    Chapter 11, Data Exploration with ChatGPT, is where we work with a review dataset and learn to identify insights into distribution, trends, correlation, and more.

    Chapter 12, Building a Classification Model with ChatGPT, looks at the same review dataset as in Chapter 11, this time performing classification and sentiment analysis.

    Chapter 13, Building a Regression Model for Customer Spend with ChatGPT, attempts to predict the yearly amount spent by customers and uses regression to create a model capable of making this prediction.

    Chapter 14, Building an MLP Model for Fashion-MNIST with ChatGPT, looks at building an MLP model based on a fashion dataset, still sticking to our general theme of e-commerce.

    Chapter 15, Building a CNN Model for CIFAR-10 with ChatGPT, focuses on building a CNN model.

    Chapter 16, Unsupervised Learning: Clustering and PCA, focuses on clustering and PCA.

    Chapter 17, Machine Learning with Copilot, covers conducting machine learning using GitHub Copilot to contrast it with ChatGPT.

    Chapter 18, Regression with Copilot Chat, is where we develop a regression model. Also, this chapter uses GitHub Copilot.

    Chapter 19, Regression with Copilot Suggestions, like the preceding chapter, focuses on regression using GitHub Copilot. The difference between this and the preceding chapter is that here we use the suggestions from writing prompts as comments in a text file, rather than writing our prompt in a chat-like interface.

    Chapter 20, Increasing Efficiency with GitHub Copilot, focuses on getting the most out of GitHub Copilot. This chapter is a must read if you want to master GitHub Copilot.

    Chapter 21, Agents in Software Development, takes a look at what’s coming next within AI, namely, agents. Agents are able to assist you to a much higher degree by acting autonomously based on a high-level goal. This is definitely worth a read if you’re curious about future trends.

    Chapter 22, Conclusion, wraps up the book by drawing some conclusions as to the greater lessons learned about working with AI assistants.

    To get the most out of this book

    You’ll get more out of this book if you’ve built a few projects in each domain as opposed to being a complete beginner. Therefore, the book focuses on empowering you in your existing development workflows. We recommend other titles by Packt if you are completely new to web development or machine learning. See the below list for recommendations:

    https://www.packtpub.com/en-us/product/html5-web-application-development-by-example-beginners-guide-9781849695947

    Machine Learning with Python: Unlocking AI Potential with Python and Machine Learning by Oliver Theobald (https://www.packtpub.com/en-US/product/machine-learning-with-python-9781835461969)

    The book is built in such a way that you’re shown the prompts you’re recommended to write followed by the results from the chosen AI tool.

    To follow along with the chapters on web development, we recommend installing Visual Studio Code. There are dedicated chapters in the book pointing out how to install GitHub Copilot and leverage it. See the installation instructions for Visual Studio Code here: https://code.visualstudio.com/download

    For the machine learning chapters, the majority of those chapters use ChatGPT, which can be accessed through a web browser. We do recommend solving those problems using notebooks, which can be viewed through a variety of different tools. For more detailed instructions on Notebook setup, refer to this page: https://code.visualstudio.com/docs/datascience/jupyter-notebooks

    To use GitHub Copilot, you need a GitHub account to log in to. Refer to this page on the setup process for GitHub Copilot: https://docs.github.com/en/copilot/quickstart

    Download the example code files

    The code bundle for the book is hosted on GitHub at https://github.com/PacktPublishing/AI-Assisted-Software-Development-with-GitHub-Copilot-and-ChatGPT. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

    Download the color images

    We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://packt.link/gbp/9781835086056.

    Conventions used

    There are a number of text conventions used throughout this book.

    CodeInText

    : Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and X(formerly known as Twitter) handles. For example: "Now that the

    product.css

    is created with the above content, we can include said CSS file in an HTML file."

    Bold: Indicates a new term, an important word, or words that you see on the screen. For instance, words in menus or dialog boxes appear in the text like this. For example: "Create new user: It should be possible to create a new user."

    Warnings or important notes appear like this.

    Tips and tricks appear like this.

    Get in touch

    Feedback from our readers is always welcome.

    General feedback: Email

    [email protected]

    and mention the book’s title in the subject of your message. If you have questions about any aspect of this book, please email us at

    [email protected]

    .

    Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you reported this to us. Please visit http://www.packtpub.com/submit-errata, click Submit Errata, and fill in the form.

    Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at

    [email protected]

    with a link to the material.

    If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit http://authors.packtpub.com.

    Share your thoughts

    Once you’ve read AI-Assisted Programming for Web and Machine Learning, we’d love to hear your thoughts! Please click here to go straight to the Amazon review page for this book and share your feedback.

    Your review is important to us and the tech community and will help us make sure we’re delivering excellent quality content.

    Download a free PDF copy of this book

    Thanks for purchasing this book!

    Do you like to read on the go but are unable to carry your print books everywhere?

    Is your eBook purchase not compatible with the device of your choice?

    Don’t worry, now with every Packt book you get a DRM-free PDF version of that book at no cost.

    Read anywhere, any place, on any device. Search, copy, and paste code from your favorite technical books directly into your application.

    The perks don’t stop there, you can get exclusive access to discounts, newsletters, and great free content in your inbox daily.

    Follow these simple steps to get the benefits:

    Scan the QR code or visit the link below:

    https://packt.link/free-ebook/9781835086056

    Submit your proof of purchase.

    That’s it! We’ll send your free PDF and other benefits to your email directly.

    1

    It’s a New World, One with AI Assistants, and You’re Invited

    Introduction

    In November 2022, ChatGPT arrived from seemingly nowhere. Over time, ChatGPT gained momentum, gradually evolving into a widely embraced tool. Eventually, millions actively incorporated ChatGPT into their workflows, leveraging its capabilities for generating insights, summarizing text, crafting code, and more.

    Its arrival changed many people’s workflow and improved it a lot in tasks like quickly understanding large bodies of text, writing emails, and more. Here you are, having bought this book, and hoping that you can learn how to use an AI tool like ChatGPT or GitHub Copilot to make you more efficient. That’s exactly the mission of this book: to teach you not only how to use these two AI tools but also to be able to apply them across various problem domains.

    Before we start solving problems using an AI assistant, let’s back up a bit; how did we get here? ChatGPT just didn’t arrive out of nowhere, right?

    How ChatGPT came to be, from NLP to LLMs

    To tell the story of how we got here, to AI tools like ChatGPT, powered by large language models (LLMs), let’s first cover natural language processing (NLP).

    NLP is a field of computer science, artificial intelligence, and computational linguistics. It’s concerned with the interactions between computers and human language, and how to program computers to process and analyze large amounts of natural language data. NLP is a hugely interesting area that has a range of useful applications in the real world. Here are some:

    Speech recognition: If you have a modern smartphone, you’ve likely interacted with voice assistants like Siri or Alexa, for example.

    Machine translation: Google Translate is perhaps what comes to mind when thinking of machine translation, the ability to translate from one language to another automatically.

    Sentiment analysis: A very useful area is understanding the sentiment in areas like social media, for example. Companies want to know how brands are perceived; e-commerce wants to quickly understand product reviews to boost their business.

    Chatbots and virtual assistants: You’ve likely seen chatbots being integrated on web pages even before the advent of ChatGPT. These chatbots can answer simpler questions, and companies have them to ensure you quickly get an answer to simpler questions and provide a more natural experience than an FAQ page, among other usage areas.

    Text summaries: Search engines come to mind again when thinking about text summaries. You might have seen how, when you use search engines like Bing or Google, it’s able to summarize a page and show the summary together with the link to the page in a search result page. As a user, you get a better understanding of what link to click.

    Content recommendation: This is another important area used by a variety of different domains. E-commerce uses this to present products you’re likely to be interested in, Xbox uses this to recommend what games to play and buy, and video streaming services display content you might want to watch next.

    As you can see already, with NLP, both companies and end users benefit greatly from adopting it.

    The rise of LLMs

    How did we evolve from NLP to LLMs, then? Initially, NLP used rule-based systems and statistical methods underneath. This approach, although working well for some tasks, struggled with human language.

    This changed for the better when deep learning, a subset of machine learning, was introduced to NLP, and we got models like RNN, recurrent neural networks, and transformer-based models, capable of learning patterns in data. The result was a considerable improvement in performance. With transformer-based models, we’re starting to lay the foundations of large language models.

    LLMs are a type of transformer model. They can generate human-like text and, unlike NLP models, they’re good at a variety of tasks without needing specific training data. How is this possible, you ask? The answer is a combination of improved architecture, a vast increase in computational power, and gigantic datasets.

    LLMs rest on the idea that a large enough neural network can learn to do anything, given enough data and compute. This is a paradigm shift in how we program computers. Instead of writing code, we write prompts and let the model do the rest.

    GPT models

    There are many different types of LLMs out there, but let’s focus on GPT for a second, a type of LLM on which the book’s chosen tools are based (even if GitHub Copilot uses a specific subset known as Codex).

    There have been several different versions developed in the last few years. Here are some models developed by the company OpenAI:

    GPT-1: The first one, with 117 million parameters using transformer architecture.

    GPT-2: This model has 1.5 billion parameters and is able to generate coherent and relevant text.

    GPT-3: This model has 175 billion parameters and is considerably better than its predecessor with features like answering questions, fiction generation, and even writing code.

    GPT-4: This model has been quoted to have 1.76 trillion parameters.

    The number of parameters allows the model to understand more nuanced and coherent text. It should also be said that the larger the model, the larger the computational resources that are needed to train it.

    ChatGPT recently switched to GPT-4 and the difference compared to GPT-3 is significant.

    How LLMs are better

    Now that we have a better understanding of how LLMs came to be and where they came from, what makes LLMs great? What are some good examples of why we really should adopt AI assistants based on LLMs?

    Because LLMs are bigger and more advanced, there are some areas in which they clearly outperform traditional NLP models:

    Context: LLMs can understand not just the recent input but can produce responses based on a longer conversation.

    Few-shot learning: To perform a task, LLMs usually just need a few examples to produce a correct response. This should be contrasted with NLP models, which usually use a large amount of task-specific training data to perform properly.

    Performance: LLMs are better than traditional NLP models in areas like translations, questions, and summarization.

    It’s worth mentioning that LLMs aren’t perfect; they do generate incorrect responses and can sometimes make up responses, also known as hallucinations. It’s our hope though that by reading this book, you will see the advantages of using LLM-based AI assistants and you will feel the pros clearly outweigh the cons.

    The new paradigm, programming with natural language

    Probably the biggest game changer with using LLM-based AI assistants is that you’re able to interact with them using nothing but natural language. There’s no need to learn a programming language to get the response you need. This change constitutes a new paradigm in interacting with AI. We’re moving away from writing in specific languages for producing apps, data retrieval, or even how we produce images, presentations, and more to express at a high level what we want through a prompt.

    Here is an example of things that are now possible to do using prompts, where it before needed considerably more effort:

    Programming: With a prompt, you express what app you want to build or what changes you want to make with the code.

    Image generation: Where you before needed a designer or artist, you can now generate via prompts.

    Videos: There are tools out there that, once given a prompt, will generate videos where an avatar reads out your written text.

    Text tasks: LLM-based AI assistants can generate emails, summarize large bodies of text, author interview ads, and much more; anything you can imagine with text really.

    All these application areas mentioned above make it clear that LLM-based AI tools are useful not only to programmers and data scientists but numerous different professions.

    Challenges and limitations

    Is everything working perfectly at this point? AI assistants aren’t able to replace you just yet, and should be considered more of a thinking partner. Microsoft has even, through conscious naming, called their AI assistants Copilots where you’re clearly the pilot that sets out the direction. These tools can generate text and other modalities in seconds, but you need to verify the correctness. Often, the first response you get from a tool is something you need to iterate over. The good news is that it just takes seconds to redo the instruction.

    An important thing to realize about AI assistants is that the more skilled you are at a certain topic, the more intelligent questions you can ask of it, and you’ll be able to better assess the correctness of the response.

    About this book

    The goals of this book are to:

    Introduce you to the new paradigm of programming with natural language.

    Provide you with the tools to get started using AI assistants.

    Empower you to use AI assistants effectively and responsibly by teaching you prompt engineering and specifically a set of prompting strategies (covered in Chapter 2) and some sound practices (covered in Chapter 8).

    We believe that with these tools, prompting strategies, and practices, you will be able to use AI assistants effectively and responsibly to augment your work and increase your productivity.

    Who this book is for

    This book is for professional developers within both the web and machine learning space. It is for those who want to learn how to use AI assistants like GitHub Copilot and ChatGPT to augment their work and increase their productivity.

    Evolution of programming languages

    Programming has gone through a series of changes and paradigm shifts throughout history:

    Ada Lovelace wrote the first algorithm for a machine, the Analytical Engine, in the 1840s. Lovelace is considered the first computer programmer and the first to recognize that the machine had applications beyond pure calculation.

    In the 1940s, the first programmable computers were created. These computers were programmed using punch cards. One such computer was the Harvard Mark I, which was used to calculate the trajectory of artillery shells. Also, Bombe is worth mentioning, which was used to crack the Enigma code during World War II and was instrumental in the Allies winning the war.

    In the 1950s, the first high-level programming languages were created. This time period saw the birth of FORTRAN, LISP, COBOL, and ALGOL. Some of these languages are still in use today, especially in banking systems, scientific computing, and defense.

    In the 1970s, the first object-oriented programming languages were created. The 1970s meant we got Smalltalk, C++, and Objective-C. Except for Smalltalk, these languages are heavily in use today.

    In the 1990s, the first functional programming languages were created. The 1990s gave us Haskell, OCaml, and Scala. The benefit of these languages is that they encourage immutability and pure functions, which makes them easier to reason about and test.

    In the 2000s, the first declarative programming languages were created. Declarative programming languages are used to describe what you want to do, rather than how you want to do it. The 2000s gave us SQL, HTML, and CSS.

    In the 2010s, the first low-code and no-code platforms were created. These platforms opened programming to a wider audience, and allowed anyone, regardless of technical background, to build applications.

    In the 2020s, the first AI assistants were created that leveraged natural language. If you can write a sentence, you can write code.

    In summary, programming has gone through a series of changes and paradigm shifts. Prompt-first programming is the latest paradigm shift and mastering it will be key to staying relevant in the immediate future.

    Looking ahead

    If changes and paradigm shifts took years or decades in the past, they now take months or even weeks. We’re moving toward a new world at breakneck speed.

    There’s reason to be excited, as we’re moving faster than before, but as always, we should exercise caution. We should be aware of the risks and the dangers of using these tools irresponsibly, but most of all we should be aware of the opportunities.

    As Alan Kay once said, The best way to predict the future is to invent it.

    How to use this book

    We believe the best way to use this book is to follow the chapters in order.

    Chapter 2, with the prompting strategies, is the most important chapter in the book. These patterns and strategies are referred to throughout the book and are the foundation for how to use AI assistants effectively and responsibly.

    The book is written in the following format:

    Introduction: The first chapter aims to provide you with an overview of what this book is about, its goals, and who it is for.

    Prompt strategy: The idea is to lay the foundation on how to break down problems within the domains of data science and web development. From this chapter, you will learn strategies you can adopt for your own problems.

    Tools of the trade: The third chapter introduces you to our tools, GitHub Copilot and ChatGPT, what they are, how they work, and how to install them. However, the book is written in such a way that you can take any of the prompts we suggest and feed those into any AI assistant, and get a similar experience.

    The remaining chapters of the book show how we use the prompt strategies from Chapter 2 and apply them to various domains from web development to data science and machine learning.

    Happy reading!

    Join our community on Discord

    Join our community’s Discord space for discussions with the author and other readers:

    https://packt.link/aicode

    2

    Prompt Strategy

    Introduction

    In the previous chapter, we gave some historical context to how AI has developed over the years, how we’ve gone from natural language processing (NLP) to large language models (LLMs), and how the latter serves as the underlying machine learning model in AI assistants. To use these AI assistants, you use natural language prompts as input. However, to ensure you prompt in an efficient way, so that you get what you want, it’s important to have a strategy, and that’s what this chapter aims to give you.

    How to prompt efficiently is commonly known in the industry as a prompt strategy or prompt engineering. It’s not an engineering practice in the common sense of the word but rather an art form where practitioners of AI assistants have discovered patterns and practices that seem to work well. We, the authors of this book, are building upon those discovered practices and aim to describe our findings for two domains: full-stack web development and data science. This book turns to you as either a web developer or data scientist and aims to empower you by describing how you can best approach problem solving in your domain using an AI assistant.

    This chapter constitutes a central piece of the book. It’s central in the sense that the approach being taught will be exemplified by other chapters in the book. As such, see this chapter as a guide you can refer to, providing the theory and thinking that is used in future chapters that solve specific problems within data science and full-stack web development.

    In this chapter, we will:

    Provide

    Enjoying the preview?
    Page 1 of 1