1 of 23

Generative AI Systems

Ketan Mayer-Patel

UNC Department of Computer Science

COMP 426: Modern Web Programming (Fall 2026)

Module 4: Backend

Lecture 18

2 of 23

  • We are rounding out the backend module, where we discuss infrastructure features that help us power our web applications.
    • Database: Store data for our applications.
    • Authentication: Keep track of users and ensure certain resources are protected.
    • Storage: Store user-uploaded files (such as images).
    • Realtime: Using websockets for realtime updates in our applications.
  • Familiarity with all of these classes of tools is extremely important for building production web applications (both personal projects and in industry).

Recap

COMP 426: Modern Web Programming (Fall '26)

Generative AI Systems

M4: Backend

L18

3 of 23

Another class of backend technologies has recently become increasingly important.

Generative AI ✨

  • Generative AI systems have unlocked a new set of possible features that web applications can implement.

Companies across all economic sectors are racing to adopt these features and develop more powerful Generative AI systems.

  • Companies expect new hires to have some knowledge or experience using these tools due to their increasing importance → Curriculums have to catch up!

​

​

Recap

COMP 426: Modern Web Programming (Fall '26)

Generative AI Systems

M4: Backend

L18

4 of 23

  • Generative AI: Systems that generate new content (text, images, etc) based off a user’s input prompt.

Generative AI

COMP 426: Modern Web Programming (Fall '26)

Generative AI Systems

M4: Backend

L18

�����Gen AI “Model”

Input prompt�(a question)

The AI generates new content based on this prompt

5 of 23

Generative AI

COMP 426: Modern Web Programming (Fall '26)

Generative AI Systems

  • Generative AI is often treated like magic (a black box).
    • Reinforces misconceptions about how it works.
    • Leads to incorrect expectations on generative AI and the content it generates.

M4: Backend

L18

�����Gen AI “Model”

Input prompt�(a question)

The AI generates new content based on this prompt

6 of 23

  • Generative AI is not magic!
  • Modern Generative AI tools like ChatGPT utilize large-language models (LLMs) to generate responses and create new content.
    • LLM: Algorithm that can perform a large variety of natural language processing (NLP) tasks, enabling them to recognize, translate, predict, and generate text.
      • “Large” - trained on very large data sets.
      • The algorithm extracts “meaning” from a sequence of text by understanding the relationships between words and phrases in it.
    • There are other tools such as LVMs (large vision models) for image based tasks.

Generative AI

COMP 426: Modern Web Programming (Fall '26)

Generative AI Systems

M4: Backend

L18

7 of 23

The Basics - How LLMs work

COMP 426: Modern Web Programming (Fall '26)

Generative AI Systems

M4: Backend

L18

Take n tokens

Input tokens into the language model.

Generate 1 new token back

What is a token?

8 of 23

COMP 426: Modern Web Programming (Fall '26)

Generative AI Systems

M4: Backend

L18

Tokens

  • Token: Unit or chuck of text used by language models.
    • Short words are typically their own tokens.
    • Longer words may consist of many tokens (typically indicative of meaning)
    • Punctuation is used as its own token.

​

​

​

https://tokenvisualizer.netlify.app/

Wow

,

Gener

ative

AI

is

so

cool

!

9 of 23

The Basics - How LLMs work

COMP 426: Modern Web Programming (Fall '26)

Generative AI Systems

M4: Backend

L18

Take n tokens

Input tokens into the language model.

Generate 1 new token back

10 of 23

COMP 426: Modern Web Programming (Fall '26)

Generative AI Systems

Take n tokens

…

P=

Next Token

Generate 1 new token back

The language model predicts the next token.

Basics - How Generative Language Models Work

M4: Backend

L18

Keep

calm

and

carry

0.72

0.16

on

the

0.07

it

0.02

an

on

p refers to the predicted probability that a token is the correct next response.

But wait - aren’t GPT’s responses much longer?

11 of 23

COMP 426: Modern Web Programming (Fall '26)

Generative AI Systems

M4: Backend

L18

The

`

quick

The

quick

`

brown

The

quick

brown

`

fox

The

quick

brown

fox

`

jumps

The

quick

brown

fox

jumps

`

over

The

quick

brown

fox

jumps

over

`

the

The

quick

brown

fox

jumps

over

the

`

lazy

The

quick

brown

fox

jumps

over

the

lazy

`

dog

The language model generates one new token at a time.

The old input + new token is passed as input for the next iteration.

12 of 23

  • LLMs generate text token-by-token, determining the probability of the next token at every step.
    • These probability distributions are generated based on the unique properties of the model and how the models were trained.
  • LLMs typically have a temperature parameter that allows some variation (“creativity”) in the next token chosen (so the top-ranked one is not chosen every time).
    • This is why LLMs generate new responses every time!

Discuss: What problem might arise because tokens are generated one-by-one?

​

​

Text Generation

COMP 426: Modern Web Programming (Fall '26)

Generative AI Systems

M4: Backend

L18

13 of 23

  • LLMs can easily – and confidently – hallucinate.
  • Occurs when the LLM chooses the “wrong” next token.
    • The LLM’s prediction for the next best token was wrong!
    • This can throw off an entire response – because the bad next token is used as part of the model’s input to generate subsequent tokens.

​

​

Hallucinations

COMP 426: Modern Web Programming (Fall '26)

Generative AI Systems

M4: Backend

L18

14 of 23

COMP 426: Modern Web Programming (Fall '26)

Generative AI Systems

M4: Backend

L18

The

`

quick

The

quick

`

brown

The

quick

brown

`

fox

The

quick

brown

fox

`

bought

The

quick

brown

fox

bought

`

the

The

quick

brown

fox

bought

the

`

new

The

quick

brown

fox

`

video

The

quick

brown

fox

`

game

One incorrectly generated token can essentially corrupt the rest of the response.

bought

the

new

bought

the

new

video

wrong next token chosen

15 of 23

COMP 426: Modern Web Programming (Fall '26)

Generative AI Systems

M4: Backend

L18

The

quick

brown

fox

game

bought

the

new

video

The

quick

brown

fox

jumps

over

the

lazy

dog

16 of 23

  • LLMs can easily – and confidently – hallucinate.
  • Occurs when the LLM chooses the “wrong” next token.
    • The LLM’s prediction for the next best token was wrong!
    • This can throw off an entire response – because the bad next token is used as part of the model’s input to generate subsequent tokens.

​

​

Hallucinations

COMP 426: Modern Web Programming (Fall '26)

Generative AI Systems

  • Most large language models cannot detect when they have hallucinated.

​

TL;DR - You cannot blindly trust the output of AI models!

​

​

M4: Backend

L18

17 of 23

  • There are many LLMs that are commonly used today:
    • GPT ( ) - from OpenAI
    • Gemini ( ) - from Google
    • Claude ( ) - from Anthropic

​

LLMs

COMP 426: Modern Web Programming (Fall '26)

Generative AI Systems

M4: Backend

L18

  • These companies allow developers to use their models to generate content in their own apps via an API.
    • These companies typically charge by tokens generated. (GPT Pricing)

18 of 23

AI Servers

COMP 426: Modern Web Programming (Fall '26)

Generative AI Systems

M4: Backend

L18

Router

React, etc

Next App

Vercel (or Dev) Server

Next Server Functionality

Supabase

Client

Drizzle

OpenAI

GPT-5

Discuss: What would connect to the OpenAI server?

19 of 23

AI Servers

COMP 426: Modern Web Programming (Fall '26)

Generative AI Systems

M4: Backend

L18

Router

React, etc

Next App

Vercel (or Dev) Server

Next Server Functionality

Supabase

Client

Drizzle

OpenAI

GPT-5

Our server can communicate with GPT to generate text in response to an API request.

Like with our database, we let our server connect to OpenAI so we do not expose our API key.

How do we structure our requests?

20 of 23

  • When we communicate with GPT, even from our server-side, we send over text-based prompts → Just like the prompts you write on ChatGPT’s website!

​

  • The response is also text-based.
    • Remember, this response can have variation!
    • Therefore, it is the developer’s responsibility to write good prompts.

​

​

Prompts

COMP 426: Modern Web Programming (Fall '26)

Generative AI Systems

M4: Backend

L18

21 of 23

  • Prompt Engineering: Use of various techniques to write better prompts so that LLMs respond more predictably in a format the developer expects for use in their apps.
  • Writing good prompts is essential for writing GenAI-powered apps!
    • Most AI features you use in apps online just send prompts to GPT’s API!�There is nothing special under the hood here.
    • But, these prompts need to be written well so that these AI features work reliably.
    • OpenAI Prompt Engineering Best Practices

Prompt Engineering

COMP 426: Modern Web Programming (Fall '26)

Generative AI Systems

M4: Backend

L18

22 of 23

Attendance Check

COMP 426: Modern Web Programming (Fall '26)

Generative AI Systems

Consider this GPT prompt.

Rewrite the prompt to use prompt engineering best practices.

Submit to GradeScope!

Here is my essay draft:�[paste essay here]�

Fix it and make it sound better but don’t change too much. Also, make it longer but not too long, and more formal but not too formal. And don’t change the meaning but make it different.

!

M4: Backend

L18

23 of 23

Improving the Prompt

COMP 426: Modern Web Programming (Fall '26)

Generative AI Systems

Here is my essay draft:�[paste essay here]�

Fix it and make it sound better but don’t change too much. Also, make it longer but not too long, and more formal but not too formal. And don’t change the meaning but make it different.

Here is my essay draft, denoted within triple backticks: �Essay: ```�[paste essay here].

```

Please do the following:

1. Improve grammar and clarity.

2. Expand each paragraph by 2–3 sentences to add depth.

3. Adjust the tone to be moderately formal (like an academic essay for high school).

4. Return the revised essay as full text.

​

M4: Backend

L18