Introduction to Generative AI
Introduction to Generative AI
1 Overview
Generative AI has changed what software can do: applications can now draft documents, explain code, answer questions in natural language, and generate working C# from a plain-English description. For .NET developers this is not a research curiosity — it is a set of services and SDKs you can call from ordinary C# code, the same way you call a database or a REST API.
This tutorial is the first step in the GenAI-Powered .NET learning path. It builds the mental model everything else depends on: what generative AI actually is, how it relates to AI and machine learning, how large language models (LLMs) produce text, what tokens and embeddings are, and where Microsoft's AI products — Microsoft 365 Copilot, GitHub Copilot, and Azure OpenAI — fit into a developer's world.
No AI or math background is assumed. If you can write basic C#, you can follow everything here. By the end you will be able to explain how an LLM turns your prompt into a response, estimate what a request will cost in tokens, and name the right Microsoft tool for a given job.
2 Learning Objectives
After completing this tutorial, you will be able to:
- Define generative AI and list the kinds of content it can produce.
- Explain how artificial intelligence, machine learning, deep learning, and generative AI relate to each other.
- Describe, at a high level, how a large language model generates text through next-token prediction.
- Explain what tokens are, why models are limited and billed by tokens, and roughly estimate token counts.
- Describe what embeddings are and why they power semantic search and retrieval.
- Distinguish the roles of Microsoft 365 Copilot, GitHub Copilot, and Azure OpenAI, and identify which one a .NET developer builds with.
- List common developer use cases for generative AI and pick a sensible first use case for a .NET application.
3 Prerequisites
This is a beginner tutorial. You need working knowledge of C# (classes, methods, async/await at a reading level) and a .NET development environment — Visual Studio, VS Code with the C# extension, or the .NET SDK and any editor.
- C# fundamentals: variables, methods, classes, and running a console application.
- .NET 8 SDK (or later) installed if you want to try the code samples.
- No machine learning, statistics, or Python background is required.
- An Azure subscription is NOT required for this tutorial; it becomes useful from the Azure OpenAI tutorials onward.
4 What Generative AI Is and What It Produces
Generative AI is a branch of artificial intelligence that creates new content rather than only analyzing existing content. A traditional (discriminative) model answers questions like 'is this email spam?' or 'what is this house worth?' — it assigns labels or numbers to input. A generative model instead produces something that did not exist before: a paragraph, a C# method, an image, a summary, a translation.
The models behind this are often called foundation models: very large neural networks trained on broad data that can be applied to many tasks without task-specific retraining. Large language models (LLMs) are foundation models specialized in text — and because source code is text, they handle code remarkably well.
| Content type | Example output | Example developer scenario |
|---|---|---|
| Text | A product description, an email draft, a summary | Summarize a support ticket thread in an ASP.NET admin dashboard |
| Code | A C# method, a unit test, a SQL query | Generate an EF Core LINQ query from a plain-English request |
| Structured data | JSON matching a schema you define | Extract invoice fields from free-form text into a C# record |
| Images | Illustrations, product mock-ups | Generate placeholder art for a prototype |
| Audio | Speech from text, transcriptions from speech | Read notifications aloud; transcribe meeting recordings |
| Conversation | Multi-turn chat grounded in your data | A help chatbot inside your web application |
Two properties distinguish generative AI from the software you usually write. First, it is probabilistic: the same input can produce different output on different runs. Second, it is instructable with natural language: behavior is shaped by the prompt you send, not only by code you compile. Both properties have deep consequences for how you design, test, and ship AI features — themes that run through this entire course.
5 AI vs. Machine Learning vs. Generative AI
The terms AI, machine learning, and generative AI are often used interchangeably in marketing, but they are nested concepts — each one is a subset of the one before it. Keeping the hierarchy straight makes every later topic easier.
| Layer | What it means | Example |
|---|---|---|
| Artificial intelligence (AI) | The broad field: software performing tasks that normally need human intelligence | A chess engine, a route planner, a rules-based fraud filter |
| Machine learning (ML) | A subset of AI: systems that learn patterns from data instead of following hand-written rules | A model trained on past sales that predicts next month's demand |
| Deep learning (DL) | A subset of ML using multi-layer neural networks | An image classifier that recognizes products in photos |
| Generative AI (GenAI) | A subset of deep learning: models that generate new content | An LLM that writes a C# unit test from a description |
Classic ML is mostly discriminative: given input, predict a label (spam / not spam) or a value (price). You typically train such models yourself on your own data. Generative AI flips the workflow: the model is already trained by a provider at enormous cost, and you consume it as a service — your engineering effort goes into prompts, data grounding, and integration rather than training. That is why this course spends far more time on calling and steering models from C# than on training them.
6 How Large Language Models Work (High Level)
A large language model is a neural network — almost always based on the transformer architecture — trained on a huge corpus of text and code. During training, the model repeatedly plays one game: given a sequence of tokens, predict the next token. Over billions of examples it internalizes grammar, facts, reasoning patterns, and coding conventions, purely as statistical structure in its parameters.
When you send a prompt, the model runs inference: it converts your text to tokens, computes which token is most likely to come next, appends it, and repeats — one token at a time — until it emits a stop signal or hits a length limit. Everything an LLM does, from answering questions to writing C#, emerges from this next-token loop. The transformer's attention mechanism is what makes the predictions good: it lets every token 'look at' the other tokens that matter, so the model can connect a variable's use on line 40 with its declaration on line 2.
- Training happens once, at massive scale, at the provider. You never see it.
- Inference is what you pay for per request — it is the API call your C# code makes.
- The context window caps how many tokens (your input plus the model's output) fit in one request.
- Temperature and similar settings control how random the next-token choice is: low = focused and repeatable, high = varied and creative.
- The model has a knowledge cutoff: it knows nothing after its training data ends, and nothing about your private data — unless you supply that data in the prompt.
Two practical consequences follow. First, output is not deterministic by default — asking twice may yield two different (both plausible) answers, which changes how you test AI features. Second, the model does not 'look things up'; it generates likely text. When likely text is not true text, you get a hallucination. Later tutorials cover the standard defenses: grounding the model in your data (RAG) and evaluating output quality.
7 Tokens and Tokenization
Models do not read characters or whole words — they read tokens. Tokenization splits text into pieces from a fixed vocabulary: common words like 'the' are a single token, while rarer words split into subword chunks ('tokenization' might become 'token' + 'ization'). Code, spaces, and punctuation all consume tokens too.
Tokens matter to you as a developer for three concrete reasons. Pricing: API usage is billed per token, input and output counted separately. Limits: the context window is a token budget — a model with a 128k-token window cannot accept your 200k-token document in one call. Behavior: when a response stops mid-sentence, you usually hit the output token limit, not a bug in your code.
- Rule of thumb for English: 1 token is roughly 4 characters, so 100 tokens is about 75 words.
- A rough estimate is fine for budgeting; exact counts come from a tokenizer library or the usage numbers the API returns with every response.
- Different model families use different tokenizers, so counts vary slightly between models.
- Non-English text and unusual identifiers often use more tokens per word than plain English.
// Rough heuristic: ~4 characters per token for English text.
// Use the API's returned usage figures for exact billing numbers.
static int EstimateTokens(string text) =>
(int)Math.Ceiling(text.Length / 4.0);
string prompt = "Explain dependency injection in ASP.NET Core in one paragraph.";
Console.WriteLine($"~{EstimateTokens(prompt)} tokens"); // ~16 tokens
8 Embeddings: What They Are and Why They Matter
An embedding is a numeric representation of meaning: an embedding model turns a piece of text into a vector — an array of floating-point numbers, often 1,536 or 3,072 dimensions. The defining property is that texts with similar meaning produce vectors that are close together, even when they share no words. 'How do I reset my password?' and 'I forgot my login credentials' land near each other in vector space.
Closeness is measured mathematically, most commonly with cosine similarity — a score near 1 means very similar meaning, near 0 means unrelated. That single trick unlocks a family of features: semantic search (find documents by meaning, not keywords), recommendations (find items similar to this one), clustering and de-duplication, and — most importantly for this course — retrieval-augmented generation (RAG), where you find the most relevant chunks of your own data and hand them to an LLM as context so it answers from facts instead of guessing.
// An embedding model (e.g. an Azure OpenAI embeddings deployment)
// returns a float[] for each input text. Comparing two vectors:
static double CosineSimilarity(float[] a, float[] b)
{
double dot = 0, magA = 0, magB = 0;
for (int i = 0; i < a.Length; i++)
{
dot += a[i] * b[i];
magA += a[i] * a[i];
magB += b[i] * b[i];
}
return dot / (Math.Sqrt(magA) * Math.Sqrt(magB));
}
// ~0.9 for near-identical meaning, ~0.0 for unrelated text.
9 The Microsoft AI Ecosystem
Microsoft ships generative AI at three distinct altitudes, and knowing which is which prevents endless confusion, because all three carry the word 'Copilot' or sit near it in the news.
| Product | Who it serves | What it does | How a .NET developer meets it |
|---|---|---|---|
| Microsoft 365 Copilot | End users in an organization | AI assistance inside Word, Excel, Outlook, Teams, PowerPoint — drafting, summarizing, analyzing | As a user of Office apps; optionally by building plugins/extensions for it |
| GitHub Copilot | Developers | AI pair programmer in Visual Studio / VS Code: code completions, chat, explaining and refactoring code | Daily coding aid while writing C#; covered in tutorials 8–9 |
| Azure OpenAI | Application builders | Hosts OpenAI models (GPT family, embeddings, image models) as an Azure service with enterprise security, private networking, and regional deployments | The service your C# applications call via SDK/REST to add AI features; covered from tutorial 10 onward |
The distinction that matters most for this course: Microsoft 365 Copilot and GitHub Copilot are finished products you use, while Azure OpenAI is a building block you program against. When your ASP.NET application needs to summarize a document or answer a customer question, your code calls Azure OpenAI (directly or through frameworks like Semantic Kernel, introduced later in this course). Azure OpenAI also brings the enterprise properties applications need: your prompts and data are not used to train the underlying models, access is controlled with Azure identity and networking, and usage is billed and monitored through your Azure subscription.
10 Typical Developer Use Cases for GenAI
Generative AI shows up in a .NET developer's life in two ways: as a productivity tool while you build software, and as a capability inside the software you build. Both matter, and this course covers both.
- Code generation and completion — turning intent into C# scaffolding, LINQ queries, regex, or SQL.
- Code explanation and review — understanding unfamiliar or legacy code, spotting bugs and smells.
- Test generation — drafting xUnit/NUnit tests and edge cases for existing methods.
- Documentation — generating XML doc comments, README drafts, and release notes from code and commits.
- Summarization — condensing tickets, logs, meeting notes, or long documents inside your applications.
- Conversational interfaces — chatbots and assistants that answer from your product's data.
- Data extraction and transformation — pulling structured JSON out of free-form text like emails or invoices.
- Translation and rewriting — localizing content or adjusting tone for different audiences.
When choosing a first AI feature for a real application, favor use cases where output is reviewed by a human or is easy to validate (summaries, drafts, extraction with schema validation) over use cases where wrong output acts unsupervised. A summarizer whose output a support agent reads is a far safer first project than an agent that closes tickets automatically. The reliability tutorials later in the course explain how to graduate from the first kind to the second.
11 Code Examples in C#
This first example shows the shape of a chat call to an Azure OpenAI deployment from C#. Treat it as illustrative: it reflects the Azure.AI.OpenAI SDK's general design, but package versions evolve — see section 13 and tutorial 11, which walks through this end to end against a real resource.
// NuGet: Azure.AI.OpenAI
using Azure;
using Azure.AI.OpenAI;
using OpenAI.Chat;
var client = new AzureOpenAIClient(
new Uri("https://<your-resource>.openai.azure.com/"),
new AzureKeyCredential("<your-key>")); // use Entra ID in real apps
ChatClient chat = client.GetChatClient("<your-model-deployment-name>");
ChatCompletion completion = await chat.CompleteChatAsync(
new SystemChatMessage("You are a concise assistant for C# developers."),
new UserChatMessage("In one sentence, what is a token in an LLM?"));
Console.WriteLine(completion.Content[0].Text);
Notice the three moving parts you will meet in every AI call: a client bound to your service endpoint and credentials, a deployment name identifying which model to use, and a list of messages — a system message setting behavior plus a user message carrying the request. The response also carries usage metadata (exact input and output token counts), which is how you monitor cost.
// Every response reports exactly what the request consumed:
Console.WriteLine($"Input tokens: {completion.Usage.InputTokenCount}");
Console.WriteLine($"Output tokens: {completion.Usage.OutputTokenCount}");
// Log these in production — token usage is your cost meter.
The EstimateTokens helper from section 7 and the CosineSimilarity method from section 8 complete this tutorial's code set: one estimates the budget before a call, one compares the embedding vectors that later tutorials will retrieve from an embeddings deployment.
12 Step-by-Step: From Zero to Your First AI Call
Here is the end-to-end path from nothing to a working AI feature in a .NET application. This tutorial explains the map; tutorials 10–11 execute it in detail.
- Get access: create an Azure OpenAI resource in your Azure subscription and note its endpoint URL and key (or, better, plan to use Microsoft Entra ID authentication).
- Deploy a model: in the Azure portal or Azure AI Foundry, create a deployment of a chat model — the deployment name you choose is what your code references.
- Create the project: `dotnet new console` (or start from your existing ASP.NET solution) and add the Azure.AI.OpenAI NuGet package.
- Protect secrets: store the endpoint and key with `dotnet user-secrets` or environment variables — never hard-code credentials or commit them to source control.
- Write the call: build the client, pick the deployment, send a system message and a user message, and print the response — as in section 11.
- Inspect the result: read the reply and the token usage on the response; change the user message and observe how output (and cost) changes.
- Iterate: adjust the system message and settings like temperature, and watch how the model's behavior shifts — this experimentation is the seed of prompt engineering, the subject of the next tutorial.
13 Limitations, Risks, and Caveats
- Hallucination: models generate likely text, not verified facts. They can state falsehoods fluently and confidently, invent APIs, or cite sources that do not exist. Never ship unreviewed model output where correctness matters.
- Non-determinism: the same prompt can return different responses across runs. Design features and tests around this (the evaluation tutorial covers how).
- Knowledge cutoff: a model knows nothing after its training data ends and nothing about your private systems unless you provide that context in the request.
- Token limits and cost: every request spends a token budget; long histories and big documents can hit context-window limits or produce surprising bills.
- Latency: generation takes noticeably longer than a typical database or cache call; plan for streaming and async patterns in user-facing features.
- Data handling: with Azure OpenAI your prompts are not used to train the foundation models, but you must still follow your organization's rules for what data may be sent to any external service.
- Rapid change: models, SDKs, and pricing evolve quickly; verify specifics against current official documentation when you build.
14 Best Practices and Common Mistakes
Practices worth adopting from day one:
- Start with a use case where a human reviews the output — summaries, drafts, suggestions — before attempting autonomous behavior.
- Log token usage from every response so cost is visible from the first prototype onward.
- Keep secrets out of code: user-secrets locally, managed identity and Key Vault in Azure.
- Treat prompts as real engineering artifacts — version them, review them, test them.
- Ground the model in your own data (via retrieval) whenever answers must be factual and current.
- Learn the vocabulary of this tutorial well — tokens, context window, embeddings — because every later topic assumes it.
Mistakes beginners make most often:
- Treating model output as deterministic and asserting exact strings in tests.
- Assuming the model knows current events or your internal data without being given it.
- Confusing the three Copilot-era products — building against the wrong one wastes weeks (your applications call Azure OpenAI, not Microsoft 365 Copilot).
- Ignoring token growth in chat history until the context window overflows or the bill arrives.
- Sending sensitive data in prompts without checking organizational and regulatory constraints.
- Skipping the fundamentals and jumping straight to agents — the advanced topics in this course all stand on today's concepts.
20 Summary & Key Takeaways
- Generative AI creates new content — text, code, images, structured data — and is consumed by developers as a service, not trained in-house.
- The fields nest: AI ⊃ machine learning ⊃ deep learning ⊃ generative AI. Labels and numbers = classic ML; new content = GenAI.
- LLMs work by tokenizing input and repeatedly predicting the next token; capability and failure modes (non-determinism, hallucination, knowledge cutoff) all follow from this.
- Tokens are the currency of LLMs: they bound each request (context window) and drive cost (~4 English characters ≈ 1 token).
- Embeddings turn meaning into vectors compared by cosine similarity — the engine behind semantic search and, later, RAG.
- In the Microsoft ecosystem: Microsoft 365 Copilot assists end users in Office, GitHub Copilot assists you in the editor, and Azure OpenAI is the platform your C# applications call.
- Start with human-reviewed use cases (summaries, drafts, extraction), log token usage from day one, and keep secrets out of code.
You now hold the complete conceptual toolkit this course builds on: what generative models are, how they process text, what they cost, how meaning becomes searchable, and which Microsoft services turn all of it into shippable .NET features. Everything from prompt engineering to multi-agent systems is an elaboration of the ideas on this page.
21 Next Steps
Continue with the next tutorial in the path: Foundations of Prompt Engineering for Developers. You now know that a model's behavior is shaped by the text you send it — the next tutorial teaches you to shape that text deliberately: roles, instructions, context, and the patterns that turn an unpredictable model into a dependable component.
- Practice: explain tokens, embeddings, and the three Microsoft AI products aloud in your own words — teaching it back is the fastest check of understanding.
- Practice: take a paragraph of text and estimate its token count with the ~4-characters rule, then compare against any online tokenizer.
- Practice: sketch (on paper) where an AI summarization feature would sit in an application you already maintain — which data goes into the prompt, who reviews the output.
- Reading: Microsoft Learn's introductory modules on generative AI and the Azure OpenAI documentation's 'What is Azure OpenAI?' overview.
- Reading: the GitHub Copilot documentation for editor setup, previewing tutorials 8–9.
15 Quiz
Pick an answer for each question, then press Check answer. (Notes are disabled in this tab.)
1. What primarily distinguishes generative AI from traditional (discriminative) machine learning?
2. Which ordering correctly nests the fields from broadest to most specific?
3. At its core, how does a large language model generate a response?
4. What is a token in the context of LLMs?
5. Using the standard rule of thumb for English text, about how many tokens is a 300-word document?
6. What does a model's context window limit?
7. What is an embedding?
8. Which measure is most commonly used to compare two embedding vectors?
9. Why do the sentences 'How do I reset my password?' and 'I forgot my login credentials' end up with similar embeddings despite sharing almost no words?
10. Which Microsoft offering does your C# application code call to add generative AI features to an ASP.NET web app?
11. GitHub Copilot is best described as:
12. What is an AI hallucination?
13. What does a high temperature setting do to model output?
14. Which of these is the SAFEST first generative-AI feature for a production .NET application?
15. Why does your application need to send its own data (e.g., your product docs) to the model to get accurate answers about it?
16 Exam Questions
Try answering each question yourself before expanding the model answer.
1. Define generative AI and contrast it with discriminative machine learning. Give two examples of generative output relevant to a .NET developer.
2. Explain the relationship between AI, machine learning, deep learning, and generative AI, with one example at each layer.
3. Describe, at a high level, how an LLM turns a prompt into a response. Include the terms token, next-token prediction, and inference.
4. What is the transformer architecture's attention mechanism, and why does it matter for working with code?
5. Explain what tokens are and give three concrete reasons a .NET developer must care about them.
6. A colleague plans to send a 500-page manual (~150,000 words) to a model with a 128k-token context window in one request. Evaluate the plan.
7. What is an embedding, how is one produced, and what property makes embeddings useful?
8. Explain cosine similarity's role when working with embeddings, and interpret scores near 1, near 0.
9. Compare Microsoft 365 Copilot, GitHub Copilot, and Azure OpenAI: audience, purpose, and how a .NET developer interacts with each.
10. Why would an enterprise choose Azure OpenAI to power its .NET applications? Name at least four platform properties.
11. List four developer use cases for generative AI and state, for each, why generative AI fits better than conventional code.
12. Explain non-determinism in LLM output: where it comes from and two engineering consequences.
13. What is a hallucination, why is it inherent to how LLMs work, and what are two beginner-level mitigations?
14. Outline the steps to make a first Azure OpenAI call from a C# console application, including how secrets should be handled.
15. Your team wants its first AI feature in an existing ASP.NET application. Propose one, justify it with selection criteria from this tutorial, and note one metric to log from day one.
17 Flashcards
Click a card to reveal the back.
Generative AI
AI → ML → DL → GenAI
Large language model (LLM)
Next-token prediction
Token
Tokenization
Context window
Temperature
Embedding
Cosine similarity
Semantic search
Hallucination
Microsoft 365 Copilot vs GitHub Copilot vs Azure OpenAI
Azure OpenAI (enterprise properties)
Inference vs training
Safe first AI use case
18 Interview Questions & Answers
1. In your own words, what is generative AI?
2. How would you explain the difference between machine learning and generative AI to a stakeholder?
3. How does a large language model actually generate its answer?
4. What are tokens, and why do they matter to you as an engineer?
5. What is a context window, and how has it influenced a design you'd propose?
6. What are embeddings and where would you use them in a .NET application?
7. A search feature needs to find 'password reset' articles when users type 'I forgot my login.' Keyword search fails. What do you propose?
8. What's the difference between GitHub Copilot and Azure OpenAI? When does each apply?
9. Where does Microsoft 365 Copilot fit relative to what application developers build?
10. What is hallucination and how do you design around it?
11. Why can the same prompt give different answers twice, and how does that change testing?
12. What is temperature and when would you set it low versus high?
13. Why choose Azure OpenAI over calling a model provider's public API directly for an enterprise app?
14. What's the difference between training and inference, and which one do application developers deal with?
15. If your team adopted generative AI tomorrow, what first project would you pick and why?
19 Glossary
- Artificial intelligence (AI)
- The broad field of building software that performs tasks normally requiring human intelligence, from rule-based systems to modern neural networks.
- Machine learning (ML)
- A subset of AI in which systems learn patterns from data rather than following explicitly programmed rules.
- Deep learning
- Machine learning based on neural networks with many layers; the technique underlying modern generative models.
- Generative AI
- A subset of deep learning whose models create new content — text, code, images, audio — rather than only classifying or predicting.
- Large language model (LLM)
- A transformer-based neural network trained on massive text corpora that generates language one token at a time via next-token prediction.
- Transformer
- The neural network architecture behind modern LLMs, whose attention mechanism lets tokens weigh their relationships to all other tokens in context.
- Foundation model
- A very large model trained on broad data that serves many downstream tasks without task-specific retraining.
- Token
- The unit of text an LLM reads and writes — a word, subword, or punctuation chunk; roughly 4 English characters. Models are limited and billed by tokens.
- Tokenization
- The process of splitting text into vocabulary tokens before model processing; common words map to single tokens, rare words to multiple subwords.
- Context window
- The maximum number of tokens — all input plus generated output — that one model request can involve.
- Temperature
- A generation setting controlling randomness of token selection: low values give focused, repeatable output; high values give creative variety.
- Inference
- Running a trained model to produce output for a given input — the per-request, per-token-billed operation application code performs.
- Training
- The process of building a model by adjusting its parameters over large datasets; done by the model provider, not by application developers.
- Knowledge cutoff
- The date at which a model's training data ends; the model knows nothing after it, and nothing about private data, unless supplied in the request.
- Embedding
- A high-dimensional numeric vector representing the meaning of text, such that semantically similar texts yield nearby vectors.
- Vector
- An ordered array of numbers; embeddings are vectors, typically with hundreds or thousands of dimensions.
- Cosine similarity
- A measure of the angle between two vectors used to score embedding similarity: near 1 means closely related meaning, near 0 unrelated.
- Semantic search
- Search that ranks results by meaning using embedding similarity instead of keyword overlap.
- Retrieval-augmented generation (RAG)
- A pattern that retrieves relevant data (usually via embeddings) and includes it in the prompt so the model answers from supplied facts.
- Hallucination
- Fluent, confident model output that is factually incorrect or invented — an inherent risk of likelihood-based generation.
- Prompt
- The text sent to a model — instructions, question, and context — that shapes its generated response.
- Microsoft 365 Copilot
- Microsoft's AI assistant for end users inside Word, Excel, Outlook, PowerPoint, and Teams.
- GitHub Copilot
- An AI pair programmer integrated into editors like Visual Studio and VS Code, providing code completions, chat, and explanations.
- Azure OpenAI
- An Azure service hosting OpenAI models with enterprise identity, networking, compliance, and billing; the service .NET applications call for AI features.
- Model deployment
- A named instance of a model created in Azure OpenAI; application code targets the deployment name when making calls.
- Human-in-the-loop
- A use case design in which a person reviews model output before it takes effect — the recommended pattern for a team's first generative AI features, such as drafts and summaries.