Tutorial V.I - Programming with AI

Programming: Everyday Decision-Making Algorithms

Dr. Tobias Vlćek

Kühne Logistics University Hamburg - Winter 2024

Using AI

Using AI to generate code

  • Coding by hand is not the only way to generate code
  • Most likely, a lot of you have already used ChatGPT

Large Language Models

How do

Large Language

Models work?

Large Language Models (LLMs)

  • Think of them like advanced pattern recognition systems
  • They have “read” massive amounts of text
  • Books, websites, articles, code, and more
  • Text is broken into tokens, parts of words or punctuation
  • Based on patterns, they can generate new text

Training LLMs

  • Imagine learning a language by reading millions of books
  • Learns patterns in how words and ideas connect via tokens
  • Interconnected nodes with weights representing patterns
  • During training, these weights are adjusted
  • Once trained, applying them takes much less ressources

Pattern Recognition

  • Not like a search engine!
  • When asked, it looks for relevant patterns it learned
  • Like having a huge library in its “memory” to draw from
  • It can find patterns between concepts and your question
  • Knows only limited text at once (context window)

Probability based responses

  • After each token, it predicts “what should come next?”
  • Like a advanced word prediction on your phone
  • Chooses the most likely next token based on training
  • Uses randomness to generate different responses
  • But can’t actually “think” or “understand” like humans

Limitations

  • No true understanding of cause and effect
  • Sometimes makes mistakes or “hallucinates”
  • Mostly only knows what it was trained on
  • Can reflect biases present in training data
  • No emotional understanding (but can simulate responses!)

Impact on Jobs

  • Question: What do you think about their impact on jobs?
  • Question: What are the implications for us?
  • Question: Can we use them to our advantage?

Code Generation Tools

(Current) Choices for Programmers

  • Github Copilot: Integrated into VS Code by Microsoft
  • Cursor: Fork of VS Code with AI assistance built in
  • Aider: Chat interface for AI to write code in the terminal

Tip

Currently, Cursor is my favorite one. But this might change in the future, as there is a lot of competition in this space.

Installing Cursor

  • Go to Cursor
  • Download and install Cursor
  • You will need to create an account
  • Some free usage per month, after that you need to pay
  • For us, the free plan should be more than enough

Using Cursor

  • Open the folder with your tutorial files
  • Instead of notebooks, we will use .py files
  • Create a new .py file
  • Press Ctrl + L to open the chat

Asking for help

Task: Paste the following prompt in to the chat:

Can you please write me a small random number guessing game in python? It should work for one player in the terminal. The player should guess a number between 1-10 and get hints about whether his guess was too large or too small. After 3 tries, end the game if he didn’t succeed with a nice message.

Copy the generated code and paste it into your file.

More on Cursor

  • While working with Cursor, it will suggest you code changes
  • You can accept or reject them
  • The rest you will learn by doing!

Note

And that’s it for the introduction to AI!
You now have the basic knowledge to start working with AI!.